-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.c
124 lines (107 loc) · 3.15 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "map.h"
static int map_get_index(MapObject *map, const char *key);
static void map_bloat(MapObject *map);
Variable def = {.type=Integer, .i=0};
#define MAP_DEFAULT_SIZE 10
#define new_map() new_default_map(def)
MapObject *new_default_map(Variable def_val) {
MapObject *map = malloc(sizeof(MapObject));
*map = (MapObject){
.elements = 0,
.size = MAP_DEFAULT_SIZE,
.def_val = def_val,
.keys = malloc(sizeof(char *) * MAP_DEFAULT_SIZE),
.values = malloc(sizeof(Variable) * MAP_DEFAULT_SIZE),
.parent = NULL
};
return map;
}
Variable map_set_default(MapObject *map, Variable default_value) {
Variable original = map->def_val;
map->def_val = default_value;
return original;
}
void map_free(MapObject *map) {
free(map->keys);
for(int i = 0; i < map->elements; i++) {
if(map->values[i].type == Dictionary) {
map_free(map->values[i].p);
}
}
free(map->values);
free(map);
}
static int map_get_index(MapObject *map, const char *key) {
for(int i = 0; i < map->elements; i++) {
if(strcmp(key, map->keys[i]) == 0) {
return i;
}
}
return -1;
}
Variable *map_get_address(MapObject *map, const char *key) {
for(int i = 0; i < map->elements; i++) {
if(strcmp(key, map->keys[i]) == 0) {
return &map->values[i];
}
}
return NULL;
}
static void map_bloat(MapObject *map) {
//todo double size
fprintf(stderr, "map full, todo map.h line: %d\n", __LINE__);
exit(1);
}
Variable *map_set(MapObject *map, const char *key, Variable value) {
int i = map_get_index(map, key);
if(i == -1) {
for(MapObject *parent = map->parent; parent != NULL; parent = parent->parent) {
int i = map_get_index(parent, key);
if(i != -1) {
parent->values[i] = value;
return &parent->values[i];
}
}
int last_element_index = map->elements;
if(last_element_index == map->size) {
map_bloat(map);
}
map->keys[last_element_index] = key;
map->values[last_element_index] = value;
map->elements++;
return &map->values[last_element_index];
}
map->values[i] = value;
return &map->values[i];
}
void map_setint(MapObject *map, const char *key, int value) {
map_set(map, key, (Variable){.i = value});
}
Variable map_remove(MapObject *map, const char *key) {
int ix = map_get_index(map, key);
if(ix == -1) {
return def;
}
//possibly memory leak if value or key was allocated using malloc
int last_ix = --(map->elements);
if(ix == last_ix) {
return map->values[ix];
}
Variable original = map->values[ix];
map->keys[ix] = map->keys[last_ix];
map->values[ix] = map->values[last_ix];
return original;
}
Variable map_get(MapObject *map, const char *key) {
int i = map_get_index(map, key);
if(i == -1) {
if(map->parent) {
return map_get(map->parent, key);
}
return map->def_val;
}
return map->values[i];
}