[Glacier] Move hashmap to a real hash implementation.
This commit is contained in:
parent
76fd3fc176
commit
28d9e37b87
|
@ -4,6 +4,7 @@ set(glacier_files
|
||||||
string/string_view.cpp
|
string/string_view.cpp
|
||||||
string/str_format.cpp
|
string/str_format.cpp
|
||||||
string/str_split.cpp
|
string/str_split.cpp
|
||||||
|
util/hash.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_library(glacier STATIC
|
add_library(glacier STATIC
|
||||||
|
|
|
@ -7,35 +7,11 @@
|
||||||
#include "glacier/container/pair.h"
|
#include "glacier/container/pair.h"
|
||||||
#include "glacier/status/error.h"
|
#include "glacier/status/error.h"
|
||||||
#include "glacier/string/string.h"
|
#include "glacier/string/string.h"
|
||||||
|
#include "glacier/util/hash.h"
|
||||||
|
|
||||||
namespace glcr {
|
namespace glcr {
|
||||||
|
|
||||||
template <typename T>
|
template <typename K, typename V, class H = Hash<K>>
|
||||||
struct HashFunc {
|
|
||||||
uint64_t operator()(const T&);
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct HashFunc<uint64_t> {
|
|
||||||
uint64_t operator()(const uint64_t& value) {
|
|
||||||
// FIXME: Write a real hash function.
|
|
||||||
return 0xABBAABBAABBAABBA ^ value;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct HashFunc<String> {
|
|
||||||
uint64_t operator()(const String& value) {
|
|
||||||
// FIXME: Write a real hash function.
|
|
||||||
uint64_t acc = 0;
|
|
||||||
for (uint64_t i = 0; i < value.length(); i++) {
|
|
||||||
acc += value[i];
|
|
||||||
}
|
|
||||||
return 0xABBAABBAABBAABBA ^ acc;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename K, typename V, class H = HashFunc<K>>
|
|
||||||
class HashMap {
|
class HashMap {
|
||||||
public:
|
public:
|
||||||
HashMap() = default;
|
HashMap() = default;
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
#include "util/hash.h"
|
||||||
|
|
||||||
|
#include "string/string.h"
|
||||||
|
|
||||||
|
namespace glcr {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
const uint64_t kFnvOffset = 0xcbf29ce484222325;
|
||||||
|
const uint64_t kFnvPrime = 0x100000001b3;
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
uint64_t Hash<uint64_t>::operator()(const uint64_t& value) {
|
||||||
|
uint64_t hash = kFnvOffset;
|
||||||
|
for (uint8_t i = 0; i < 8; i++) {
|
||||||
|
hash *= kFnvPrime;
|
||||||
|
hash ^= (value >> (8 * i)) & 0xFF;
|
||||||
|
}
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
uint64_t Hash<String>::operator()(const String& value) {
|
||||||
|
uint64_t hash = kFnvOffset;
|
||||||
|
for (uint8_t i = 0; i < value.length(); i++) {
|
||||||
|
hash *= kFnvPrime;
|
||||||
|
hash ^= value[i];
|
||||||
|
}
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace glcr
|
|
@ -0,0 +1,27 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
namespace glcr {
|
||||||
|
|
||||||
|
// General purpose templated hash function.
|
||||||
|
// Currently the template speciializations
|
||||||
|
// implement FNV hashing:
|
||||||
|
// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
|
||||||
|
template <typename T>
|
||||||
|
struct Hash {
|
||||||
|
uint64_t operator()(const T&);
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct Hash<uint64_t> {
|
||||||
|
uint64_t operator()(const uint64_t&);
|
||||||
|
};
|
||||||
|
|
||||||
|
class String;
|
||||||
|
template <>
|
||||||
|
struct Hash<String> {
|
||||||
|
uint64_t operator()(const String&);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace glcr
|
Loading…
Reference in New Issue