From 9cd9c99e4bb4bbace8ba2d4a2dbd6830750ad521 Mon Sep 17 00:00:00 2001 From: Aleksi Lindeman <0xdec05eba@gmail.com> Date: Tue, 13 Feb 2018 00:46:46 +0100 Subject: Add database storage (in memory), need to store it on disk later --- include/Key.hpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'include/Key.hpp') 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 +#include 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 + using KeyMap = std::unordered_map; +} -- cgit v1.2.3