From 11fcbe7ea462936b79807a1f12273e5685f3f744 Mon Sep 17 00:00:00 2001 From: Alexei Sheplyakov Date: Wed, 15 Oct 2008 15:32:11 +0400 Subject: [PATCH] basic, expairseq: use standard C++ RTTI in compare(), is_equal(), match(), etc. Custom RTTI considered harmful, part 2. * basic.cpp: use standard C++ RTTI in compare(), is_equal(), operator=(), match(). * expairseq.cpp: use standard C++ RTTI in match(), make_flat(), construct_from_2_ex(). --- ginac/basic.cpp | 21 ++++++++------------- ginac/expairseq.cpp | 16 ++++++++-------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/ginac/basic.cpp b/ginac/basic.cpp index e8c893ba..1f9ccbac 100644 --- a/ginac/basic.cpp +++ b/ginac/basic.cpp @@ -22,9 +22,7 @@ #include #include -#ifdef DO_GINAC_ASSERT -# include -#endif +#include #include "basic.h" #include "ex.h" @@ -65,7 +63,7 @@ basic::basic(const basic & other) : tinfo_key(other.tinfo_key), flags(other.flag const basic & basic::operator=(const basic & other) { unsigned fl = other.flags & ~status_flags::dynallocated; - if (tinfo_key != other.tinfo_key) { + if (typeid(*this) != typeid(other)) { // The other object is of a derived class, so clear the flags as they // might no longer apply (especially hash_calculated). Oh, and don't // copy the tinfo_key: it is already set correctly for this object. @@ -569,7 +567,7 @@ bool basic::match(const ex & pattern, exmap& repl_lst) const } else { // Expression must be of the same type as the pattern - if (tinfo() != ex_to(pattern).tinfo()) + if (typeid(*this) != typeid(ex_to(pattern))) return false; // Number of subexpressions must match @@ -846,10 +844,9 @@ int basic::compare(const basic & other) const compare_statistics.compare_same_hashvalue++; #endif - const tinfo_t typeid_this = tinfo(); - const tinfo_t typeid_other = other.tinfo(); - if (typeid_this==typeid_other) { - GINAC_ASSERT(typeid(*this)==typeid(other)); + const std::type_info& typeid_this = typeid(*this); + const std::type_info& typeid_other = typeid(other); + if (typeid_this == typeid_other) { // int cmpval = compare_same_type(other); // if (cmpval!=0) { // std::cout << "hash collision, same type: " @@ -871,7 +868,7 @@ int basic::compare(const basic & other) const // std::cout << " and "; // other.print(print_tree(std::cout)); // std::cout << std::endl; - return (typeid_thistinfo()!=other.tinfo()) + if (typeid(*this) != typeid(other)) return false; - GINAC_ASSERT(typeid(*this)==typeid(other)); - #ifdef GINAC_COMPARE_STATISTICS compare_statistics.is_equal_same_type++; #endif diff --git a/ginac/expairseq.cpp b/ginac/expairseq.cpp index 67aa4f8e..120672f7 100644 --- a/ginac/expairseq.cpp +++ b/ginac/expairseq.cpp @@ -396,7 +396,7 @@ bool expairseq::match(const ex & pattern, exmap & repl_lst) const // This differs from basic::match() because we want "a+b+c+d" to // match "d+*+b" with "*" being "a+c", and we want to honor commutativity - if (this->tinfo() == ex_to(pattern).tinfo()) { + if (typeid(*this) == typeid(ex_to(pattern))) { // Check whether global wildcard (one that matches the "rest of the // expression", like "*" above) is present @@ -793,8 +793,8 @@ void expairseq::construct_from_2_ex_via_exvector(const ex &lh, const ex &rh) void expairseq::construct_from_2_ex(const ex &lh, const ex &rh) { - if (ex_to(lh).tinfo()==this->tinfo()) { - if (ex_to(rh).tinfo()==this->tinfo()) { + if (typeid(ex_to(lh)) == typeid(*this)) { + if (typeid(ex_to(rh)) == typeid(*this)) { #if EXPAIRSEQ_USE_HASHTAB unsigned totalsize = ex_to(lh).seq.size() + ex_to(rh).seq.size(); @@ -828,7 +828,7 @@ void expairseq::construct_from_2_ex(const ex &lh, const ex &rh) #endif // EXPAIRSEQ_USE_HASHTAB return; } - } else if (ex_to(rh).tinfo()==this->tinfo()) { + } else if (typeid(ex_to(rh)) == typeid(*this)) { #if EXPAIRSEQ_USE_HASHTAB unsigned totalsize=ex_to(rh).seq.size()+1; if (calc_hashtabsize(totalsize)!=0) { @@ -1051,7 +1051,7 @@ void expairseq::make_flat(const exvector &v) cit = v.begin(); while (cit!=v.end()) { - if (ex_to(*cit).tinfo()==this->tinfo()) { + if (typeid(ex_to(*cit)) == typeid(*this)) { ++nexpairseqs; noperands += ex_to(*cit).seq.size(); } @@ -1068,7 +1068,7 @@ void expairseq::make_flat(const exvector &v) make_flat_inserter mf(v, do_idx_rename); cit = v.begin(); while (cit!=v.end()) { - if (ex_to(*cit).tinfo()==this->tinfo()) { + if (typeid(ex_to(*cit)) == typeid(*this)) { ex newfactor = mf.handle_factor(*cit, _ex1); const expairseq &subseqref = ex_to(newfactor); combine_overall_coeff(subseqref.overall_coeff); @@ -1103,7 +1103,7 @@ void expairseq::make_flat(const epvector &v, bool do_index_renaming) cit = v.begin(); while (cit!=v.end()) { - if (ex_to(cit->rest).tinfo()==this->tinfo()) { + if (typeid(ex_to(cit->rest)) == typeid(*this)) { ++nexpairseqs; noperands += ex_to(cit->rest).seq.size(); } @@ -1121,7 +1121,7 @@ void expairseq::make_flat(const epvector &v, bool do_index_renaming) // copy elements and split off numerical part cit = v.begin(); while (cit!=v.end()) { - if (ex_to(cit->rest).tinfo()==this->tinfo() && + if ((typeid(ex_to(cit->rest)) == typeid(*this)) && this->can_make_flat(*cit)) { ex newrest = mf.handle_factor(cit->rest, cit->coeff); const expairseq &subseqref = ex_to(newrest); -- 2.44.0