-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconst_hash.cpp
312 lines (219 loc) · 6.7 KB
/
const_hash.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
#include <cstdint>
#include <climits>
#include <stdexcept>
#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
#include <iostream>
#include <memory>
#include <sstream>
#include <deque>
#include <limits>
#include <array>
#include <iterator>
#include <cassert>
template<typename K>
int hash_value( const K &k ) {
return k.hash();
}
template<>
int hash_value( const std::string &s ) {
int h = 0;
for( size_t i = 0; i < s.size(); ++i ) {
h *= 13;
h += s[i];
}
return h;
}
template<typename K, typename V, size_t s, size_t max_size>
class hashtable {
public:
hashtable() {
std::fill( table_, table_ + s, (link*)0 );
}
void insert( const K &k, const V &v ) {
const int slot = get_slot(k);
//link *l = new link( k, v );
link *l = alloc_.alloc(k, v);
l->next_ = table_[slot];
table_[slot] = l;
}
const V * find( const K &k ) {
const int slot = get_slot(k);
link *l = table_[slot];
while( l != 0 ) {
if( l->key_ == k ) {
return &l->value_;
}
l = l->next_;
}
return &l->value_;;
}
void remove( const K &k ) {
const int slot = get_slot(k);
link *l = table_[slot];
link *l_prev = 0;
while( l != 0 ) {
if( l->key_ == k ) {
if( l_prev != 0 ) {
l_prev->next_ = l->next_;
} else {
table_[slot] = l->next_;
}
l_prev = l;
link *l_del = l;
l = l->next_;
//delete l_del;
alloc_.dealloc(l_del);
} else {
l_prev = l;
l = l->next_;
}
}
}
void print_stat() {
size_t len_min = std::numeric_limits<size_t>::max();
size_t len_max = 0;
size_t len_sum = 0;
for( size_t i = 0; i < s; ++i ) {
size_t chain_len = 0;
for( link *l = table_[i]; l != 0; l = l->next_, ++chain_len ) {
}
std::cout << "slot: " << i << " " << chain_len << "\n";
len_min = std::min( len_min, chain_len );
len_max = std::max( len_max, chain_len );
len_sum += chain_len;
}
std::cout << "min/max/avg: " << len_min << " " << len_max << " " << len_sum / float(s) << "\n";
}
private:
size_t get_slot( const K &k ) {
return hash_value( k ) % s;
}
struct link {
link() {}
link( const K &k, const V &v ) : key_(k), value_(v) {
}
K key_;
V value_;
link *next_;
};
class link_allocator {
public:
//link_allocator() : free_list_(0) {}
link_allocator() : heap_next_(0), free_list_(0) {}
link *alloc( const K &k, const V &v ) {
link *ret = 0;
if( free_list_ != 0 ) {
free_list_->key_ = k;
free_list_->value_ = v;
ret = free_list_;
free_list_ = free_list_->next_;
} else {
//links_.push_back( link(k, v) );
if( heap_next_ >= max_size ) {
throw std::runtime_error( "out of heap" );
}
ret = &heap_[heap_next_];
ret->key_ = k;
ret->value_ = v;
++heap_next_;
//ret = &links_.back();
}
return ret;
}
void dealloc( link *l ) {
l->next_ = free_list_;
free_list_ = l;
}
//std::deque<link> links_;
link heap_[max_size];
size_t heap_next_;
link *free_list_;
};
link *table_[s];
link_allocator alloc_;
};
template<typename T>
T partition( T left, T right, T pivot ) {
std::swap( *pivot, *right );
T insert = left;
T cur = left;
while( cur < right ) {
if( *cur < *right ) {
std::swap( *cur, *insert );
++insert;
}
++cur;
}
std::swap( *right, *insert );
return insert;
}
template<typename T>
void quicksort( T start, T end ) {
assert( start <= end );
if( start == end || start == end - 1 ) {
return;
}
size_t len = std::distance( start, end );
size_t p_ind = len / 2;
T pivot = start + p_ind;
T bound = partition( start, end - 1, pivot );
quicksort( start, bound );
quicksort( bound + 1, end );
}
int partition_simple( int *arr, int left, int right, int pivot ) {
assert( left < right );
std::swap( arr[pivot], arr[right] );
int insert = left;
int cur = left;
while( cur != right ) {
if( arr[cur] < arr[right] ) {
std::swap( arr[cur], arr[insert] );
++insert;
}
++cur;
}
std::swap( arr[insert], arr[right] );
return insert;
}
void quicksort_simple( int *arr, int left, int right ) {
if( right - left >= 2 ) {
int mid = (left + right) / 2;
// partition
int bound = partition_simple( arr, left, right, mid );
quicksort_simple(arr, left, bound - 1 );
quicksort_simple(arr, bound + 1, right );
}
}
int main() {
{
std::array<int,10> x = {{2,5,6,3,4,2,6,5,3,2}};
// std::array<int,2> x = {{5,2}};
quicksort( x.begin(), x.end() );
// quicksort_simple(x.data(), 0, x.size() - 1 );
std::copy( x.begin(), x.end(), std::ostream_iterator<int>(std::cout, "\n" ));
return 0;
}
hashtable<std::string,int,256,1000> ht;
for( size_t i = 0; i < 1000; ++i ) {
std::stringstream ss;
ss << "key" << i;
ht.insert( ss.str(), i );
}
for( size_t i = 0; i < 1000; ++i ) {
std::stringstream ss;
ss << "key" << i;
std::string k(ss.str());
const int *x = ht.find( k );
if( x != 0 ) {
std::cout << "find " << k << " " << *x << "\n";
} else {
std::cout << "meeeeep\n";
}
if( i % 2 == 0 ) {
ht.remove( k );
}
}
ht.print_stat();
return 0;
}