This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
dict.c
294 lines (228 loc) · 6.22 KB
/
dict.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
#include "object.h"
KHASH_DEFINE(dict, struct symbol *, pic_value, kh_ptr_hash_func, kh_ptr_hash_equal)
pic_value
pic_make_dict(pic_state *pic)
{
struct dict *dict;
dict = (struct dict *)pic_obj_alloc(pic, PIC_TYPE_DICT);
kh_init(dict, &dict->hash);
return obj_value(pic, dict);
}
pic_value
pic_dict_ref(pic_state *pic, pic_value dict, pic_value key)
{
khash_t(dict) *h = &dict_ptr(pic, dict)->hash;
int it;
it = kh_get(dict, h, sym_ptr(pic, key));
if (it == kh_end(h)) {
pic_error(pic, "element not found for given key", 1, key);
}
return kh_val(h, it);
}
void
pic_dict_set(pic_state *pic, pic_value dict, pic_value key, pic_value val)
{
khash_t(dict) *h = &dict_ptr(pic, dict)->hash;
int ret;
int it;
it = kh_put(dict, h, sym_ptr(pic, key), &ret);
kh_val(h, it) = val;
}
int
pic_dict_size(pic_state *PIC_UNUSED(pic), pic_value dict)
{
return kh_size(&dict_ptr(pic, dict)->hash);
}
bool
pic_dict_has(pic_state *pic, pic_value dict, pic_value key)
{
khash_t(dict) *h = &dict_ptr(pic, dict)->hash;
return kh_get(dict, h, sym_ptr(pic, key)) != kh_end(h);
}
void
pic_dict_del(pic_state *pic, pic_value dict, pic_value key)
{
khash_t(dict) *h = &dict_ptr(pic, dict)->hash;
int it;
it = kh_get(dict, h, sym_ptr(pic, key));
if (it == kh_end(h)) {
pic_error(pic, "element not found for given key", 1, key);
}
kh_del(dict, h, it);
}
bool
pic_dict_next(pic_state *PIC_UNUSED(pic), pic_value dict, int *iter, pic_value *key, pic_value *val)
{
khash_t(dict) *h = &dict_ptr(pic, dict)->hash;
int it = *iter;
for (it = *iter; it != kh_end(h); ++it) {
if (kh_exist(h, it)) {
if (key) *key = obj_value(pic, kh_key(h, it));
if (val) *val = kh_val(h, it);
*iter = ++it;
return true;
}
}
return false;
}
static pic_value
pic_dict_make_dictionary(pic_state *pic)
{
pic_get_args(pic, "");
return pic_make_dict(pic);
}
static pic_value
pic_dict_dictionary(pic_state *pic)
{
pic_value dict, *argv;
int argc, i;
pic_get_args(pic, "*", &argc, &argv);
dict = pic_make_dict(pic);
for (i = 0; i < argc; i += 2) {
TYPE_CHECK(pic, argv[i], sym);
pic_dict_set(pic, dict, argv[i], argv[i+1]);
}
return dict;
}
static pic_value
pic_dict_dictionary_p(pic_state *pic)
{
pic_value obj;
pic_get_args(pic, "o", &obj);
return pic_bool_value(pic, pic_dict_p(pic, obj));
}
static pic_value
pic_dict_dictionary_has_p(pic_state *pic)
{
pic_value dict, key;
pic_get_args(pic, "dm", &dict, &key);
return pic_bool_value(pic, pic_dict_has(pic, dict, key));
}
static pic_value
pic_dict_dictionary_ref(pic_state *pic)
{
pic_value dict, key;
pic_get_args(pic, "dm", &dict, &key);
return pic_dict_ref(pic, dict, key);
}
static pic_value
pic_dict_dictionary_set(pic_state *pic)
{
pic_value dict, key, val;
pic_get_args(pic, "dmo", &dict, &key, &val);
pic_dict_set(pic, dict, key, val);
return pic_undef_value(pic);
}
static pic_value
pic_dict_dictionary_delete(pic_state *pic)
{
pic_value dict, key;
pic_get_args(pic, "dm", &dict, &key);
pic_dict_del(pic, dict, key);
return pic_undef_value(pic);
}
static pic_value
pic_dict_dictionary_size(pic_state *pic)
{
pic_value dict;
pic_get_args(pic, "d", &dict);
return pic_int_value(pic, pic_dict_size(pic, dict));
}
static pic_value
pic_dict_dictionary_map(pic_state *pic)
{
pic_value dict, proc, key, ret = pic_nil_value(pic);
int it = 0;
size_t ai;
pic_get_args(pic, "ld", &proc, &dict);
ai = pic_enter(pic);
while (pic_dict_next(pic, dict, &it, &key, NULL)) {
pic_push(pic, pic_call(pic, proc, 1, key), ret);
pic_leave(pic, ai);
pic_protect(pic, ret);
}
return pic_reverse(pic, ret);
}
static pic_value
pic_dict_dictionary_for_each(pic_state *pic)
{
pic_value dict, proc, key;
int it = 0;
size_t ai;
pic_get_args(pic, "ld", &proc, &dict);
ai = pic_enter(pic);
while (pic_dict_next(pic, dict, &it, &key, NULL)) {
pic_call(pic, proc, 1, key);
pic_leave(pic, ai);
}
return pic_undef_value(pic);
}
static pic_value
pic_dict_dictionary_to_alist(pic_state *pic)
{
pic_value dict, key, val, alist = pic_nil_value(pic);
int it = 0;
pic_get_args(pic, "d", &dict);
while (pic_dict_next(pic, dict, &it, &key, &val)) {
pic_push(pic, pic_cons(pic, key, val), alist);
}
return alist;
}
static pic_value
pic_dict_alist_to_dictionary(pic_state *pic)
{
pic_value dict, alist, e, it;
pic_get_args(pic, "o", &alist);
dict = pic_make_dict(pic);
pic_for_each (e, pic_reverse(pic, alist), it) {
TYPE_CHECK(pic, pic_car(pic, e), sym);
pic_dict_set(pic, dict, pic_car(pic, e), pic_cdr(pic, e));
}
return dict;
}
static pic_value
pic_dict_dictionary_to_plist(pic_state *pic)
{
pic_value dict, key, val, plist = pic_nil_value(pic);
int it = 0;
pic_get_args(pic, "d", &dict);
while (pic_dict_next(pic, dict, &it, &key, &val)) {
pic_push(pic, val, plist);
pic_push(pic, key, plist);
}
return plist;
}
static pic_value
pic_dict_plist_to_dictionary(pic_state *pic)
{
pic_value dict, plist, e;
pic_get_args(pic, "o", &plist);
dict = pic_make_dict(pic);
for (e = pic_reverse(pic, plist); ! pic_nil_p(pic, e); e = pic_cddr(pic, e)) {
TYPE_CHECK(pic, pic_cadr(pic, e), sym);
pic_dict_set(pic, dict, pic_cadr(pic, e), pic_car(pic, e));
}
return dict;
}
void
pic_init_dict(pic_state *pic)
{
pic_defun(pic, "make-dictionary", pic_dict_make_dictionary);
pic_defun(pic, "dictionary?", pic_dict_dictionary_p);
pic_defun(pic, "dictionary", pic_dict_dictionary);
pic_defun(pic, "dictionary-has?", pic_dict_dictionary_has_p);
pic_defun(pic, "dictionary-ref", pic_dict_dictionary_ref);
pic_defun(pic, "dictionary-set!", pic_dict_dictionary_set);
pic_defun(pic, "dictionary-delete!", pic_dict_dictionary_delete);
pic_defun(pic, "dictionary-size", pic_dict_dictionary_size);
pic_defun(pic, "dictionary-map", pic_dict_dictionary_map);
pic_defun(pic, "dictionary-for-each", pic_dict_dictionary_for_each);
pic_defun(pic, "dictionary->alist", pic_dict_dictionary_to_alist);
pic_defun(pic, "alist->dictionary", pic_dict_alist_to_dictionary);
pic_defun(pic, "dictionary->plist", pic_dict_dictionary_to_plist);
pic_defun(pic, "plist->dictionary", pic_dict_plist_to_dictionary);
}