From 47f6c6302fae346da71913149a59b104b404847d Mon Sep 17 00:00:00 2001 From: Tor Egge Date: Mon, 4 Feb 2019 12:54:47 +0100 Subject: Simplify legacy hash function in juniper. --- juniper/src/vespa/juniper/hashbase.h | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) (limited to 'juniper/src') diff --git a/juniper/src/vespa/juniper/hashbase.h b/juniper/src/vespa/juniper/hashbase.h index d651e1d2705..e826ef876c8 100644 --- a/juniper/src/vespa/juniper/hashbase.h +++ b/juniper/src/vespa/juniper/hashbase.h @@ -112,14 +112,6 @@ public: }; -// Basis for specialization -template -struct Int2Type -{ - enum { value = v }; -}; - - template class Fast_HashTable { @@ -140,19 +132,13 @@ protected: element **_lookupTable; Comparator _compare; - inline int HashFunction(Key key, Int2Type) - { - return key & (_tableSize-1); - } - - inline int HashFunction(Key key, Int2Type) - { - return key % _tableSize; - } - inline int HashFunction(Key key) { - return HasFunction(key, Int2Type<(_tableSize & (_tableSize-1) == _tableSize)>()); + if constexpr ((_tableSize & (_tableSize - 1)) == 0) { + return (key & (_tableSize - 1)); + } else { + return (key % _tableSize); + } } public: @@ -197,7 +183,7 @@ public: Key Insert(Key key, T item) { - int pos = HashFunction(key, Int2Type<((_tableSize & (_tableSize-1)) == _tableSize)>()); + int pos = HashFunction(key); if (_lookupTable[pos] == NULL || !_compare(item, _lookupTable[pos]->GetItem())) { @@ -226,7 +212,7 @@ public: T retVal; retVal = NULL; - int pos = HashFunction(key, Int2Type<(_tableSize & (_tableSize-1) == _tableSize)>()); + int pos = HashFunction(key); for (element *curr=_lookupTable[pos]; curr != NULL; curr=curr->GetNext()) { @@ -243,7 +229,7 @@ public: element* FindRef(Key key) { - int pos = HashFunction(key, Int2Type<((_tableSize & (_tableSize-1)) == _tableSize)>()); + int pos = HashFunction(key); for (element *curr=_lookupTable[pos]; curr != NULL; curr=curr->GetNext()) if (curr->GetKey() == key) return curr; @@ -255,7 +241,7 @@ public: { T retVal = NULL; - int pos = HashFunction(key, Int2Type<(_tableSize & (_tableSize-1) == _tableSize)>()); + int pos = HashFunction(key); element *curr=_lookupTable[pos]; element *prev = NULL; -- cgit v1.2.3