about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorckrinitsin <101062646+ckrinitsin@users.noreply.github.com>2025-03-20 12:15:53 +0100
committerGitHub <noreply@github.com>2025-03-20 12:15:53 +0100
commitae8de5f25623ed6f39f8f6c9c0f837c8f286b8c0 (patch)
treeffae5c5b9498ce82fe53244aebb7c7dafc46d4ab
parent481c97567aa1cb54edd169841d1266a7a59a0227 (diff)
parenta509d8e7c953aa80eed3b7288bd3af9bca68dd48 (diff)
downloadBT-Programming-Assignment-ae8de5f25623ed6f39f8f6c9c0f837c8f286b8c0.tar.gz
BT-Programming-Assignment-ae8de5f25623ed6f39f8f6c9c0f837c8f286b8c0.zip
Merge pull request #1 from ckrinitsin/hashtable
Hashtable
-rw-r--r--.gitignore5
-rw-r--r--Makefile13
-rw-r--r--src/server/hashtable.h92
-rw-r--r--src/server/main.cpp27
4 files changed, 126 insertions, 11 deletions
diff --git a/.gitignore b/.gitignore
index cb724a5..9093ab3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
-server_bin
-client_bin
+server
+client
+!src
diff --git a/Makefile b/Makefile
index 0e899bb..16376f5 100644
--- a/Makefile
+++ b/Makefile
@@ -1,10 +1,11 @@
-flags := -std=c++20 -Wall -Wextra -Wpedantic -O3
+CXX = g++
+CXXFLAGS := -std=c++23 -Wall -Wextra -Wpedantic -O3
 
-server_bin: server/src/main.cpp
-	g++ $^ ${flags} -o $@ 
-client_bin:
-	g++ $^ ${flags} -o $@
+server: src/server/main.cpp
+	${CXX} ${CXXFLAGS} $^ -o $@ 
+client:
+	${CXX} ${CXXFLAGS} $^ -o $@
 
 .PHONY: clean
 clean:
-	rm -f server_bin client_bin
+	rm -f server client
diff --git a/src/server/hashtable.h b/src/server/hashtable.h
new file mode 100644
index 0000000..0989a79
--- /dev/null
+++ b/src/server/hashtable.h
@@ -0,0 +1,92 @@
+#pragma once
+
+#include <algorithm>
+#include <iostream>
+#include <list>
+#include <optional>
+#include <vector>
+
+template <typename K, typename V>
+class HashTable {
+public:
+    HashTable(size_t size)
+        : size { size }
+        , table(size)
+    {
+    }
+
+    bool insert(K key, V value)
+    {
+        std::list<std::pair<K, V>>& list = get_bucket(key);
+
+        if (bucket_contains_key(list, key)) {
+            return false;
+        }
+
+        list.push_back(std::pair(key, value));
+
+        return true;
+    }
+
+    std::optional<V> get(K key)
+    {
+        std::list<std::pair<K, V>>& list = get_bucket(key);
+
+        auto iter = bucket_find_key(list, key);
+        if (iter != list.end()) {
+            return std::optional<V>((*iter).second);
+        }
+
+        return std::optional<V>();
+    }
+
+    bool remove(K key)
+    {
+        std::list<std::pair<K, V>>& list = get_bucket(key);
+
+        auto iter = bucket_find_key(list, key);
+        if (iter != list.end()) {
+            list.erase(iter);
+            return true;
+        }
+
+        return false;
+    }
+
+    void print()
+    {
+        size_t index { 0 };
+        for (auto bucket : table) {
+            std::cout << "Bucket " << index++ << ": [";
+            for (auto pair : bucket) {
+                std::cout << "(" << pair.first << ", " << pair.second << ")";
+            }
+            std::cout << "]" << "\n";
+        }
+    }
+
+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 list.begin() != list.end() && 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);
+    }
+};
diff --git a/src/server/main.cpp b/src/server/main.cpp
index e04a1cf..b2f8bed 100644
--- a/src/server/main.cpp
+++ b/src/server/main.cpp
@@ -1,5 +1,4 @@
 #include <cstdint>
-#include <cstdio>
 #include <stdexcept>
 #include <string>
 
@@ -8,7 +7,7 @@
 int main(int argc, char* argv[])
 {
     if (argc != 2) {
-        std::printf("One argument required");
+        std::cout << "One argument required" << '\n';
         return 1;
     }
 
@@ -16,10 +15,32 @@ int main(int argc, char* argv[])
     try {
         size = std::stoi(std::string(argv[1]));
     } catch (const std::invalid_argument& e) {
-        std::printf("Invalid argument");
+        std::cout << "Invalid argument" << '\n';
         return 1;
     }
+  
+    HashTable<int, std::string> hash_table { size };
 
+    std::cout << "Add various kv-pairs" << '\n';
+    hash_table.insert(1, "1");
+    hash_table.insert(2, "2");
+    hash_table.insert(3, "3");
+    hash_table.insert(4, "4");
+    hash_table.insert(5, "5");
+    hash_table.insert(6, "6");
+    hash_table.insert(7, "7");
+
+    hash_table.print();
+
+    std::cout << '\n';
+
+    std::cout << "Value for key 8: " << hash_table.get(8).value_or("Key not found!") << '\n';
+    std::cout << "Value for key 4: " << hash_table.get(4).value_or("Key not found!") << '\n';
+
+    std::cout << '\n';
+    std::cout << "Remove pair with key 5" << '\n';
+    hash_table.remove(5);
+    hash_table.print();
 
     return 0;
 }