"Fossies" - the Fresh Open Source Software Archive

Member "gtybalt-1.3.6/ginac_headers.in/structure.h" (26 Oct 2020, 8393 Bytes) of package /linux/privat/gtybalt-1.3.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "structure.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.3.5_vs_1.3.6.

    1 /** @file structure.h
    2  *
    3  *  Wrapper template for making GiNaC classes out of C++ structures. */
    4 
    5 /*
    6  *  GiNaC Copyright (C) 1999-2020 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 "ex.h"
   27 #include "ncmul.h"
   28 #include "numeric.h"
   29 #include "operators.h"
   30 #include "print.h"
   31 
   32 #include <functional>
   33 
   34 namespace GiNaC {
   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 const char *get_class_name() { return "structure"; }
  120     // constructors
  121 public:
  122     /** Construct structure as a copy of a given C++ structure. */
  123     structure(const T & t) : obj(t) { }
  124 
  125     // functions overriding virtual functions from base classes
  126     // All these are just defaults that can be specialized by the user
  127 public:
  128     // evaluation
  129     ex eval() const override { return hold(); }
  130     ex evalm() const override { return inherited::evalm(); }
  131 protected:
  132     ex eval_ncmul(const exvector & v) const override { return hold_ncmul(v); }
  133 public:
  134     ex eval_indexed(const basic & i) const override { return i.hold(); }
  135 
  136     // printing
  137     void print(const print_context & c, unsigned level = 0) const override { inherited::print(c, level); }
  138     unsigned precedence() const override { return 70; }
  139 
  140     // info
  141     bool info(unsigned inf) const override { return false; }
  142 
  143     // operand access
  144     size_t nops() const override { return 0; }
  145     ex op(size_t i) const override { return inherited::op(i); }
  146     ex operator[](const ex & index) const override { return inherited::operator[](index); }
  147     ex operator[](size_t i) const override { return inherited::operator[](i); }
  148     ex & let_op(size_t i) override { return inherited::let_op(i); }
  149     ex & operator[](const ex & index) override { return inherited::operator[](index); }
  150     ex & operator[](size_t i) override { return inherited::operator[](i); }
  151 
  152     // pattern matching
  153     bool has(const ex & other, unsigned options = 0) const override { return inherited::has(other, options); }
  154     bool match(const ex & pattern, exmap& repl_lst) const override { return inherited::match(pattern, repl_lst); }
  155 protected:
  156     bool match_same_type(const basic & other) const override { return true; }
  157 public:
  158 
  159     // substitutions
  160     ex subs(const exmap & m, unsigned options = 0) const override { return inherited::subs(m, options); }
  161 
  162     // function mapping
  163     ex map(map_function & f) const override { return inherited::map(f); }
  164 
  165     // degree/coeff
  166     int degree(const ex & s) const override { return inherited::degree(s); }
  167     int ldegree(const ex & s) const override { return inherited::ldegree(s); }
  168     ex coeff(const ex & s, int n = 1) const override { return inherited::coeff(s, n); }
  169 
  170     // expand/collect
  171     ex expand(unsigned options = 0) const override { return inherited::expand(options); }
  172     ex collect(const ex & s, bool distributed = false) const override { return inherited::collect(s, distributed); }
  173 
  174     // differentiation and series expansion
  175 protected:
  176     ex derivative(const symbol & s) const override { return inherited::derivative(s); }
  177 public:
  178     ex series(const relational & r, int order, unsigned options = 0) const override { return inherited::series(r, order, options); }
  179 
  180     // rational functions
  181     ex normal(exmap & repl, exmap & rev_lookup, lst & modifier) const override { return inherited::normal(repl, rev_lookup, modifier); }
  182     ex to_rational(exmap & repl) const override { return inherited::to_rational(repl); }
  183     ex to_polynomial(exmap & repl) const override { return inherited::to_polynomial(repl); }
  184 
  185     // polynomial algorithms
  186     numeric integer_content() const override { return 1; }
  187     ex smod(const numeric & xi) const override { return *this; }
  188     numeric max_coefficient() const override { return 1; }
  189 
  190     // indexed objects
  191     exvector get_free_indices() const override { return exvector(); }
  192     ex add_indexed(const ex & self, const ex & other) const override { return self + other; }
  193     ex scalar_mul_indexed(const ex & self, const numeric & other) const override { return self * ex(other); }
  194     bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const override { return false; }
  195 
  196     // noncommutativity
  197     unsigned return_type() const override { return return_types::commutative; }
  198     return_type_t return_type_tinfo() const override
  199     {
  200         return_type_t r;
  201         r.rl = 0;
  202         r.tinfo = &typeid(*this);
  203         return r;
  204     }
  205 
  206 protected:
  207     bool is_equal_same_type(const basic & other) const override
  208     {
  209         GINAC_ASSERT(is_a<structure>(other));
  210         const structure & o = static_cast<const structure &>(other);
  211 
  212         return this->struct_is_equal(&obj, &o.obj);
  213     }
  214 
  215     unsigned calchash() const override { 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 private:
  224     T obj;
  225 };
  226 
  227 
  228 /** Default constructor */
  229 template <class T, template <class> class CP>
  230 structure<T, CP>::structure() { }
  231 
  232 /** Compare two structures of the same type. */
  233 template <class T, template <class> class CP>
  234 int structure<T, CP>::compare_same_type(const basic & other) const
  235 {
  236     GINAC_ASSERT(is_a<structure>(other));
  237     const structure & o = static_cast<const structure &>(other);
  238 
  239     return this->struct_compare(&obj, &o.obj);
  240 }
  241 
  242 template <class T, template <class> class CP>
  243 registered_class_info structure<T, CP>::reg_info = registered_class_info(registered_class_options(structure::get_class_name(), "basic", typeid(structure<T, CP>)));
  244 
  245 } // namespace GiNaC
  246 
  247 #endif // ndef GINAC_STRUCTURE_H