From e8abcfc7425167e96bf00a8b79de9a30ee2e2800 Mon Sep 17 00:00:00 2001 From: Christian Krinitsin Date: Thu, 20 Mar 2025 12:04:25 +0100 Subject: server: fix reference issues of hashtable --- src/server/hashtable.h | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/server') diff --git a/src/server/hashtable.h b/src/server/hashtable.h index daca492..0989a79 100644 --- a/src/server/hashtable.h +++ b/src/server/hashtable.h @@ -17,19 +17,20 @@ public: bool insert(K key, V value) { - std::list> list = get_bucket(key); + std::list>& list = get_bucket(key); if (bucket_contains_key(list, key)) { return false; } - list.insert(value); + list.push_back(std::pair(key, value)); + return true; } std::optional get(K key) { - std::list> list = get_bucket(key); + std::list>& list = get_bucket(key); auto iter = bucket_find_key(list, key); if (iter != list.end()) { @@ -41,7 +42,7 @@ public: bool remove(K key) { - std::list> list = get_bucket(key); + std::list>& list = get_bucket(key); auto iter = bucket_find_key(list, key); if (iter != list.end()) { @@ -71,19 +72,19 @@ private: std::hash hash_function; - auto bucket_find_key(std::list> list, K key) + auto bucket_find_key(std::list>& list, K key) { return std::find_if(list.begin(), list.end(), [&key](const std::pair& pair) { return pair.first == key; }); } - bool bucket_contains_key(std::list> list, K key) + bool bucket_contains_key(std::list>& list, K key) { - return bucket_find_key(list, key) != list.end(); + return list.begin() != list.end() && bucket_find_key(list, key) != list.end(); } - std::list> get_bucket(K key) + std::list>& get_bucket(K key) { size_t index = hash_function(key) % size; return table.at(index); -- cgit 1.4.1