3 * Wrapper template for making GiNaC classes out of C++ structures. */
6 * GiNaC Copyright (C) 1999-2016 Johannes Gutenberg University Mainz, Germany
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.
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.
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
23 #ifndef GINAC_STRUCTURE_H
24 #define GINAC_STRUCTURE_H
29 #include "operators.h"
36 /** Comparison policy: all structures of one type are equal */
38 class compare_all_equal {
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; }
43 // disallow destruction of structure through a compare_all_equal*
45 ~compare_all_equal() {}
49 /** Comparison policy: use std::equal_to/std::less (defaults to operators
50 * == and <) to compare structures. */
52 class compare_std_less {
54 static bool struct_is_equal(const T * t1, const T * t2)
56 return std::equal_to<T>()(*t1, *t2);
59 static int struct_compare(const T * t1, const T * t2)
61 if (std::less<T>()(*t1, *t2))
63 else if (std::less<T>()(*t2, *t1))
69 // disallow destruction of structure through a compare_std_less*
71 ~compare_std_less() {}
75 /** Comparison policy: use bit-wise comparison to compare structures. */
77 class compare_bitwise {
79 static bool struct_is_equal(const T * t1, const T * t2)
81 const char * cp1 = reinterpret_cast<const char *>(t1);
82 const char * cp2 = reinterpret_cast<const char *>(t2);
84 return std::equal(cp1, cp1 + sizeof(T), cp2);
87 static int struct_compare(const T * t1, const T * t2)
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;
93 cppair res = std::mismatch(cp1, cp1 + sizeof(T), cp2);
95 if (res.first == cp1 + sizeof(T))
97 else if (*res.first < *res.second)
103 // disallow destruction of structure through a compare_bitwise*
105 ~compare_bitwise() {}
109 // Select default comparison policy
110 template <class T, template <class> class ComparisonPolicy = compare_all_equal> class structure;
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)
119 static const char *get_class_name() { return "structure"; }
122 /** Construct structure as a copy of a given C++ structure. */
123 structure(const T & t) : obj(t) { }
125 // functions overriding virtual functions from base classes
126 // All these are just defaults that can be specialized by the user
129 ex eval() const override { return hold(); }
130 ex evalf(int level = 0) const override { return inherited::evalf(level); }
131 ex evalm() const override { return inherited::evalm(); }
133 ex eval_ncmul(const exvector & v) const override { return hold_ncmul(v); }
135 ex eval_indexed(const basic & i) const override { return i.hold(); }
138 void print(const print_context & c, unsigned level = 0) const override { inherited::print(c, level); }
139 unsigned precedence() const override { return 70; }
142 bool info(unsigned inf) const override { return false; }
145 size_t nops() const override { return 0; }
146 ex op(size_t i) const override { return inherited::op(i); }
147 ex operator[](const ex & index) const override { return inherited::operator[](index); }
148 ex operator[](size_t i) const override { return inherited::operator[](i); }
149 ex & let_op(size_t i) override { return inherited::let_op(i); }
150 ex & operator[](const ex & index) override { return inherited::operator[](index); }
151 ex & operator[](size_t i) override { return inherited::operator[](i); }
154 bool has(const ex & other, unsigned options = 0) const override { return inherited::has(other, options); }
155 bool match(const ex & pattern, exmap& repl_lst) const override { return inherited::match(pattern, repl_lst); }
157 bool match_same_type(const basic & other) const override { return true; }
161 ex subs(const exmap & m, unsigned options = 0) const override { return inherited::subs(m, options); }
164 ex map(map_function & f) const override { return inherited::map(f); }
167 int degree(const ex & s) const override { return inherited::degree(s); }
168 int ldegree(const ex & s) const override { return inherited::ldegree(s); }
169 ex coeff(const ex & s, int n = 1) const override { return inherited::coeff(s, n); }
172 ex expand(unsigned options = 0) const override { return inherited::expand(options); }
173 ex collect(const ex & s, bool distributed = false) const override { return inherited::collect(s, distributed); }
175 // differentiation and series expansion
177 ex derivative(const symbol & s) const override { return inherited::derivative(s); }
179 ex series(const relational & r, int order, unsigned options = 0) const override { return inherited::series(r, order, options); }
181 // rational functions
182 ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const override { return inherited::normal(repl, rev_lookup, level); }
183 ex to_rational(exmap & repl) const override { return inherited::to_rational(repl); }
184 ex to_polynomial(exmap & repl) const override { return inherited::to_polynomial(repl); }
186 // polynomial algorithms
187 numeric integer_content() const override { return 1; }
188 ex smod(const numeric & xi) const override { return *this; }
189 numeric max_coefficient() const override { return 1; }
192 exvector get_free_indices() const override { return exvector(); }
193 ex add_indexed(const ex & self, const ex & other) const override { return self + other; }
194 ex scalar_mul_indexed(const ex & self, const numeric & other) const override { return self * ex(other); }
195 bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const override { return false; }
198 unsigned return_type() const override { return return_types::commutative; }
199 return_type_t return_type_tinfo() const override
203 r.tinfo = &typeid(*this);
208 bool is_equal_same_type(const basic & other) const override
210 GINAC_ASSERT(is_a<structure>(other));
211 const structure & o = static_cast<const structure &>(other);
213 return this->struct_is_equal(&obj, &o.obj);
216 unsigned calchash() const override { return inherited::calchash(); }
218 // non-virtual functions in this class
220 // access to embedded structure
221 const T *operator->() const { return &obj; }
222 T &get_struct() { return obj; }
223 const T &get_struct() const { return obj; }
229 /** Default constructor */
230 template <class T, template <class> class CP>
231 structure<T, CP>::structure() { }
233 /** Compare two structures of the same type. */
234 template <class T, template <class> class CP>
235 int structure<T, CP>::compare_same_type(const basic & other) const
237 GINAC_ASSERT(is_a<structure>(other));
238 const structure & o = static_cast<const structure &>(other);
240 return this->struct_compare(&obj, &o.obj);
243 template <class T, template <class> class CP>
244 registered_class_info structure<T, CP>::reg_info = registered_class_info(registered_class_options(structure::get_class_name(), "basic", typeid(structure<T, CP>)));
248 #endif // ndef GINAC_STRUCTURE_H