From e9a32a966d49ab798a4c6f24471669fc03adb395 Mon Sep 17 00:00:00 2001 From: Dylan Date: Thu, 2 Apr 2026 11:58:24 +1300 Subject: feat: Added basic zlib compression Currently not incredibly well implemented, but it can handle fixed codes --- src/HashTable.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/HashTable.cpp (limited to 'src/HashTable.cpp') diff --git a/src/HashTable.cpp b/src/HashTable.cpp new file mode 100644 index 0000000..685dad6 --- /dev/null +++ b/src/HashTable.cpp @@ -0,0 +1,85 @@ +#include "HashTable.h" +#include + +namespace TehImage +{ + HashTable::HashTable(size_t size) + :TABLE_SIZE(size), + table(TABLE_SIZE) + { } + + unsigned int HashTable::hashFunction(char key[3]) + { + unsigned int hash = 0; + std::memcpy(&hash, key, 3); + return hash % TABLE_SIZE; + } + + void HashTable::insert(char key[3], unsigned int value) + { + unsigned int index = hashFunction(key); + unsigned int keyMem = 0; + std::memcpy(&keyMem, key, 3); + + for(auto& pair : table[index]) + { + if(pair.first == keyMem) + { + pair.second = value; + return; + } + } + + table[index].push_back({keyMem, value}); + } + + void HashTable::remove(char key[3]) + { + + unsigned int index = hashFunction(key); + unsigned int keyMem = 0; + std::memcpy(&keyMem, key, 3); + + auto& chain = table[index]; + for (auto it = chain.begin(); it != chain.end(); ++it) { + if (it->first == keyMem) { + chain.erase(it); + return; + } + } + } + + unsigned int HashTable::get(char key[3]) + { + unsigned int index = hashFunction(key); + unsigned int keyMem = 0; + std::memcpy(&keyMem, key, 3); + + for(auto& pair : table[index]) + { + if(pair.first == keyMem) + { + return pair.second; + } + } + + throw std::out_of_range("Key not present"); + } + + bool HashTable::contains(char key[3]) + { + unsigned int index = hashFunction(key); + unsigned int keyMem = 0; + std::memcpy(&keyMem, key, 3); + + for(auto& pair : table[index]) + { + if(pair.first == keyMem) + { + return true; + } + } + + return false; + } +} -- cgit v1.2.3