GiNaC 1.8.8
wildcard.cpp
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2025 Johannes Gutenberg University Mainz, Germany
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include "wildcard.h"
24#include "archive.h"
25#include "utils.h"
26#include "hash_seed.h"
27
28namespace GiNaC {
29
32 print_func<print_tree>(&wildcard::do_print_tree).
33 print_func<print_python_repr>(&wildcard::do_print_python_repr))
34
35
36// default constructor
38
39wildcard::wildcard() : label(0)
40{
42}
43
45// other constructors
47
48wildcard::wildcard(unsigned l) : label(l)
49{
51}
52
54// archiving
56
58{
59 inherited::read_archive(n, sym_lst);
60 n.find_unsigned("label", label);
62}
64
66{
67 inherited::archive(n);
68 n.add_unsigned("label", label);
69}
70
72// functions overriding virtual functions from base classes
74
75int wildcard::compare_same_type(const basic & other) const
76{
77 GINAC_ASSERT(is_a<wildcard>(other));
78 const wildcard &o = static_cast<const wildcard &>(other);
79
80 if (label == o.label)
81 return 0;
82 else
83 return label < o.label ? -1 : 1;
84}
85
86void wildcard::do_print(const print_context & c, unsigned level) const
87{
88 c.s << "$" << label;
89}
90
91void wildcard::do_print_tree(const print_tree & c, unsigned level) const
92{
93 c.s << std::string(level, ' ') << class_name() << "(" << label << ")" << " @" << this
94 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
95 << std::endl;
96}
97
98void wildcard::do_print_python_repr(const print_python_repr & c, unsigned level) const
99{
100 c.s << class_name() << '(' << label << ')';
101}
102
103unsigned wildcard::calchash() const
104{
105 // this is where the schoolbook method
106 // (golden_ratio_hash(typeid(*this).name()) ^ label)
107 // is not good enough yet...
108 unsigned seed = make_hash_seed(typeid(*this));
111 return hashvalue;
112}
113
114bool wildcard::match(const ex & pattern, exmap& repl_lst) const
115{
116 // Wildcards must match each other exactly (this is required for
117 // subs() to work properly because in the final step it substitutes
118 // all wildcards by their matching expressions)
119 return is_equal(ex_to<basic>(pattern));
120}
121
122bool haswild(const ex & x)
123{
124 if (is_a<wildcard>(x))
125 return true;
126 for (size_t i=0; i<x.nops(); ++i)
127 if (haswild(x.op(i)))
128 return true;
129 return false;
130}
131
132} // namespace GiNaC
Archiving of GiNaC expressions.
#define GINAC_ASSERT(X)
Assertion macro for checking invariances.
Definition assertion.h:33
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition archive.h:49
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition basic.h:105
const basic & setflag(unsigned f) const
Set some status_flags.
Definition basic.h:288
unsigned hashvalue
hash value
Definition basic.h:303
unsigned flags
of type status_flags
Definition basic.h:302
bool is_equal(const basic &other) const
Test for syntactic equality.
Definition basic.cpp:863
virtual int compare_same_type(const basic &other) const
Returns order relation between two objects of same type.
Definition basic.cpp:719
Wrapper template for making GiNaC classes out of STL containers.
Definition container.h:73
Lightweight wrapper for GiNaC's symbolic objects.
Definition ex.h:73
size_t nops() const
Definition ex.h:136
ex op(size_t i) const
Definition ex.h:137
Base class for print_contexts.
Definition print.h:102
Context for python-parsable output.
Definition print.h:138
Context for tree-like output for debugging.
Definition print.h:146
@ expanded
.expand(0) has already done its job (other expand() options ignore this flag)
Definition flags.h:204
@ evaluated
.eval() has already done its job
Definition flags.h:203
@ hash_calculated
.calchash() has already done its job
Definition flags.h:205
This class acts as a wildcard for subs(), match(), has() and find().
Definition wildcard.h:34
bool match(const ex &pattern, exmap &repl_lst) const override
Check whether the expression matches a given pattern.
Definition wildcard.cpp:114
void read_archive(const archive_node &n, lst &syms) override
Read (a.k.a.
Definition wildcard.cpp:57
wildcard(unsigned label)
Construct wildcard with specified label.
Definition wildcard.cpp:48
unsigned calchash() const override
Compute the hash value of an object and if it makes sense to store it in the objects status_flags,...
Definition wildcard.cpp:103
void archive(archive_node &n) const override
Save (a.k.a.
Definition wildcard.cpp:65
void do_print(const print_context &c, unsigned level) const
Definition wildcard.cpp:86
unsigned label
Label used to distinguish different wildcards.
Definition wildcard.h:64
void do_print_python_repr(const print_python_repr &c, unsigned level) const
Definition wildcard.cpp:98
void do_print_tree(const print_tree &c, unsigned level) const
Definition wildcard.cpp:91
size_t n
Definition factor.cpp:1432
size_t c
Definition factor.cpp:757
ex x
Definition factor.cpp:1610
Type-specific hash seed.
Definition add.cpp:36
bool haswild(const ex &x)
Check whether x has a wildcard anywhere as a subexpression.
Definition wildcard.cpp:122
unsigned golden_ratio_hash(uintptr_t n)
Truncated multiplication with golden ratio, for computing hash values.
Definition utils.h:68
std::map< ex, ex, ex_is_less > exmap
Definition basic.h:50
print_func< print_context >(&varidx::do_print). print_func< print_latex >(&varidx
Definition idx.cpp:44
static unsigned make_hash_seed(const std::type_info &tinfo)
We need a hash function which gives different values for objects of different types.
Definition hash_seed.h:36
GINAC_IMPLEMENT_REGISTERED_CLASS_OPT_T(lst, basic, print_func< print_context >(&lst::do_print). print_func< print_tree >(&lst::do_print_tree)) template<> bool lst GINAC_BIND_UNARCHIVER(lst)
Specialization of container::info() for lst.
Definition lst.cpp:42
#define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options)
Macro for inclusion in the implementation of each registered class.
Definition registrar.h:184
Interface to several small and furry utilities needed within GiNaC but not of any interest to the use...
Interface to GiNaC's wildcard objects.

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.