X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fstructure.h;h=22e684008fd5395fc5e191d47efd6518c6782e88;hp=a0fe9b7dfb5e26d58087482a54958a15583717b5;hb=e8c6a2891bc68ca8f97a7d4b8bd0dd3ac322d982;hpb=15d4b353c85f8815a95d97ab977c9ca48e155574;ds=inline diff --git a/ginac/structure.h b/ginac/structure.h index a0fe9b7d..22e68400 100644 --- a/ginac/structure.h +++ b/ginac/structure.h @@ -1,9 +1,9 @@ /** @file structure.h * - * Interface to 'abstract' class structure. */ + * Wrapper template for making GiNaC classes out of C++ structures. */ /* - * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,71 +23,245 @@ #ifndef __GINAC_STRUCTURE_H__ #define __GINAC_STRUCTURE_H__ -#include +#include + +#include "ex.h" +#include "ncmul.h" +#include "numeric.h" +#include "operators.h" +#include "print.h" -#ifndef NO_GINAC_NAMESPACE namespace GiNaC { -#endif // ndef NO_GINAC_NAMESPACE -struct registered_structure_info { - char const * name; +extern unsigned next_structure_tinfo_key; + + +/** Comparison policy: all structures of one type are equal */ +template +class compare_all_equal { +protected: + static bool struct_is_equal(const T * t1, const T * t2) { return true; } + static int struct_compare(const T * t1, const T * t2) { return 0; } + + // disallow destruction of structure through a compare_all_equal* +protected: + ~compare_all_equal() {} }; -/** The class structure is used to implement user defined classes - with named members which behave similar to ordinary C structs. - structure is an 'abstract' base class (it is possible but not - meaningful to make instances), the user defined structures - will be create by the perl script structure.pl */ -class structure : public basic -{ -// member functions +/** Comparison policy: use std::equal_to/std::less (defaults to operators + * == and <) to compare structures. */ +template +class compare_std_less { +protected: + static bool struct_is_equal(const T * t1, const T * t2) + { + return std::equal_to()(*t1, *t2); + } - // default constructor, destructor, copy constructor assignment operator and helpers -public: - structure(); - ~structure(); - structure(structure const & other); - structure const & operator=(structure const & other); + static int struct_compare(const T * t1, const T * t2) + { + if (std::less()(*t1, *t2)) + return -1; + else if (std::less()(*t2, *t1)) + return 1; + else + return 0; + } + + // disallow destruction of structure through a compare_std_less* protected: - void copy(structure const & other); - void destroy(bool call_parent); + ~compare_std_less() {} +}; + + +/** Comparison policy: use bit-wise comparison to compare structures. */ +template +class compare_bitwise { +protected: + static bool struct_is_equal(const T * t1, const T * t2) + { + const char * cp1 = reinterpret_cast(t1); + const char * cp2 = reinterpret_cast(t2); + + return std::equal(cp1, cp1 + sizeof(T), cp2); + } - // other constructors - // none + static int struct_compare(const T * t1, const T * t2) + { + const unsigned char * cp1 = reinterpret_cast(t1); + const unsigned char * cp2 = reinterpret_cast(t2); + typedef std::pair cppair; - // functions overriding virtual functions from bases classes + cppair res = std::mismatch(cp1, cp1 + sizeof(T), cp2); + + if (res.first == cp1 + sizeof(T)) + return 0; + else if (*res.first < *res.second) + return -1; + else + return 1; + } + + // disallow destruction of structure through a compare_bitwise* +protected: + ~compare_bitwise() {} +}; + + +// Select default comparison policy +template class ComparisonPolicy = compare_all_equal> class structure; + + +/** Wrapper template for making GiNaC classes out of C++ structures. */ +template class ComparisonPolicy> +class structure : public basic, public ComparisonPolicy { + GINAC_DECLARE_REGISTERED_CLASS(structure, basic) + + // helpers + static unsigned get_tinfo() { return reg_info.options.get_id(); } + static const char *get_class_name() { return "structure"; } + + // constructors +public: + /** Construct structure as a copy of a given C++ structure. */ + structure(const T & t) : inherited(get_tinfo()), obj(t) { } + + // functions overriding virtual functions from base classes + // All these are just defaults that can be specialized by the user +public: + // evaluation + ex eval(int level = 0) const { return hold(); } + ex evalf(int level = 0) const { return inherited::evalf(level); } + ex evalm() const { return inherited::evalm(); } +protected: + ex eval_ncmul(const exvector & v) const { return hold_ncmul(v); } public: - basic * duplicate() const; - void printraw(ostream & os) const; - void print(ostream & os, unsigned upper_precedence=0) const; - void printtree(ostream & os, unsigned indent) const; - void printcsrc(ostream & os, unsigned type, unsigned upper_precedence=0) const; + ex eval_indexed(const basic & i) const { return i.hold(); } + + // printing + void print(const print_context & c, unsigned level = 0) const { inherited::print(c, level); } + unsigned precedence() const { return 70; } + + // info + bool info(unsigned inf) const { return false; } + + // operand access + size_t nops() const { return 0; } + ex op(size_t i) const { return inherited::op(i); } + ex operator[](const ex & index) const { return inherited::operator[](index); } + ex operator[](size_t i) const { return inherited::operator[](i); } + ex & let_op(size_t i) { return inherited::let_op(i); } + ex & operator[](const ex & index) { return inherited::operator[](index); } + ex & operator[](size_t i) { return inherited::operator[](i); } + + // pattern matching + bool has(const ex & other) const { return inherited::has(other); } + bool match(const ex & pattern, lst & repl_lst) const { return inherited::match(pattern, repl_lst); } protected: - int compare_same_type(basic const & other) const; - bool is_equal_same_type(basic const & other) const; - - // new virtual functions which can be overridden by derived classes - // none - - // non-virtual functions in this class + bool match_same_type(const basic & other) const { return true; } +public: + + // substitutions + ex subs(const exmap & m, unsigned options = 0) const { return inherited::subs(m, options); } + + // function mapping + ex map(map_function & f) const { return inherited::map(f); } + + // degree/coeff + int degree(const ex & s) const { return inherited::degree(s); } + int ldegree(const ex & s) const { return inherited::ldegree(s); } + ex coeff(const ex & s, int n = 1) const { return inherited::coeff(s, n); } + + // expand/collect + ex expand(unsigned options = 0) const { return inherited::expand(options); } + ex collect(const ex & s, bool distributed = false) const { return inherited::collect(s, distributed); } + + // differentiation and series expansion protected: - static vector & registered_structures(void); + ex derivative(const symbol & s) const { return inherited::derivative(s); } public: - static unsigned register_new(char const * nm); + ex series(const relational & r, int order, unsigned options = 0) const { return inherited::series(r, order, options); } + + // rational functions + ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const { return inherited::normal(repl, rev_lookup, level); } + ex to_rational(exmap & repl) const { return inherited::to_rational(repl); } + ex to_polynomial(exmap & repl) const { return inherited::to_polynomial(repl); } + + // polynomial algorithms + numeric integer_content() const { return 1; } + ex smod(const numeric & xi) const { return *this; } + numeric max_coefficient() const { return 1; } -// member variables -// none + // indexed objects + exvector get_free_indices() const { return exvector(); } + ex add_indexed(const ex & self, const ex & other) const { return self + other; } + ex scalar_mul_indexed(const ex & self, const numeric & other) const { return self * ex(other); } + bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const { return false; } + + // noncommutativity + unsigned return_type() const { return return_types::commutative; } + unsigned return_type_tinfo() const { return tinfo(); } + +protected: + bool is_equal_same_type(const basic & other) const + { + GINAC_ASSERT(is_a(other)); + const structure & o = static_cast(other); + return struct_is_equal(&obj, &o.obj); + } + + unsigned calchash() const { return inherited::calchash(); } + + // non-virtual functions in this class +public: + // access to embedded structure + const T *operator->() const { return &obj; } + T &get_struct() { return obj; } + const T &get_struct() const { return obj; } + +private: + T obj; }; -// global constants -extern const structure some_structure; -extern type_info const & typeid_structure; +/** Default constructor */ +template class CP> +structure::structure() : inherited(get_tinfo()) { } + +/** Construct object from archive_node. */ +template class CP> +structure::structure(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {} + +/** Unarchive the object. */ +template class CP> +ex structure::unarchive(const archive_node &n, lst &sym_lst) +{ + return (new structure(n, sym_lst))->setflag(status_flags::dynallocated); +} + +/** Archive the object. */ +template class CP> +void structure::archive(archive_node &n) const +{ + inherited::archive(n); +} + +/** Compare two structures of the same type. */ +template class CP> +int structure::compare_same_type(const basic & other) const +{ + GINAC_ASSERT(is_a(other)); + const structure & o = static_cast(other); + + return struct_compare(&obj, &o.obj); +} + +template class CP> +registered_class_info structure::reg_info = registered_class_info(registered_class_options(structure::get_class_name(), "basic", next_structure_tinfo_key++, &structure::unarchive)); + -#ifndef NO_GINAC_NAMESPACE } // namespace GiNaC -#endif // ndef NO_GINAC_NAMESPACE #endif // ndef __GINAC_STRUCTURE_H__