-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxl_ht_array.c
More file actions
185 lines (149 loc) · 4.18 KB
/
xl_ht_array.c
File metadata and controls
185 lines (149 loc) · 4.18 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
/*
hash table: use array to construct bucket
*/
typedef int xl_ht_array_compare_fun_t(const void *node1, const void *node2);
typedef uint32_t xl_ht_array_hash_fun_t(const void *node);
typedef int xl_ht_array_eliminate_fun_t(const void *node, const void *arg);
typedef struct xl_ht_array {
void *mem;
void *zero;
size_t unit_size;
size_t unit_num;
size_t bukcet_num;
size_t *mods;
xl_ht_array_compare_fun_t *compare_fun;
xl_ht_array_hash_fun_t *hash_fun;
xl_ht_array_eliminate_fun_t *eliminate_fun;
} xl_ht_array_t;
static int _is_prime(size_t x){
if(x <= 1) return 0;
size_t i = 2;
for(;i * i <= x; i ++){
if(x % i == 0) return 0;
}
return 1;
}
static size_t _get_primes(size_t base, size_t *primes, size_t num){
size_t total_num = 0;
size_t i = base;
int index = 0;
while(1){
if(_is_prime(i)) {
*(primes + index) = i;
total_num += i;
index ++;
if(index == num) break;
}
i ++;
}
}
xl_ht_array_t* xl_ht_array_init(size_t unit_size, size_t unit_num, size_t bucket_num,
xl_ht_array_compare_fun_t *compare_fun, xl_ht_array_hash_fun_t *hash_fun,
xl_ht_array_eliminate_fun_t *eliminate_fun){
xl_ht_array_t *ht = (xl_ht_array_t*)calloc(sizeof(xl_ht_array_t), 1);
if(ht == NULL) return NULL;
ht->unit_size = unit_size;
ht->bucket_num = bucket_num;
ht->mods = (size_t*)calloc(sizeof(size_t), ht->bucket_num);
ht->unit_num = _get_primes((unit_num + bucket_num - 1) / bucket_num, ht->mods, bucket_num);
ht->mem = calloc(ht->unit_size, ht->unit_num + 1);
ht->zero = ht->mem + ht->unit_size * ht->unit_num;
ht->compare_fun = compare_fun;
ht->hash_fun = hash_fun;
ht->eliminate_fun = eliminate_fun;
return ht;
}
int xl_ht_array_destroy(xl_ht_array_t *ht){
if(ht == NULL) return 0;
if(ht->mem != NULL) free(ht->mem);
if(ht->mods != NULL) free(ht->mods);
free(ht);
return 0;
}
void* _xl_ht_array_find(xl_ht_array_t *ht, uint32_t key, const void *data){
void *base = ht->mem, *target;
size_t i;
for(i = 0; i < ht->bucket_num; i ++){
target = base + ((key % ht->mods[i]) * ht->unit_size);
if(0 == ht->compare_fun(data, target)){
return target;
}
base += ht->mods[i] * ht->unit_size;
}
return NULL;
}
void* xl_ht_array_get(xl_ht_array_t *ht, const void *data){
return _xl_ht_array_find(ht, ht->hash_fun(data), data);
}
void* _eliminate(xl_ht_array_t *ht, uint32_t key, void *data){
void *base = ht->mem, *target, *node;
size_t i;
int x = 0;
for(i = 0; i < ht->bucket_num; i ++){
node = base + ((key % ht->mods[i]) * ht->unit_size);
int ret = ht->eliminate(target, data);
if(ret > x){
x = ret;
target = node;
}
base += ht->mods[i] * ht->unit_size;
}
return target;
}
void* xl_ht_array_put(xl_ht_array_t *ht, void *data, void *eliminate_arg){
uint32_t key = ht->hash_fun(data);
void* node = _xl_ht_array_find(ht, key, ht->zero);
if(node){
memcpy(node, data, ht->unit_size);
return node;
}
if(ht->eliminate_fun){
node = ht->eliminate_fun(key, eliminate_arg);
memcpy(node, data, ht->unit_size);
return node;
}
return NULL;
}
void* xl_ht_array_add(xl_ht_array_t *ht, void *data, int *exist, void *eliminate_arg){
uint32_t key = ht->hash_fun(data);
void* node = _xl_ht_array_find(ht, key, data);
if(node){
if(exist)
*exist = 1;
return node;
}
if(exist)
*exist = 0;
return xl_ht_array_put(ht, data, eliminate_arg);
}
int xl_ht_array_delete(xl_ht_array_t *ht, void *data, int *exist){
uint32_t key = ht->hash_fun(data);
void *node = _xl_ht_array_find(ht, key, data);
if(node){
memcpy(node, ht->zero, ht->unit_size);
if(exist) *exist = 1;
return 0;
}
if(exist) *exist = 0;
return 0;
}
typedef int xl_ht_array_travel_fun_t(void *node, void *arg);
size_t xl_ht_array_travel(xl_ht_array_t *ht, xl_ht_array_travel_fun_t *travel_fun, void *arg){
size_t success_node = 0;
void *node = ht->mem;
size_t i = 0;
for( ; i < ht->unit_num; i ++){
if(0 != ht->comapre_fun(node, ht->zero)){
int travel_ret = travel_fun(node, arg);
if(travel_ret == 0) success_node ++;
}
node += ht->unit_size;
}
return success_node;
}
static int _xl_ht_array_is_empty(void *node, void *arg){
return 0;
}
size_t xl_ht_array_node_used(xl_ht_array_t *ht){
return xl_ht_array_travel(ht, _xl_ht_array_is_empty, NULL);
}