-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.c
46 lines (35 loc) · 738 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
#include "cc.h"
Map *map_new() {
Map *map = (Map *) calloc(1, sizeof(Map));
map->count = 0;
return map;
}
int map_count(Map *map) {
return map->count;
}
bool map_put(Map *map, char *key, void *value) {
for (int i = 0; i < map->count; i++) {
if (strcmp(map->keys[i], key) == 0) {
map->values[i] = value;
return true;
}
}
if (map->count >= 1024) {
return false;
}
map->keys[map->count] = key;
map->values[map->count] = value;
map->count++;
return true;
}
void *map_lookup(Map *map, char *key) {
for (int i = 0; i < map->count; i++) {
if (strcmp(map->keys[i], key) == 0) {
return map->values[i];
}
}
return NULL;
}
void map_clear(Map *map) {
map->count = 0;
}