-
Notifications
You must be signed in to change notification settings - Fork 128
/
hashtable.h
248 lines (227 loc) · 6.79 KB
/
hashtable.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
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
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <functional>
#include <iostream>
#include <vector>
template<class Key, class Data>
struct HashEntry
{
Key key;
Data data;
int next;
};
// a useful core hash function
inline unsigned int hash(unsigned int k)
{ return k*2654435769u; }
// default hash function object
struct DefaultHashFunction
{
template<typename Key>
unsigned int operator() (const Key &k) const { return hash(k); }
};
struct equal
{
template<typename T>
bool operator() (const T &a, const T &b) const { return a==b; }
};
template<typename Key, typename Data, class HashFunction=DefaultHashFunction, class KeyEqual=equal>
struct HashTable
{
unsigned int table_rank;
unsigned int table_bits;
std::vector<int> table;
unsigned int num_entries;
std::vector<HashEntry<Key, Data> > pool;
int free_list;
const HashFunction hash_function;
const KeyEqual key_equal;
explicit HashTable(unsigned int expected_size=64)
: hash_function(HashFunction()), key_equal(KeyEqual())
{ init(expected_size); }
explicit HashTable(const HashFunction &hf, unsigned int expected_size=64)
: hash_function(hf), key_equal(KeyEqual())
{ init(expected_size); }
void init(unsigned int expected_size)
{
unsigned int i;
num_entries=0;
table_rank=4;
while(1u<<table_rank < expected_size)
++table_rank;
++table_rank; // give us some extra room
table_bits=(1u<<table_rank)-1;
table.resize(1u<<table_rank);
for(i=0; i<table.size(); ++i)
table[i]=-1; // empty list
pool.resize(1u<<table_rank);
free_list=0;
for(unsigned int i=0; i<pool.size()-1; ++i)
pool[i].next=i+1;
pool[pool.size()-1].next=-1; // end of free list
}
void add(const Key &k, const Data &d)
{
if(free_list==-1)
reserve(1u<<(table_rank+1));
int i=free_list; // where we're going to put the new entry
free_list=pool[i].next;
unsigned int t=hash_function(k)&table_bits; // index into table
pool[i].key=k;
pool[i].data=d;
pool[i].next=table[t]; // put the new entry at the start of table[t]'s list
table[t]=i;
++num_entries;
}
void delete_entry(const Key &k, const Data &d) // delete first entry that matches both key and data
{
unsigned int t=hash_function(k)&table_bits;
int i=table[t], *p_i=&table[t];
while(i!=-1){
if(key_equal(k, pool[i].key) && d==pool[i].data){
*p_i=pool[i].next; // make list skip over this entry
pool[i].next=free_list; // and put it on the front of the free list
free_list=i;
return; // and we're done
}
p_i=&pool[i].next;
i=*p_i;
}
}
unsigned int size() const
{ return num_entries; }
void clear()
{
unsigned int i=0;
num_entries=0;
for(i=0; i<table.size(); ++i)
table[i]=-1; // empty list
free_list=0;
for(i=0; i<pool.size()-1; ++i)
pool[i].next=i+1;
pool[pool.size()-1].next=-1;
}
void reserve(unsigned int expected_size)
{
if(expected_size<=pool.size())
return;
while(1u<<table_rank < expected_size)
++table_rank;
table_bits=(1u<<table_rank)-1;
// increase room for new entries
unsigned int old_size=(unsigned int)pool.size(), i;
pool.resize(1u<<table_rank);
for(i=old_size; i<pool.size()-1; ++i)
pool[i].next=i+1;
pool[i].next=free_list;
free_list=old_size;
// And finally need to redo table (rehash entries)
old_size=(unsigned int)table.size();
table.resize(1u<<table_rank);
unsigned int t;
for(t=old_size; t<table.size(); ++t)
table[t]=-1; // initially make new lists empty
int j, *p_j;
for(t=0; t<old_size; ++t){
j=table[t];
p_j=&table[t];
while(j!=-1){
unsigned int new_t=hash_function(pool[j].key)&table_bits;
if(new_t!=t){ // j doesn't belong in this list anymore?
// delete from this list
*p_j=pool[j].next;
// add to correct list
pool[j].next=table[new_t];
table[new_t]=j;
}else
p_j=&(pool[j].next);
j=*p_j;
}
}
}
bool has_entry(const Key &k) const
{
unsigned int t=hash_function(k)&table_bits;
int i=table[t];
while(i!=-1){
if(key_equal(k, pool[i].key))
return true;
i=pool[i].next;
}
return false;
}
bool get_entry(const Key &k, Data &data_return) const
{
unsigned int t=hash_function(k)&table_bits;
int i=table[t];
while(i!=-1){
if(key_equal(k, pool[i].key)){
data_return=pool[i].data;
return true;
}
i=pool[i].next;
}
return false;
}
void append_all_entries(const Key& k, std::vector<Data>& data_return) const
{
unsigned int t=hash_function(k)&table_bits;
int i=table[t];
while(i!=-1){
if(key_equal(k, pool[i].key)) data_return.push_back(pool[i].data);
i=pool[i].next;
}
}
Data &operator() (const Key &k, const Data &missing_data)
{
unsigned int t=hash_function(k)&table_bits;
int i=table[t];
while(i!=-1){
if(key_equal(k, pool[i].key))
return pool[i].data;
i=pool[i].next;
}
add(k, missing_data); // note - this could cause the table to be resized, and t made out-of-date
return pool[table[hash_function(k)&table_bits]].data; // we know that add() puts it here!
}
const Data &operator() (const Key &k, const Data &missing_data) const
{
unsigned int t=hash_function(k)&table_bits;
int i=table[t];
while(i!=-1){
if(key_equal(k, pool[i].key))
return pool[i].data;
i=pool[i].next;
}
return missing_data;
}
void output_statistics() const
{
std::vector<int> lengthcount(table.size());
unsigned int t;
int total=0;
for(t=0; t<table.size(); ++t){
int i=table[t], length=0;
while(i!=-1){
++length;
i=pool[i].next;
}
++lengthcount[length];
++total;
}
int subtotal=0;
int maxlength=0;
for(t=0; t<lengthcount.size() && t<10; ++t){
subtotal+=lengthcount[t];
if(lengthcount[t]>0){
std::cout<<"length "<<t<<": "<<lengthcount[t]<<" ("<<lengthcount[t]/(float)total*100.0<<"%)"<<std::endl;
maxlength=t;
}
}
std::cout<<"rest: "<<total-subtotal<<" ("<<100.0*(1.0-subtotal/(float)total)<<"%)"<<std::endl;
for(; t<lengthcount.size(); ++t)
if(lengthcount[t]>0)
maxlength=t;
std::cout<<"longest list: "<<maxlength<<std::endl;
}
};
#endif