forked from cunha/dstructs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.c
244 lines (216 loc) · 5.99 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
241
242
243
#include "map.h"
#include <assert.h>
#include <inttypes.h>
#include <stdlib.h>
/* declarations **************************************************************/
static struct pavl_map_entry * map_entry_create(struct pavl_map *,
const void *, void *);
static void map_entry_destroy(void *, void *);
static int map_entry_cmp(const void *, const void *, void *);
/* public functions **********************************************************/
struct pavl_map *
map_create(map_keycmp_func *cmp, void *param, struct libavl_allocator *alloc)
{
struct pavl_map *map;
assert(cmp != NULL);
if(alloc == NULL) alloc = &pavl_allocator_default;
map = alloc->libavl_malloc(alloc, sizeof(struct pavl_map));
if(map == NULL) return NULL;
map->compare = cmp;
map->alloc = alloc;
map->param = param;
map->table = pavl_create(map_entry_cmp, map, alloc);
map->itemfunc = NULL;
return map;
}
void map_destroy(struct pavl_map *map, map_item_func *destroy)
{
map->itemfunc = destroy;
pavl_destroy(map->table, map_entry_destroy);
map->alloc->libavl_free(map->alloc, map);
}
void *map_insert(struct pavl_map *map, const void *key, void *data) {
struct pavl_map_entry *entry, **ret;
entry = map_entry_create(map, key, data);
ret = (struct pavl_map_entry **)pavl_probe(map->table, entry);
if(ret == NULL || *ret == entry) return NULL;
else {
map_entry_destroy(entry, map);
return (*ret)->data;
}
}
void *map_replace(struct pavl_map *map, void *key, void *data)
{
struct pavl_map_entry *entry, **ret;
entry = map_entry_create(map, key, data);
ret = (struct pavl_map_entry **)pavl_probe(map->table, entry);
if(ret == NULL || *ret == entry) {
return NULL;
} else {
void *old;
old = (*ret)->data;
(*ret)->data = data;
map_entry_destroy(entry, map);
return old;
}
}
void *map_delete(struct pavl_map *map, const void *key)
{
struct pavl_map_entry entry, *ret;
entry.key = key;
ret = pavl_delete(map->table, &entry);
if(ret == NULL) return NULL;
else {
void *data = ret->data;
map_entry_destroy(ret, map);
return data;
}
}
void *map_find(const struct pavl_map *map, const void *key, const void **oldk)
{
struct pavl_map_entry entry, *ret;
entry.key = key;
ret = pavl_find(map->table, &entry);
if(ret == NULL) return NULL;
else {
if(oldk) *oldk = (void *)ret->key;
return ret->data;
}
}
void map_assert_insert(struct pavl_map *map, const void *key, void *data)
{
struct pavl_map_entry *entry;
entry = map_entry_create(map, key, data);
pavl_assert_insert(map->table, entry);
}
void *map_assert_delete(struct pavl_map *map, const void *key)
{
struct pavl_map_entry entry, *ret;
entry.key = key;
ret = pavl_assert_delete(map->table, &entry);
void *data = ret->data;
map_entry_destroy(ret, map);
return data;
}
/* traverser functions *******************************************************/
void map_t_init(struct pavl_map_trav *trav, struct pavl_map *map)
{
pavl_t_init(&((*trav).trav), map->table);
}
void *
map_t_first(struct pavl_map_trav *trav, struct pavl_map *map, const void **key)
{
struct pavl_map_entry *entry;
entry = pavl_t_first(&((*trav).trav), map->table);
if(!entry) {
if(key != NULL) *key = NULL;
return NULL;
} else {
if(key != NULL) *key = entry->key;
return entry->data;
}
}
void *
map_t_last(struct pavl_map_trav *trav, struct pavl_map *map, const void **key)
{
struct pavl_map_entry *entry;
entry = pavl_t_last(&((*trav).trav), map->table);
if(!entry) {
if(key != NULL) *key = NULL;
return NULL;
} else {
if(key != NULL) *key = entry->key;
return entry->data;
}
}
void *
map_t_find(struct pavl_map_trav *trav, struct pavl_map *map, const void *key)
{
struct pavl_map_entry ekey, *entry;
ekey.key = key;
entry = pavl_t_find(&((*trav).trav), map->table, &ekey);
if(entry == NULL) return NULL;
else return entry->data;
}
void *map_t_next(struct pavl_map_trav *trav, const void **key)
{
struct pavl_map_entry *entry;
entry = pavl_t_next(&((*trav).trav));
if(!entry) {
if(key != NULL) *key = NULL;
return NULL;
} else {
if(key != NULL) *key = entry->key;
return entry->data;
}
}
void *map_t_prev(struct pavl_map_trav *trav, const void **key)
{
struct pavl_map_entry *entry;
entry = pavl_t_next(&((*trav).trav));
if(!entry) {
if(key != NULL) *key = NULL;
return NULL;
} else {
if(key != NULL) *key = entry->key;
return entry->data;
}
}
void *map_t_cur(struct pavl_map_trav *trav, const void **key)
{
struct pavl_map_entry *entry;
entry = pavl_t_cur(&((*trav).trav));
if(!entry) {
if(key != NULL) *key = NULL;
return NULL;
} else {
if(key != NULL) *key = entry->key;
return entry->data;
}
}
/* struct pavl_map_entry *****************************************************/
static struct pavl_map_entry *
map_entry_create(struct pavl_map *map, const void *key, void *data)
{
struct pavl_map_entry *entry;
entry = map->alloc->libavl_malloc(map->alloc,
sizeof(struct pavl_map_entry));
if(entry == NULL) return NULL;
entry->key = key;
entry->data = data;
return entry;
}
static int map_entry_cmp(const void *ve1, const void *ve2, void *vmap)
{
struct pavl_map *map = (struct pavl_map *)vmap;
const struct pavl_map_entry *e1 = (const struct pavl_map_entry *)ve1;
const struct pavl_map_entry *e2 = (const struct pavl_map_entry *)ve2;
return map->compare(e1->key, e2->key, map->param);
}
static void map_entry_destroy(void *ventry, void *vmap)
{
struct pavl_map *map = (struct pavl_map *)vmap;
struct pavl_map_entry *entry = (struct pavl_map_entry *)ventry;
if(map->itemfunc) {
map->itemfunc((void *)entry->key, entry->data, map->param);
}
map->alloc->libavl_free(map->alloc, entry);
}
/* map_cmp functions *********************************************************/
int map_cmp_uint32(const void *v1, const void *v2, void *dummy)
{
uint32_t *i1 = (uint32_t *)v1;
uint32_t *i2 = (uint32_t *)v2;
return (*i1 > *i2) - (*i1 < *i2);
}
int map_cmp_int(const void *v1, const void *v2, void *dummy)
{
int *i1 = (int *)v1;
int *i2 = (int *)v2;
return (*i1 > *i2) - (*i1 < *i2);
}
void map_free(void *key, void *data, void *unused)
{
if(key) free(key);
if(data) free(data);
}