Skip to content

Commit 4bc8718

Browse files
author
VirtualRoyalty
committed
added string performance, boosted universal hash functions, added real life example, +fixes
1 parent 56ccdbf commit 4bc8718

21 files changed

+60344
-841
lines changed

algorithm/include/ChainedHashMap.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,33 @@
77
// Hash map class template
88
template <typename K, typename V> class HashMap {
99
private:
10-
HashMap(const HashMap &other);
11-
const HashMap &operator=(const HashMap &other);
10+
// HashMap(const HashMap &other);
11+
// const HashMap &operator=(const HashMap &other);
1212
// std::vector<HashNode<K, V>*> *table;
1313
KeyHash<K> hashFunc;
1414
HashNode<K, V> **table; // int *ptr;
1515
VComparator<K> comp;
1616
size_t tableSize;
17+
unsigned int collisions = 0;
1718

1819
public:
1920
HashMap(const size_t tableSize) : hashFunc(tableSize), comp() {
2021
this->tableSize = tableSize;
2122
table = new HashNode<K, V> *[tableSize]();
2223
}
2324
bool insert(const K &key, const V &value) {
25+
unsigned int attempt = 0;
2426
unsigned int hashValue = hashFunc[key];
2527
HashNode<K, V> *prev = NULL;
2628
HashNode<K, V> *entry = table[hashValue];
2729
// std::cout << "before while" << &entry <<std::endl;
2830
while (entry != NULL && !comp.compare(entry->getKey(), key)) {
2931
// std::cout << "entry != NULL" << std::endl;
32+
++attempt;
3033
prev = entry;
3134
entry = entry->getNext();
3235
}
36+
collisions += attempt;
3337

3438
if (entry == NULL) {
3539
// std::cout << "entry == NULL" << std::endl;
@@ -55,33 +59,34 @@ template <typename K, typename V> class HashMap {
5559
bool search(const K &key, V &value) {
5660
unsigned int hashValue = hashFunc[key];
5761
HashNode<K, V> *entry = table[hashValue];
58-
unsigned int ccounter = 0;
62+
unsigned int attempt = 0;
5963
while (entry != NULL) {
60-
ccounter++;
61-
// std::cout << "hashkey: "<< hashValue << " value: " << entry->getValue()
62-
// <<std::endl;
64+
++attempt;
6365
if (comp.compare(entry->getKey(), key)) {
66+
collisions += attempt;
6467
value = entry->getValue();
65-
// std::cout << "Number of tries: "<< ccounter <<std::endl;
6668
return true;
6769
}
68-
6970
entry = entry->getNext();
7071
}
72+
collisions += attempt;
7173
// std::cout << "Number of tries: "<< ccounter <<std::endl;
7274
// std::cout << "UKNOWN KEY!"<< std::endl;
7375
return false;
7476
}
7577

7678
void remove(const K &key) {
79+
unsigned int attempt = 0;
7780
unsigned int hashValue = hashFunc[key];
7881
HashNode<K, V> *prev = NULL;
7982
HashNode<K, V> *entry = table[hashValue];
8083

8184
while (entry != NULL && !comp.compare(entry->getKey(), key)) {
85+
++attempt;
8286
prev = entry;
8387
entry = entry->getNext();
8488
}
89+
collisions += attempt;
8590

8691
if (entry == NULL) {
8792
// key not found
@@ -95,10 +100,14 @@ template <typename K, typename V> class HashMap {
95100
} else {
96101
prev->setNext(entry->getNext());
97102
}
98-
99103
delete entry;
100104
}
101105
}
106+
107+
void resetCollisions(){ collisions = 0; }
108+
109+
unsigned int getNcollisions(){ return collisions; }
110+
102111
// void displayHash() {
103112
// HashNode<K,V>* entry = NULL;
104113
// for (int i = 0; i < tableSize; i++) {

algorithm/include/ChainedHashMapOLD.h

Lines changed: 0 additions & 108 deletions
This file was deleted.

algorithm/include/OpenedHash.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ template <typename K> class OpenKeyHash {
1616
private:
1717
size_t tableSize;
1818
KeyHash<K> uniHashFunc;
19-
KeyHash<K> uniHashFuncAUX;
2019
unsigned int prime = 101027;
2120
unsigned int c1 = 1;
2221
unsigned int c2 = 3;
@@ -26,7 +25,6 @@ template <typename K> class OpenKeyHash {
2625
OpenKeyHash(size_t tableSize, std::string hashType)
2726
: tableSize(tableSize),
2827
uniHashFunc(tableSize),
29-
uniHashFuncAUX(tableSize),
3028
hashType(hashType){};
3129

3230
unsigned int operator[](KeyAttempt<K> const &pairKA) const {
@@ -40,12 +38,11 @@ template <typename K> class OpenKeyHash {
4038
return (l >= tableSize) ? l % tableSize : l;
4139
} else if (hashType == "DOUBLE") {
4240
unsigned int auxHash1 = uniHashFunc[pairKA.key];
43-
unsigned int auxHash2 = (PRIME[pairKA.key % 420]);//1 + (pairKA.key % 8);
41+
unsigned int auxHash2 = (PRIME[pairKA.key % PRIME.size()]);
4442
unsigned int l = auxHash1 + (pairKA.attempt * auxHash2);
4543
return (l >= tableSize) ? l % tableSize : l;
4644
} else {
47-
unsigned int l = uniHashFunc[pairKA.key] + pairKA.attempt;
48-
return (l >= tableSize) ? l % tableSize : l;
45+
throw;
4946
}
5047
}
5148
};
@@ -54,15 +51,14 @@ template <> class OpenKeyHash<std::string> {
5451
private:
5552
size_t tableSize;
5653
KeyHash<std::string> uniHashFunc;
57-
KeyHash<std::string> uniHashFuncAUX;
58-
unsigned int prime = 97;
5954
unsigned int c1 = 1;
6055
unsigned int c2 = 3;
6156
std::string hashType;
6257

6358
public:
6459
OpenKeyHash(size_t tableSize, std::string hashType)
65-
: tableSize(tableSize), uniHashFunc(tableSize), uniHashFuncAUX(tableSize),
60+
: tableSize(tableSize),
61+
uniHashFunc(tableSize),
6662
hashType(hashType){};
6763

6864
unsigned int operator[](KeyAttempt<std::string> const &pairKA) const {
@@ -80,8 +76,7 @@ template <> class OpenKeyHash<std::string> {
8076
unsigned int l = auxHash1 + pairKA.attempt * auxHash2;
8177
return (l >= tableSize) ? l % tableSize : l;
8278
} else {
83-
unsigned int l = uniHashFunc[pairKA.key] + pairKA.attempt;
84-
return (l >= tableSize) ? l % tableSize : l;
79+
throw;
8580
}
8681
}
8782
};

algorithm/include/OpenedHashMap.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99

1010
template <typename K, typename V> class OpenHashMap {
1111
private:
12-
OpenHashMap(const OpenHashMap &other);
13-
const OpenHashMap &operator=(const OpenHashMap &other);
12+
// OpenHashMap(const OpenHashMap &other);
13+
// const OpenHashMap &operator=(const OpenHashMap &other);
1414
size_t tableSize;
1515
OpenHashNode<K, V> **table;
1616
VComparator<K> comp;
1717
OpenKeyHash<K> hashFunc;
18+
unsigned int collisions = 0;
1819

1920
public:
2021
OpenHashMap(size_t tableSize, std::string hashType)
@@ -38,20 +39,21 @@ template <typename K, typename V> class OpenHashMap {
3839
OpenHashNode<K, V> *entry = table[hashValue];
3940
// std::cout <<"\nkey: " << key << " hash: " << hashValue << std::endl;
4041
while (entry != NULL && entry->getState() != DELETED) {
41-
attempt++;
4242
// std::cout << attempt << " ";
43-
pairKA.attempt = attempt;
43+
pairKA.attempt = ++attempt;
4444
hashValue = hashFunc[pairKA];
4545
entry = table[hashValue];
4646
if (attempt > tableSize) {
4747
// std::cout << "Opened Hash Table is full! " << value << std::endl;
48+
collisions += attempt;
4849
return false;
4950
}
5051
}
5152
if (entry == NULL) {
5253
// std::cout << "entry == NULL"<< std::endl;
5354
// std::cout << "entry set " << value << std::endl;
5455
table[hashValue] = new OpenHashNode<K, V>(key, value);
56+
collisions += attempt;
5557
return true;
5658
} else if (entry->getState() == DELETED) {
5759
// std::cout << "entry == DELETED"<< std::endl;
@@ -61,6 +63,7 @@ template <typename K, typename V> class OpenHashMap {
6163
return true;
6264
} else {
6365
// std::cout << "entry set new value " << value << std::endl;
66+
collisions += attempt;
6467
entry->setValue(value);
6568
return true;
6669
}
@@ -77,6 +80,7 @@ template <typename K, typename V> class OpenHashMap {
7780
if (entry->getState() != DELETED) {
7881
if (comp.compare(entry->getKey(), key)) {
7982
value = entry->getValue();
83+
collisions += attempt;
8084
return true;
8185
}
8286
}
@@ -87,6 +91,7 @@ template <typename K, typename V> class OpenHashMap {
8791
// std::cout << "Opened Hash Table is full!";
8892
// std::cout << "Number of tries: "<< attempt <<std::endl;
8993
// std::cout << "UKNOWN KEY!"<< std::endl;
94+
collisions += attempt;
9095
return false;
9196
}
9297
}
@@ -100,29 +105,35 @@ template <typename K, typename V> class OpenHashMap {
100105
OpenHashNode<K, V> *entry = table[hashValue];
101106

102107
while (entry != NULL && !comp.compare(entry->getKey(), key)) {
103-
attempt++;
104-
pairKA.attempt = attempt;
108+
pairKA.attempt = ++attempt;
105109
hashValue = hashFunc[pairKA]; //(hashFunc[key] + attempt) % tableSize;
106110
entry = table[hashValue];
107-
if (attempt > tableSize - 1) {
111+
if (attempt >= tableSize) {
108112
// std::cout << "Opened Hash Table is full!";
109113
// std::cout << "Number of tries: "<< attempt <<std::endl;
110114
// std::cout << "UKNOWN KEY!"<< std::endl;
115+
collisions += attempt;
111116
return;
112117
}
113118
}
114119

115120
if (entry == NULL) {
116121
// std::cout << "Number of tries: "<< attempt <<std::endl;
117122
// std::cout << "UKNOWN KEY!"<< std::endl;
123+
collisions += attempt;
118124
return;
119125

120126
} else {
127+
collisions += attempt;
121128
entry->setState(DELETED);
122129
entry->setState(DELETED);
123130
}
124131
}
125132

133+
void resetCollisions(){ collisions = 0; }
134+
135+
unsigned int getNcollisions(){ return collisions; }
136+
126137
void displayHash() {
127138
std::cout << std::endl;
128139
for (int i = 0; i < tableSize; i++) {

0 commit comments

Comments
 (0)