forked from preshing/CompareIntegerMaps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.cpp
248 lines (226 loc) · 6.56 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <config.h>
#include "hashtable.h"
#include "util.h"
#include <assert.h>
#include <memory.h>
#define FIRST_CELL(hash) (m_cells + ((hash) & (m_arraySize - 1)))
#define CIRCULAR_NEXT(c) ((c) + 1 != m_cells + m_arraySize ? (c) + 1 : m_cells)
#define CIRCULAR_OFFSET(a, b) ((b) >= (a) ? (b) - (a) : m_arraySize + (b) - (a))
//----------------------------------------------
// HashTable::HashTable
//----------------------------------------------
HashTable::HashTable(size_t initialSize)
{
// Initialize regular cells
m_arraySize = initialSize;
assert((m_arraySize & (m_arraySize - 1)) == 0); // Must be a power of 2
m_cells = new Cell[m_arraySize];
memset(m_cells, 0, sizeof(Cell) * m_arraySize);
m_population = 0;
// Initialize zero cell
m_zeroUsed = 0;
m_zeroCell.key = 0;
m_zeroCell.value = 0;
}
//----------------------------------------------
// HashTable::~HashTable
//----------------------------------------------
HashTable::~HashTable()
{
// Delete regular cells
delete[] m_cells;
}
//----------------------------------------------
// HashTable::Lookup
//----------------------------------------------
HashTable::Cell* HashTable::Lookup(size_t key)
{
if (key)
{
// Check regular cells
for (Cell* cell = FIRST_CELL(integerHash(key));; cell = CIRCULAR_NEXT(cell))
{
if (cell->key == key)
return cell;
if (!cell->key)
return NULL;
}
}
else
{
// Check zero cell
if (m_zeroUsed)
return &m_zeroCell;
return NULL;
}
};
//----------------------------------------------
// HashTable::Insert
//----------------------------------------------
HashTable::Cell* HashTable::Insert(size_t key)
{
if (key)
{
// Check regular cells
for (;;)
{
for (Cell* cell = FIRST_CELL(integerHash(key));; cell = CIRCULAR_NEXT(cell))
{
if (cell->key == key)
return cell; // Found
if (cell->key == 0)
{
// Insert here
if ((m_population + 1) * 4 >= m_arraySize * 3)
{
// Time to resize
Repopulate(m_arraySize * 2);
break;
}
++m_population;
cell->key = key;
return cell;
}
}
}
}
else
{
// Check zero cell
if (!m_zeroUsed)
{
// Insert here
m_zeroUsed = true;
if (++m_population * 4 >= m_arraySize * 3)
{
// Even though we didn't use a regular slot, let's keep the sizing rules consistent
Repopulate(m_arraySize * 2);
}
}
return &m_zeroCell;
}
}
//----------------------------------------------
// HashTable::Delete
//----------------------------------------------
void HashTable::Delete(Cell* cell)
{
if (cell != &m_zeroCell)
{
// Delete from regular cells
assert(cell >= m_cells && cell - m_cells < m_arraySize);
assert(cell->key);
// Remove this cell by shuffling neighboring cells so there are no gaps in anyone's probe chain
for (Cell* neighbor = CIRCULAR_NEXT(cell);; neighbor = CIRCULAR_NEXT(neighbor))
{
if (!neighbor->key)
{
// There's nobody to swap with. Go ahead and clear this cell, then return
cell->key = 0;
cell->value = 0;
m_population--;
return;
}
Cell* ideal = FIRST_CELL(integerHash(neighbor->key));
if (CIRCULAR_OFFSET(ideal, cell) < CIRCULAR_OFFSET(ideal, neighbor))
{
// Swap with neighbor, then make neighbor the new cell to remove.
*cell = *neighbor;
cell = neighbor;
}
}
}
else
{
// Delete zero cell
assert(m_zeroUsed);
m_zeroUsed = false;
cell->value = 0;
m_population--;
return;
}
}
//----------------------------------------------
// HashTable::Clear
//----------------------------------------------
void HashTable::Clear()
{
// (Does not resize the array)
// Clear regular cells
memset(m_cells, 0, sizeof(Cell) * m_arraySize);
m_population = 0;
// Clear zero cell
m_zeroUsed = false;
m_zeroCell.value = 0;
}
//----------------------------------------------
// HashTable::Compact
//----------------------------------------------
void HashTable::Compact()
{
Repopulate(upper_power_of_two((m_population * 4 + 3) / 3));
}
//----------------------------------------------
// HashTable::Repopulate
//----------------------------------------------
void HashTable::Repopulate(size_t desiredSize)
{
assert((desiredSize & (desiredSize - 1)) == 0); // Must be a power of 2
assert(m_population * 4 <= desiredSize * 3);
// Get start/end pointers of old array
Cell* oldCells = m_cells;
Cell* end = m_cells + m_arraySize;
// Allocate new array
m_arraySize = desiredSize;
m_cells = new Cell[m_arraySize];
memset(m_cells, 0, sizeof(Cell) * m_arraySize);
// Iterate through old array
for (Cell* c = oldCells; c != end; c++)
{
if (c->key)
{
// Insert this element into new array
for (Cell* cell = FIRST_CELL(integerHash(c->key));; cell = CIRCULAR_NEXT(cell))
{
if (!cell->key)
{
// Insert here
*cell = *c;
break;
}
}
}
}
// Delete old array
delete[] oldCells;
}
//----------------------------------------------
// Iterator::Iterator
//----------------------------------------------
HashTable::Iterator::Iterator(HashTable &table) : m_table(table)
{
m_cur = &m_table.m_zeroCell;
if (!m_table.m_zeroUsed)
Next();
}
//----------------------------------------------
// Iterator::Next
//----------------------------------------------
HashTable::Cell* HashTable::Iterator::Next()
{
// Already finished?
if (!m_cur)
return m_cur;
// Iterate past zero cell
if (m_cur == &m_table.m_zeroCell)
m_cur = &m_table.m_cells[-1];
// Iterate through the regular cells
Cell* end = m_table.m_cells + m_table.m_arraySize;
while (++m_cur != end)
{
if (m_cur->key)
return m_cur;
}
// Finished
return m_cur = NULL;
}