Last active
August 29, 2015 14:01
-
-
Save DieHertz/293317b239b6f4451f62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <memory> | |
inline int32_t murmur_hash_3_finalizer(int32_t key) { | |
key ^= key >> 16; | |
key *= 0x85ebca6b; | |
key ^= key >> 13; | |
key *= 0xc2b2ae35; | |
key ^= key >> 16; | |
return key; | |
} | |
class fixed_hash_map { | |
struct bucket { | |
int32_t key = 0; | |
int32_t val = 0; | |
}; | |
const int32_t size; | |
std::unique_ptr<bucket[]> buckets; | |
public: | |
fixed_hash_map(const int32_t size) : size{size}, buckets{new bucket[size]} {} | |
int32_t get(const int32_t key) const { | |
const auto hash = murmur_hash_3_finalizer(key); | |
auto idx = hash % size; | |
while (buckets[idx].key != key && buckets[idx].key != 0) idx = (idx + 1) % size; | |
return buckets[idx].val; | |
} | |
void set(const int32_t key, const int32_t val) { | |
const auto hash = murmur_hash_3_finalizer(key); | |
auto idx = hash % size; | |
while (buckets[idx].key != 0) idx = (idx + 1) % size; | |
buckets[idx].key = key; | |
buckets[idx].val = val; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment