diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-02-13 00:46:46 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-08-18 23:25:12 +0200 |
commit | 00a2777fc154537fe9fc9cfac082a29f70bf6b75 (patch) | |
tree | 8c8a1489afe9d430cfdb1a19c63341d320f7543e /include/Key.hpp | |
parent | a9a7ecaa25e6bc11062e21affd458e2de78747ff (diff) |
Add database storage (in memory), need to store it on disk later
Diffstat (limited to 'include/Key.hpp')
-rw-r--r-- | include/Key.hpp | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/include/Key.hpp b/include/Key.hpp index 505050d..f7a600b 100644 --- a/include/Key.hpp +++ b/include/Key.hpp @@ -1,6 +1,7 @@ #pragma once #include <opendht/infohash.h> +#include <unordered_map> namespace odhtdb { @@ -12,4 +13,32 @@ namespace odhtdb dht::InfoHash hashedKey; }; -}
\ No newline at end of file + + // Source: https://stackoverflow.com/a/11414104 (public license) + static unsigned int fnvHash(const unsigned char *key, int len) + { + unsigned int h = 2166136261; + for (int i = 0; i < len; i++) + h = (h * 16777619) ^ key[i]; + return h; + } + + struct KeyHash + { + size_t operator()(const Key &key) const + { + return fnvHash(key.hashedKey.data(), key.hashedKey.size()); + } + }; + + struct KeyCompare + { + bool operator()(const Key &lhs, const Key &rhs) const + { + return lhs.hashedKey == rhs.hashedKey; + } + }; + + template <typename ValueType> + using KeyMap = std::unordered_map<Key, ValueType, KeyHash, KeyCompare>; +} |