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