Skip to content
Snippets Groups Projects
Commit ce8b0430 authored by Christopher Jones's avatar Christopher Jones Committed by MarkusFrankATcernch
Browse files

minor improvements to ::hash_map

- improved const correctness
- allow find to take an argument which is equivalent to the key
parent 2cb9bd8c
No related branches found
No related tags found
No related merge requests found
......@@ -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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment