-
Notifications
You must be signed in to change notification settings - Fork 14
/
hashtable1.cpp
129 lines (105 loc) · 3.21 KB
/
hashtable1.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
#include "hashtable1.h"
#include <assert.h>
#include <memory.h>
#define USE_FAST_SETITEM 1
//----------------------------------------------
// from code.google.com/p/smhasher/wiki/MurmurHash3
inline static uint32_t integerHash(uint32_t h)
{
h ^= h >> 16;
h *= 0x85ebca6b;
h ^= h >> 13;
h *= 0xc2b2ae35;
h ^= h >> 16;
return h;
}
//----------------------------------------------
HashTable1::HashTable1(uint32_t arraySize)
{
// Initialize cells
assert((arraySize & (arraySize - 1)) == 0); // Must be a power of 2
m_arraySize = arraySize;
m_entries = new Entry[arraySize];
Clear();
}
//----------------------------------------------
HashTable1::~HashTable1()
{
// Delete cells
delete[] m_entries;
}
//----------------------------------------------
#if USE_FAST_SETITEM
void HashTable1::SetItem(uint32_t key, uint32_t value)
{
assert(key != 0);
assert(value != 0);
for (uint32_t idx = integerHash(key);; idx++)
{
idx &= m_arraySize - 1;
// Load the key that was there.
uint32_t probedKey = mint_load_32_relaxed(&m_entries[idx].key);
if (probedKey != key)
{
// The entry was either free, or contains another key.
if (probedKey != 0)
continue; // Usually, it contains another key. Keep probing.
// The entry was free. Now let's try to take it using a CAS.
uint32_t prevKey = mint_compare_exchange_strong_32_relaxed(&m_entries[idx].key, 0, key);
if ((prevKey != 0) && (prevKey != key))
continue; // Another thread just stole it from underneath us.
// Either we just added the key, or another thread did.
}
// Store the value in this array entry.
mint_store_32_relaxed(&m_entries[idx].value, value);
return;
}
}
#else
void HashTable1::SetItem(uint32_t key, uint32_t value)
{
assert(key != 0);
assert(value != 0);
for (uint32_t idx = integerHash(key);; idx++)
{
idx &= m_arraySize - 1;
uint32_t prevKey = mint_compare_exchange_strong_32_relaxed(&m_entries[idx].key, 0, key);
if ((prevKey == 0) || (prevKey == key))
{
mint_store_32_relaxed(&m_entries[idx].value, value);
return;
}
}
}
#endif
//----------------------------------------------
uint32_t HashTable1::GetItem(uint32_t key)
{
assert(key != 0);
for (uint32_t idx = integerHash(key);; idx++)
{
idx &= m_arraySize - 1;
uint32_t probedKey = mint_load_32_relaxed(&m_entries[idx].key);
if (probedKey == key)
return mint_load_32_relaxed(&m_entries[idx].value);
if (probedKey == 0)
return 0;
}
}
//----------------------------------------------
uint32_t HashTable1::GetItemCount()
{
uint32_t itemCount = 0;
for (uint32_t idx = 0; idx < m_arraySize; idx++)
{
if ((mint_load_32_relaxed(&m_entries[idx].key) != 0)
&& (mint_load_32_relaxed(&m_entries[idx].value) != 0))
itemCount++;
}
return itemCount;
}
//----------------------------------------------
void HashTable1::Clear()
{
memset(m_entries, 0, sizeof(Entry) * m_arraySize);
}