]> www.ginac.de Git - ginac.git/blob - ginac/wildcard.cpp
* As agreed upon at lunch, remove debugmsg since it is hardly of any use.
[ginac.git] / ginac / wildcard.cpp
1 /** @file wildcard.cpp
2  *
3  *  Implementation of GiNaC's wildcard objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 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 "wildcard.h"
24 #include "print.h"
25 #include "archive.h"
26 #include "utils.h"
27
28 namespace GiNaC {
29
30 GINAC_IMPLEMENT_REGISTERED_CLASS(wildcard, basic)
31
32 //////////
33 // default ctor, dtor, copy ctor, assignment operator and helpers
34 //////////
35
36 wildcard::wildcard() : label(0)
37 {
38         tinfo_key = TINFO_wildcard;
39 }
40
41 void wildcard::copy(const wildcard & other)
42 {
43         inherited::copy(other);
44         label = other.label;
45 }
46
47 DEFAULT_DESTROY(wildcard)
48
49 //////////
50 // other constructors
51 //////////
52
53 wildcard::wildcard(unsigned l) : label(l)
54 {
55         tinfo_key = TINFO_wildcard;
56 }
57
58 //////////
59 // archiving
60 //////////
61
62 wildcard::wildcard(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
63 {
64         n.find_unsigned("label", label);
65 }
66
67 void wildcard::archive(archive_node &n) const
68 {
69         inherited::archive(n);
70         n.add_unsigned("label", label);
71 }
72
73 DEFAULT_UNARCHIVE(wildcard)
74
75 //////////
76 // functions overriding virtual functions from base classes
77 //////////
78
79 int wildcard::compare_same_type(const basic & other) const
80 {
81         GINAC_ASSERT(is_a<wildcard>(other));
82         const wildcard &o = static_cast<const wildcard &>(other);
83
84         if (label == o.label)
85                 return 0;
86         else
87                 return label < o.label ? -1 : 1;
88 }
89
90 void wildcard::print(const print_context & c, unsigned level) const
91 {
92         if (is_a<print_tree>(c)) {
93                 c.s << std::string(level, ' ') << class_name() << " (" << label << ")"
94                     << std::hex << ", hash=0x" << hashvalue << ", flags=0x" << flags << std::dec
95                     << std::endl;
96         } else
97                 c.s << "$" << label;
98 }
99
100 unsigned wildcard::calchash(void) const
101 {
102         // this is where the schoolbook method
103         // (golden_ratio_hash(tinfo()) ^ label)
104         // is not good enough yet...
105         hashvalue = golden_ratio_hash(golden_ratio_hash(tinfo()) ^ label);
106         setflag(status_flags::hash_calculated);
107         return hashvalue;
108 }
109
110 bool wildcard::match(const ex & pattern, lst & repl_lst) const
111 {
112         // Wildcards must match each other exactly (this is required for
113         // subs() to work properly because in the final step it substitutes
114         // all wildcards by their matching expressions)
115         return is_equal(ex_to<basic>(pattern));
116 }
117
118 } // namespace GiNaC