-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashTable.cpp
More file actions
203 lines (167 loc) · 5.93 KB
/
hashTable.cpp
File metadata and controls
203 lines (167 loc) · 5.93 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
194
195
196
197
198
199
200
201
202
203
#include "hashTable.h"
List &bucket::getTuples() { return this->tuples; }
bool bucket::getOccupied() const { return this->occupied; }
bool bucket::getBitmapIndex(int64_t index) const { return this->Bitmap[index]; }
void bucket::setTuple(tuple *t) { this->tuples.append(t); }
void bucket::setTuple(List &l) { this->tuples = l; }
void bucket::setOccupied(bool flag) { this->occupied = flag; }
void bucket::setBitmapIndex(int64_t index, bool flag) {
this->Bitmap[index] = flag;
}
bucket::bucket() : occupied{false}, Bitmap{} {}
// ------------------------------------------------------------------
bucket *hashTable::getBucket(int64_t index) const {
if (index >= this->num_buckets) return nullptr;
return &(this->buckets[index]);
}
int64_t hashTable::getBucketCount() const { return num_buckets; }
int64_t hashTable::hash2(int64_t key) {
if (this->num_buckets == 0) return -1; // a mod 0 is undefined
return key % this->num_buckets;
}
void hashTable::rehash() {
// std::printf("Rehashing\n");
int64_t old_bucket_count = num_buckets;
bucket *old_buckets = buckets;
num_buckets = old_bucket_count * 2 + 1;
buckets = new bucket[num_buckets];
for (int64_t i = 0; i < old_bucket_count; i++) {
Node *traverse = old_buckets[i].getTuples().getRoot();
while (traverse) {
insert(traverse->mytuple);
traverse = traverse->next;
}
}
delete[] old_buckets;
}
// Insert all tuples of a partition into the hashTable
void hashTable::insert(tuple *t) {
int64_t hashVal = hash2(t->getKey());
if (hashVal == -1) return; // Cannot insert to an empty HT
bool flag;
// ----- Implement Hopscotch Hashing -----
if (this->buckets[hashVal].getOccupied() == false) {
this->buckets[hashVal].setTuple(t);
this->buckets[hashVal].setOccupied(true);
this->buckets[hashVal].setBitmapIndex(0, true);
} else {
// Check if Neighbourhood is FULL and Check for Duplicates
flag = true; // Assume it's full
for (int i = 0; i < NBHD_SIZE; i++) {
if (this->buckets[hashVal].getBitmapIndex(i) == false)
flag = false; // Neighbourhood NOT full - Empty slot FOUND
else {
// Check if exact same key (R.a) exists
if (this->buckets[(hashVal + i) % num_buckets]
.getTuples()
.getRoot()
->mytuple->getKey() == t->getKey()) {
// Add argument tuple into the List tuples of bucket
this->buckets[(hashVal + i) % num_buckets].getTuples().append(t);
return;
}
}
}
// Step 1. FULL Neighbourhood
if (flag == true) {
// std::printf("full neighb\n");
rehash();
insert(t);
return;
}
// Step 2.
int64_t j = (hashVal + 1) % this->num_buckets;
flag = false;
while (j != hashVal) {
if (this->buckets[j].getOccupied() == false) { // Empty slot found
flag = true;
break;
}
j = (j + 1) % this->num_buckets;
}
// FULL HashTable
if (flag == false) {
// std::printf("HT is full\n");
// No empty slot - Rehash needed
rehash();
insert(t);
return;
}
int64_t dist;
if (j < hashVal)
dist = (j - hashVal) + this->num_buckets;
else
dist = (j - hashVal) % this->num_buckets;
// Step 3.
while (dist >= NBHD_SIZE) {
flag = false; // Indicates if element for swap is found
// Step 3.a.
int64_t k = j - NBHD_SIZE + 1;
// In case where the index turns out negative, cycle back to the end
if (k < 0) k = num_buckets + k;
// Search NBHD_SIZE - 1
for (int x = 0; x < NBHD_SIZE - 1; x++) {
if (this->buckets[k].getBitmapIndex(x) == true) {
flag = true; // Element found
// Step 3.c
this->buckets[j].setTuple(
this->buckets[(k + x) % num_buckets].getTuples());
this->buckets[j].setOccupied(true);
this->buckets[k].setBitmapIndex((NBHD_SIZE - 1), true);
List newlist;
this->buckets[(k + x) % num_buckets].setTuple(newlist);
this->buckets[(k + x) % num_buckets].setOccupied(false);
this->buckets[k].setBitmapIndex(x, false);
// Step 3.d.
j = (k + x) % this->num_buckets;
if (j < hashVal)
dist = (j - hashVal) + this->num_buckets;
else
dist = (j - hashVal) % this->num_buckets;
// std::printf("swap done\n");
break;
}
}
// Step 3.b. | No element found
if (flag == false) {
// std::printf("no element found\n");
// Rehash needed
rehash();
insert(t);
return;
}
}
// Step 4 | Save tuple here
this->buckets[j].setTuple(t);
this->buckets[j].setOccupied(true);
this->buckets[hashVal].setBitmapIndex(dist, true);
}
}
List *hashTable::findEntry(int64_t key) {
int64_t hashVal = hash2(key);
// Empty HT, does not exist
if (hashVal == -1) return nullptr;
if (this->buckets[hashVal].getTuples().getLen() >
0) // so that we won't try to access mytuple if root is nullptr
if (this->buckets[hashVal].getTuples().getRoot()->mytuple->getKey() ==
key) {
// std::printf("Found item with key %ld\n", key);
return &(this->buckets[hashVal].getTuples());
}
// Search inside neighbourhood
for (int i = 0; i < NBHD_SIZE; i++)
if (this->buckets[(hashVal + i) % num_buckets].getTuples().getLen() >
0) // so that we won't try to access mytuple if root is nullptr
if (this->buckets[(hashVal + i) % num_buckets]
.getTuples()
.getRoot()
->mytuple->getKey() == key) {
// std::printf("Found item with key %ld\n", key);
return &(this->buckets[(hashVal + i) % num_buckets].getTuples());
}
// std::printf("Item with key %ld NOT FOUND\n", key);
return nullptr;
}
hashTable::hashTable(int64_t num_tuples)
: buckets{new bucket[num_tuples]}, num_buckets{num_tuples} {}
hashTable::~hashTable() { delete[] buckets; }