From 808c9d678e7b1a05713387b7c12def5a60a1ada2 Mon Sep 17 00:00:00 2001 From: Chris Dams Date: Fri, 23 Jun 2006 12:56:39 +0000 Subject: [PATCH] Added calchash and compare_same_type for symmetries. Necessary for archiving. --- ginac/symmetry.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++-- ginac/symmetry.h | 1 + 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/ginac/symmetry.cpp b/ginac/symmetry.cpp index 96225e74..9cee0ffb 100644 --- a/ginac/symmetry.cpp +++ b/ginac/symmetry.cpp @@ -142,11 +142,69 @@ int symmetry::compare_same_type(const basic & other) const { GINAC_ASSERT(is_a(other)); - // All symmetry trees are equal. They are not supposed to appear in - // ordinary expressions anyway... + // For archiving purposes we need to have an ordering of symmetries. + const symmetry &othersymm = ex_to(other); + + // Compare type. + if (type > othersymm.type) + return 1; + if (type < othersymm.type) + return -1; + + // Compare the index set. + size_t this_size = indices.size(); + size_t that_size = othersymm.indices.size(); + if (this_size > that_size) + return 1; + if (this_size < that_size) + return -1; + typedef std::set::iterator set_it; + set_it end = indices.end(); + for (set_it i=indices.begin(),j=othersymm.indices.begin(); i!=end; ++i,++j) { + if(*i < *j) + return 1; + if(*i > *j) + return -1; + } + + // Compare the children. + if (children.size() > othersymm.children.size()) + return 1; + if (children.size() < othersymm.children.size()) + return -1; + for (size_t i=0; i(children[i]) + .compare_same_type(ex_to(othersymm.children[i])); + if (cmpval) + return cmpval; + } + return 0; } +unsigned symmetry::calchash() const +{ + unsigned v = golden_ratio_hash((p_int)tinfo()); + + if (type == none) { + v = rotate_left(v); + v ^= *(indices.begin()); + } else { + for (exvector::const_iterator i=children.begin(); i!=children.end(); ++i) + { + v = rotate_left(v); + v ^= i->gethash(); + } + } + + if (flags & status_flags::evaluated) { + setflag(status_flags::hash_calculated); + hashvalue = v; + } + + return v; +} + void symmetry::do_print(const print_context & c, unsigned level) const { if (children.empty()) { diff --git a/ginac/symmetry.h b/ginac/symmetry.h index 3720ee72..4168013e 100644 --- a/ginac/symmetry.h +++ b/ginac/symmetry.h @@ -84,6 +84,7 @@ public: protected: void do_print(const print_context & c, unsigned level) const; void do_print_tree(const print_tree & c, unsigned level) const; + unsigned calchash() const; // member variables private: -- 2.29.2