This is a C++ header only template implementation of Cuckoo-Hash Map and Set with an iterator.
There are 2 constructors for the Cuckoo HashMap:
CuckooHashMap():
Default constructor, sets downsizeThresh
to 0.2. These were values taken from the original paper on cuckoo hashing by Rasmus Pugh and Flemming Friche Rodler.
CuckooHashMap(epsilon, downsizeThresh):
Allows the user to set the epsilon value and downsizing threshold.
bool contains(key):
Checks if the key is in the table
void insert(key, value):
Insert an item into the hash table
type lookup(key):
Finds the value associated with key
.
void erase(key):
Removes a key-value pair from the hash table
type operator[]:
looks up a value in the table. If the value already exists, supports reassignment, but not insertion.
size_t size():
Returns the number of elements in the map
bool empty():
Checks if the map is empty or not.
double loadFactor():
Returns the load factor of the hash map
void clear():
Clears the hashmap
There are 2 constructors for the Cuckoo HashSet, same as the Cuckoo HashMap
CuckooHashSet():
Default constructor, sets downsizeThresh
to 0.2.
CuckooHashSet(epsilon, downsizeThresh)
allows the user to specify epsilon and downsize threshold.
bool contains(key):
Checks if the key is in the set
void insert(key):
Insert an item into the hash set
void erase(key):
Removes a key from the set. Possibly downsizes the table
size_t size():
Returns the number of elements in the set
bool empty():
Checks if the set is empty or not.
double loadFactor():
Returns the load factor of the hash map
void clear:
Clears the hash map.
- The iterator uses
begin()
andend()
and works with the notationfor (auto& x : map)
to iterate over the entire map. - Iterator is invalidated when inserting, erasing or clearing.
- Keys (and Values) must implement a copy constructor for insertion.
- Both the hash set and hashmap allow for printing with the
<<
streaming operator. - The file
cuckoo-test.cpp
is a completely non-comprehensive test suite for both the CuckooHashSet and CuckooHashMap.
contains(key)
:
lookup(key)
:
insert(key, value)
:
erase(key)
:
empty(), size(), loadFactor():
Insert and remove sometimes will resize the table and rehash all keys. Insertion triggers a rehash when