-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
57 lines (48 loc) · 1.19 KB
/
util.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
#include "./9cc.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
Vector *new_vector() {
Vector *vec = malloc(sizeof(Vector));
vec->data = malloc(sizeof(void *) * 16);
vec->capacity = 16;
vec->len = 0;
return vec;
}
void vec_push(Vector *vec, void* elem) {
if (vec->capacity == vec->len) {
vec->capacity *= 2;
vec->data = realloc(vec->data, sizeof(void *) * vec->capacity);
}
vec->data[vec->len++] = elem;
}
String *new_string() {
String *str = malloc(sizeof(String));
str->data = malloc(sizeof(void *) * 16);
str->capacity = 16;
str->len = 0;
return str;
}
void str_push(String *str, char c) {
if (str->capacity == str->len) {
str->capacity *= 2;
str->data = realloc(str->data, sizeof(char) * str->capacity);
}
str->data[str->len++] = c;
}
Map *new_map() {
Map *map = malloc(sizeof(Map));
map->keys = new_vector();
map->vals = new_vector();
return map;
}
void map_put(Map *map, char *key, void *val) {
vec_push(map->keys, key);
vec_push(map->vals, val);
}
void* map_get(Map *map, char *key) {
for (int i = map->keys->len - 1; i >= 0; i--)
if (strcmp(map->keys->data[i], key) == 0)
return map->vals->data[i];
return NULL;
}