forked from ShootMe/Klondike-Solver
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHashMap.h
136 lines (120 loc) · 2.76 KB
/
HashMap.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
#ifndef HashMap_h
#define HashMap_h
using namespace std;
#define KEYSIZE 21
#define KEYSIZEM1 KEYSIZE - 1
struct HashKey {
char Key[KEYSIZE];
HashKey() {
for (int i = 0; i < KEYSIZE; i++) {
Key[i] = 0;
}
}
int ComputeHash() const {
unsigned long hash = 0;
int i = 0;
while (i < KEYSIZE) {
hash = (int)Key[i++] + (hash << 7) + (hash << 16) - hash;
}
return hash;
}
bool operator==(HashKey const& key) const {
int i = 0;
while (i < KEYSIZEM1 && Key[i] == key.Key[i]) { i++; }
return Key[i] == key.Key[i];
}
char & operator[](int index) {
return Key[index];
}
};
template <typename T> struct KeyValue {
KeyValue<T> * Next;
HashKey Key;
T Value;
int Hash;
KeyValue();
KeyValue(int hash, HashKey const& key, T const& value, KeyValue<T> * next);
~KeyValue();
};
template <typename T> KeyValue<T>::KeyValue() {
Next = NULL;
}
template <typename T> KeyValue<T>::KeyValue(int hash, HashKey const& key, T const& value, KeyValue<T> * next) {
Next = next;
Key = key;
Hash = hash;
Value = value;
}
template <typename T> KeyValue<T>::~KeyValue() {
delete Next;
}
template <typename T> class HashMap {
private:
KeyValue<T> * table;
int size, capacity, powerOf2, maxLength, slotsUsed;
public:
HashMap(int powerOf2);
~HashMap();
int Size();
int Capacity();
int SlotsUsed();
int MaxLength();
void Clear();
KeyValue<T> * Add(HashKey const& key, T const& value);
};
template <typename T> HashMap<T>::HashMap(int powerOf2) {
this->powerOf2 = powerOf2;
size = 0;
slotsUsed = 0;
maxLength = 0;
capacity = (1 << powerOf2) - 1;
table = new KeyValue<T>[capacity + 1];
}
template <typename T> HashMap<T>::~HashMap() {
delete[] table;
}
template <typename T> int HashMap<T>::Size() {
return size;
}
template <typename T> int HashMap<T>::MaxLength() {
return maxLength;
}
template <typename T> int HashMap<T>::SlotsUsed() {
return slotsUsed;
}
template <typename T> int HashMap<T>::Capacity() {
return capacity + 1;
}
template <typename T> KeyValue<T> * HashMap<T>::Add(HashKey const& key, T const& value) {
int hash = key.ComputeHash();
int i = hash;
i ^= (hash >> 16);
i &= capacity;
KeyValue<T> * node = &table[i];
int length = 1;
while (node != NULL && node->Key[0] != 0) {
if (node->Hash == hash && key == node->Key) { return node; }
node = node->Next;
length++;
}
++size;
node = &table[i];
if (node->Key[0] != 0) {
node->Next = new KeyValue<T>(node->Hash, node->Key, node->Value, node->Next);
}
node->Hash = hash;
node->Key = key;
node->Value = value;
if (length > maxLength) { maxLength = length; }
if (length == 1) { slotsUsed++; }
return NULL;
}
template <typename T> void HashMap<T>::Clear() {
KeyValue<T> * old = table;
delete[] old;
table = new KeyValue<T>[capacity + 1];
size = 0;
slotsUsed = 0;
maxLength = 0;
}
#endif