-
Notifications
You must be signed in to change notification settings - Fork 0
/
brent.cpp
484 lines (433 loc) · 11.6 KB
/
brent.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
Module: brent.cpp
Function:
Test program and implemetation of Brent's variation for hashing.
Copyright and License:
This file copyright (C) 2022 by
MCCI Corporation
3520 Krums Corners Road
Ithaca, NY 14850
See accompanying LICENSE file for copyright and license information.
Author:
Terry Moore, MCCI Corporation November 2022
*/
#include <algorithm>
#include <cassert>
#include <iostream>
/****************************************************************************\
|
| Types
|
\****************************************************************************/
enum class hashMode_t : unsigned
{
kLookup = 1,
kAdd = 2,
kDelete = 3,
};
/// \brief abstract type for keys in the table.
typedef int key_t;
constexpr key_t keyFree = 0;
constexpr key_t keyDeleted = -1;
/// \brief length of hash table -- this must be a prime!
constexpr int len = 127;
/// \brief secondary length, used for re-hashing. Must be len - 2.
constexpr int len2 = len - 2;
/// \brief the contents of the hash table
struct hashEntry_t
{
key_t key = 0;
int contents = 0;
void markDeleted() { this->key = keyDeleted; }
bool isFree() const { return this->key == keyFree; }
bool isDeleted() const { return this->key == keyDeleted; }
/// \brief does an entry contain real data (neither free nor deleted)?
bool isOccupied() const { return !(this->isFree() || this->isDeleted()); }
};
/// \brief the hash table
hashEntry_t keytab[len];
/// \brief statistics
struct stats_t
{
int nCall = 0;
int nProbe = 0;
int nRelocTry = 0;
int nRelocProbe = 0;
int nRelocMove = 0;
int nDeleteTry = 0;
int nDeleteProbe = 0;
int nDeleteMove = 0;
void clear()
{
this->nCall = 0;
this->nProbe = 0;
this->nRelocProbe = 0;
this->nRelocTry = 0;
this->nRelocMove = 0;
this->nDeleteTry = 0;
this->nDeleteProbe = 0;
this->nDeleteMove = 0;
}
void addCall() { ++this->nCall; }
void addProbe() { ++this->nProbe; }
void addRelocTry() { ++this->nRelocTry; }
void addRelocProbe() { ++this->nRelocProbe; }
void addReloc() { ++this->nRelocMove; }
void addDeleteTry() { ++this->nDeleteTry; }
void addDeleteProbe() { ++this->nDeleteProbe; }
void addDeleteMove() { ++this->nDeleteMove; }
stats_t &add(const stats_t &b)
{
this->nCall += b.nCall;
this->nProbe += b.nProbe;
this->nRelocProbe += b.nRelocProbe;
this->nRelocTry += b.nRelocTry;
this->nRelocMove += b.nRelocMove;
this->nDeleteTry += b.nDeleteTry;
this->nDeleteProbe += b.nDeleteProbe;
this->nDeleteMove += b.nDeleteMove;
return *this;
}
void print()
{
std::cout << "nCall: " << this->nCall << " "
<< "nProbe: " << this->nProbe << " "
<< "nDeleteTry:" << this->nDeleteTry << " "
<< "nDeleteProbe: " << this->nDeleteProbe << " "
<< "nDeleteMove: " << this->nDeleteMove << " "
<< "nRelocTry: " << this->nRelocTry << " "
<< "nRelocProbe: " << this->nRelocProbe << " "
<< "nRelocMove: " << this->nRelocMove << " "
<< "\n";
}
};
///
/// \brief bit-reverse a 32-bit number
///
/// \details
/// I found that bit reversing seemed to do a good job of handling
/// situations where the input key sequence was a multiple of the
/// primary and secondary key rehashing value. This is definitely
/// a personal experiment.
///
/// This is a the well known loopless bit reversal scheme.
///
constexpr std::uint32_t bitreverse(
std::uint32_t v
)
{
// swap odd and even bits
v = ((v >> 1) & UINT32_C(0x55555555)) | ((v & UINT32_C(0x55555555)) << 1);
// swap pairs
v = ((v >> 2) & UINT32_C(0x33333333)) | ((v & UINT32_C(0x33333333)) << 2);
// swap nibbles
v = ((v >> 4) & UINT32_C(0x0F0F0F0F)) | ((v & UINT32_C(0x0F0F0F0F)) << 4);
// swap bytes
v = ((v >> 8) & UINT32_C(0x00FF00FF)) | ((v & UINT32_C(0x00FF00FF)) << 8);
// swap halves
v = ((v >> 16) & UINT32_C(0x0000FFFF)) | ((v & UINT32_C(0x0000FFFF)) << 16);
return v;
}
/// \brief bit-reverse a signed integer.
constexpr std::int32_t bitreverse(
std::int32_t v
)
{
return std::int32_t(bitreverse(std::uint32_t(v)));
}
///
/// \brief calculate Brent's Q (secondary hash) for a given key
///
/// \return
/// a number in [1..len-1].
///
constexpr int hash_Q(const key_t key)
{
return bitreverse(std::uint32_t(key)) % len2 + 1;
}
///
/// \brief look up, add, or delete a key using a table managed according
/// to Brent.
///
/// \param [in] key key of the entry to find or create
/// \param [in] mode specifies whether to search (only),
/// to search and add if not found, or
/// to search and delete if found.
/// \param [out] pEntry Set to point to the key if found or
/// successfully added.
/// \param [inout] pStat Optionally points to a statistics object
/// that will be updated reflecting the
/// performance of the search.
///
/// \return
/// If \c true, the key was previously in the table. Otherwise,
/// it was not. For adds, if \c false, \c pEntry indicates whether
/// the value was added (nullptr if table was full, not nullptr
/// if a slot was located).
///
bool hash(const key_t key, const hashMode_t mode, hashEntry_t *&pEntry,
stats_t *pStat)
{
// start up the iteration count so taht it will end
// at len2 (which is len - 2)
int iterationCount = 1 - 2;
// secondary hash code. Note that this must be
// in [1 .. len). Per [brent], this may be any
// independent pseudo-random function of key,
// but note that it must not be zero.
auto secondary_Q = hash_Q(key);
// primary hash code.
int primary_R = std::abs(key) % len;
auto iEntry_s = primary_R;
pEntry = &keytab[iEntry_s];
if (pStat)
pStat->addCall();
while (true)
{
if (pStat != nullptr)
pStat->addProbe();
auto const thisEntryKey = pEntry->key;
if (thisEntryKey == keyFree)
{
// empty slot, end search.
break;
}
else if (thisEntryKey == keyDeleted)
{
// 40 a deleted entry has been found
auto iScan = iEntry_s;
// compute address of next probe
bool needToExit = false;
key_t searchKey;
if (pStat != nullptr) pStat->addDeleteTry();
// scan forward to a free entry; if found,
// we'll move the entry to here, to shorten
// probes.
do
{
if (pStat != nullptr)
pStat->addDeleteProbe();
iScan += secondary_Q;
if (iScan >= len)
iScan -= len;
searchKey = keytab[iScan].key;
// check for empty speace or complete scan of table
if (searchKey == keyFree || iScan == primary_R)
{
needToExit = true;
break;
}
// check for mismatch or deleted entry
} while (searchKey != key || searchKey == keyDeleted);
// empty space or complete scan?
if (needToExit)
break;
// key found. Move it and the associated value to
// save probes on the next search for the same key.
// (or simply nuke the one we found if we're deleting)
if (pStat != nullptr) pStat->addDeleteMove();
if (mode != hashMode_t::kDelete)
keytab[iEntry_s] = keytab[iScan];
// where were were is now nothing.
keytab[iScan].markDeleted();
return true;
}
else if (thisEntryKey == key)
{
// 60: found it. delete if needed.
if (mode == hashMode_t::kDelete)
{
pEntry->markDeleted();
}
return true;
}
else
{
// advance
++iterationCount;
iEntry_s += secondary_Q;
if (iEntry_s >= len)
{
iEntry_s -= len;
}
// invariant...
pEntry = &keytab[iEntry_s];
if (iEntry_s == primary_R)
break;
}
}
// 30 the key is not in the table
// the key is not in the table. return unless an
// entry must be made. This also checks for invalid
// key values.
if (!(mode == hashMode_t::kAdd && iterationCount <= len2 && key != keyFree &&
key != keyDeleted))
{
pEntry = nullptr;
return false;
}
// 70 add the key
if (iterationCount <= 0)
{
// 120 enter the new key.
pEntry->key = key;
// we added it.
return false;
}
else
{
//
// we probed more than twice, so ... we need
// to shuffle things around. iEntry_s.key is
// zero.
//
auto const brent_s = iterationCount + 2;
if (pStat) pStat->addRelocTry();
// we need to iterate over Brent's h[c,d]:
// h[0,1]..h[0,s-1], h[1,1]..h[1,s-2], h[2,1]..h[2,s-3], ..., h[s-2,1]..h[s-2,1]
for (int c = 0; c <= brent_s - 2; ++c)
{
auto const h_i = (primary_R + c * secondary_Q) % len;
auto const q_i = hash_Q(keytab[h_i].key);
for (int d = 1; d <= brent_s - c - 1; ++d)
{
if (pStat)
pStat->addRelocProbe();
auto const h_ij = (h_i + d * q_i) % len;
if (!keytab[h_ij].isOccupied())
{
if (pStat) pStat->addReloc();
// move key[h_i] to key[h_ij], and put new key at key[h_i].
keytab[h_ij] = keytab[h_i];
keytab[h_i].key = key;
pEntry = &keytab[h_i];
return false;
}
}
}
// no point in moving things around. Just return.
assert(!pEntry->isOccupied());
pEntry->key = key;
return false;
}
assert(false && "Not reached");
}
/****************************************************************************\
|
| Test
|
\****************************************************************************/
key_t testKey(int j) { return uint16_t(j * 31413); }
int main()
{
std::cout << "brent hashing test\n";
hashEntry_t *pEntry;
stats_t stats;
for (auto j = 1; j < 128; ++j)
{
auto i = testKey(j);
auto fResult = hash(i, hashMode_t::kAdd, pEntry, &stats);
if (fResult)
std::cout << "add found existing key: "
<< "try=" << j << " "
<< "key=" << i << " "
<< "iEntry=" << pEntry - &keytab[0] << " "
<< ".key=" << pEntry->key << "\n";
if (pEntry == nullptr)
std::cout << "add returned full table: "
<< "try=" << j << " "
<< "key=" << i << "\n";
else if (pEntry->key != i)
std::cout << "add didn't change key: "
<< "try=" << j << " "
<< "key=" << i << " "
<< "iEntry=" << pEntry - &keytab[0] << " "
<< ".key=" << pEntry->key << "\n";
}
stats.print();
std::cout << "done with inserts\n";
stats.clear();
for (auto j = 1; j < 128; ++j)
{
auto i = testKey(j);
auto fResult = hash(i, hashMode_t::kLookup, pEntry, &stats);
if (!fResult)
{
if (pEntry == nullptr)
{
std::cout << "lookup returned false: "
<< "try=" << j << " "
<< "key=" << i << "\n";
}
else
{
std::cout << "lookup returned false and existing key: "
<< "try=" << j << " "
<< "key=" << i << " "
<< "iEntry=" << pEntry - &keytab[0] << " "
<< ".key=" << pEntry->key << "\n";
}
}
else
{
if (pEntry == nullptr)
{
std::cout << "lookup returned true but null pointer: "
<< "try=" << j << " "
<< "key=" << i << "\n";
}
else if (pEntry->key != i)
{
std::cout << "lookup returned true but wrong key: "
<< "try=" << j << " "
<< "key=" << i << " "
<< "iEntry=" << pEntry - &keytab[0] << " "
<< ".key=" << pEntry->key << "\n";
}
}
}
stats.print();
std::cout << "done with lookups\n";
stats.clear();
for (auto j = 1; j < 128; ++j)
{
auto i = testKey(j);
auto fResult = hash(i, hashMode_t::kDelete, pEntry, &stats);
if (!fResult)
{
if (pEntry == nullptr)
{
std::cout << "delete returned false: "
<< "try=" << j << " "
<< "key=" << i << "\n";
}
else
{
std::cout << "delete returned false and existing key: "
<< "try=" << j << " "
<< "key=" << i << " "
<< "iEntry=" << pEntry - &keytab[0] << " "
<< ".key=" << pEntry->key << "\n";
}
}
else
{
if (pEntry == nullptr)
{
std::cout << "delete returned true but null pointer: "
<< "try=" << j << " "
<< "key=" << i << "\n";
}
else if (pEntry->key != keyDeleted)
{
std::cout << "delete returned true but wrong key: "
<< "try=" << j << " "
<< "key=" << i << " "
<< "iEntry=" << pEntry - &keytab[0] << " "
<< ".key=" << pEntry->key << "\n";
}
}
}
stats.print();
std::cout << "done with deletes\n";
}