GiNaC 1.8.7
wildcard.cpp
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2023 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
28#include <iostream>
29
30namespace GiNaC {
31
34 print_func<print_tree>(&wildcard::do_print_tree).
35 print_func<print_python_repr>(&wildcard::do_print_python_repr))
36
37
38// default constructor
40
41wildcard::wildcard() : label(0)
42{
44}
45
47// other constructors
49
50wildcard::wildcard(unsigned l) : label(l)
51{
53}
54
56// archiving
58
60{
61 inherited::read_archive(n, sym_lst);
62 n.find_unsigned("label", label);
64}
66
68{
69 inherited::archive(n);
70 n.add_unsigned("label", label);
71}
72
74// functions overriding virtual functions from base classes
76
77int wildcard::compare_same_type(const basic & other) const
78{
79 GINAC_ASSERT(is_a<wildcard>(other));
80 const wildcard &o = static_cast<const wildcard &>(other);
81
82 if (label == o.label)
83 return 0;
84 else
85 return label < o.label ? -1 : 1;
86}
87
88void wildcard::do_print(const print_context & c, unsigned level) const
89{
90 c.s << "$" << label;
91}
92
93void wildcard::do_print_tree(const print_tree & c, unsigned level) const
94{
95 c.s << std::string(level, ' ') << class_name() << "(" << label << ")" << " @" << this
96 << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
97 << std::endl;
98}
99
100void wildcard::do_print_python_repr(const print_python_repr & c, unsigned level) const
101{
102 c.s << class_name() << '(' << label << ')';
103}
104
105unsigned wildcard::calchash() const
106{
107 // this is where the schoolbook method
108 // (golden_ratio_hash(typeid(*this).name()) ^ label)
109 // is not good enough yet...
110 unsigned seed = make_hash_seed(typeid(*this));
113 return hashvalue;
114}
115
116bool wildcard::match(const ex & pattern, exmap& repl_lst) const
117{
118 // Wildcards must match each other exactly (this is required for
119 // subs() to work properly because in the final step it substitutes
120 // all wildcards by their matching expressions)
121 return is_equal(ex_to<basic>(pattern));
122}
123
124bool haswild(const ex & x)
125{
126 if (is_a<wildcard>(x))
127 return true;
128 for (size_t i=0; i<x.nops(); ++i)
129 if (haswild(x.op(i)))
130 return true;
131 return false;
132}
133
134} // 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: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:103
Context for python-parsable output.
Definition print.h:139
Context for tree-like output for debugging.
Definition print.h:147
@ 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:116
void read_archive(const archive_node &n, lst &syms) override
Read (a.k.a.
Definition wildcard.cpp:59
wildcard(unsigned label)
Construct wildcard with specified label.
Definition wildcard.cpp:50
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:105
void archive(archive_node &n) const override
Save (a.k.a.
Definition wildcard.cpp:67
void do_print(const print_context &c, unsigned level) const
Definition wildcard.cpp:88
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:100
void do_print_tree(const print_tree &c, unsigned level) const
Definition wildcard.cpp:93
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:38
bool haswild(const ex &x)
Check whether x has a wildcard anywhere as a subexpression.
Definition wildcard.cpp:124
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
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 ls GINAC_BIND_UNARCHIVER)(lst)
Specialization of container::info() for lst.
Definition lst.cpp:42
print_func< print_context >(&varidx::do_print). print_func< print_latex >(&varidx
Definition idx.cpp:45
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
#define GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(classname, supername, options)
Macro for inclusion in the implementation of each registered class.
Definition registrar.h:185
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.