GiNaC  1.8.0
wildcard.cpp
Go to the documentation of this file.
1 
5 /*
6  * GiNaC Copyright (C) 1999-2020 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 
30 namespace GiNaC {
31 
34  print_func<print_tree>(&wildcard::do_print_tree).
35  print_func<print_python_repr>(&wildcard::do_print_python_repr))
36 
37 // default constructor
40 
41 wildcard::wildcard() : label(0)
42 {
44 }
45 
47 // other constructors
49 
50 wildcard::wildcard(unsigned l) : label(l)
51 {
53 }
54 
56 // archiving
58 
59 void wildcard::read_archive(const archive_node& n, lst& sym_lst)
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 
77 int 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 
88 void wildcard::do_print(const print_context & c, unsigned level) const
89 {
90  c.s << "$" << label;
91 }
92 
93 void 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 
100 void wildcard::do_print_python_repr(const print_python_repr & c, unsigned level) const
101 {
102  c.s << class_name() << '(' << label << ')';
103 }
104 
105 unsigned 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 
116 bool 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 
124 bool 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
unsigned hashvalue
hash value
Definition: basic.h:303
wildcard(unsigned label)
Construct wildcard with specified label.
Definition: wildcard.cpp:50
void read_archive(const archive_node &n, lst &syms) override
Read (a.k.a.
Definition: wildcard.cpp:59
const basic & setflag(unsigned f) const
Set some status_flags.
Definition: basic.h:288
This class acts as a wildcard for subs(), match(), has() and find().
Definition: wildcard.h:33
Definition: add.cpp:38
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
Archiving of GiNaC expressions.
void do_print_python_repr(const print_python_repr &c, unsigned level) const
Definition: wildcard.cpp:100
This class is the ABC (abstract base class) of GiNaC&#39;s class hierarchy.
Definition: basic.h:104
size_t nops() const
Definition: ex.h:135
Context for python-parsable output.
Definition: print.h:138
void archive(archive_node &n) const override
Save (a.k.a.
Definition: wildcard.cpp:67
ex x
Definition: factor.cpp:1641
void do_print(const print_context &c, unsigned level) const
Definition: wildcard.cpp:88
Interface to GiNaC&#39;s wildcard objects.
void do_print_tree(const print_tree &c, unsigned level) const
Definition: wildcard.cpp:93
ex op(size_t i) const
Definition: ex.h:136
Interface to several small and furry utilities needed within GiNaC but not of any interest to the use...
bool haswild(const ex &x)
Check whether x has a wildcard anywhere as a subexpression.
Definition: wildcard.cpp:124
print_func< print_context >(&varidx::do_print). print_func< print_latex >(&varidx
Definition: idx.cpp:45
#define GINAC_ASSERT(X)
Assertion macro for checking invariances.
Definition: assertion.h:33
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
unsigned label
Label used to distinguish different wildcards.
Definition: wildcard.h:64
GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(add, expairseq, print_func< print_context >(&add::do_print). print_func< print_latex >(&add::do_print_latex). print_func< print_csrc >(&add::do_print_csrc). print_func< print_tree >(&add::do_print_tree). print_func< print_python_repr >(&add::do_print_python_repr)) add
Definition: add.cpp:40
std::map< ex, ex, ex_is_less > exmap
Definition: basic.h:50
unsigned golden_ratio_hash(uintptr_t n)
Truncated multiplication with golden ratio, for computing hash values.
Definition: utils.h:68
size_t n
Definition: factor.cpp:1463
Base class for print_contexts.
Definition: print.h:102
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition: archive.h:48
Lightweight wrapper for GiNaC&#39;s symbolic objects.
Definition: ex.h:72
.calchash() has already done its job
Definition: flags.h:205
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
bool match(const ex &pattern, exmap &repl_lst) const override
Check whether the expression matches a given pattern.
Definition: wildcard.cpp:116
.expand(0) has already done its job (other expand() options ignore this flag)
Definition: flags.h:204
size_t c
Definition: factor.cpp:770
.eval() has already done its job
Definition: flags.h:203
Context for tree-like output for debugging.
Definition: print.h:146
unsigned flags
of type status_flags
Definition: basic.h:302
GINAC_BIND_UNARCHIVER(add)
Type-specific hash seed.

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