GiNaC 1.8.10
wildcard.cpp
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2026 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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include "wildcard.h"
23#include "archive.h"
24#include "utils.h"
25#include "hash_seed.h"
26
27namespace GiNaC {
28
31 print_func<print_tree>(&wildcard::do_print_tree).
32 print_func<print_python_repr>(&wildcard::do_print_python_repr))
33
34
35// default constructor
37
38wildcard::wildcard() : label(0)
39{
41}
42
44// other constructors
46
47wildcard::wildcard(unsigned l) : label(l)
48{
50}
51
53// archiving
55
57{
58 inherited::read_archive(n, sym_lst);
59 n.find_unsigned("label", label);
61}
63
65{
66 inherited::archive(n);
67 n.add_unsigned("label", label);
68}
69
71// functions overriding virtual functions from base classes
73
74int wildcard::compare_same_type(const basic & other) const
75{
76 GINAC_ASSERT(is_a<wildcard>(other));
77 const wildcard &o = static_cast<const wildcard &>(other);
78
79 if (label == o.label)
80 return 0;
81 else
82 return label < o.label ? -1 : 1;
83}
84
85void wildcard::do_print(const print_context & c, unsigned level) const
86{
87 c.s << "$" << label;
88}
89
90void wildcard::do_print_tree(const print_tree & c, unsigned level) const
91{
92 c.s << std::string(level, ' ') << class_name() << "(" << label << ")" << " @" << this
93 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
94 << std::endl;
95}
96
97void wildcard::do_print_python_repr(const print_python_repr & c, unsigned level) const
98{
99 c.s << class_name() << '(' << label << ')';
100}
101
102unsigned wildcard::calchash() const
103{
104 // this is where the schoolbook method
105 // (golden_ratio_hash(typeid(*this).name()) ^ label)
106 // is not good enough yet...
107 unsigned seed = make_hash_seed(typeid(*this));
110 return hashvalue;
111}
112
113bool wildcard::match(const ex & pattern, exmap& repl_lst) const
114{
115 // Wildcards must match each other exactly (this is required for
116 // subs() to work properly because in the final step it substitutes
117 // all wildcards by their matching expressions)
118 return is_equal(ex_to<basic>(pattern));
119}
120
121bool haswild(const ex & x)
122{
123 if (is_a<wildcard>(x))
124 return true;
125 for (size_t i=0; i<x.nops(); ++i)
126 if (haswild(x.op(i)))
127 return true;
128 return false;
129}
130
131} // namespace GiNaC
Archiving of GiNaC expressions.
#define GINAC_ASSERT(X)
Assertion macro for checking invariances.
Definition assertion.h:32
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition archive.h:48
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition basic.h:104
const basic & setflag(unsigned f) const
Set some status_flags.
Definition basic.h:287
unsigned hashvalue
hash value
Definition basic.h:302
unsigned flags
of type status_flags
Definition basic.h:301
bool is_equal(const basic &other) const
Test for syntactic equality.
Definition basic.cpp:862
virtual int compare_same_type(const basic &other) const
Returns order relation between two objects of same type.
Definition basic.cpp:718
Wrapper template for making GiNaC classes out of STL containers.
Definition container.h:72
Lightweight wrapper for GiNaC's symbolic objects.
Definition ex.h:72
size_t nops() const
Definition ex.h:135
ex op(size_t i) const
Definition ex.h:136
Base class for print_contexts.
Definition print.h:101
Context for python-parsable output.
Definition print.h:137
Context for tree-like output for debugging.
Definition print.h:145
@ expanded
.expand(0) has already done its job (other expand() options ignore this flag)
Definition flags.h:203
@ evaluated
.eval() has already done its job
Definition flags.h:202
@ hash_calculated
.calchash() has already done its job
Definition flags.h:204
This class acts as a wildcard for subs(), match(), has() and find().
Definition wildcard.h:33
bool match(const ex &pattern, exmap &repl_lst) const override
Check whether the expression matches a given pattern.
Definition wildcard.cpp:113
void read_archive(const archive_node &n, lst &syms) override
Read (a.k.a.
Definition wildcard.cpp:56
wildcard(unsigned label)
Construct wildcard with specified label.
Definition wildcard.cpp:47
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:102
void archive(archive_node &n) const override
Save (a.k.a.
Definition wildcard.cpp:64
void do_print(const print_context &c, unsigned level) const
Definition wildcard.cpp:85
unsigned label
Label used to distinguish different wildcards.
Definition wildcard.h:63
void do_print_python_repr(const print_python_repr &c, unsigned level) const
Definition wildcard.cpp:97
void do_print_tree(const print_tree &c, unsigned level) const
Definition wildcard.cpp:90
size_t n
Definition factor.cpp:1431
size_t c
Definition factor.cpp:756
ex x
Definition factor.cpp:1609
Type-specific hash seed.
Definition add.cpp:35
bool haswild(const ex &x)
Check whether x has a wildcard anywhere as a subexpression.
Definition wildcard.cpp:121
unsigned golden_ratio_hash(uintptr_t n)
Truncated multiplication with golden ratio, for computing hash values.
Definition utils.h:67
std::map< ex, ex, ex_is_less > exmap
Definition basic.h:49
print_func< print_context >(&varidx::do_print). print_func< print_latex >(&varidx
Definition idx.cpp:43
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:41
#define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options)
Macro for inclusion in the implementation of each registered class.
Definition registrar.h:183
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.