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