diff --git a/NineChess/src/hashmap.cpp b/NineChess/src/hashmap.cpp deleted file mode 100644 index 54156500..00000000 --- a/NineChess/src/hashmap.cpp +++ /dev/null @@ -1,137 +0,0 @@ -#include "hashmap.h" - -using namespace CTSL; - -namespace CTSL //Concurrent Thread Safe Library -{ -// Function to find an entry in the bucket matching the key -// If key is found, the corresponding value is copied into the parameter "value" and function returns true. -// If key is not found, function returns false -template -bool HashBucket::find(const K &key, V &value) const -{ - // A shared mutex is used to enable mutiple concurrent reads - std::shared_lock lock(mutex_); - HashNode *node = head; - - while (node != nullptr) { - if (node->getKey() == key) { - value = node->getValue(); - return true; - } - node = node->next; - } - return false; -} - -// Function to insert into the bucket -// If key already exists, update the value, else insert a new node in the bucket with the pair -template -void HashBucket::insert(const K &key, const V &value) -{ - // Exclusive lock to enable single write in the bucket - std::unique_lock lock(mutex_); - HashNode *prev = nullptr; - HashNode *node = head; - - while (node != nullptr && node->getKey() != key) { - prev = node; - node = node->next; - } - - if (nullptr == node) // New entry, create a node and add to bucket - { - if (nullptr == head) { - head = new HashNode(key, value); - } else { - prev->next = new HashNode(key, value); - } - } else { - node->setValue(value); // Key found in bucket, update the value - } -} - -// Function to remove an entry from the bucket, if found -template -void HashBucket::erase(const K &key) -{ - // Exclusive lock to enable single write in the bucket - std::unique_lock lock(mutex_); - HashNode *prev = nullptr; - HashNode *node = head; - - while (node != nullptr && node->getKey() != key) { - prev = node; - node = node->next; - } - - if (nullptr == node) //Key not found, nothing to be done - { - return; - } else //Remove the node from the bucket - { - if (head == node) { - head = node->next; - } else { - prev->next = node->next; - } - delete node; //Free up the memory - } -} - -#if 0 -// Function to clear the bucket -template -void HashBucket::clear() -{ - // Exclusive lock to enable single write in the bucket - std::unique_lock lock(mutex_); - HashNode *prev = nullptr; - HashNode *node = head; - while (node != nullptr) { - prev = node; - node = node->next; - delete prev; - } - head = nullptr; -} -#endif - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -// Function to find an entry in the hash map matching the key. -// If key is found, the corresponding value is copied into the parameter "value" and function returns true. -// If key is not found, function returns false. -template -bool HashMap::find(const K &key, V &value) const -{ - size_t hashValue = hashFn(key) % hashSize; - return hashTable[hashValue].find(key, value); -} - -// Function to insert into the hash map. -// If key already exists, update the value, else insert a new node in the bucket with the pair. -template -void HashMap::insert(const K &key, const V &value) -{ - size_t hashValue = hashFn(key) % hashSize; - hashTable[hashValue].insert(key, value); -} - -// Function to remove an entry from the bucket, if found -template -void HashMap::erase(const K &key) -{ - size_t hashValue = hashFn(key) % hashSize; - hashTable[hashValue].erase(key); -} - -// Function to clean up the hasp map, i.e., remove all entries from it -template -void HashMap::clear() -{ - for (size_t i = 0; i < hashSize; i++) { - (hashTable[i]).clear(); - } -} -}