-
Notifications
You must be signed in to change notification settings - Fork 9
/
chtbl.c
152 lines (135 loc) · 2.93 KB
/
chtbl.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
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
/**
* @filename: chtbl.c
*
* @author: QinYUN575
*
* @create date: 2019/11/16
*
*
*
*/
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "chtbl.h"
/**
* 初始化链式哈希表
*
*
*
*/
int chtbl_init(CHTbl *htbl, int buckets, int (*h)(const void *key),
int (*match)(const void *key1, const void *key2),
void (*destroy)(void *data))
{
int i = 0;
/* 为链式哈希表申请内存空间 */
if ((htbl->table = (List *)malloc(sizeof(List) * buckets)) == NULL)
{
return -1;
}
/* 初始化 Buckets */
htbl->buckets = buckets;
for (i = 0; i < buckets; i++)
{
list_init(&htbl->table[i], destroy);
}
htbl->h = h;
htbl->match = match;
htbl->destroy = destroy;
htbl->size = 0;
return 0;
}
/**
* 销毁链式哈希表
*
*/
void chtbl_destroy(CHTbl *htbl)
{
int i;
for (i = 0; i < htbl->buckets; i++)
{
list_destroy(&htbl->table[i]);
}
free(&htbl->table);
memset(htbl, 0, sizeof(CHTbl));
return;
}
/**
* 哈希操作
*
*
*/
int chtbl_insert(CHTbl *htbl, const void *data)
{
void *temp;
int bucket, retval;
/* 如果数据已存在于哈希表中,则不进行操作 */
temp = (void *)data;
if (chtbl_lookup(htbl, &temp) == 0)
{
return 1;
}
/* 哈希键 */
bucket = htbl->h(data) % htbl->buckets;
/* 插入数据到 bucket 中 */
if ((retval = list_ins_next(&htbl->table[bucket], NULL, data)) == 0)
{
htbl->size++;
}
return retval;
}
/**
* 移除哈希表中的元素
*
*/
int chtbl_remove(CHTbl *htbl, void **data)
{
ListElmt *element, *prev;
int bucket;
/* 哈希键 */
bucket = htbl->h(*data) % htbl->buckets;
/* 在 bucket 中查找数据 */
prev = NULL;
for (element = list_head(&htbl->table[bucket]); element != NULL;
element = list_next(element))
{
if (htbl->match(*data, list_data(element)))
{
/* Remove the data from the bucket. */
if (list_rem_next(&htbl->table[bucket], prev, data) == 0)
{
htbl->size--;
return 0;
}
else
{
return -1;
}
}
prev = element;
}
return -1;
}
/**
* 查找哈希表中的元素
*
*/
int chtbl_lookup(CHTbl *htbl, void **data)
{
ListElmt *element;
int bucket;
/* 哈希键 */
bucket = htbl->h(*data) % htbl->buckets;
for (element = list_head(&htbl->table[bucket]); element != NULL;
element = list_next(element))
{
if (htbl->match(*data, list_data(element)))
{
*data = list_data(element);
return 0;
}
}
/* 未找到数据 */
return -1;
}