-
Notifications
You must be signed in to change notification settings - Fork 77
/
hashtable.cpp
167 lines (147 loc) · 3.96 KB
/
hashtable.cpp
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
#ifndef HASH_TABLE_H
#define HASH_TABLE_H
#define TABLE_SIZE 100
// Hash node class template
template <typename K, typename V>
class HashNode {
public:
HashNode(const K &key, const V &value)
: key(key)
, value(value)
, next(nullptr) {
}
K getKey() const {
return key;
}
V& getValue() {
return value;
}
V const& getValue() const {
return value;
}
void setValue(V value) {
HashNode::value = value;
}
HashNode *getNext() const {
return next;
}
void setNext(HashNode *next) {
HashNode::next = next;
}
private:
// key-value pair
K key;
V value;
// next bucket with the same key
HashNode *next;
};
// Default hash function class
template <typename K>
struct KeyHash {
unsigned long operator()(const K& key) const {
return static_cast<unsigned long>(key) % TABLE_SIZE;
}
};
// Hash map class template
template <typename K, typename V, typename F = KeyHash<K>>
class HashTable {
public:
HashTable() {
// construct zero initialized hash table of size
table = new HashNode<K, V> *[TABLE_SIZE]();
elements = 0;
}
~HashTable() {
// destroy all buckets one by one
for (int i = 0; i < TABLE_SIZE; ++i) {
HashNode<K, V> *entry = table[i];
while (entry != nullptr) {
HashNode<K, V> *prev = entry;
entry = entry->getNext();
delete prev;
}
table[i] = nullptr;
}
// destroy the hash table
delete [] table;
}
bool get(const K &key, V &value) {
unsigned long hashValue = hashFunc(key);
HashNode<K, V> *entry = table[hashValue];
while (entry != NULL) {
if (entry->getKey() == key) {
value = entry->getValue();
return true;
}
entry = entry->getNext();
}
return false;
}
void put(const K &key, const V &value) {
unsigned long hashValue = hashFunc(key);
HashNode<K, V> *prev = nullptr;
HashNode<K, V> *entry = table[hashValue];
while (entry != nullptr and entry->getKey() != key) {
prev = entry;
entry = entry->getNext();
}
if (entry == nullptr) {
entry = new HashNode<K, V>(key, value);
if (prev == nullptr) {
// insert as first bucket
table[hashValue] = entry;
} else {
prev->setNext(entry);
}
++elements;
} else {
// just update the value
entry->setValue(value);
}
}
V& operator [] (const K& key) {
unsigned long hashValue = hashFunc(key);
HashNode<K, V> *entry = table[hashValue];
while (entry != NULL) {
if (entry->getKey() == key) {
return entry->getValue();
}
entry = entry->getNext();
}
// key is not found
put(key, V{});
return (*this)[key];
}
void remove(const K &key) {
unsigned long hashValue = hashFunc(key);
HashNode<K, V> *prev = nullptr;
HashNode<K, V> *entry = table[hashValue];
while (entry != nullptr and entry->getKey() != key) {
prev = entry;
entry = entry->getNext();
}
if (entry != nullptr) {
if (prev == nullptr) {
// remove first bucket of the list
table[hashValue] = entry->getNext();
} else {
prev->setNext(entry->getNext());
}
--elements;
delete entry;
}
}
V& insert(K const& key) {
put(key, V{});
return (*this)[key];
}
size_t size() const {
return elements;
}
private:
// hash table
HashNode<K, V> **table;
F hashFunc;
size_t elements;
};
#endif // HASH_TABLE_H