[kutil] Add kutil::map hash map
Added the kutil::map collection, an open addressing, robinhood hash map with backwards shift deletes. Also added hash.h with templated implementations of the FNV-1a 64 bit hash function, and pulled the log2 function out of the heap_allocator code into the new util.h.
This commit is contained in:
63
src/tests/map.cpp
Normal file
63
src/tests/map.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include "kutil/map.h"
|
||||
#include "catch.hpp"
|
||||
|
||||
TEST_CASE( "map insertion", "[containers] [vector]" )
|
||||
{
|
||||
using clock = std::chrono::system_clock;
|
||||
unsigned seed = clock::now().time_since_epoch().count();
|
||||
std::default_random_engine rng {seed};
|
||||
std::uniform_int_distribution<int> distrib {0, 10000};
|
||||
|
||||
|
||||
size_t sizes[] = {1, 2, 3, 5, 100};
|
||||
for (size_t s : sizes) {
|
||||
kutil::map<int, int> v;
|
||||
std::vector<int> r;
|
||||
|
||||
for (int i = 0; i < s; ++i) {
|
||||
int j = distrib(rng);
|
||||
r.push_back(j);
|
||||
v.insert(j, j);
|
||||
}
|
||||
|
||||
for (int i : r) {
|
||||
int *p = v.find(i);
|
||||
CAPTURE( i );
|
||||
CHECK( p );
|
||||
CHECK( *p == i );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE( "map deletion", "[containers] [vector]" )
|
||||
{
|
||||
using clock = std::chrono::system_clock;
|
||||
unsigned seed = clock::now().time_since_epoch().count();
|
||||
std::default_random_engine rng {seed};
|
||||
std::uniform_int_distribution<int> distrib {0, 10000};
|
||||
|
||||
size_t sizes[] = {1, 2, 3, 5, 100};
|
||||
for (size_t s : sizes) {
|
||||
kutil::map<int, int> v;
|
||||
std::vector<int> r;
|
||||
|
||||
for (int i = 0; i < s; ++i) {
|
||||
int j = distrib(rng);
|
||||
r.push_back(j);
|
||||
v.insert(j, j);
|
||||
}
|
||||
|
||||
for (int i = 0; i < s; i += 2) {
|
||||
v.erase(r[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < s; ++i) {
|
||||
int *p = v.find(r[i]);
|
||||
CAPTURE( i );
|
||||
if ( i%2 )
|
||||
CHECK( p );
|
||||
else
|
||||
CHECK( !p );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user