]> www.ginac.de Git - ginac.git/blob - ginac/wildcard.cpp
[bugfix] integer_cra: check if arguments contain at least 2 moduli
[ginac.git] / ginac / wildcard.cpp
1 /** @file wildcard.cpp
2  *
3  *  Implementation of GiNaC's wildcard objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2008 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 <iostream>
24
25 #include "wildcard.h"
26 #include "archive.h"
27 #include "utils.h"
28
29 namespace GiNaC {
30
31 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(wildcard, basic,
32   print_func<print_context>(&wildcard::do_print).
33   print_func<print_tree>(&wildcard::do_print_tree).
34   print_func<print_python_repr>(&wildcard::do_print_python_repr))
35
36 //////////
37 // default constructor
38 //////////
39
40 wildcard::wildcard() : label(0)
41 {
42         setflag(status_flags::evaluated | status_flags::expanded);
43 }
44
45 //////////
46 // other constructors
47 //////////
48
49 wildcard::wildcard(unsigned l) : label(l)
50 {
51         setflag(status_flags::evaluated | status_flags::expanded);
52 }
53
54 //////////
55 // archiving
56 //////////
57
58 void wildcard::read_archive(const archive_node& n, lst& sym_lst)
59 {
60         inherited::read_archive(n, sym_lst);
61         n.find_unsigned("label", label);
62         setflag(status_flags::evaluated | status_flags::expanded);
63 }
64 GINAC_BIND_UNARCHIVER(wildcard);
65
66 void wildcard::archive(archive_node &n) const
67 {
68         inherited::archive(n);
69         n.add_unsigned("label", label);
70 }
71
72 //////////
73 // functions overriding virtual functions from base classes
74 //////////
75
76 int wildcard::compare_same_type(const basic & other) const
77 {
78         GINAC_ASSERT(is_a<wildcard>(other));
79         const wildcard &o = static_cast<const wildcard &>(other);
80
81         if (label == o.label)
82                 return 0;
83         else
84                 return label < o.label ? -1 : 1;
85 }
86
87 void wildcard::do_print(const print_context & c, unsigned level) const
88 {
89         c.s << "$" << label;
90 }
91
92 void wildcard::do_print_tree(const print_tree & c, unsigned level) const
93 {
94         c.s << std::string(level, ' ') << class_name() << "(" << label << ")" << " @" << this
95             << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
96             << std::endl;
97 }
98
99 void wildcard::do_print_python_repr(const print_python_repr & c, unsigned level) const
100 {
101         c.s << class_name() << '(' << label << ')';
102 }
103
104 unsigned wildcard::calchash() const
105 {
106         // this is where the schoolbook method
107         // (golden_ratio_hash(typeid(*this).name()) ^ label)
108         // is not good enough yet...
109         const void* this_tinfo = (const void*)typeid(*this).name();
110         hashvalue = golden_ratio_hash(golden_ratio_hash((p_int)this_tinfo) ^ label);
111         setflag(status_flags::hash_calculated);
112         return hashvalue;
113 }
114
115 bool wildcard::match(const ex & pattern, exmap& repl_lst) const
116 {
117         // Wildcards must match each other exactly (this is required for
118         // subs() to work properly because in the final step it substitutes
119         // all wildcards by their matching expressions)
120         return is_equal(ex_to<basic>(pattern));
121 }
122
123 bool haswild(const ex & x)
124 {
125         if (is_a<wildcard>(x))
126                 return true;
127         for (size_t i=0; i<x.nops(); ++i)
128                 if (haswild(x.op(i)))
129                         return true;
130         return false;
131 }
132
133 } // namespace GiNaC