]> www.ginac.de Git - ginac.git/blob - ginac/wildcard.cpp
Use C style cast when converting void* into function pointer.
[ginac.git] / ginac / wildcard.cpp
1 /** @file wildcard.cpp
2  *
3  *  Implementation of GiNaC's wildcard objects. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2009 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
32 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(wildcard, basic,
33   print_func<print_context>(&wildcard::do_print).
34   print_func<print_tree>(&wildcard::do_print_tree).
35   print_func<print_python_repr>(&wildcard::do_print_python_repr))
36
37 //////////
38 // default constructor
39 //////////
40
41 wildcard::wildcard() : label(0)
42 {
43         setflag(status_flags::evaluated | status_flags::expanded);
44 }
45
46 //////////
47 // other constructors
48 //////////
49
50 wildcard::wildcard(unsigned l) : label(l)
51 {
52         setflag(status_flags::evaluated | status_flags::expanded);
53 }
54
55 //////////
56 // archiving
57 //////////
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);
63         setflag(status_flags::evaluated | status_flags::expanded);
64 }
65 GINAC_BIND_UNARCHIVER(wildcard);
66
67 void wildcard::archive(archive_node &n) const
68 {
69         inherited::archive(n);
70         n.add_unsigned("label", label);
71 }
72
73 //////////
74 // functions overriding virtual functions from base classes
75 //////////
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));
111         hashvalue = golden_ratio_hash(seed ^ label);
112         setflag(status_flags::hash_calculated);
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