diff --git a/DDParsers/src/Evaluator/hash_map.src b/DDParsers/src/Evaluator/hash_map.src index 662254ec9f8c4ef21c327e79da24a241d2398659..c68ffb780406b90dfb0e535ab84c22268f2e7030 100644 --- a/DDParsers/src/Evaluator/hash_map.src +++ b/DDParsers/src/Evaluator/hash_map.src @@ -55,8 +55,8 @@ public: public: hash_map_iterator() : entry(0) {} hash_map_iterator(Entry & e) : entry(&e) {} - std::pair<const K,T> & operator * () const { return entry->data; } - std::pair<const K,T> * operator ->() const { return &(operator*()); } + std::pair<const K,T>& operator * () const { return entry->data; } + std::pair<const K,T>* operator ->() const { return &(operator*()); } bool operator==(hash_map_iterator i) const { return (entry == i.entry); } @@ -65,11 +65,26 @@ public: } }; + class hash_map_const_iterator { // Hash_map iterator + Entry const* entry; + public: + hash_map_const_iterator() : entry(0) {} + hash_map_const_iterator(Entry & e) : entry(&e) {} + std::pair<const K,T> const& operator * () const { return entry->data; } + std::pair<const K,T> const* operator ->() const { return &(operator*()); } + bool operator==(hash_map_const_iterator i) const { + return (entry == i.entry); + } + bool operator!=(hash_map_const_iterator i) const { + return (entry != i.entry); + } + }; + public: typedef unsigned int size_type; typedef std::pair<const K,T> value_type; typedef hash_map_iterator iterator; - typedef hash_map_iterator const_iterator; + typedef hash_map_const_iterator const_iterator; private: Entry** table; // Hash table: pointers to entries @@ -97,6 +112,11 @@ private: return (a == b); } + bool eq(const char * a, const string& b) const { + return (b == a); + } + + public: // Constructor. @@ -160,7 +180,15 @@ public: } // Finds element with given key. - iterator find(const K & key) const { + const_iterator find(const K & key) const { + size_type i = hash(key) % max_size; + for (Entry* p=table[i]; p; p=p->next) { + if (eq(key,p->data.first)) return const_iterator(*p); + } + return end(); + } + + iterator find(const K & key) { size_type i = hash(key) % max_size; for (Entry* p=table[i]; p; p=p->next) { if (eq(key,p->data.first)) return iterator(*p); @@ -168,6 +196,16 @@ public: return end(); } + // Finds element with a key equivalent. + template<typename K2> + const_iterator find(const K2 & key) const { + size_type i = hash(key) % max_size; + for (Entry* p=table[i]; p; p=p->next) { + if (eq(key,p->data.first)) return const_iterator(*p); + } + return end(); + } + // Erases element with given key. size_type erase(const K & key) { size_type i = hash(key) % max_size; @@ -199,7 +237,8 @@ public: } // Returns end iterator. - iterator end() const { return iterator(); } + const_iterator end() const { return const_iterator(); } + iterator end() { return iterator(); } #ifdef DEBUG_MODE // Prints content of the hash table.