-
Notifications
You must be signed in to change notification settings - Fork 65
/
dict.h
75 lines (64 loc) · 1.56 KB
/
dict.h
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
#ifndef MAZUCC_DICT_H
#define MAZUCC_DICT_H
#include <stdlib.h>
#include <string.h>
#include "list.h"
typedef struct Dict {
List *list;
struct Dict *parent;
} Dict;
#define EMPTY_DICT ((Dict){&EMPTY_LIST, NULL})
typedef struct {
char *key;
void *val;
} DictEntry;
static inline void *make_dict(void *parent)
{
Dict *r = malloc(sizeof(Dict));
r->list = make_list();
r->parent = parent;
return r;
}
static inline void *dict_get(Dict *dict, char *key)
{
for (; dict; dict = dict->parent) {
for (Iter i = list_iter(dict->list); !iter_end(i);) {
DictEntry *e = iter_next(&i);
if (!strcmp(key, e->key))
return e->val;
}
}
return NULL;
}
static inline void dict_put(Dict *dict, char *key, void *val)
{
DictEntry *e = malloc(sizeof(DictEntry));
e->key = key;
e->val = val;
list_push(dict->list, e);
}
static inline List *dict_keys(Dict *dict)
{
List *r = make_list();
for (; dict; dict = dict->parent)
for (Iter i = list_iter(dict->list); !iter_end(i);)
list_push(r, ((DictEntry *) iter_next(&i))->key);
return r;
}
static inline List *dict_values(Dict *dict)
{
List *r = make_list();
for (; dict; dict = dict->parent)
for (Iter i = list_iter(dict->list); !iter_end(i);)
list_push(r, ((DictEntry *) iter_next(&i))->val);
return r;
}
static inline void *dict_parent(Dict *dict)
{
void *r = dict->parent;
list_free(dict->list);
free(dict->list);
free(dict);
return r;
}
#endif /* MAZUCC_DICT_H */