-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.h
100 lines (83 loc) · 2.25 KB
/
hash.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
#ifndef HASH_H
#define HASH_H
#include <deque>
#include <string>
template <class valueType>
class hashTable
{
std::deque<valueType*> value;
unsigned amount;
public:
hashTable(){
amount = 0;
}
hashTable(unsigned n){
amount = n;
for (unsigned i = 0; i < n; ++i){
value.push_back(NULL);
}
}
bool add(valueType *element, unsigned (*hash_function)(valueType*)){
unsigned index = hash_function(element);
index %= amount;
unsigned count = 0;
while (value[index]!=NULL){
++count;
if (count > amount){
return false;
}
++index;
index %= amount;
}
value[index] = element;
return true;
}
valueType *find(valueType *X, unsigned (*hash_function)(valueType*), bool (*comp)(valueType*, valueType*)){
unsigned index = hash_function(X);
index %= amount;
unsigned count = 0;
while (!comp(value[index], X) && value[index]!=NULL){
++count;
if (count > amount){
return NULL;
}
++index;
index %= amount;
}
return value[index];
}
bool del(valueType *X, unsigned (*hash_function)(valueType*), bool (*comp)(valueType*, valueType*)){
//FIND
unsigned index = hash_function(X);
index %= amount;
unsigned count = 0;
while (!comp(value[index], X) && value[index]!=NULL){
++count;
if (count > amount){
return false;
}
++index;
index %= amount;
}
unsigned del = index;
++index;
index %= amount;
unsigned prev;
while(value[index] != NULL && del != index){
if (hash_function(value[del]) == hash_function(value[index])){
if (index == 0){
prev = amount - 1;
}else{
prev = index - 1;
}
value[prev] = value[index];
//value[index] = NULL;
}
++index;
index %= amount;
}
value[index] = NULL;
return true;
}
};
#endif // HASH_H