From: Alexei Sheplyakov Date: Wed, 15 Oct 2008 11:32:11 +0000 (+0400) Subject: calchash(): use type_info::name() instead of tinfo(). X-Git-Tag: release_1-5-0~48 X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=commitdiff_plain;h=b573b0146341709649f373e0fc5c0d440877ebaf calchash(): use type_info::name() instead of tinfo(). Custom RTTI considered harmful, part 4. The hash value of the object of different types should be different whenever possible. Hence calcash() needs a unique per type number. Previously we used tinfo_key for this. typeinfo::name() (a *pointer* to implementation dependent string) is also unique for each class, so it's just as good as tinfo() is. --- diff --git a/ginac/basic.cpp b/ginac/basic.cpp index 1f9ccbac..fa6e1d96 100644 --- a/ginac/basic.cpp +++ b/ginac/basic.cpp @@ -787,7 +787,8 @@ tinfo_t basic::return_type_tinfo() const * would all end up with the same hashvalue. */ unsigned basic::calchash() const { - unsigned v = golden_ratio_hash((p_int)tinfo()); + const void* this_tinfo = (const void*)typeid(*this).name(); + unsigned v = golden_ratio_hash((p_int)this_tinfo); for (size_t i=0; iop(i).gethash(); diff --git a/ginac/constant.cpp b/ginac/constant.cpp index 13acc363..b63427fc 100644 --- a/ginac/constant.cpp +++ b/ginac/constant.cpp @@ -216,7 +216,8 @@ bool constant::is_equal_same_type(const basic & other) const unsigned constant::calchash() const { - hashvalue = golden_ratio_hash((p_int)tinfo() ^ serial); + const void* typeid_this = (const void*)typeid(*this).name(); + hashvalue = golden_ratio_hash((p_int)typeid_this ^ serial); setflag(status_flags::hash_calculated); diff --git a/ginac/expairseq.cpp b/ginac/expairseq.cpp index 120672f7..39f7931f 100644 --- a/ginac/expairseq.cpp +++ b/ginac/expairseq.cpp @@ -615,7 +615,8 @@ unsigned expairseq::return_type() const unsigned expairseq::calchash() const { - unsigned v = golden_ratio_hash((p_int)this->tinfo()); + const void* this_tinfo = (const void*)typeid(*this).name(); + unsigned v = golden_ratio_hash((p_int)this_tinfo); epvector::const_iterator i = seq.begin(); const epvector::const_iterator end = seq.end(); while (i != end) { diff --git a/ginac/function.pl b/ginac/function.pl index 7a4d5e78..b478023f 100644 --- a/ginac/function.pl +++ b/ginac/function.pl @@ -1066,7 +1066,8 @@ ex function::eval_ncmul(const exvector & v) const unsigned function::calchash() const { - unsigned v = golden_ratio_hash(golden_ratio_hash((p_int)tinfo()) ^ serial); + const void* this_tinfo = (const void*)typeid(*this).name(); + unsigned v = golden_ratio_hash(golden_ratio_hash((p_int)this_tinfo) ^ serial); for (size_t i=0; iop(i).gethash(); diff --git a/ginac/idx.cpp b/ginac/idx.cpp index c1412b9d..174f95f0 100644 --- a/ginac/idx.cpp +++ b/ginac/idx.cpp @@ -352,7 +352,8 @@ unsigned idx::calchash() const // hash keys. That is, the hash values must not depend on the index // dimensions or other attributes (variance etc.). // The compare_same_type() methods will take care of the rest. - unsigned v = golden_ratio_hash((p_int)tinfo()); + const void* this_tinfo = (const void*)(typeid(*this).name()); + unsigned v = golden_ratio_hash((p_int)this_tinfo); v = rotate_left(v); v ^= value.gethash(); @@ -504,7 +505,7 @@ ex spinidx::toggle_variance_dot() const bool is_dummy_pair(const idx & i1, const idx & i2) { // The indices must be of exactly the same type - if (i1.tinfo() != i2.tinfo()) + if (typeid(i1) != typeid(i2)) return false; // Same type, let the indices decide whether they are paired diff --git a/ginac/relational.cpp b/ginac/relational.cpp index 434b620a..2f19d213 100644 --- a/ginac/relational.cpp +++ b/ginac/relational.cpp @@ -258,7 +258,8 @@ tinfo_t relational::return_type_tinfo() const unsigned relational::calchash() const { - unsigned v = golden_ratio_hash((p_int)tinfo()); + const void* this_tinfo = (const void*)typeid(*this).name(); + unsigned v = golden_ratio_hash((p_int)this_tinfo); unsigned lhash = lh.gethash(); unsigned rhash = rh.gethash(); diff --git a/ginac/symbol.cpp b/ginac/symbol.cpp index 6eedfef3..f233d84e 100644 --- a/ginac/symbol.cpp +++ b/ginac/symbol.cpp @@ -287,7 +287,8 @@ bool symbol::is_equal_same_type(const basic & other) const unsigned symbol::calchash() const { - hashvalue = golden_ratio_hash((p_int)tinfo() ^ serial); + const void* this_tinfo = (const void*)typeid(*this).name(); + hashvalue = golden_ratio_hash((p_int)this_tinfo ^ serial); setflag(status_flags::hash_calculated); return hashvalue; } diff --git a/ginac/symmetry.cpp b/ginac/symmetry.cpp index 215a48a0..9a158926 100644 --- a/ginac/symmetry.cpp +++ b/ginac/symmetry.cpp @@ -185,7 +185,8 @@ int symmetry::compare_same_type(const basic & other) const unsigned symmetry::calchash() const { - unsigned v = golden_ratio_hash((p_int)tinfo()); + const void* this_tinfo = (const void*)typeid(*this).name(); + unsigned v = golden_ratio_hash((p_int)this_tinfo); if (type == none) { v = rotate_left(v); diff --git a/ginac/wildcard.cpp b/ginac/wildcard.cpp index b5abfa14..4a89957b 100644 --- a/ginac/wildcard.cpp +++ b/ginac/wildcard.cpp @@ -104,9 +104,10 @@ void wildcard::do_print_python_repr(const print_python_repr & c, unsigned level) unsigned wildcard::calchash() const { // this is where the schoolbook method - // (golden_ratio_hash(tinfo()) ^ label) + // (golden_ratio_hash(typeid(*this).name()) ^ label) // is not good enough yet... - hashvalue = golden_ratio_hash(golden_ratio_hash((p_int)tinfo()) ^ label); + const void* this_tinfo = (const void*)typeid(*this).name(); + hashvalue = golden_ratio_hash(golden_ratio_hash((p_int)this_tinfo) ^ label); setflag(status_flags::hash_calculated); return hashvalue; }