}
}
+uint32_t hash_bytes(uint8_t* key, size_t len)
+{
+ uint32_t a=31415u, b=27183u, hash;
+ for (hash=0; len > 0; key++, len--, a*=b)
+ hash = (a * hash) + *key;
+ return hash32(hash);
+}
+
+uint64_t hash64(uint64_t key)
+{
+ key = (~key) + (key << 21); // key = (key << 21) - key - 1;
+ key = key ^ (key >> 24);
+ key = (key + (key << 3)) + (key << 8); // key * 265
+ key = key ^ (key >> 14);
+ key = (key + (key << 2)) + (key << 4); // key * 21
+ key = key ^ (key >> 28);
+ key = key + (key << 31);
+ return key;
+}
+
+uint32_t hash32(uint32_t key)
+{
+ key = (key+0x7ed55d16) + (key<<12);
+ key = (key^0xc761c23c) ^ (key>>19);
+ key = (key+0x165667b1) + (key<<5);
+ key = (key+0xd3a2646c) ^ (key<<9);
+ key = (key+0xfd7046c5) + (key<<3);
+ key = (key^0xb55a4f09) ^ (key>>16);
+ return key;
+}
+
void hash_init(hash_t* hash, hash_hashfn_t hashfn, hash_cmpfn_t cmpfn, hash_freefn_t delfn)
{
hash->size = 0;
#include <data/hash.h>
#include <carl.h>
+static const uint Num_Iterations = 1000000;
+
typedef struct {
hash_entry_t link;
uint val;
TEST_SUITE(Hash) {
TEST(Verify sequential hash inserts and lookups)
{
- uint maxval = 1000000;
hash_t hash;
hash_init(&hash, hash_func, compare_func, delete_func);
- for (uint i = 0; i < maxval; i++)
+ for (uint i = 0; i < Num_Iterations; i++)
{
int_node_t* entry = (int_node_t*)malloc(sizeof(int_node_t));
entry->val = i;
CHECK(i+1 == hash_size(&hash));
CHECK(&(entry->link) == hash_get(&hash, &(entry->link)));
}
- for (uint i = 0; i < maxval; i++)
+ for (uint i = 0; i < Num_Iterations; i++)
{
int_node_t search = { .val = i };
hash_entry_t* entry = hash_get(&hash, &(search.link));
TEST(Verify ping pong hash inserts and lookups)
{
- uint maxval = 1000000;
hash_t hash;
hash_init(&hash, hash_func, compare_func, delete_func);
- for (uint i = 0; i < (maxval/2); i++) {
+ for (uint i = 0; i < (Num_Iterations/2); i++) {
/* Insert the lower number */
int_node_t* entry = (int_node_t*)malloc(sizeof(int_node_t));
entry->val = i;
CHECK(&(entry->link) == hash_get(&hash, &(entry->link)));
/* Insert the higher number */
entry = (int_node_t*)malloc(sizeof(int_node_t));
- entry->val = maxval - i;
+ entry->val = Num_Iterations - i;
hash_set(&hash, &(entry->link));
CHECK(&(entry->link) == hash_get(&hash, &(entry->link)));
}
- for (uint i = 0; i < (maxval/2); i++)
+ for (uint i = 0; i < (Num_Iterations/2); i++)
{
/* Find the lower number */
int_node_t search = { .val = i };
CHECK(entry != NULL);
CHECK(search.val == ientry->val);
/* Find the higher number */
- search.val = maxval-i;
+ search.val = Num_Iterations-i;
entry = hash_get(&hash, &(search.link));
ientry = container_of(entry, int_node_t, link);
CHECK(entry != NULL);
TEST(Verify sequential inserts and deletions)
{
- uint maxval = 1000000;
hash_t hash;
hash_init(&hash, hash_func, compare_func, delete_func);
- for (uint i = 0; i < maxval; i++)
+ for (uint i = 0; i < Num_Iterations; i++)
{
int_node_t* entry = (int_node_t*)malloc(sizeof(int_node_t));
entry->val = i;
CHECK(i+1 == hash_size(&hash));
CHECK(&(entry->link) == hash_get(&hash, &(entry->link)));
}
- for (uint i = 0; i < maxval; i++)
+ for (uint i = 0; i < Num_Iterations; i++)
{
int_node_t search = { .val = i };
CHECK(hash_del(&hash, &(search.link)));
- CHECK((maxval - (i+1)) == hash_size(&hash));
+ CHECK((Num_Iterations - (i+1)) == hash_size(&hash));
}
hash_deinit(&hash);
}
TEST(Verify reverse inserts and deletions)
{
- uint maxval = 1000000;
hash_t hash;
hash_init(&hash, hash_func, compare_func, delete_func);
- for (uint i = 0; i < maxval; i++)
+ for (uint i = 0; i < Num_Iterations; i++)
{
int_node_t* entry = (int_node_t*)malloc(sizeof(int_node_t));
- entry->val = maxval - i;
+ entry->val = Num_Iterations - i;
hash_set(&hash, &(entry->link));
CHECK(i+1 == hash_size(&hash));
CHECK(&(entry->link) == hash_get(&hash, &(entry->link)));
}
- for (uint i = 0; i < maxval; i++)
+ for (uint i = 0; i < Num_Iterations; i++)
{
int_node_t search = { .val = i+1 };
CHECK(hash_del(&hash, &(search.link)));
- CHECK((maxval - (i+1)) == hash_size(&hash));
+ CHECK((Num_Iterations - (i+1)) == hash_size(&hash));
}
hash_deinit(&hash);
}
TEST(Verify ping pong hash inserts and deletions)
{
- uint maxval = 1000000;
hash_t hash;
hash_init(&hash, hash_func, compare_func, delete_func);
- for (uint i = 0; i < (maxval/2); i++) {
+ for (uint i = 0; i < (Num_Iterations/2); i++) {
/* Insert the lower number */
int_node_t* entry = (int_node_t*)malloc(sizeof(int_node_t));
entry->val = i;
CHECK(&(entry->link) == hash_get(&hash, &(entry->link)));
/* Insert the higher number */
entry = (int_node_t*)malloc(sizeof(int_node_t));
- entry->val = maxval - i;
+ entry->val = Num_Iterations - i;
hash_set(&hash, &(entry->link));
CHECK(&(entry->link) == hash_get(&hash, &(entry->link)));
}
- for (uint i = 0; i < maxval; i++)
+ for (uint i = 0; i < Num_Iterations; i++)
{
int_node_t search = { .val = i };
if (hash_get(&hash, &(search.link)))