|
| 1 | +#pragma once |
| 2 | +#include <iostream> |
| 3 | +#include <math.h> |
| 4 | + |
| 5 | +#include "utils.h" |
| 6 | +#include "CuckooHash.h" |
| 7 | +#include "OpenedHashNode.h" |
| 8 | +#include "VComparator.h" |
| 9 | + |
| 10 | + |
| 11 | +// m - tableSize |
| 12 | +template <typename K, typename V> |
| 13 | +class CuckooHashMap |
| 14 | +{ |
| 15 | +private: |
| 16 | + // CuckooHashMap(const CuckooHashMap &item); |
| 17 | + // const CuckooHashMap& operator=(const CuckooHashMap& other); |
| 18 | + OpenHashNode<K, V>** table_left; |
| 19 | + OpenHashNode<K, V>** table_right; |
| 20 | + unsigned int max_cuckoo; |
| 21 | + unsigned int collisions = 0; |
| 22 | + CuckooKeyHash<K> hashFunc_1; |
| 23 | + CuckooKeyHash<K> hashFunc_2; |
| 24 | + size_t tableSize; |
| 25 | + VComparator<K> comp; |
| 26 | + bool verbose = 0; |
| 27 | + int numberOfInserts = 0, numberOfSearch = 0, numberOfRemove = 0, numberOfRehash = 0; |
| 28 | +public: |
| 29 | + // m - tableSize, n - numberOfElements m >= 2n |
| 30 | + CuckooHashMap(const size_t tableSize, const size_t numberOfElements): |
| 31 | + hashFunc_1(tableSize), |
| 32 | + hashFunc_2(tableSize), |
| 33 | + tableSize(tableSize), |
| 34 | + comp() |
| 35 | + |
| 36 | + { |
| 37 | + if ((this->tableSize < 2*numberOfElements) && (verbose)) { |
| 38 | + std::cerr<<"\nWarning: m - tableSize, n - numberOfElements m < 2n!"; |
| 39 | + } |
| 40 | + max_cuckoo = 6*log2(numberOfElements); |
| 41 | + table_left = new OpenHashNode<K, V>*[tableSize](); |
| 42 | + table_right = new OpenHashNode<K, V>*[tableSize](); |
| 43 | + } |
| 44 | + |
| 45 | + void swap(OpenHashNode<K, V>* item, OpenHashNode<K, V>* item_) { |
| 46 | + OpenHashNode<K, V> temp_item = *item; |
| 47 | + *item = *item_; |
| 48 | + *item_ = temp_item; |
| 49 | + } |
| 50 | + |
| 51 | + bool insert(const K& key, const V& value) { |
| 52 | + numberOfInserts++; |
| 53 | + auto* item = new OpenHashNode<K, V>(key, value); |
| 54 | + |
| 55 | + for (auto i = 0; i < max_cuckoo; ++i) { |
| 56 | + unsigned int pos_1, pos_2; |
| 57 | + pos_1 = hashFunc_1[item->getKey()]; |
| 58 | + pos_2 = hashFunc_2[item->getKey()]; |
| 59 | + if ((table_left[pos_1] == NULL) || |
| 60 | + (comp.compare(table_left[pos_1]->getKey(), item->getKey()))) { |
| 61 | + table_left[pos_1] = item; |
| 62 | + return true; |
| 63 | + } |
| 64 | + if ((table_right[pos_2] == NULL) || |
| 65 | + (comp.compare(table_right[pos_2]->getKey(), item->getKey()))) { |
| 66 | + table_right[pos_2] = item; |
| 67 | + return true; |
| 68 | + } |
| 69 | + if (verbose) { |
| 70 | + //std::cout << "item:\n K, V = " << item->getKey() << ", " << item->getValue() << ", table K, V = " << table_left[pos_1]->getKey() << ", " << table_left[pos_1]->getValue() << "\n"; |
| 71 | + } |
| 72 | + swap(item, table_left[pos_1]); |
| 73 | + collisions++; |
| 74 | + |
| 75 | + if (verbose) { |
| 76 | + //std::cout << "item:\n K, V = " << item->getKey() << ", " << item->getValue() << ", table K, V = " << table_left[pos_1]->getKey() << ", " << table_left[pos_1]->getValue() << "\n"; |
| 77 | + //std::cout << "pos_1: " << pos_1 << ", pos_2: " << pos_2 << std::endl; |
| 78 | + } |
| 79 | + } |
| 80 | + rehash(); |
| 81 | + return insert(item->getKey(), item->getValue()); |
| 82 | + } |
| 83 | + |
| 84 | + bool search(const K& key, V& value) { |
| 85 | + numberOfSearch++; |
| 86 | + if (verbose) { |
| 87 | + std::cout << "\nsearch: key [" << key << "] "; |
| 88 | + } |
| 89 | + OpenHashNode<K, V>* entry; |
| 90 | + if ((entry = table_left[ hashFunc_1[key] ]) != NULL) { |
| 91 | + if (comp.compare(entry->getKey(), key)) { |
| 92 | + value = entry->getValue(); |
| 93 | + if (verbose) { |
| 94 | + std::cout << "key equal: hash1: " << hashFunc_1[key] << " hash2: " << hashFunc_2[key] << std::endl; |
| 95 | + } |
| 96 | + // std::cout << entry->getKey() << |
| 97 | + return true; |
| 98 | + } |
| 99 | + if (verbose) { |
| 100 | + std::cout << "not key equal: hash1: " << hashFunc_1[key] << " hash2: " << hashFunc_2[key] << std::endl; |
| 101 | + } |
| 102 | + } |
| 103 | + if ((entry = table_right[ hashFunc_2[key] ]) != NULL) { |
| 104 | + if (comp.compare(entry->getKey(), key)) { |
| 105 | + value = entry->getValue(); |
| 106 | + if (verbose) { |
| 107 | + std::cout << "search(key equal): hash1: " << hashFunc_1[key] << " hash2: " << hashFunc_2[key] |
| 108 | + << std::endl; |
| 109 | + } |
| 110 | + return true; |
| 111 | + } |
| 112 | + if (verbose) { |
| 113 | + std::cout << "search(not key equal): hash1: " << hashFunc_1[key] << " hash2: " << hashFunc_2[key] |
| 114 | + << std::endl; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + void remove(const K& key) { |
| 123 | + numberOfRemove++; |
| 124 | + if (verbose) { |
| 125 | + std::cout << "\ndelete: key [" << key << "] "; |
| 126 | + } |
| 127 | + OpenHashNode<K, V>* entry; |
| 128 | + unsigned long hash1 = hashFunc_1[key]; |
| 129 | + unsigned long hash2 = hashFunc_2[key]; |
| 130 | + |
| 131 | + if ((entry = table_left[ hash1 ]) != NULL) { |
| 132 | + if (comp.compare(entry->getKey(), key)) { |
| 133 | + table_left[ hash1 ] = NULL; |
| 134 | + } |
| 135 | + } |
| 136 | + if ((entry = table_right[ hash2 ]) != NULL) { |
| 137 | + if (comp.compare(entry->getKey(), key)) { |
| 138 | + table_right[ hash2 ] = NULL; |
| 139 | + } |
| 140 | + } |
| 141 | + if (verbose) { |
| 142 | + std::cerr << "Error: cannot remove by key: " << key << std::endl; |
| 143 | + std::cerr << "hash1: " << hash1 << " hash2: " << hash2 << std::endl; |
| 144 | + } |
| 145 | + } |
| 146 | + void rehash() { |
| 147 | + numberOfRehash++; |
| 148 | + hashFunc_1 = CuckooKeyHash<K>(tableSize); |
| 149 | + hashFunc_2 = CuckooKeyHash<K>(tableSize); |
| 150 | + auto** table_copy_left = new OpenHashNode<K, V>*[tableSize](); |
| 151 | + auto** table_copy_right = new OpenHashNode<K, V>*[tableSize](); |
| 152 | + |
| 153 | + for (int i = 0; i < tableSize; i++) { |
| 154 | + table_copy_left[i] = table_left[i]; |
| 155 | + } |
| 156 | + for (int i = 0; i < tableSize; i++) { |
| 157 | + table_copy_right[i] = table_right[i]; |
| 158 | + } |
| 159 | + |
| 160 | + delete table_left, table_right; |
| 161 | + table_left = new OpenHashNode<K, V>*[tableSize](); |
| 162 | + table_right = new OpenHashNode<K, V>*[tableSize](); |
| 163 | + for (int i = 0; i < tableSize; i++) { |
| 164 | + if (table_copy_left[i] != NULL) { |
| 165 | + OpenHashNode<K, V>* entry = table_copy_left[i]; |
| 166 | + insert(entry->getKey(), entry->getValue()); |
| 167 | + } |
| 168 | + } |
| 169 | + for (int i = 0; i < tableSize; i++) { |
| 170 | + if (table_copy_right[i] != NULL) { |
| 171 | + OpenHashNode<K, V>* entry = table_copy_right[i]; |
| 172 | + insert(entry->getKey(), entry->getValue()); |
| 173 | + } |
| 174 | + } |
| 175 | + if (verbose) { |
| 176 | + std::cout << "\n\trehashed: \n"; |
| 177 | + } |
| 178 | + delete[] table_copy_left, table_copy_right; |
| 179 | + } |
| 180 | + |
| 181 | + unsigned int getNcollisions(){ return collisions; } |
| 182 | + |
| 183 | + void resetCollisions(){ collisions = 0; } |
| 184 | + |
| 185 | + // unsigned int getNumberOfRemoves() { |
| 186 | + // return numberOfRemove; |
| 187 | + // } |
| 188 | + // unsigned int getNumberOfSearches() { |
| 189 | + // return numberOfSearch; |
| 190 | + // } |
| 191 | + // unsigned int getNumberOfInserts() { |
| 192 | + // return numberOfInserts; |
| 193 | + // } |
| 194 | + |
| 195 | + // void resetCounters() { |
| 196 | + // numberOfRemove = 0; |
| 197 | + // numberOfSearch = 0; |
| 198 | + // numberOfInserts = 0; |
| 199 | + // } |
| 200 | + // void displayHash() { |
| 201 | + // OpenHashNode <K, V> *entry = NULL; |
| 202 | + // if (verbose) { |
| 203 | + // std::cout << "\ntable1: table2:\n"; |
| 204 | + // } |
| 205 | + // for (int i = 0; i < tableSize; i++) { |
| 206 | + // entry = table_left[i]; |
| 207 | + // std::cout << i; |
| 208 | + // if (verbose) { |
| 209 | + // if (entry != NULL) { |
| 210 | + // std::cout << " -> (" << entry->getKey() << ", " << entry->getValue() << ") " ; |
| 211 | + // } else { |
| 212 | + // std::cout << " "; |
| 213 | + // } |
| 214 | + // } |
| 215 | + // entry = table_right[i]; |
| 216 | + // if (verbose) { |
| 217 | + // std::cout << i; |
| 218 | + // if (entry != NULL) { |
| 219 | + // std::cout << " -> (" << entry->getKey() << ", " << entry->getValue() << ")"; |
| 220 | + // } |
| 221 | + // std::cout << std::endl; |
| 222 | + // } |
| 223 | + // } |
| 224 | + // } |
| 225 | +}; |
0 commit comments