-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.h
211 lines (163 loc) · 3.98 KB
/
HashTable.h
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
204
205
206
207
208
209
210
211
#pragma once
#ifndef _HASHTABLE_H
#define _HASHTABLE_H
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool isPrime(int n)
{
// Corner cases
if (n <= 1) return false;
if (n <= 3) return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
int nextPrime(int N)
{
// Base case
if (N <= 1)
return 2;
int prime = N;
bool found = false;
// Loop continuously until isPrime returns
// true for a number greater than n
while (!found) {
prime++;
if (isPrime(prime))
found = true;
}
return prime;
}
template <class HashedObj>
class HashTable
{
public:
template <class HashedObj>
HashTable(const HashedObj& notFound, int size) : ITEM_NOT_FOUND(notFound), array(nextPrime(size))
{
makeEmpty();
}
HashTable(const HashTable& rhs)
: ITEM_NOT_FOUND(rhs.ITEM_NOT_FOUND),
array(rhs.array), currentSize(rhs.currentSize) { }
template <class HashedObj>
void remove(const HashedObj& x)
{
int currentPos = findPos(x);
if (isActive(currentPos))
array[currentPos].info = DELETED;
}
template <class HashedObj>
const HashedObj& find(const HashedObj& x)const
{
int currentPos = findPos(x);
if (isActive(currentPos))
return array[currentPos].element;
return ITEM_NOT_FOUND;
}
void makeEmpty()
{
currentSize = 0;
for (unsigned int i = 0; i < array.size(); i++)
array[i].info = EMPTY;
}
template <class HashedObj>
bool insert(const HashedObj& x)
{
// Insert x as active
int currentPos = findPos(x);
if (isActive(currentPos))
return false;
array[currentPos] = HashEntry(x, ACTIVE);
// enlarge the hash table if necessary
if (++currentSize >= (array.size()*2) / 3) {
rehash();
return true;
}
return false;
}
const double loadFactor() const
{
return currentSize/double(array.capacity());
}
int size() const
{
return currentSize;
}
int capacity() const
{
return array.capacity();
}
template <class HashedObj>
HashedObj* findPosition( HashedObj& x)
{
int currentPos = findPos(x);
if(array[currentPos].info != ACTIVE)
return NULL;
return &(array[currentPos].element);
}
//const HashTable& operator=(const HashTable& rhs);
enum EntryType { ACTIVE, EMPTY, DELETED };
private:
struct HashEntry
{
HashedObj element;
EntryType info;
HashEntry(const HashedObj& e = HashedObj(),
EntryType i = EMPTY)
: element(e), info(i) { }
};
vector<HashEntry> array;
int currentSize;
const HashedObj ITEM_NOT_FOUND;
void rehash()
{
vector<HashEntry> oldArray = array;
// Create new double-sized, empty table
array.resize(nextPrime(2 * oldArray.size()));
for (int j = 0; j < array.size(); j++)
array[j].info = EMPTY;
// Copy table over
currentSize = 0;
for (int i = 0; i < oldArray.size(); i++)
if (oldArray[i].info == ACTIVE)
insert(oldArray[i].element);
}
template <class HashedObj>
int hash(const HashedObj& key, int tableSize)const
{
string keyStr = key.key();
int hashVal = 0;
for (int i = 0; i < keyStr.length(); i++)
hashVal = 37 * hashVal + keyStr[i];
hashVal = hashVal % tableSize;
if (hashVal < 0)
hashVal = hashVal + tableSize;
return(hashVal);
}
template <class HashedObj>
int findPos(const HashedObj& x)const
{
int collisionNum = 0;
int currentPos = hash(x, array.size());
while (array[currentPos].info != EMPTY &&
array[currentPos].element != x)
{
currentPos += pow(++collisionNum, 2); //add the difference
if (currentPos >= array.size()) // perform the mod
currentPos -= array.size();
}
return currentPos;
}
bool isActive(int currentPos) const
{
return array[currentPos].info == ACTIVE;
}
};
#endif