-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.c
107 lines (89 loc) · 2.53 KB
/
hashtable.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
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
#include "hashtable.h"
#define likely(exp) __builtin_expect (!!(exp), 1)
#define unlikely(exp) __builtin_expect (!!(exp), 0)
void
hashtable_rehash (hashtable_node_t **data, size_t cap, hashtable_t *ht)
{
size_t ht_cap = ht->cap;
hashtable_node_t **ht_data = ht->data;
for (size_t i = 0; i < ht_cap; i++)
for (hashtable_node_t **head, *next, *curr = *ht_data++; curr; curr = next)
{
next = curr->next;
head = data + curr->hash % cap;
if ((curr->next = *head))
(*head)->prev = curr;
curr->prev = NULL;
*head = curr;
}
}
void
hashtable_link (hashtable_t *ht, hashtable_node_t **inpos,
hashtable_node_t *prev, hashtable_node_t *node)
{
hashtable_node_t *next = *inpos;
node->prev = prev;
node->next = next;
*inpos = node;
if (prev)
prev->next = node;
if (next)
next->prev = node;
ht->size++;
}
void
hashtable_erase (hashtable_t *ht, hashtable_node_t *node)
{
hashtable_node_t *prev = node->prev;
hashtable_node_t *next = node->next;
if (next)
next->prev = prev;
if (prev)
prev->next = next;
else
ht->data[node->hash % ht->cap] = next;
ht->size--;
}
/* **************************************************************** */
/* ext */
/* **************************************************************** */
hashtable_node_t *
hashtable_find (const hashtable_t *ht, const hashtable_node_t *target,
hashtable_comp_t *comp)
{
size_t hash = target->hash;
for (hashtable_node_t *curr = ht->data[hash % ht->cap]; curr;
curr = curr->next)
if (hash == curr->hash && comp (target, curr) == 0)
return curr;
return NULL;
}
hashtable_node_t *
hashtable_insert (hashtable_t *ht, hashtable_node_t *node,
hashtable_comp_t *comp)
{
size_t hash = node->hash;
hashtable_node_t *prev = NULL;
hashtable_node_t **head = ht->data + hash % ht->cap;
for (hashtable_node_t *curr = *head; curr; curr = curr->next)
{
if (unlikely (hash == curr->hash && comp (node, curr) == 0))
return NULL;
prev = curr;
}
hashtable_node_t **inpos = prev ? &prev->next : head;
hashtable_link (ht, inpos, prev, node);
return node;
}
void
hashtable_visit (hashtable_t *ht, hashtable_visit_t *visit)
{
size_t cap = ht->cap;
hashtable_node_t **data = ht->data;
for (size_t i = 0; i < cap; i++)
for (hashtable_node_t *next, *curr = *data++; curr; curr = next)
{
next = curr->next;
visit (curr);
}
}