-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn_hash_remove.c
59 lines (45 loc) · 1.21 KB
/
n_hash_remove.c
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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "n_hash_int.h"
static inline
int remove_bucket_ll(struct hash_bucket **tbl, const char *key,
unsigned val, void **data)
{
struct hash_bucket *ptr, *last = NULL;
n_assert (tbl[val] != NULL);
for (last = NULL, ptr = tbl[val]; ptr != NULL;
last = ptr, ptr = ptr->next) {
if (strcmp(key, ptr->key) == 0) {
*data = ptr->data;
if (last != NULL)
last->next = ptr->next;
else
tbl[val] = ptr->next;
free(ptr);
return 1;
}
}
return 0;
}
void *n_hash_remove(tn_hash *ht, const char *key)
{
struct hash_bucket **tbl;
void *ptr = NULL;
unsigned val;
int klen;
val = n_hash_dohash(ht, key, &klen);
tbl = ht->table;
if (tbl[val] != NULL)
if (remove_bucket_ll(tbl, key, val, &ptr)) {
ht->items--;
return ptr;
}
val += n_hash_nextslotv(key, klen);
if (val < ht->size && tbl[val])
if (remove_bucket_ll(tbl, key, val, &ptr)) {
ht->items--;
return ptr;
}
return NULL;
}