-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.c
54 lines (46 loc) · 1.15 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
#include "map.h"
#include <stdio.h>
map_t *map_new(void) {
map_t *ret = malloc(sizeof(map_t));
if (!ret)
return NULL;
ret->pairs = list_new();
if (!ret->pairs)
return NULL;
return ret;
}
static pair_t *map_get_pair_from_key(map_t *map, string_t *key) {
pair_t *pair;
list_for_each(map->pairs, pair) {
if (string_eq(key, pair->key) == 0) {
return pair;
}
}
return NULL;
}
void *map_get(map_t *map, string_t *key) {
if (!map)
return NULL;
pair_t *pair = map_get_pair_from_key(map, key);
return pair ? pair->value : NULL;
}
int map_set(map_t *map, string_t *key, void *value) {
if (!map)
return -1;
// Overwrite the value if the key already exists in the map
pair_t *pair = map_get_pair_from_key(map, key);
if (pair) {
pair->value = value;
return 0;
}
pair = malloc(sizeof(pair_t));
if (!pair)
return -1;
pair->key = key;
pair->value = value;
list_push(map->pairs, pair);
return 0;
}
bool map_contains(map_t *map, string_t *key) {
return map_get_pair_from_key(map, key) ? true : false;
}