Skip to content

Commit

Permalink
make Map use equals from Hash (defaults to operator== anyway)
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Suda committed Nov 15, 2017
1 parent 5c17f89 commit 6cd9650
Showing 1 changed file with 27 additions and 45 deletions.
72 changes: 27 additions & 45 deletions Lib/Map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace Lib {
* anything that can be hashed to an unsigned integer
* and compared using ==
* @param Val values, can be anything
* @param Hash class containig the hash function for keys
* @param Hash class containing the "hash" and "equals" functions for keys
*/
template <typename Key, typename Val,class Hash>
class Map
Expand Down Expand Up @@ -124,13 +124,10 @@ class Map
code = 1;
}
Entry* entry;
for (entry = firstEntryForCode(code);
entry->occupied();
entry = nextEntry(entry)) {
if (entry->code == code &&
entry->key == key) {
found = entry->value;
return true;
for (entry = firstEntryForCode(code); entry->occupied(); entry = nextEntry(entry)) {
if (entry->code == code && Hash::equals(entry->key,key)) {
found = entry->value;
return true;
}
}

Expand All @@ -151,9 +148,7 @@ class Map
code = 1;
}
Entry* entry;
for (entry = firstEntryForCode(code);
entry->key != key;
entry = nextEntry(entry)) {
for (entry = firstEntryForCode(code); !Hash::equals(entry->key,key); entry = nextEntry(entry)) {
ASS(entry->occupied());
}
ASS(entry->occupied());
Expand Down Expand Up @@ -216,12 +211,9 @@ class Map
CALL("Map::insert/2");

Entry* entry;
for (entry = firstEntryForCode(code);
entry->occupied();
entry = nextEntry(entry)) {
if (entry->code == code &&
entry->key == key) {
return entry->value;
for (entry = firstEntryForCode(code); entry->occupied(); entry = nextEntry(entry)) {
if (entry->code == code && Hash::equals(entry->key,key)) {
return entry->value;
}
}
// entry is not occupied
Expand Down Expand Up @@ -252,11 +244,8 @@ class Map
code = 1;
}
Entry* entry;
for (entry = firstEntryForCode(code);
entry->occupied();
entry = nextEntry(entry)) {
if (entry->code == code &&
entry->key == key) {
for (entry = firstEntryForCode(code); entry->occupied(); entry = nextEntry(entry)) {
if (entry->code == code && Hash::equals(entry->key, key)) {
entry->value = val;
return true;
}
Expand Down Expand Up @@ -288,13 +277,10 @@ class Map
code = 1;
}
Entry* entry;
for (entry = firstEntryForCode(code);
entry->occupied();
entry = nextEntry(entry)) {
if (entry->code == code &&
entry->key == key) {
entry->value = val;
return;
for (entry = firstEntryForCode(code); entry->occupied(); entry = nextEntry(entry)) {
if (entry->code == code && Hash::equals(entry->key, key)) {
entry->value = val;
return;
}
}
#if VDEBUG
Expand All @@ -321,13 +307,10 @@ class Map
code = 1;
}
Entry* entry;
for (entry = firstEntryForCode(code);
entry->occupied();
entry = nextEntry(entry)) {
if (entry->code == code &&
Lib::DefaultEq::equals(entry->key, key)) {
pval = &entry->value;
return false;
for (entry = firstEntryForCode(code); entry->occupied(); entry = nextEntry(entry)) {
if (entry->code == code && Hash::equals(entry->key, key)) {
pval = &entry->value;
return false;
}
}
// entry is not occupied
Expand All @@ -352,7 +335,7 @@ class Map
for (int i = _capacity-1;i >= 0;i--) {
Entry& e = _entries[i];
if (e.occupied()) {
delete e.value;
delete e.value;
}
}
} // deleteAll
Expand All @@ -376,7 +359,7 @@ class Map
for (int i = _capacity-1;i >= 0;i--) {
Entry& e = _entries[i];
if (e.occupied()) {
e.value->destroy();
e.value->destroy();
}
}
} // destroyAll
Expand Down Expand Up @@ -421,7 +404,7 @@ class Map
while (remaining != 0) {
// find first occupied entry
while (! current->occupied()) {
current ++;
current ++;
}
// now current is occupied
insert(current->key,current->value,current->code);
Expand All @@ -443,8 +426,7 @@ class Map
public:
/** Create a new iterator */
inline Iterator(const Map& map)
: _next(map._entries),
_last(map._afterLast)
: _next(map._entries), _last(map._afterLast)
{
} // Map::Iterator

Expand All @@ -455,10 +437,10 @@ class Map
bool hasNext()
{
while (_next != _last) {
if (_next->occupied()) {
return true;
}
_next++;
if (_next->occupied()) {
return true;
}
_next++;
}
return false;
}
Expand Down

0 comments on commit 6cd9650

Please sign in to comment.