diff options
| author | Dylan <boss@tehbox.org> | 2026-04-02 11:58:24 +1300 |
|---|---|---|
| committer | Dylan <boss@tehbox.org> | 2026-04-02 11:58:24 +1300 |
| commit | e9a32a966d49ab798a4c6f24471669fc03adb395 (patch) | |
| tree | 6373120682604c489816df391c45f17841a838b6 /src/HashTable.cpp | |
| parent | 93a78ac64327b53f53952b625c7ce8a11bcc8651 (diff) | |
| download | tehimage-e9a32a966d49ab798a4c6f24471669fc03adb395.tar.gz tehimage-e9a32a966d49ab798a4c6f24471669fc03adb395.zip | |
feat: Added basic zlib compression
Currently not incredibly well implemented, but it can handle fixed
codes
Diffstat (limited to 'src/HashTable.cpp')
| -rw-r--r-- | src/HashTable.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
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 <stdexcept> + +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; + } +} |
