-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.c
75 lines (52 loc) · 1.43 KB
/
container.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "container.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;
}
void vec_test() {
Vector *vec = new_vector();
expect(__LINE__, 0, vec->len);
for (int i = 0; i < 100; i++)
vec_push(vec, (void *)i);
expect(__LINE__, 100, vec->len);
expect(__LINE__, 0, (int)vec->data[0]);
expect(__LINE__, 50, (int)vec->data[50]);
expect(__LINE__, 99, (int)vec->data[99]);
printf("Vector test OK\n");
}
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;
}
void map_test() {
Map *map = new_map();
expect(__LINE__, 0, (int)map_get(map, "foo"));
map_put(map, "foo", (void *)2);
expect(__LINE__, 2, (int)map_get(map, "foo"));
map_put(map, "bar", (void *)4);
expect(__LINE__, 4, (int)map_get(map, "bar"));
map_put(map, "foo", (void *)6);
expect(__LINE__, 6, (int)map_get(map, "foo"));
}