]> www.ginac.de Git - ginac.git/blob - ginac/wildcard.cpp
simplify_indexed() raises/lowers dummy indices to canonicalize their variance
[ginac.git] / ginac / wildcard.cpp
1 /** @file wildcard.cpp
2  *
3  *  Implementation of GiNaC's wildcard objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2002 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <iostream>
24
25 #include "wildcard.h"
26 #include "print.h"
27 #include "archive.h"
28 #include "utils.h"
29
30 namespace GiNaC {
31
32 GINAC_IMPLEMENT_REGISTERED_CLASS(wildcard, basic)
33
34 //////////
35 // default ctor, dtor, copy ctor, assignment operator and helpers
36 //////////
37
38 wildcard::wildcard() : label(0)
39 {
40         tinfo_key = TINFO_wildcard;
41 }
42
43 void wildcard::copy(const wildcard & other)
44 {
45         inherited::copy(other);
46         label = other.label;
47 }
48
49 DEFAULT_DESTROY(wildcard)
50
51 //////////
52 // other constructors
53 //////////
54
55 wildcard::wildcard(unsigned l) : label(l)
56 {
57         tinfo_key = TINFO_wildcard;
58 }
59
60 //////////
61 // archiving
62 //////////
63
64 wildcard::wildcard(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
65 {
66         n.find_unsigned("label", label);
67 }
68
69 void wildcard::archive(archive_node &n) const
70 {
71         inherited::archive(n);
72         n.add_unsigned("label", label);
73 }
74
75 DEFAULT_UNARCHIVE(wildcard)
76
77 //////////
78 // functions overriding virtual functions from base classes
79 //////////
80
81 int wildcard::compare_same_type(const basic & other) const
82 {
83         GINAC_ASSERT(is_a<wildcard>(other));
84         const wildcard &o = static_cast<const wildcard &>(other);
85
86         if (label == o.label)
87                 return 0;
88         else
89                 return label < o.label ? -1 : 1;
90 }
91
92 void wildcard::print(const print_context & c, unsigned level) const
93 {
94         if (is_a<print_tree>(c)) {
95                 c.s << std::string(level, ' ') << class_name() << " (" << label << ")"
96                     << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
97                     << std::endl;
98         } else if (is_a<print_python_repr>(c)) {
99                 c.s << class_name() << '(' << label << ')';
100         } else
101                 c.s << "$" << label;
102 }
103
104 unsigned wildcard::calchash(void) const
105 {
106         // this is where the schoolbook method
107         // (golden_ratio_hash(tinfo()) ^ label)
108         // is not good enough yet...
109         hashvalue = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ label);
110         setflag(status_flags::hash_calculated);
111         return hashvalue;
112 }
113
114 bool wildcard::match(const ex & pattern, lst & 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
122 } // namespace GiNaC