-
Notifications
You must be signed in to change notification settings - Fork 0
/
htchained.h
60 lines (53 loc) · 1.6 KB
/
htchained.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
#ifndef HTCHAINED_H
#define HTCHAINED_H
#include <deque>
#include <list>
#include <array>
template <class valueType>
class HTChained
{
std::array<std::list<valueType>, 1> *value;
unsigned amount;
public:
HTChained(){
amount = 0;
}
HTChained(unsigned n){
amount = n;
value = new std::array<std::list<valueType>, this->amount>;
}
bool add(valueType *element, unsigned (*hash_function)(valueType*)){
unsigned index = hash_function(element);
index %= amount;
value->operator [](index).push_front(*element);
return true;
}
valueType *find(valueType *X, unsigned (*hash_function)(valueType*), bool (*comp)(valueType*, valueType*)){
unsigned index = hash_function(X);
index %= amount;
std::list<valueType> temp = value->operator [](index);
for (typename std::list<valueType>::iterator it=temp.begin(); it != temp.end(); ++it){
valueType *tmp = &*it;
if (comp(tmp, X)){
return tmp;
}
}
return NULL;
}
bool del(valueType *X, unsigned (*hash_function)(valueType*), bool (*comp)(valueType*, valueType*)){
unsigned index = hash_function(X);
index %= amount;
std::list<valueType> temp = value[index];
unsigned i = 0;
while(!temp.empty()){
if (comp(X, &temp.front())){
value[index].erase(value[index].begin() + i);
return true;
}
temp.pop_front();
++i;
}
return false;
}
};
#endif // HTCHAINED_H