|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "VComparator.h" |
| 4 | +#include "UniversalHash.h" |
| 5 | +#include "OpenedHashNode.h" |
| 6 | + |
| 7 | +const int DELETED = -1; |
| 8 | + |
| 9 | +template <typename K, typename V, size_t tableSize> |
| 10 | +class OpenHashMap |
| 11 | +{ |
| 12 | +private: |
| 13 | + OpenHashMap(const OpenHashMap& other); |
| 14 | + const OpenHashMap& operator=(const OpenHashMap& other); |
| 15 | + OpenHashNode<K, V>* table[tableSize]; |
| 16 | + VComparator<K> comp; |
| 17 | + KeyHash<K> hashFunc; |
| 18 | + |
| 19 | +public: |
| 20 | + OpenHashMap(): |
| 21 | + table(), |
| 22 | + hashFunc(tableSize), |
| 23 | + comp() |
| 24 | + { |
| 25 | + } |
| 26 | + |
| 27 | + ~OpenHashMap() |
| 28 | + { |
| 29 | + // destroy all buckets one by one |
| 30 | + for (size_t i = 0; i < tableSize; ++i) { |
| 31 | + OpenHashNode<K, V>* entry = table[i]; |
| 32 | + delete entry; |
| 33 | + table[i] = NULL; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + bool insert(const K& key, const V& value) |
| 38 | + { |
| 39 | + unsigned int attempt = 0; |
| 40 | + unsigned long hashValue = hashFunc[key]; |
| 41 | + OpenHashNode<K, V>* entry = table[hashValue]; |
| 42 | + // std::cout <<"\nkey: " << key << " hash: " << hashValue << std::endl; |
| 43 | + while (entry != NULL && entry->getKey() != DELETED) { |
| 44 | + attempt++; |
| 45 | + // std::cout << attempt << " "; |
| 46 | + hashValue = (hashFunc[key] + attempt) % tableSize; |
| 47 | + entry = table[hashValue]; |
| 48 | + if (attempt >= tableSize-1) |
| 49 | + { |
| 50 | + // std::cout << "Opened Hash Table is full!"; |
| 51 | + return false; |
| 52 | + |
| 53 | + } |
| 54 | + } |
| 55 | + if (entry == NULL) { |
| 56 | + // std::cout << "entry == NULL"<< std::endl; |
| 57 | + entry = new OpenHashNode<K, V>(key, value); |
| 58 | + table[hashValue] = entry; |
| 59 | + return true; |
| 60 | + } |
| 61 | + else if (entry->getKey() == DELETED) { |
| 62 | + // std::cout << "entry == DELETED"<< std::endl; |
| 63 | + entry->setKey(key); |
| 64 | + entry->setValue(value); |
| 65 | + return true; |
| 66 | + } |
| 67 | + else { |
| 68 | + // std::cout << "entry set new value" << std::endl; |
| 69 | + entry->setValue(value); |
| 70 | + return true; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + bool search(const K& key, V& value) |
| 75 | + { |
| 76 | + unsigned int attempt = 0; |
| 77 | + unsigned long hashValue = hashFunc[key]; |
| 78 | + OpenHashNode<K, V>* entry = table[hashValue]; |
| 79 | + while (entry != NULL) { |
| 80 | + // std::cout << "hashkey: "<< hashValue << " value: " << entry->getValue() <<std::endl; |
| 81 | + if (comp.compare(entry->getKey(), key)) { |
| 82 | + value = entry->getValue(); |
| 83 | + return true; |
| 84 | + } |
| 85 | + attempt++; |
| 86 | + hashValue = (hashFunc[key] + attempt) % tableSize; |
| 87 | + entry = table[hashValue]; |
| 88 | + if (attempt >= tableSize-1) |
| 89 | + { |
| 90 | + // std::cout << "Opened Hash Table is full!"; |
| 91 | + // std::cout << "Number of tries: "<< attempt <<std::endl; |
| 92 | + // std::cout << "UKNOWN KEY!"<< std::endl; |
| 93 | + return false; |
| 94 | + |
| 95 | + } |
| 96 | + } |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + void remove(const K& key) |
| 102 | + { |
| 103 | + unsigned int attempt = 0; |
| 104 | + unsigned long hashValue = hashFunc[key]; |
| 105 | + OpenHashNode<K, V>* entry = table[hashValue]; |
| 106 | + |
| 107 | + while (entry != NULL && !comp.compare(entry->getKey(), key)) { |
| 108 | + attempt++; |
| 109 | + hashValue = (hashFunc[key] + attempt) % tableSize; |
| 110 | + entry = table[hashValue]; |
| 111 | + if (attempt >= tableSize-1) |
| 112 | + { |
| 113 | + // std::cout << "Opened Hash Table is full!"; |
| 114 | + // std::cout << "Number of tries: "<< attempt <<std::endl; |
| 115 | + // std::cout << "UKNOWN KEY!"<< std::endl; |
| 116 | + return; |
| 117 | + |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (entry == NULL) { |
| 122 | + // std::cout << "Number of tries: "<< attempt <<std::endl; |
| 123 | + // std::cout << "UKNOWN KEY!"<< std::endl; |
| 124 | + return; |
| 125 | + |
| 126 | + } |
| 127 | + else { |
| 128 | + entry -> setKey(DELETED); |
| 129 | + entry -> setValue(DELETED); |
| 130 | + } |
| 131 | + } |
| 132 | +}; |
0 commit comments