From 12fefbca9b424cb8e9ae05d83883b96e17c7b96e Mon Sep 17 00:00:00 2001 From: Alexei Sheplyakov Date: Fri, 17 Oct 2008 13:15:03 +0400 Subject: [PATCH] A better return_type_tinfo() mechanism. return_type_tinfo() is used in order to distingish between non-commutative objects of different type. However, often it's necessary to distingish between non-commutative objects of the same type, for example, between gamma matrices with different representation label. return_type_tinfo() does not provide any clean way to do that. Hence, introduce return_type_t type which holds representation label along with type information, and use it for return_type_tinfo(). --- ginac/add.cpp | 4 ++-- ginac/add.h | 2 +- ginac/basic.cpp | 7 +++++-- ginac/basic.h | 2 +- ginac/clifford.cpp | 19 +++++++------------ ginac/clifford.h | 13 ++++++------- ginac/color.cpp | 16 +++++++++------- ginac/color.h | 5 +---- ginac/ex.h | 2 +- ginac/function.pl | 17 ++++++++++------- ginac/indexed.h | 2 +- ginac/integral.cpp | 2 +- ginac/integral.h | 2 +- ginac/mul.cpp | 6 +++--- ginac/mul.h | 2 +- ginac/ncmul.cpp | 10 +++++----- ginac/ncmul.h | 2 +- ginac/power.cpp | 2 +- ginac/power.h | 2 +- ginac/registrar.h | 37 +++++++++++++++++++++++++++++++++++++ ginac/relational.cpp | 2 +- ginac/relational.h | 2 +- ginac/structure.h | 8 +++++++- ginac/symbol.cpp | 18 +++++++++--------- ginac/symbol.h | 16 ++++++++-------- 25 files changed, 121 insertions(+), 79 deletions(-) diff --git a/ginac/add.cpp b/ginac/add.cpp index 2fa73dc2..547bee32 100644 --- a/ginac/add.cpp +++ b/ginac/add.cpp @@ -498,10 +498,10 @@ unsigned add::return_type() const return seq.begin()->rest.return_type(); } -tinfo_t add::return_type_tinfo() const +return_type_t add::return_type_tinfo() const { if (seq.empty()) - return this; + return make_return_type_t(); else return seq.begin()->rest.return_type_tinfo(); } diff --git a/ginac/add.h b/ginac/add.h index 18f43517..d96e1263 100644 --- a/ginac/add.h +++ b/ginac/add.h @@ -65,7 +65,7 @@ public: protected: ex derivative(const symbol & s) const; unsigned return_type() const; - tinfo_t return_type_tinfo() const; + return_type_t return_type_tinfo() const; ex thisexpairseq(const epvector & v, const ex & oc, bool do_index_renaming = false) const; ex thisexpairseq(std::auto_ptr vp, const ex & oc, bool do_index_renaming = false) const; expair split_ex_to_pair(const ex & e) const; diff --git a/ginac/basic.cpp b/ginac/basic.cpp index fa6e1d96..567760c9 100644 --- a/ginac/basic.cpp +++ b/ginac/basic.cpp @@ -774,9 +774,12 @@ unsigned basic::return_type() const return return_types::commutative; } -tinfo_t basic::return_type_tinfo() const +return_type_t basic::return_type_tinfo() const { - return tinfo_key; + return_type_t rt; + rt.tinfo = &typeid(*this); + rt.rl = 0; + return rt; } /** Compute the hash value of an object and if it makes sense to store it in diff --git a/ginac/basic.h b/ginac/basic.h index 304adcfa..3685fb53 100644 --- a/ginac/basic.h +++ b/ginac/basic.h @@ -216,7 +216,7 @@ public: // noncommutativity virtual unsigned return_type() const; - virtual tinfo_t return_type_tinfo() const; + virtual return_type_t return_type_tinfo() const; // functions for complex expressions virtual ex conjugate() const; diff --git a/ginac/clifford.cpp b/ginac/clifford.cpp index c76fff23..e6600372 100644 --- a/ginac/clifford.cpp +++ b/ginac/clifford.cpp @@ -46,8 +46,6 @@ GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(clifford, indexed, print_func(&clifford::do_print_dflt). print_func(&clifford::do_print_latex)) -const tinfo_static_t clifford::return_type_tinfo_static[256] = {{}}; - GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(diracone, tensor, print_func(&diracone::do_print). print_func(&diracone::do_print_latex)) @@ -120,6 +118,11 @@ clifford::clifford(unsigned char rl, const ex & metr, int comm_sign, std::auto_p tinfo_key = &clifford::tinfo_static; } +return_type_t clifford::return_type_tinfo() const +{ + return make_return_type_t(representation_label); +} + ////////// // archiving ////////// @@ -811,19 +814,11 @@ ex dirac_slash(const ex & e, const ex & dim, unsigned char rl) return clifford(e, varidx(0, dim), indexed((new minkmetric)->setflag(status_flags::dynallocated), symmetric2(), xi, chi), rl); } -/** Check whether a given tinfo key (as returned by return_type_tinfo() - * is that of a clifford object (with an arbitrary representation label). */ -bool is_clifford_tinfo(tinfo_t ti) -{ - p_int start_loc=(p_int)&clifford::return_type_tinfo_static; - return (p_int)ti>=start_loc && (p_int)ti vp) const; unsigned return_type() const { return return_types::noncommutative; } - tinfo_t return_type_tinfo() const { return clifford::return_type_tinfo_static+representation_label; } - + return_type_t return_type_tinfo() const; // non-virtual functions in this class public: unsigned char get_representation_label() const { return representation_label; } @@ -186,11 +182,14 @@ protected: // global functions -/** Check whether a given tinfo key (as returned by return_type_tinfo() +/** Check whether a given return_type_t object (as returned by return_type_tinfo() * is that of a clifford object (with an arbitrary representation label). * * @param ti tinfo key */ -bool is_clifford_tinfo(tinfo_t ti); +inline bool is_clifford_tinfo(const return_type_t& ti) +{ + return *(ti.tinfo) == typeid(clifford); +} /** Create a Clifford unity object. * diff --git a/ginac/color.cpp b/ginac/color.cpp index b70298b6..43768633 100644 --- a/ginac/color.cpp +++ b/ginac/color.cpp @@ -39,8 +39,6 @@ namespace GiNaC { GINAC_IMPLEMENT_REGISTERED_CLASS(color, indexed) -const tinfo_static_t color::return_type_tinfo_static[256] = {{}}; - GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(su3one, tensor, print_func(&su3one::do_print). print_func(&su3one::do_print_latex)) @@ -101,6 +99,11 @@ color::color(unsigned char rl, std::auto_ptr vp) : inherited(not_symme tinfo_key = &color::tinfo_static; } +return_type_t color::return_type_tinfo() const +{ + return make_return_type_t(representation_label); +} + ////////// // archiving ////////// @@ -524,17 +527,16 @@ ex color_h(const ex & a, const ex & b, const ex & c) /** Check whether a given tinfo key (as returned by return_type_tinfo() * is that of a color object (with an arbitrary representation label). */ -static bool is_color_tinfo(tinfo_t ti) +static bool is_color_tinfo(const return_type_t& ti) { - p_int start_loc=(p_int)&color::return_type_tinfo_static; - return (p_int)ti>=start_loc && (p_int)ti & rls) diff --git a/ginac/color.h b/ginac/color.h index ebe21226..360223e2 100644 --- a/ginac/color.h +++ b/ginac/color.h @@ -41,9 +41,6 @@ namespace GiNaC { class color : public indexed { GINAC_DECLARE_REGISTERED_CLASS(color, indexed) -public: - static const tinfo_static_t return_type_tinfo_static[256]; - // other constructors public: color(const ex & b, unsigned char rl = 0); @@ -60,7 +57,7 @@ protected: ex thiscontainer(const exvector & v) const; ex thiscontainer(std::auto_ptr vp) const; unsigned return_type() const { return return_types::noncommutative; } - tinfo_t return_type_tinfo() const { return color::return_type_tinfo_static+representation_label; } + return_type_t return_type_tinfo() const; // non-virtual functions in this class public: diff --git a/ginac/ex.h b/ginac/ex.h index 4a603ce0..60d4080c 100644 --- a/ginac/ex.h +++ b/ginac/ex.h @@ -217,7 +217,7 @@ public: // noncommutativity unsigned return_type() const { return bp->return_type(); } - tinfo_t return_type_tinfo() const { return bp->return_type_tinfo(); } + return_type_t return_type_tinfo() const { return bp->return_type_tinfo(); } unsigned gethash() const { return bp->gethash(); } diff --git a/ginac/function.pl b/ginac/function.pl index b478023f..88be68d8 100644 --- a/ginac/function.pl +++ b/ginac/function.pl @@ -410,7 +410,7 @@ $print_func_interface return *this; } - function_options & set_return_type(unsigned rt, tinfo_t rtt=NULL); + function_options & set_return_type(unsigned rt, const return_type_t* rtt = 0); function_options & do_not_evalf_params(); function_options & remember(unsigned size, unsigned assoc_size=0, unsigned strategy=remember_strategies::delete_never); @@ -445,7 +445,7 @@ protected: bool use_return_type; unsigned return_type; - tinfo_t return_type_tinfo; + return_type_t return_type_tinfo; bool use_remember; unsigned remember_size; @@ -520,7 +520,7 @@ protected: bool is_equal_same_type(const basic & other) const; bool match_same_type(const basic & other) const; unsigned return_type() const; - tinfo_t return_type_tinfo() const; + return_type_t return_type_tinfo() const; // new virtual functions which can be overridden by derived classes // none @@ -739,11 +739,14 @@ function_options& function_options::series_func(series_funcp_exvector s) return *this; } -function_options & function_options::set_return_type(unsigned rt, tinfo_t rtt) +function_options & function_options::set_return_type(unsigned rt, const return_type_t* rtt) { use_return_type = true; return_type = rt; - return_type_tinfo = rtt; + if (rtt != 0) + return_type_tinfo = *rtt; + else + return_type_tinfo = make_return_type_t(); return *this; } @@ -1258,7 +1261,7 @@ unsigned function::return_type() const } } -tinfo_t function::return_type_tinfo() const +return_type_t function::return_type_tinfo() const { GINAC_ASSERT(serial(); else return seq.begin()->return_type_tinfo(); } diff --git a/ginac/indexed.h b/ginac/indexed.h index 3c8951a1..82a45c34 100644 --- a/ginac/indexed.h +++ b/ginac/indexed.h @@ -156,7 +156,7 @@ protected: ex thiscontainer(const exvector & v) const; ex thiscontainer(std::auto_ptr vp) const; unsigned return_type() const; - tinfo_t return_type_tinfo() const { return op(0).return_type_tinfo(); } + return_type_t return_type_tinfo() const { return op(0).return_type_tinfo(); } ex expand(unsigned options = 0) const; // new virtual functions which can be overridden by derived classes diff --git a/ginac/integral.cpp b/ginac/integral.cpp index 2a332083..22004756 100644 --- a/ginac/integral.cpp +++ b/ginac/integral.cpp @@ -436,7 +436,7 @@ unsigned integral::return_type() const return f.return_type(); } -tinfo_t integral::return_type_tinfo() const +return_type_t integral::return_type_tinfo() const { return f.return_type_tinfo(); } diff --git a/ginac/integral.h b/ginac/integral.h index c85a6827..e2843c82 100644 --- a/ginac/integral.h +++ b/ginac/integral.h @@ -51,7 +51,7 @@ public: ex expand(unsigned options = 0) const; exvector get_free_indices() const; unsigned return_type() const; - tinfo_t return_type_tinfo() const; + return_type_t return_type_tinfo() const; ex conjugate() const; ex eval_integ() const; protected: diff --git a/ginac/mul.cpp b/ginac/mul.cpp index 63f92e14..96b965d1 100644 --- a/ginac/mul.cpp +++ b/ginac/mul.cpp @@ -855,10 +855,10 @@ unsigned mul::return_type() const return all_commutative ? return_types::commutative : return_types::noncommutative; } -tinfo_t mul::return_type_tinfo() const +return_type_t mul::return_type_tinfo() const { if (seq.empty()) - return this; // mul without factors: should not happen + return make_return_type_t(); // mul without factors: should not happen // return type_info of first noncommutative element epvector::const_iterator i = seq.begin(), end = seq.end(); @@ -868,7 +868,7 @@ tinfo_t mul::return_type_tinfo() const ++i; } // no noncommutative element found, should not happen - return this; + return make_return_type_t(); } ex mul::thisexpairseq(const epvector & v, const ex & oc, bool do_index_renaming) const diff --git a/ginac/mul.h b/ginac/mul.h index 20faf6af..a11b29b5 100644 --- a/ginac/mul.h +++ b/ginac/mul.h @@ -68,7 +68,7 @@ protected: ex derivative(const symbol & s) const; ex eval_ncmul(const exvector & v) const; unsigned return_type() const; - tinfo_t return_type_tinfo() const; + return_type_t return_type_tinfo() const; ex thisexpairseq(const epvector & v, const ex & oc, bool do_index_renaming = false) const; ex thisexpairseq(std::auto_ptr vp, const ex & oc, bool do_index_renaming = false) const; expair split_ex_to_pair(const ex & e) const; diff --git a/ginac/ncmul.cpp b/ginac/ncmul.cpp index a5969f31..e0bc7dee 100644 --- a/ginac/ncmul.cpp +++ b/ginac/ncmul.cpp @@ -401,13 +401,13 @@ ex ncmul::eval(int level) const size_t assoc_num = assocseq.size(); exvectorvector evv; - std::vector rttinfos; + std::vector rttinfos; evv.reserve(assoc_num); rttinfos.reserve(assoc_num); cit = assocseq.begin(), citend = assocseq.end(); while (cit != citend) { - tinfo_t ti = cit->return_type_tinfo(); + return_type_t ti = cit->return_type_tinfo(); size_t rtt_num = rttinfos.size(); // search type in vector of known types for (i=0; i(); // return type_info of first noncommutative element exvector::const_iterator i = seq.begin(), end = seq.end(); @@ -592,7 +592,7 @@ tinfo_t ncmul::return_type_tinfo() const } // no noncommutative element found, should not happen - return this; + return make_return_type_t(); } ////////// diff --git a/ginac/ncmul.h b/ginac/ncmul.h index 7b21cf8b..91014274 100644 --- a/ginac/ncmul.h +++ b/ginac/ncmul.h @@ -71,7 +71,7 @@ public: protected: ex derivative(const symbol & s) const; unsigned return_type() const; - tinfo_t return_type_tinfo() const; + return_type_t return_type_tinfo() const; // new virtual functions which can be overridden by derived classes // none diff --git a/ginac/power.cpp b/ginac/power.cpp index a4a33c57..ca7fea54 100644 --- a/ginac/power.cpp +++ b/ginac/power.cpp @@ -769,7 +769,7 @@ unsigned power::return_type() const return basis.return_type(); } -tinfo_t power::return_type_tinfo() const +return_type_t power::return_type_tinfo() const { return basis.return_type_tinfo(); } diff --git a/ginac/power.h b/ginac/power.h index 5c59ee7f..a13f7fcf 100644 --- a/ginac/power.h +++ b/ginac/power.h @@ -74,7 +74,7 @@ protected: ex derivative(const symbol & s) const; ex eval_ncmul(const exvector & v) const; unsigned return_type() const; - tinfo_t return_type_tinfo() const; + return_type_t return_type_tinfo() const; ex expand(unsigned options = 0) const; // new virtual functions which can be overridden by derived classes diff --git a/ginac/registrar.h b/ginac/registrar.h index d1f4a5c9..d8a64db2 100644 --- a/ginac/registrar.h +++ b/ginac/registrar.h @@ -38,6 +38,43 @@ class archive_node; template