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