-
Notifications
You must be signed in to change notification settings - Fork 54
/
map.c
240 lines (229 loc) · 5.78 KB
/
map.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "map.h"
//elf hash
unsigned int hash(char *data, int len)
{
unsigned int hash = 0;
unsigned int x = 0;
unsigned int i = 0;
char *str = data;
for(i = 0; i < len; ++str, ++i)
{
hash = (hash << 4) + (*data);
if((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
}
hash &= ~x;
}
return hash;
}
double get_conflict_ratio(map *map)
{
assert(map != NULL);
if(map->puts == 0) return 0.0;
return (1.0 * map->conflict) / map->puts;
}
void map_init(map *map)
{
assert(map != NULL);
map->table = (map_entry**)malloc(DEFAULT_TABLE_SIZE * sizeof(map_entry*));
assert(map->table != NULL);
memset(map->table, 0, DEFAULT_TABLE_SIZE * sizeof(map_entry*));
map->hash = hash;//default elf hash.
map->table_size = DEFAULT_TABLE_SIZE;
map->__map_size = 0;
map->conflict = 0;
map->puts = 0;
}
size_t map_size(map *map)
{
assert(map != NULL);
return map->__map_size;
}
void map_init_custom(map *map, size_t spar, unsigned int (*hashfunc)(char*,int))
{
assert(map != NULL);
if(spar < DEFAULT_TABLE_SIZE)
{
spar = DEFAULT_TABLE_SIZE;
}
map->table = (map_entry**)malloc(spar * sizeof(map_entry*));
assert(map->table != NULL);
memset(map->table, 0, spar * sizeof(map_entry*));
if(hashfunc == NULL)
{
hashfunc = hash;
}
map->hash = hashfunc;//default elf hash.
map->table_size = spar;
map->__map_size = 0;
map->conflict = 0;
map->puts = 0;
}
int map_put(map *map, char *key, int key_size, void *value)
{
assert(map != NULL && key != NULL);
if(key_size < 0 || value == NULL)
{
return OP_MAP_FAIL;
}
map_entry *entry = (map_entry*)value;
entry->next = NULL;//fix seg fault.
entry->hashcode = map->hash(key, key_size);
unsigned int pos = entry->hashcode % map->table_size;
if(map->table[pos] == NULL)//no conflict,directly insert.
{
entry->key = key;
entry->key_size = key_size;
(map->table)[pos] = entry;
}
else
{
++(map->conflict);
map_entry *temp = NULL;
map_entry *found = NULL;
for(temp = (map->table)[pos]; temp != NULL; temp = temp->next)
{
found = temp;
if(temp->hashcode == entry->hashcode && temp->key_size == key_size)
{
int is_same = memcmp(temp->key, key, key_size);
if(is_same == 0)//key already exist.
{
fprintf(stderr,"key: %s already exist!\n", key);
return OP_MAP_FAIL;
}
}
}
entry->key = key;
entry->key_size = key_size;
found->next = entry;//insert at tail.
entry->next = NULL;
}
++(map->__map_size);
++(map->puts);
return OP_MAP_SUCCESS;
}
map_entry *map_get(map *map, char *key, int key_size)
{
assert(map != NULL && key != NULL);
if(key_size < 0)
{
return NULL;
}
map_entry *found = NULL;
unsigned int hashcode = map->hash(key, key_size);
unsigned int pos = hashcode % map->table_size;
map_entry *temp = NULL;
for(temp = (map->table)[pos]; temp != NULL; temp = temp->next)
{
if(temp->hashcode == hashcode && temp->key_size == key_size)
{
int is_same = memcmp(temp->key, key, key_size);
if(is_same == 0)
{
found = temp;
break;
}
}
}
return found;
}
void map_free_deep(map *map)
{
assert(map != NULL);
int i; map_entry *temp = NULL;
for(i = 0; i < map->table_size; ++i)
{
for(temp = map->table[i]; temp != NULL;)
{
map_entry *next = temp->next;
free(temp);
temp = next;
}
}
free(map->table);
map->table = NULL;
map->__map_size = 0;
map->conflict = 0;
map->puts = 0;
}
void map_clear(map *map)
{
assert(map != NULL);
int i; map_entry *temp = NULL;
for(i = 0; i < map->table_size; ++i)
{
for(temp = map->table[i]; temp != NULL;)
{
map_entry *next = temp->next;
temp->next = NULL;//rm ref.
temp = next;
}
map->table[i] = NULL;
}
map->__map_size = 0;
map->conflict = 0;
map->puts = 0;
}
map_entry *map_remove(map *map, char *key, int key_size)
{
assert(map != NULL && key != NULL);
if(key_size < 0 || map->__map_size == 0)
{
return NULL;
}
map_entry *found = NULL;
unsigned int hashcode = map->hash(key, key_size);
unsigned int pos = hashcode % map->table_size;
map_entry *temp = NULL;
map_entry *prev = NULL;
for(temp = (map->table)[pos]; temp != NULL; temp = temp->next)
{
if(temp->hashcode == hashcode && temp->key_size == key_size)
{
int is_same = memcmp(temp->key, key, key_size);
if(is_same == 0)
{
found = temp;
break;
}
}
prev = temp;
}
if(found != NULL)
{
if(prev == NULL)//found is head.
{
map_entry *p = found->next;
found->next = NULL;
map->table[pos] = p;
}
else
{
prev->next = found->next;
found->next = NULL;
}
--(map->__map_size);
}
return found;
}
void map_free_shallow(map *map)
{
assert(map != NULL);
int i; map_entry *temp = NULL;
for(i = 0; i < map->table_size; ++i)
{
for(temp = map->table[i]; temp != NULL;)
{
map_entry *next = temp->next;
temp->next = NULL;//rm ref.
temp = next;
}
}
free(map->table);
map->table = NULL;
map->__map_size = 0;
map->conflict = 0;
map->puts = 0;
}