-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.c
155 lines (110 loc) · 2.51 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
#define _GNU_SOURCE
#include <assert.h>
#include <search.h>
#include <stdlib.h>
#include <string.h>
#include "map.h"
typedef struct {
void *key;
void *value;
} map_entry_t;
static inline void *map_entry_new(void *key, void *value)
{
map_entry_t *entry = malloc(sizeof(map_entry_t));
assert(entry);
entry->key = key;
entry->value = value;
return entry;
}
static inline void map_entry_free(map_t *map, map_entry_t *entry)
{
if (map->key_destroy)
map->key_destroy(entry->key);
if (map->value_destroy)
map->value_destroy(entry->value);
free(entry);
}
static void *call_tfunc(map_t *map, void *data, void *(*func)())
{
__extension__
int compare(map_entry_t *a, map_entry_t *b)
{
return map->cmp(a->key, b->key);
}
return func(data, &map->root, compare);
}
void map_init(map_t *map, cmp_func_t cmp)
{
memset(map, 0, sizeof(*map));
map->cmp = cmp;
}
void map_init_full(map_t *map, cmp_func_t cmp, free_func_t key_destroy, free_func_t value_destroy)
{
map_init(map, cmp);
map->key_destroy = key_destroy;
map->value_destroy = value_destroy;
}
bool map_add(map_t *map, void *key, void *value)
{
bool is_new_item = true;
map_entry_t entry = { key, NULL };
void **node = call_tfunc(map, &entry, tsearch);
assert(node);
if (*node != &entry) {
map_entry_free(map, *node);
is_new_item = false;
}
*node = map_entry_new(key, value);
return is_new_item;
}
void *map_find(map_t *map, void *key)
{
void *value = NULL;
map_lookup(map, key, NULL, &value);
return value;
}
bool map_lookup(map_t *map, void *key, void **orig_key, void **orig_value)
{
map_entry_t entry = { key, NULL };
void **node = call_tfunc(map, &entry, tfind);
if (! node)
return false;
map_entry_t *real = *node;
if (orig_key)
*orig_key = real->key;
if (orig_value)
*orig_value = real->value;
return true;
}
bool map_remove(map_t *map, void *key)
{
map_entry_t entry = { key, NULL };
void **node = call_tfunc(map, &entry, tfind);
if (! node)
return false;
void *tmp = *node;
call_tfunc(map, &entry, tdelete);
map_entry_free(map, tmp);
return true;
}
void map_foreach(map_t *map, map_walker_t walk, void *data)
{
__extension__
void action(const void *nodep, const VISIT which, const int depth)
{
map_entry_t *entry = *(void **) nodep;
if (which == postorder || which == leaf)
walk(entry->key, entry->value, data);
}
twalk(map->root, action);
}
void map_clean(map_t *map)
{
__extension__
void free_node(void *node)
{
map_entry_free(map, node);
}
tdestroy(map->root, free_node);
map->root = NULL;
}