-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_hashmap.h
executable file
·276 lines (250 loc) · 6.12 KB
/
_hashmap.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
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
#ifndef _HASHMAP_H_
#define _HASHMAP_H_
#include <iostream>
using namespace std;
#define TEST_HASHMAP
#ifdef TEST_HASHMAP
int test_hashmap();
#endif
// 计算超过或等于capacity的最小 2^n
size_t getNextPow2(size_t capacity);
template<class Key, class Val>
class HashNode;
template <class Key, class Val, class HashFunc, class EqualKey>
class HashMap
{
public:
HashMap(int size,const Val val);
~HashMap();
bool add(const Key& key, const Val& val);
bool del(const Key& key);
Val& get(const Key& key);
Val& operator [](const Key& key);
void clear();
private:
HashFunc hash;
EqualKey equal;
unsigned int _size;
HashNode<Key, Val> **table;
Val nullValue;
};
template<class Key, class Val>
class HashNode
{
public:
Key _key;
Val _val;
HashNode* next;
HashNode(Key key, Val val)
{
_key = key;
_val = val;
next = NULL;
}
~HashNode()
{
}
HashNode& operator=(const HashNode& node)
{
_key = node.key;
_val = node.val;
next = node.next;
return *this;
}
};
template <class Key, class Value, class HashFunc, class EqualKey>
HashMap<Key, Value, HashFunc, EqualKey>::HashMap(int size, Value nullVal)
: nullValue(nullVal)
{
hash = HashFunc();
equal = EqualKey();
_size = getNextPow2(size);
table = new HashNode<Key, Value>*[_size];
for (unsigned i = 0; i < _size; i++)
table[i] = NULL;
}
template <class Key, class Value, class HashFunc, class EqualKey>
HashMap<Key, Value, HashFunc, EqualKey>::~HashMap()
{
clear();
delete table;
}
template <class Key, class Value, class HashFunc, class EqualKey>
void HashMap<Key, Value, HashFunc, EqualKey>::clear()
{
for (unsigned i = 0; i < _size; i++)
{
HashNode<Key, Value> *currentNode = table[i];
while (currentNode)
{
HashNode<Key, Value> *temp = currentNode;
currentNode = currentNode->next;
delete temp;
}
}
}
template <class Key, class Value, class HashFunc, class EqualKey>
bool HashMap<Key, Value, HashFunc, EqualKey>::add(const Key& key, const Value& value)
{
int index = hash(key) & _size;
HashNode<Key, Value>* node = new HashNode<Key, Value>(key, value);
node->next = table[index];
table[index] = node;
node = node->next;
HashNode<Key, Value>* prev = NULL;
while (node)
{
if (node->_key == key)
{
if (prev == NULL)
table[index] = node->next;
else
prev->next = node->next;
delete node;
return true;
}
prev = node;
node = node->next;
}
return true;
}
template <class Key, class Value, class HashFunc, class EqualKey>
bool HashMap<Key, Value, HashFunc, EqualKey>::del(const Key& key)
{
unsigned index = hash(key) & _size;
HashNode<Key, Value>* node = table[index];
HashNode<Key, Value>* prev = NULL;
while (node)
{
if (node->_key == key)
{
if (prev == NULL)
table[index] = node->next;
else
prev->next = node->next;
delete node;
return true;
}
prev = node;
node = node->next;
}
return false;
}
template <class Key, class Value, class HashFunc, class EqualKey>
Value& HashMap<Key, Value, HashFunc, EqualKey>::get(const Key& key)
{
unsigned index = hash(key) & _size;
if (table[index] == NULL)
return nullValue;
else
{
HashNode<Key, Value> * node = table[index];
while (node)
{
if (node->_key == key)
return node->_val;
node = node->next;
}
return nullValue;
}
}
template <class Key, class Value, class HashFunc, class EqualKey>
Value& HashMap<Key, Value, HashFunc, EqualKey>::operator [](const Key& key)
{
return get(key);
}
/*
//比较取模 数值取并集的效率
#include <stdio.h>
#include <windows.h>
#include <time.h> //time_t time() clock_t clock()
#include <Mmsystem.h> //timeGetTime()
#pragma comment(lib, "Winmm.lib") //timeGetTime()
//位运算(&)效率要比代替取模运算(%)高很多,
// 主要原因是位运算直接对内存数据进行操作,不需要转成十进制,因此处理速度非常快
int test_4_win()
{
{
LARGE_INTEGER large_interger;
double dff;
__int64 c1, c2;
QueryPerformanceFrequency(&large_interger);
dff = large_interger.QuadPart;
QueryPerformanceCounter(&large_interger);
c1 = large_interger.QuadPart;
for (size_t i = 0; i < 100000; i++)
{
int tt = i % 256;
}
QueryPerformanceCounter(&large_interger);
c2 = large_interger.QuadPart;
printf("本机高精度计时器频率%lf\n", dff);
printf("第一次计时器值%I64d 第二次计时器值%I64d 计时器差%I64d\n", c1, c2, c2 - c1);
printf("计时%lf毫秒\n", (c2 - c1) * 1000 / dff);
}
cout << "" << endl << endl << endl << endl;
{
LARGE_INTEGER large_interger;
double dff;
__int64 c1, c2;
QueryPerformanceFrequency(&large_interger);
dff = large_interger.QuadPart;
QueryPerformanceCounter(&large_interger);
c1 = large_interger.QuadPart;
for (size_t i = 0; i < 100000; i++)
{
int tt = (i & (2 ^ 8 - 1));
}
QueryPerformanceCounter(&large_interger);
c2 = large_interger.QuadPart;
printf("本机高精度计时器频率%lf\n", dff);
printf("第一次计时器值%I64d 第二次计时器值%I64d 计时器差%I64d\n", c1, c2, c2 - c1);
printf("计时%lf毫秒\n", (c2 - c1) * 1000 / dff);
}
}
*/
/*
//增加高位扰动
size_t getHash(size_t h) {
h ^= (h >> 20) ^ (h >> 12);
return h ^ (h >> 7) ^ (h >> 4);
}
*/
/*
static int indexFor(int h, int length) {
return h & (length - 1);
}
*/
/**
* hash算法仿函数
*/
/*
template<class KeyType>
struct cache_hash_func {
};
inline std::size_t cache_hash_string(const char* __s) {
unsigned long __h = 0;
for (; *__s; ++__s)
__h = 5 * __h + *__s;
return std::size_t(__h);
}
template<>
struct cache_hash_func<std::string> {
std::size_t operator()(const std::string & __s) const {
return cache_hash_string(__s.c_str());
}
};
template<>
struct cache_hash_func<char*> {
std::size_t operator()(const char* __s) const {
return cache_hash_string(__s);
}
};
template<>
struct cache_hash_func<const char*> {
std::size_t operator()(const char* __s) const {
return cache_hash_string(__s);
}
};
*/
#endif