-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMap.cpp
More file actions
193 lines (181 loc) · 4.66 KB
/
HashMap.cpp
File metadata and controls
193 lines (181 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* HashMap.cpp
*
* Author: Irvin Favors
* F22-CISC220-010
*/
#include "hashMap.hpp"
#include "hashNode.hpp"
#include <iostream>
#include <math.h>
#include <bits/stdc++.h>
using namespace std;
hashMap::hashMap(bool hash1, bool coll1) {
map = new hashNode *[101];
for (int i = 0; i < 101; i++){
map[i] = NULL;
}
first = "";
numKeys = 0;
mapSize = 101;
hashfn = hash1;
collfn = coll1;
collisions = 0;
hashcoll = 0;
}
void hashMap::addKeyValue(string k, string v) {
reHash();
int x = calcHash(k);
if (map[x] == NULL){
map[x] = new hashNode(k,v);
++numKeys;
}else if (map[x] != NULL && map[x]->keyword == k){
checkValsSize(x);
map[x]->values[map[x]->currSize] = v;
++map[x]->currSize;
}else{
hashcoll++;
x = coll(x, k);
if (map[x] == NULL){
map[x] = new hashNode(k,v);
++numKeys;
}else if (map[x] != NULL && map[x]->keyword == k){
checkValsSize(x);
map[x]->values[map[x]->currSize] = v;
++map[x]->currSize;
}
}
}
void hashMap::checkValsSize(int h){
if (map[h]->currSize == map[h]->valuesSize){
auto tmp = map[h]->values;
int tmpSize = map[h]->currSize;
map[h]->values = new string[map[h]->valuesSize*2];
map[h]->valuesSize *= 2;
for (int i = 0; i < tmpSize; i++){
map[h]->values[i] = tmp[i];
}
}
}
int hashMap::getIndex(string k) {
reHash();
int x = calcHash(k);
if (map[x] == NULL) {
return -1;
}
if (map[x] != NULL && map[x]->keyword != k) x = coll(x, k);
return x;
}
int hashMap::calcHash(string k){
if (hashfn == true){
return calcHash1(k);
}
return calcHash2(k);
}
int hashMap::calcHash2(string k){
int hash = 0, l = k.length();
for (int i = 0; i < l; ++i){
hash = (hash*11 + ((int)k[l-i-1])) % mapSize;
}
return hash;
}
int hashMap::calcHash1(string k){
/*
* Retrieves the ascii value of every character in the string,
* adds the value to 7 (a prime #) * hash (an int variable which updates
* for every character in the string), to produce a unique hash value based on the
* total of the ascii values of the characters within the string.
*/
int len = k.length(), hash = 0;
for (int i = len - 1; i > 0; i--){
hash = (7*hash + (int)k[i]) % mapSize;
}
return hash;
}
int hashMap::getClosestPrime(int n) {
if (n & 1) n -= 2;
else n--;
int j;
for (int i = n; i >= 2; i-=2){
if (i % 2 == 0) continue;
for (j = 3; j <= sqrt(i); j += 2){
if (i % j == 0) break;
}
if (j > sqrt(i)) return i;
}
return 2;
}
void hashMap::reHash() {
if (numKeys < mapSize*0.7) return;
auto old = map;
int newSize = getClosestPrime(mapSize*2);
map = new hashNode *[newSize];
int oldSize = mapSize;
mapSize = newSize;
numKeys = 0;
for (int x = 0; x < mapSize; x++){
map[x] = NULL;
}
for (int i = 0; i < oldSize; i++){
if (old[i] != NULL){
for (int j = 0; j < old[i]->currSize; j++){
addKeyValue(old[i]->keyword, old[i]->values[j]);
}
}
}
}
int hashMap::coll(int h, string k){
if (collfn == true){
return coll1(h, k);
}
return coll2(h, k);
}
int hashMap::coll1(int h, string k) {
//Linear Probing:
//once a value is mapped to an index, if index is occupied
//look at the next index until you find one that is empty and then insert the value
//
//If you hit the end of the array (index == size of the array) loop to the beginning
//and keep searching for an empty index
int index = h;
if (map[index] != NULL && map[index]->keyword == k) return index;
while (map[index] != NULL){
if (index == mapSize-1) index = -1;
++index;
++collisions;
if (map[index] == NULL) break;
if (map[index]->keyword == k) break;
}
return index;
}
int hashMap::coll2(int h, string k) {
/*
* Quadratic Probing:
* Makes use of powers to find a new index, rather than continuously looking at the next index,
* in an effort to avoid clustering in a section of the array. Quadratic probing resulted in less
* sub-collisions for collisions that stem from indexes that have been mapped multiple times previously.
*/
int index = h, i = 1;
if (map[index] != NULL && map[index]->keyword == k) return index;
while (map[index] != NULL){
index = (index + (int)pow(i,i)) % mapSize;
++i; if (i == 10) i = 0;
++collisions;
if (map[index] == NULL) break;
if (map[index]->keyword == k) break;
}
return index;
}
void hashMap::printMap() {
cout << "In printMap()" << endl;
for (int i = 0; i < mapSize; i++) {
//cout << "In loop" << endl;
if (map[i] != NULL) {
cout << map[i]->keyword << ": ";
for (int j = 0; j < map[i]->currSize;j++) {
cout << map[i]->values[j] << ", ";
}
cout << endl;
}
}
}