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