From f35b4223160406ed3e871d5ed134bd0a67fa177d Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Fri, 30 Oct 2009 06:50:26 +0100 Subject: [PATCH] SubID: add (preliminary) hash impl; unit test pass --- src/lib/sub-id.hpp | 43 +++++++++++++++++++------- tests/40components.tests | 9 +++++- tests/lib/sub-id-test.cpp | 63 +++++++++++++++++++++++++++++++-------- 3 files changed, 91 insertions(+), 24 deletions(-) diff --git a/src/lib/sub-id.hpp b/src/lib/sub-id.hpp index 257dc2638..98df7c4e7 100644 --- a/src/lib/sub-id.hpp +++ b/src/lib/sub-id.hpp @@ -49,14 +49,19 @@ #include "lib/format.hpp" //#include +#include /////TODO better push the hash implementation into a cpp file (and btw, do it more seriously!) + #include #include namespace lib { - using std::string; + using boost::hash_value; + using std::ostream; + using std::string; + using std::cout; @@ -73,15 +78,32 @@ namespace lib { virtual operator string() const =0; }; - - - ostream& - operator<< (ostream& os, SubID const& sID) - { - return os << string(sID); - } - + + inline ostream& + operator<< (ostream& os, SubID const& sID) + { + return os << string(sID); + } + + inline size_t + hash_value (SubID const& sID) + { + return hash_value (string (sID)); + } + + inline bool + operator== (SubID const& id1, SubID const& id2) + { + return (string (id1) == string (id2)); + } + + ////////TODO a serious implementation should descend recursively, instead of relying on the string representation + + + + + template class SubId @@ -96,7 +118,8 @@ namespace lib { operator string() const { - return util::str (baseID_); + using util::str; + return str (baseID_); // note: extension point } }; diff --git a/tests/40components.tests b/tests/40components.tests index f5d4d3d5e..a88881147 100644 --- a/tests/40components.tests +++ b/tests/40components.tests @@ -872,7 +872,14 @@ return: 0 END -PLANNED "extensible symbolic identifier" SubID_test < (buildIDs() ); -// buildHashtable (buildIDs() ); + checkSubIDHash(); } @@ -87,7 +94,7 @@ namespace test{ CID c2 (G); CID c3 (B); - cout << "..." << c1 << c2 << c3 << endl; + cout << "...." << c1 << c2 << c3 << endl; } @@ -107,6 +114,33 @@ namespace test{ } + void + checkSubIDHash() + { + typedef SubId CID; + typedef SubId UID; + typedef ExtendedSubId CUID; + + vector simpleIDs; + simpleIDs.push_back(CID(R)); + simpleIDs.push_back(CID(R)); + simpleIDs.push_back(CID(G)); + simpleIDs.push_back(CID(B)); + + vector extendedIDs; + extendedIDs.push_back(CUID(R,22)); + extendedIDs.push_back(CUID(R,22)); // note the duplicates get dropped + extendedIDs.push_back(CUID(R,23)); + extendedIDs.push_back(CUID(R,24)); + extendedIDs.push_back(CUID(G,24)); + extendedIDs.push_back(CUID(B,25)); + + buildHashtable (simpleIDs); + buildHashtable (extendedIDs); + } + + + template struct HashTable : std::tr1::unordered_map > @@ -114,13 +148,14 @@ namespace test{ void add (KEY key) { - *this[key] = string(key); + (*this)[key] = string(key); } void verify (KEY key) { - ASSERT (string(key) == *this[key]); + cout << "verify....." << key << endl; + ASSERT (string(key) == (*this)[key]); } }; @@ -133,8 +168,10 @@ namespace test{ typedef HashTable HTab; HTab tab; - for_each (keys, bind (&HTab::add, tab, _1 )); - for_each (keys, bind (&HTab::verify, tab, _1 )); + for_each (keys, bind (&HTab::add, ref(tab), _1 )); + for_each (keys, bind (&HTab::verify, ref(tab), _1 )); + + cout << "Elements in hashtable: " << tab.size() << endl; }