-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChainHashTable.h
More file actions
329 lines (265 loc) · 7.01 KB
/
Copy pathChainHashTable.h
File metadata and controls
329 lines (265 loc) · 7.01 KB
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
/*
* chtbl.h
*
* Created on: 2016Äê2ÔÂ17ÈÕ
* Author: LeoBrilliant
*/
#ifndef C_CHTBL_H_
#define C_CHTBL_H_
#include "ProgramMessage.h"
#include "List.h"
#include <cstdlib>
#include <cstdio>
//#include "UnitTest/ChainHashTableTest.h"
const int ELEMENT_FOUND = 0;
const int ELEMENT_NOT_FOUND = 1;
template<typename Key, typename Value>
class ChainHashTable
{
public:
ChainHashTable(){
this->buckets = 0;
this->hashfunc = NULL;
this->match = NULL;
this->destroy = NULL;
this->size = 0;
this->table = NULL;
cout << "ChainHashTable Default Constructor" << endl;
}
ChainHashTable(
int buckets,
int (*hashfunc)(const Key key),
int (*match)(Key key1, Key key2),
void (*destroy)(Key data)
);
virtual ~ChainHashTable();
private:
int buckets;
int (*hashfunc)(const Key key);
int (*match)(Key key1, Key key2);
void (*destroy)(Key data);
int size;
List<Key> * table;
int chtbl_init(int buckets, int (*h)(const Key key),
int (*match)(const Key key1, const Key key2),
void (*destroy)(Key data));
void chtbl_destroy();
int chtbl_insert(Key data);
int chtbl_remove(Key data);
int chtbl_lookup(Key data);
public:
inline int Size() {return this->size;}
inline int Insert(Key data){ return chtbl_insert(data); }
inline int Remove(Key data){ return chtbl_remove(data); }
inline int LookUp(Key data){ return chtbl_lookup(data); }
virtual typename List<Key>::LstCstIter GetBeginIter(int i);
virtual typename List<Key>::LstCstIter GetEndIter(int i);
virtual inline void Clear();
virtual void GetSummary();
virtual void GetDetail(const string& s);
virtual int GetBuckets() const;
virtual const List<Key>* GetTable() const;
void SetHashfunc(int (*hashfunc)(const Key key));
void SetMatch(int (*match)(Key key1, Key key2));
void SetBuckets(int buckets) {
this->buckets = buckets;
}
void SetTable(List<Key>* table) {
this->table = table;
}
static const enum ConstValue
{
ELEMENT_FOUND = 0,
ELEMENT_NOT_FOUND = 1,
SUCCESS = 0,
FAILED = -1,
EXCEPTION = -1
} x ;
//template<typename Key1, typename Value1>
//friend class ChainHashTableTest;
};
template<typename Key, typename Value>
ChainHashTable<Key,Value>::ChainHashTable(
int buckets,
int (*hash)(const Key key),
int (*match)(Key key1, Key key2),
void (*destroy)(Key data)) :
buckets(buckets), hashfunc(hash), match(match), destroy(destroy)
{
//int i;
//Key k;
ProgramMessage::Debug("ChainHashTable Constructor");
try
{
ProgramMessage::Debug("Allocate New buckets");
this->table = new List<Key>[buckets];
}
catch(...)
{
ProgramMessage::Fatal("Allocate New buckets Failed");
//return ConstValue::EXCEPTION;
}
this->buckets = buckets;
//for(i = 0; i < this->buckets; ++i)
{
//list_init(&this->table[i], destroy);
}
this->size = 0;
//return 0;
}
template<typename Key, typename Value>
ChainHashTable<Key,Value>::~ChainHashTable()
{
ProgramMessage::Debug("ChainHashTable Destructor");
if(this->table)
{
delete [] this->table;
this->table = NULL;
}
}
template<typename Key, typename Value>
int ChainHashTable<Key,Value>::chtbl_init(int buckets, int (*hashfunc)(const Key key),
int (*match)(const Key key1, const Key key2),
void (*destroy)(Key data))
{
int i;
Key k;
try
{
this->table = new List<Key>[buckets];
}
catch(...)
{
return ConstValue::EXCEPTION;
}
this->buckets = buckets;
//for(i = 0; i < this->buckets; ++i)
{
//list_init(&this->table[i], destroy);
}
this->hashfunc = hashfunc;
this->match = match;
this->destroy = destroy;
this->size = 0;
return 0;
}
template<typename Key, typename Value>
void ChainHashTable<Key,Value>::chtbl_destroy()
{
int i;
//for(i = 0; i < this->buckets; ++i)
{
//list_destroy(&this->table[i]);
}
delete [] this->table ;
//ToDo
//other destruct code below
return;
}
template<typename Key, typename Value>
int ChainHashTable<Key,Value>::chtbl_insert(Key data)
{
Key temp;
int bucket, retval;
temp = data;
if(this->chtbl_lookup(temp) == ConstValue::ELEMENT_FOUND)
{
return ConstValue::FAILED;
}
bucket = this->hashfunc(data) % this->buckets;
this->table[bucket].PushBack(data);
this->size++;
return retval=this->Size();
}
template<typename Key, typename Value>
int ChainHashTable<Key,Value>::chtbl_remove(Key data)
{
//Key element, prev;
int bucket;
bucket = this->hashfunc(data) % this->buckets;
for(typename List<Key>::LstCstIter element = this->table[bucket].Begin(); element != this->table[bucket].Begin(); element++)
{
if(this->match(data, *element))
{
this->table[bucket].Erase(element);
this->size--;
return ConstValue::SUCCESS;
}
}
return ConstValue::FAILED;
}
template<typename Key, typename Value>
int ChainHashTable<Key,Value>::chtbl_lookup(Key data)
{
int bucket;
bucket = this->hashfunc(data) % this->buckets;
for(typename List<Key>::LstCstIter element = this->table[bucket].Begin(); element != this->table[bucket].End(); element++)
{
if(this->match(data, *element))
{
data = *element;
return ConstValue::ELEMENT_FOUND;
}
}
return ConstValue::ELEMENT_NOT_FOUND;
}
template<typename Key, typename Value>
void ChainHashTable<Key,Value>::GetSummary()
{
ProgramMessage::Debug("Summary[");
ProgramMessage::Debug("size:\t");
ProgramMessage::Debug<int>(this->Size());
ProgramMessage::Debug("]");
}
template<typename Key, typename Value>
inline typename List<Key>::LstCstIter ChainHashTable<Key, Value>::GetBeginIter(int i) {
return this->table[i].Begin();
}
template<typename Key, typename Value>
inline typename List<Key>::LstCstIter ChainHashTable<Key, Value>::GetEndIter(int i) {
if(i >= 0 && i < this->buckets)
return this->table[i].End();
return this->table[i].End();
}
template<typename Key, typename Value>
inline void ChainHashTable<Key, Value>::Clear() {
for(int i = 0; i < this->buckets; ++i)
{
this->table[i].Clear();
}
this->size = 0;
}
template<typename Key, typename Value>
inline int ChainHashTable<Key, Value>::GetBuckets() const {
return buckets;
}
template<typename Key, typename Value>
inline void ChainHashTable<Key,Value>::SetHashfunc(int (*hashfunc)(const Key key))
{
this->hashfunc = hashfunc;
}
template<typename Key, typename Value>
inline void ChainHashTable<Key,Value>::SetMatch(int (*match)(Key key1, Key key2))
{
this->match = match;
}
template<typename Key, typename Value>
inline const List<Key>* ChainHashTable<Key, Value>::GetTable() const {
return table;
}
template<typename Key, typename Value>
void ChainHashTable<Key,Value>::GetDetail(const string & s)
{
ProgramMessage::Debug(s);
this->GetSummary();
ProgramMessage::Debug("Elements[");
for(int i=0; i < this->buckets; ++i)
{
ProgramMessage::Debug("bucket");
char str[20];
sprintf(str, "%d", i);
this->table[i].GetDetail(str);
}
ProgramMessage::Debug("]");
}
#endif /* C_CHTBL_H_ */