]> www.ginac.de Git - ginac.git/blob - ginac/structure.h
tiny optimization in subs()
[ginac.git] / ginac / structure.h
1 /** @file structure.h
2  *
3  *  Wrapper template for making GiNaC classes out of C++ structures. */
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 #ifndef __GINAC_STRUCTURE_H__
24 #define __GINAC_STRUCTURE_H__
25
26 #include <functional>
27
28 #include "ex.h"
29 #include "print.h"
30
31 namespace GiNaC {
32
33 extern unsigned next_structure_tinfo_key;
34
35
36 /** Comparison policy: all structures of one type are equal */
37 template <class T>
38 class compare_all_equal {
39 protected:
40         static bool struct_is_equal(const T * t1, const T * t2) { return true; }
41         static int struct_compare(const T * t1, const T * t2) { return 0; }
42
43         // disallow destruction of structure through a compare_all_equal*
44 protected:
45         ~compare_all_equal() {}
46 };
47
48
49 /** Comparison policy: use std::equal_to/std::less (defaults to operators
50  *  == and <) to compare structures. */
51 template <class T>
52 class compare_std_less {
53 protected:
54         static bool struct_is_equal(const T * t1, const T * t2)
55         {
56                 return std::equal_to<T>()(*t1, *t2);
57         }
58
59         static int struct_compare(const T * t1, const T * t2)
60         {
61                 if (std::less<T>()(*t1, *t2))
62                         return -1;
63                 else if (std::less<T>()(*t2, *t1))
64                         return 1;
65                 else
66                         return 0;
67         }
68
69         // disallow destruction of structure through a compare_std_less*
70 protected:
71         ~compare_std_less() {}
72 };
73
74
75 /** Comparison policy: use bit-wise comparison to compare structures. */
76 template <class T>
77 class compare_bitwise {
78 protected:
79         static bool struct_is_equal(const T * t1, const T * t2)
80         {
81                 const char * cp1 = reinterpret_cast<const char *>(t1);
82                 const char * cp2 = reinterpret_cast<const char *>(t2);
83
84                 return std::equal(cp1, cp1 + sizeof(T), cp2);
85         }
86
87         static int struct_compare(const T * t1, const T * t2)
88         {
89                 const unsigned char * cp1 = reinterpret_cast<const unsigned char *>(t1);
90                 const unsigned char * cp2 = reinterpret_cast<const unsigned char *>(t2);
91                 typedef std::pair<const unsigned char *, const unsigned char *> cppair;
92
93                 cppair res = std::mismatch(cp1, cp1 + sizeof(T), cp2);
94
95                 if (res.first == cp1 + sizeof(T))
96                         return 0;
97                 else if (*res.first < *res.second)
98                         return -1;
99                 else
100                         return 1;
101         }
102
103         // disallow destruction of structure through a compare_bitwise*
104 protected:
105         ~compare_bitwise() {}
106 };
107
108
109 // Select default comparison policy
110 template <class T, template <class> class ComparisonPolicy = compare_all_equal> class structure;
111
112
113 /** Wrapper template for making GiNaC classes out of C++ structures. */
114 template <class T, template <class> class ComparisonPolicy>
115 class structure : public basic, public ComparisonPolicy<T> {
116         GINAC_DECLARE_REGISTERED_CLASS(structure, basic)
117
118         // helpers
119         static unsigned get_tinfo() { return reg_info.tinfo_key; }
120         static const char *get_class_name() { return "structure"; }
121
122         // constructors
123 public:
124         /** Construct structure as a copy of a given C++ structure. */
125         structure(const T & t) : inherited(get_tinfo()), obj(t) { }
126
127         // functions overriding virtual functions from base classes
128         // All these are just defaults that can be specialized by the user
129 public:
130         // evaluation
131         ex eval(int level = 0) const { return hold(); }
132         ex evalf(int level = 0) const { return inherited::evalf(level); }
133         ex evalm() const { return inherited::evalm(); }
134 protected:
135         ex eval_ncmul(const exvector & v) const { return hold_ncmul(v); }
136 public:
137         ex eval_indexed(const basic & i) const { return i.hold(); }
138
139         // printing
140         void print(const print_context & c, unsigned level = 0) const { inherited::print(c, level); }
141         unsigned precedence() const { return 70; }
142
143         // info
144         bool info(unsigned inf) const { return false; }
145
146         // operand access
147         size_t nops() const { return 0; }
148         ex op(size_t i) const { return inherited::op(i); }
149         ex operator[](const ex & index) const { return inherited::operator[](index); }
150         ex operator[](size_t i) const { return inherited::operator[](i); }
151         ex & let_op(size_t i) { return inherited::let_op(i); }
152         ex & operator[](const ex & index) { return inherited::operator[](index); }
153         ex & operator[](size_t i) { return inherited::operator[](i); }
154
155         // pattern matching
156         bool has(const ex & other) const { return inherited::has(other); }
157         bool match(const ex & pattern, lst & repl_lst) const { return inherited::match(pattern, repl_lst); }
158 protected:
159         bool match_same_type(const basic & other) const { return true; }
160 public:
161
162         // substitutions
163         ex subs(const lst & ls, const lst & lr, unsigned options = 0) const { return inherited::subs(ls, lr, options); }
164
165         // function mapping
166         ex map(map_function & f) const { return inherited::map(f); }
167
168         // degree/coeff
169         int degree(const ex & s) const { return inherited::degree(s); }
170         int ldegree(const ex & s) const { return inherited::ldegree(s); }
171         ex coeff(const ex & s, int n = 1) const { return inherited::coeff(s, n); }
172
173         // expand/collect
174         ex expand(unsigned options = 0) const { return inherited::expand(options); }
175         ex collect(const ex & s, bool distributed = false) const { return inherited::collect(s, distributed); }
176
177         // differentiation and series expansion
178 protected:
179         ex derivative(const symbol & s) const { return inherited::derivative(s); }
180 public:
181         ex series(const relational & r, int order, unsigned options = 0) const { return inherited::series(r, order, options); }
182
183         // rational functions
184         ex normal(lst & sym_lst, lst & repl_lst, int level = 0) const { return inherited::normal(sym_lst, repl_lst, level); }
185         ex to_rational(lst & repl_lst) const { return inherited::to_rational(repl_lst); }
186         ex to_polynomial(lst & repl_lst) const { return inherited::to_polynomial(repl_lst); }
187
188         // polynomial algorithms
189         numeric integer_content() const { return 1; }
190         ex smod(const numeric & xi) const { return *this; }
191         numeric max_coefficient() const { return 1; }
192
193         // indexed objects
194         exvector get_free_indices() const { return exvector(); }
195         ex add_indexed(const ex & self, const ex & other) const { return self + other; }
196         ex scalar_mul_indexed(const ex & self, const numeric & other) const { return self * other; }
197         bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const { return false; }
198
199         // noncommutativity
200         unsigned return_type() const { return return_types::commutative; }
201         unsigned return_type_tinfo() const { return tinfo(); }
202
203 protected:
204         bool is_equal_same_type(const basic & other) const
205         {
206                 GINAC_ASSERT(is_a<structure>(other));
207                 const structure & o = static_cast<const structure &>(other);
208
209                 return struct_is_equal(&obj, &o.obj);
210         }
211
212         unsigned calchash() const { return inherited::calchash(); }
213
214         // non-virtual functions in this class
215 public:
216         // access to embedded structure
217         const T *operator->() const { return &obj; }
218         T &get_struct() { return obj; }
219         const T &get_struct() const { return obj; }
220
221 private:
222         T obj;
223 };
224
225
226 /** Default constructor */
227 template <class T, template <class> class CP>
228 structure<T, CP>::structure() : inherited(get_tinfo()) { }
229
230 /** Construct object from archive_node. */
231 template <class T, template <class> class CP>
232 structure<T, CP>::structure(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {}
233
234 /** Unarchive the object. */
235 template <class T, template <class> class CP>
236 ex structure<T, CP>::unarchive(const archive_node &n, lst &sym_lst)
237 {
238         return (new structure(n, sym_lst))->setflag(status_flags::dynallocated);
239 }
240
241 /** Archive the object. */
242 template <class T, template <class> class CP>
243 void structure<T, CP>::archive(archive_node &n) const
244 {
245         inherited::archive(n);
246 }
247
248 /** Compare two structures of the same type. */
249 template <class T, template <class> class CP>
250 int structure<T, CP>::compare_same_type(const basic & other) const
251 {
252         GINAC_ASSERT(is_a<structure>(other));
253         const structure & o = static_cast<const structure &>(other);
254
255         return struct_compare(&obj, &o.obj);
256 }
257
258 template <class T, template <class> class CP>
259 registered_class_info structure<T, CP>::reg_info(structure::get_class_name(), "basic", next_structure_tinfo_key++, &structure::unarchive);
260
261 template <class T, template <class> class CP>
262 const char *structure<T, CP>::class_name() const { return reg_info.name; }
263
264
265 } // namespace GiNaC
266
267 #endif // ndef __GINAC_STRUCTURE_H__