aboutsummaryrefslogtreecommitdiff
path: root/include/odhtdb/Hash.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/odhtdb/Hash.hpp')
-rw-r--r--include/odhtdb/Hash.hpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/include/odhtdb/Hash.hpp b/include/odhtdb/Hash.hpp
new file mode 100644
index 0000000..9dce168
--- /dev/null
+++ b/include/odhtdb/Hash.hpp
@@ -0,0 +1,58 @@
+#pragma once
+
+#include "utils.hpp"
+#include <stdexcept>
+#include <unordered_map>
+#include <unordered_set>
+
+namespace odhtdb
+{
+ const int HASH_BYTE_SIZE = 32;
+
+ // Source: https://stackoverflow.com/a/11414104 (public license)
+ static size_t fnvHash(const unsigned char *key, int len)
+ {
+ size_t h = 2166136261;
+ for (int i = 0; i < len; i++)
+ h = (h * 16777619) ^ key[i];
+ return h;
+ }
+
+ class HashException : public std::runtime_error
+ {
+ public:
+ HashException(const std::string &errMsg) : std::runtime_error(errMsg) {}
+ };
+
+ class Hash
+ {
+ public:
+ Hash();
+ // Throws HashException on failure
+ Hash(const void *input, const size_t inputSize);
+ Hash(const Hash &other);
+
+ void* getData() const { return (void*)data; }
+ size_t getSize() const { return HASH_BYTE_SIZE; }
+
+ size_t operator()() const;
+ bool operator==(const Hash &other) const;
+
+ std::string toString() const;
+ private:
+ char data[HASH_BYTE_SIZE];
+ };
+
+ struct HashHasher
+ {
+ size_t operator()(const Hash &hash) const
+ {
+ return hash();
+ }
+ };
+
+ template <typename ValueType>
+ using MapHash = std::unordered_map<Hash, ValueType, HashHasher>;
+
+ using SetHash = std::unordered_set<Hash, HashHasher>;
+}