diff options
| author | Christian Krinitsin <mail@krinitsin.com> | 2025-03-20 10:35:43 +0100 |
|---|---|---|
| committer | Christian Krinitsin <mail@krinitsin.com> | 2025-03-20 10:35:43 +0100 |
| commit | 8f0c081c188a526ff2bd7115b31e48c4ed5bd47d (patch) | |
| tree | d0e151b002717fecb7ae79d0e88581dd2a49822f | |
| parent | 6d220177b1e0e13ac9d16c7ca87ed2ded652a175 (diff) | |
| download | BT-Programming-Assignment-8f0c081c188a526ff2bd7115b31e48c4ed5bd47d.tar.gz BT-Programming-Assignment-8f0c081c188a526ff2bd7115b31e48c4ed5bd47d.zip | |
server: add hashtable util functions
| -rw-r--r-- | src/server/hashtable.h | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/server/hashtable.h b/src/server/hashtable.h index 0fd0d2b..9d31cfc 100644 --- a/src/server/hashtable.h +++ b/src/server/hashtable.h @@ -1,5 +1,6 @@ #pragma once +#include <algorithm> #include <list> #include <vector> @@ -16,4 +17,24 @@ private: size_t size; std::vector<std::list<std::pair<K, V>>> table; + + std::hash<K> hash_function; + + auto bucket_find_key(std::list<std::pair<K, V>> list, K key) + { + return std::find_if(list.begin(), list.end(), [&key](const std::pair<K, V>& pair) { + return pair.first == key; + }); + } + + bool bucket_contains_key(std::list<std::pair<K, V>> list, K key) + { + return bucket_find_key(list, key) != list.end(); + } + + std::list<std::pair<K, V>> get_bucket(K key) + { + size_t index = hash_function(key) % size; + return table.at(index); + } }; |