about summary refs log tree commit diff stats
path: root/src/server/hashtable.h
diff options
context:
space:
mode:
authorckrinitsin <101062646+ckrinitsin@users.noreply.github.com>2025-03-21 22:15:01 +0100
committerGitHub <noreply@github.com>2025-03-21 22:15:01 +0100
commit5fca08baa98058508c743101dcf10cd1b178ec7a (patch)
tree9346b38f25ab5880af3f40e49ba1e64c4f7e0ca2 /src/server/hashtable.h
parent2a22b4123dce661b0500dc07012d61215bdce161 (diff)
parent7212db13b9013aa15673ae65da65eeaf97ee0d12 (diff)
downloadBT-Programming-Assignment-5fca08baa98058508c743101dcf10cd1b178ec7a.tar.gz
BT-Programming-Assignment-5fca08baa98058508c743101dcf10cd1b178ec7a.zip
Merge pull request #4 from ckrinitsin/client-input
Client input
Diffstat (limited to 'src/server/hashtable.h')
-rw-r--r--src/server/hashtable.h78
1 files changed, 74 insertions, 4 deletions
diff --git a/src/server/hashtable.h b/src/server/hashtable.h
index 786d8f5..e09fcd4 100644
--- a/src/server/hashtable.h
+++ b/src/server/hashtable.h
@@ -6,11 +6,21 @@
 #include <mutex>
 #include <optional>
 #include <shared_mutex>
+#include <sstream>
 #include <vector>
 
+/**
+ * @class HashTable
+ * @brief Represents a generic hashtable with simple operations.
+ */
 template <typename K, typename V>
 class HashTable {
 public:
+    /**
+     * @brief Constructs a new Hashtable.
+     *
+     * @param size The number of buckets of the table.
+     */
     HashTable(size_t size)
         : size { size }
         , table(size)
@@ -18,6 +28,13 @@ public:
     {
     }
 
+    /**
+     * @brief Insert a kv-pair into the hashtable.
+     *
+     * @param key The key to determine the bucket.
+     * @param value The value to insert.
+     * @return bool Successful insert of the pair.
+     */
     bool insert(K key, V value)
     {
         size_t index = get_bucket_index(key);
@@ -34,6 +51,12 @@ public:
         return true;
     }
 
+    /**
+     * @brief Gets the value which corresponds to the key.
+     *
+     * @param key The key to look for.
+     * @return std::optional The value, if the key could be found.
+     */
     std::optional<V> get(K key)
     {
         size_t index = get_bucket_index(key);
@@ -49,6 +72,12 @@ public:
         return std::optional<V>();
     }
 
+    /**
+     * @brief Removes the kv-pair which corresponds to the key.
+     *
+     * @param key The key to look for.
+     * @return bool The pair could be removed successfully.
+     */
     bool remove(K key)
     {
         size_t index = get_bucket_index(key);
@@ -65,29 +94,57 @@ public:
         return false;
     }
 
-    void print()
+    /**
+     * @brief Constructs a string representation of the hashtable.
+     *
+     * @return std::string The string of the hashtable.
+     */
+    std::string string()
     {
+        std::ostringstream output;
+
         size_t index { 0 };
         for (auto bucket : table) {
-            std::cout << "Bucket " << index << ": [";
+            output << "Bucket " << index << ": [";
             std::shared_lock<std::shared_mutex> lock(bucket_mutexes.at(index));
             for (auto pair : bucket) {
-                std::cout << "(" << pair.first << ", " << pair.second << ")";
+                output << "(" << pair.first << ", " << pair.second << ")";
             }
-            std::cout << "]" << "\n";
+            output << "]" << "\n";
             ++index;
         }
+
+        return output.str();
     }
 
 private:
+    /**
+     * @brief The number of buckets.
+     */
     size_t size;
 
+    /**
+     * @brief The hashtable.
+     */
     std::vector<std::list<std::pair<K, V>>> table;
 
+    /**
+     * @brief A mutex for every button.
+     */
     std::vector<std::shared_mutex> bucket_mutexes;
 
+    /**
+     * @brief The hashfunction to use for the bucket determination.
+     */
     std::hash<K> hash_function;
 
+    /**
+     * @brief Finds the kv-pair inside a bucket.
+     *
+     * @param list The bucket.
+     * @param key The key to look for.
+     * @return auto The iterator element, which points to the kv-pair or list.end().
+     */
     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) {
@@ -95,10 +152,23 @@ private:
         });
     }
 
+    /**
+     * @brief Checks if the bucket contains the key.
+     *
+     * @param list The bucket.
+     * @param key The key to look for.
+     * @return bool The bucket contains the key.
+     */
     bool bucket_contains_key(std::list<std::pair<K, V>>& list, K key)
     {
         return list.begin() != list.end() && bucket_find_key(list, key) != list.end();
     }
 
+    /**
+     * @brief Uses the hashfunction and the key to determine, which bucket to use.
+     *
+     * @param key The key.
+     * @return size_t The index of the bucket.
+     */
     size_t get_bucket_index(K key) { return hash_function(key) % size; }
 };