-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.c
56 lines (47 loc) · 992 Bytes
/
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
#include "map.h"
#include "std.h"
struct Map2 {
int _length;
int _alloc;
struct _mapchip *_vec;
};
struct _mapchip {
const char *ptr;
void *value;
};
/* it overrides; it does not overwrite. */
void insert(struct Map2 *map, const char *key, void *value)
{
struct _mapchip a;
a.ptr = key;
a.value = value;
if (map->_alloc < map->_length + 1) {
map->_vec =
realloc(map->_vec, map->_alloc * 2 * sizeof(struct _mapchip));
map->_alloc *= 2;
}
map->_vec[map->_length] = a;
++(map->_length);
}
void *lookup(const struct Map2 *m, const char *key)
{
for (int i = (m->_length) - 1; i >= 0; --i) {
if (strcmp(m->_vec[i].ptr, key) == 0) {
return m->_vec[i].value;
}
}
return 0;
}
int isElem(const struct Map2 *m, const char *key)
{
return lookup(m, key) != 0;
}
struct Map2 *init_map(void)
{
struct Map2 *res;
res = calloc(1, sizeof(struct Map2));
res->_length = 0;
res->_alloc = 256;
res->_vec = calloc(res->_alloc, sizeof(struct _mapchip));
return res;
}