-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.c
50 lines (44 loc) · 1.1 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
#include "boncc.h"
#include <assert.h>
#include <stdlib.h>
typedef struct KeyValue KeyValue;
struct KeyValue {
String *ident;
void *val;
};
Map *new_map() { return new_vector(0, sizeof(KeyValue *)); }
void *map_geti(Map *map, int idx) {
assert(0 <= idx && idx < map->size);
KeyValue *e = *(KeyValue **)vector_get(map, idx);
assert(e);
return e->val;
}
void *map_get(Map *map, String *key) {
int sz = map->size;
for (int i = 0; i < sz; ++i) {
KeyValue *e = *(KeyValue **)vector_get(map, i);
if (same_string(e->ident, key))
return e->val;
}
return NULL;
}
void map_push(Map *map, String *key, void *val) {
assert(map_get(map, key) == NULL);
KeyValue *e = calloc(1, sizeof(KeyValue));
e->ident = key;
e->val = val;
vector_push(map, &e);
}
void map_erase(Map *map, String *key) {
int sz = map->size;
for (int i = 0; i < sz; ++i) {
KeyValue *e = *(KeyValue **)vector_get(map, i);
if (same_string(e->ident, key)) {
KeyValue *last = *(KeyValue **)vector_last(map);
vector_set(map, i, &last);
vector_pop(map);
return;
}
}
assert(false);
}