-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.c
218 lines (167 loc) · 4.28 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
//
// map.c
//
// Copyright (c) 2014 Joao Cordeiro
// MIT licensed
#ifdef __APPLE__
#include <stdlib.h>
#include <strings.h>
#else
#include <malloc.h>
#endif
#include <stdio.h>
#include "map.h"
// Utility functions
/**
* \brief Create a node.
* \param key Entry key.
* \param value Entry value.
* \return A new node object. In case of error, returns NULL.
*/
static struct map_node_t* new_map_node(const char* key, void* value)
{
struct map_node_t* new_node = NULL;
new_node = (struct map_node_t*)malloc(sizeof(struct map_node_t));
if (new_node) {
new_node->next = NULL;
new_node->value = value;
new_node->key = (char*)malloc(strlen(key) + 1);
if (!new_node->key) {
free(new_node);
return NULL;
}
strcpy(new_node->key, key);
}
return new_node;
}
struct map_find_results_t {
struct map_node_t* prev_node;
struct map_node_t* node;
unsigned short exact_match;
};
/**
* \brief Find a node with a given key.
* \param map Map to search for the key in.
* \param key Key to search for.
* \return Pointer to node if found. NULL otherwise.
*/
static struct map_find_results_t map_find(struct map_t* map, const char* key)
{
struct map_find_results_t results;
results.prev_node = NULL;
results.node = NULL;
results.exact_match = 0;
struct map_node_t* node;
for (node = map->head; node != NULL; node = node->next) {
int cmp = (*map->cmp_func)(key, node->key);
if (cmp <= 0) {
results.node = node;
results.exact_match = (cmp == 0);
return results;
}
results.prev_node = node;
}
return results;
}
// Lib functions
struct map_t* new_map()
{
struct map_t* map = NULL;
map = (struct map_t*)malloc(sizeof(struct map_t));
if (map) {
map->head = NULL;
map->size = 0;
map->cmp_func = strcmp;
map->free_func = NULL;
}
return map;
}
void map_set_free_func(struct map_t* map, void (*f)(void*))
{
map->free_func = f;
}
void map_set_cmp_func(struct map_t* map, int (*f)(const char*, const char*))
{
map->cmp_func = f;
}
void destroy_map(struct map_t** map)
{
struct map_node_t* node;
struct map_node_t* next_node;
for (node = (*map)->head; node != NULL; node = next_node) {
free(node->key);
if ((*map)->free_func && node->value) {
(*(*map)->free_func)(node->value);
}
next_node = node->next;
free(node);
node = NULL;
}
free(*map);
*map = NULL;
}
void* map_get(struct map_t* map, const char* key)
{
const struct map_find_results_t find_result = map_find(map, key);
if (find_result.exact_match) {
return find_result.node->value;
}
return NULL;
}
int map_set(struct map_t* map, const char* key, void* value)
{
const struct map_find_results_t find_result = map_find(map, key);
// found node with same key
if (find_result.exact_match) {
if (map->free_func && find_result.node->value) {
(*map->free_func)(find_result.node->value);
}
find_result.node->value = value;
return 0;
}
// create the new node.
struct map_node_t* new_node = new_map_node(key, value);
if (!new_node) {
return -1;
}
// empty list
if (map->head == NULL) {
map->head = new_node;
map->size = 1;
return 0;
}
// put the new node in the proper place
struct map_node_t* node = find_result.node;
struct map_node_t* prev_node = find_result.prev_node;
new_node->next = node;
if (prev_node) {
prev_node->next = new_node;
} else {
map->head = new_node;
}
map->size++;
return 0;
}
void map_del(struct map_t* map, const char* key)
{
struct map_find_results_t find_results = map_find(map, key);
if (!find_results.exact_match) {
return;
}
struct map_node_t* node = find_results.node;
if (map->free_func && node->value) {
(*map->free_func)(node->value);
}
if (node == map->head) {
map->head = map->head->next;
} else {
find_results.prev_node->next = node->next;
}
free(node->key);
free(node);
map->size--;
}
size_t map_size(const struct map_t* map)
{
return map->size;
}