-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhash_tables.c
183 lines (147 loc) · 3.13 KB
/
hash_tables.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
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
/***
* @simple hash table implementation in C
*
* Author: Dev Naga <devendra.aaru@gmail.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* List elements
*/
struct list_elem {
void *spec_data;
struct list_elem *next;
};
/**
* Hash table
*/
struct hash_list {
struct list_elem *item;
};
static int table_size;
static struct hash_list *hlist;
/**
* Init the hash tables witha pre-defined table
*/
int hlist_init(int hlist_size)
{
hlist = calloc(hlist_size, sizeof(struct hash_list));
if (!hlist) {
return -1;
}
table_size = hlist_size;
return 0;
}
/**
* Hash the data with a simple addition of characters
*/
static int hlist_hash_data(char *hash_str)
{
int hash = 0;
int i;
for (i = 0; i < strlen(hash_str); i ++) {
hash += hash_str[i];
}
return hash % table_size;
}
static int hlist_add_item_new(int index, void *item)
{
hlist[index].item = calloc(1, sizeof(struct list_elem));
if (!hlist[index].item) {
return -1;
}
hlist[index].item->spec_data = item;
hlist[index].item->next = NULL;
return 0;
}
static int hlist_add_item_append(int index, void *item)
{
struct list_elem *last, *iter;
iter = hlist[index].item;
last = iter;
while (iter) {
last = iter;
iter = iter->next;
}
struct list_elem *new;
new = calloc(1, sizeof(struct list_elem));
if (!new) {
return -1;
}
new->spec_data = item;
last->next = new;
return 0;
}
/**
* Find item in the hash tables if exist
*/
int hlist_find_item(void *item, int (*compare)(void *a, void *b))
{
int i;
for (i = 0; i < table_size; i ++) {
struct list_elem *list;
int res;
for (list = hlist[i].item; list; list = list->next) {
res = compare(item, list->spec_data);
if (res) {
return 1;
}
}
}
return 0;
}
/**
* Add an item in list.. append if already hashed to same data, add new if does not
*/
int hlist_add_item(void *item, char *hash_str)
{
int index;
index = hlist_hash_data(hash_str);
if (hlist[index].item == NULL) {
hlist_add_item_new(index, item);
} else {
hlist_add_item_append(index, item);
}
return 0;
}
/**
* Print hash table elements
*/
void hlist_print(void (*data_callback)(void *data))
{
int i;
for (i = 0; i < table_size; i ++) {
struct list_elem *list;
printf("hash item: <%d>\n", i);
for (list = hlist[i].item; list; list = list->next) {
printf("\t list->spec_data <%p>\n", list->spec_data);
data_callback(list->spec_data);
}
}
}
/***
* Test program
*/
void print_items(void *data)
{
//printf("item : <%s>\n", data);
}
int compare_item(void *a, void *b)
{
char *a_1 = a;
char *a_2 = b;
return (strcmp(a_1, a_2) == 0);
}
int main(int argc, char **argv)
{
int i;
hlist_init(20);
for (i = 1; i <= argc - 1; i ++) {
if (!hlist_find_item(argv[i], compare_item)) {
hlist_add_item(argv[i], argv[i]);
}
}
hlist_print(print_items);
return 0;
}