blob: 170724de18829c3c5d74023dbb6725b60caf5e8c [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damiend99b0522013-12-21 18:17:45 +00002#include <string.h>
3#include <assert.h>
4
5#include "nlr.h"
6#include "misc.h"
7#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +00009#include "obj.h"
10#include "runtime0.h"
11#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +000012
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020013STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur);
14STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in);
Paul Sokolovskyea85a122014-04-06 20:08:56 +030015STATIC mp_obj_t dict_copy(mp_obj_t self_in);
John R. Lentona41fe312014-01-06 17:17:02 +000016
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020017STATIC void dict_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +000018 mp_obj_dict_t *self = self_in;
19 bool first = true;
20 print(env, "{");
John R. Lentona41fe312014-01-06 17:17:02 +000021 mp_obj_t *dict_iter = mp_obj_new_dict_iterator(self, 0);
22 mp_map_elem_t *next = NULL;
23 while ((next = dict_it_iternext_elem(dict_iter)) != NULL) {
24 if (!first) {
25 print(env, ", ");
Damiend99b0522013-12-21 18:17:45 +000026 }
John R. Lentona41fe312014-01-06 17:17:02 +000027 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020028 mp_obj_print_helper(print, env, next->key, PRINT_REPR);
John R. Lentona41fe312014-01-06 17:17:02 +000029 print(env, ": ");
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020030 mp_obj_print_helper(print, env, next->value, PRINT_REPR);
Damiend99b0522013-12-21 18:17:45 +000031 }
32 print(env, "}");
33}
34
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020035STATIC mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien Georged7aadcf2014-04-04 15:08:00 +010036 mp_obj_t dict;
37 switch (n_args) {
38 case 0:
39 dict = mp_obj_new_dict(0);
40 break;
41
42 case 1:
Paul Sokolovskyea85a122014-04-06 20:08:56 +030043 if (MP_OBJ_IS_TYPE(args[0], &mp_type_dict)) {
44 return dict_copy(args[0]);
45 }
46 // TODO create dict from an arbitrary mapping!
Damien Georged7aadcf2014-04-04 15:08:00 +010047 // TODO create dict from an iterable!
48 assert(false);
49
50 default:
Damien Georgeea13f402014-04-05 18:32:08 +010051 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "dict takes at most 1 argument"));
Damien Georged7aadcf2014-04-04 15:08:00 +010052 }
53
54 // add to the new dict any keyword args
55 for (const mp_obj_t *a = args + n_args; n_kw > 0; n_kw--, a += 2) {
56 mp_obj_dict_store(dict, a[0], a[1]);
57 }
58
59 return dict;
Damien George71c51812014-01-04 20:21:15 +000060}
61
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020062STATIC mp_obj_t dict_unary_op(int op, mp_obj_t self_in) {
Damien George4e8dc8c2014-01-27 23:15:32 +000063 mp_obj_dict_t *self = self_in;
64 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010065 case MP_UNARY_OP_BOOL: return MP_BOOL(self->map.used != 0);
66 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT((machine_int_t)self->map.used);
Damien George4e8dc8c2014-01-27 23:15:32 +000067 default: return MP_OBJ_NULL; // op not supported for None
68 }
69}
70
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020071STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damiend99b0522013-12-21 18:17:45 +000072 mp_obj_dict_t *o = lhs_in;
73 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010074 case MP_BINARY_OP_SUBSCR:
Damiend99b0522013-12-21 18:17:45 +000075 {
76 // dict load
Damien George38a2da62014-01-08 17:33:12 +000077 mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
Damiend99b0522013-12-21 18:17:45 +000078 if (elem == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +010079 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
Damiend99b0522013-12-21 18:17:45 +000080 } else {
81 return elem->value;
82 }
83 }
Damien Georged17926d2014-03-30 13:35:08 +010084 case MP_BINARY_OP_IN:
John R. Lentonc1bef212014-01-11 12:39:33 +000085 {
86 mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
Damien George9aa2a522014-02-01 23:04:09 +000087 return MP_BOOL(elem != NULL);
John R. Lentonc1bef212014-01-11 12:39:33 +000088 }
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +030089 case MP_BINARY_OP_EQUAL: {
90 // TODO: Support equality to other object types
91 if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_dict)) {
92 mp_obj_dict_t *rhs = rhs_in;
93 if (o->map.used != rhs->map.used) {
94 return mp_const_false;
95 }
96
97 machine_uint_t size = o->map.alloc;
98 mp_map_t *map = &o->map;
99
100 for (machine_uint_t i = 0; i < size; i++) {
101 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
102 mp_map_elem_t *elem = mp_map_lookup(&rhs->map, map->table[i].key, MP_MAP_LOOKUP);
103 if (elem == NULL || !mp_obj_equal(map->table[i].value, elem->value)) {
104 return mp_const_false;
105 }
106 }
107 }
108 return mp_const_true;
109 }
110 }
Damiend99b0522013-12-21 18:17:45 +0000111 default:
112 // op not supported
113 return NULL;
114 }
115}
116
Damien Georgef4c9b332014-04-08 21:32:29 +0100117STATIC bool dict_store_item(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
118 if (value == MP_OBJ_NULL) {
119 mp_obj_dict_delete(self_in, index);
120 } else {
121 mp_obj_dict_store(self_in, index, value);
122 }
123 return true;
124}
John R. Lentona41fe312014-01-06 17:17:02 +0000125
Paul Sokolovsky12a04392014-04-11 05:24:44 +0300126STATIC mp_obj_t dict_getitem(mp_obj_t lhs_in, mp_obj_t rhs_in) {
127 return dict_binary_op(MP_BINARY_OP_SUBSCR, lhs_in, rhs_in);
128}
129
130STATIC MP_DEFINE_CONST_FUN_OBJ_2(dict_getitem_obj, dict_getitem);
131
John R. Lentona41fe312014-01-06 17:17:02 +0000132/******************************************************************************/
133/* dict iterator */
134
135typedef struct _mp_obj_dict_it_t {
136 mp_obj_base_t base;
137 mp_obj_dict_t *dict;
138 machine_uint_t cur;
139} mp_obj_dict_it_t;
140
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200141STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000142 mp_obj_dict_it_t *self = self_in;
143 machine_uint_t max = self->dict->map.alloc;
Damien George8b0535e2014-04-05 21:53:54 +0100144 mp_map_t *map = &self->dict->map;
John R. Lentona41fe312014-01-06 17:17:02 +0000145
146 for (int i = self->cur; i < max; i++) {
Damien George8b0535e2014-04-05 21:53:54 +0100147 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
John R. Lentona41fe312014-01-06 17:17:02 +0000148 self->cur = i + 1;
Damien George8b0535e2014-04-05 21:53:54 +0100149 return &(map->table[i]);
John R. Lentona41fe312014-01-06 17:17:02 +0000150 }
151 }
152
153 return NULL;
154}
155
156mp_obj_t dict_it_iternext(mp_obj_t self_in) {
157 mp_map_elem_t *next = dict_it_iternext_elem(self_in);
158
159 if (next != NULL) {
160 return next->key;
161 } else {
Damien George66eaf842014-03-26 19:27:58 +0000162 return MP_OBJ_NULL;
John R. Lentona41fe312014-01-06 17:17:02 +0000163 }
164}
165
Damien George3e1a5c12014-03-29 13:43:38 +0000166STATIC const mp_obj_type_t mp_type_dict_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000167 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000168 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300169 .getiter = mp_identity,
John R. Lentona41fe312014-01-06 17:17:02 +0000170 .iternext = dict_it_iternext,
John R. Lentona41fe312014-01-06 17:17:02 +0000171};
172
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200173STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur) {
John R. Lentona41fe312014-01-06 17:17:02 +0000174 mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000175 o->base.type = &mp_type_dict_it;
John R. Lentona41fe312014-01-06 17:17:02 +0000176 o->dict = dict;
177 o->cur = cur;
178 return o;
179}
180
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200181STATIC mp_obj_t dict_getiter(mp_obj_t o_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000182 return mp_obj_new_dict_iterator(o_in, 0);
183}
184
185/******************************************************************************/
186/* dict methods */
187
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200188STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000189 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000190 mp_obj_dict_t *self = self_in;
191
192 mp_map_clear(&self->map);
193
194 return mp_const_none;
195}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200196STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
John R. Lenton7d21d512014-01-06 17:54:04 +0000197
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200198STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000199 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lentond90b19e2014-01-06 18:11:20 +0000200 mp_obj_dict_t *self = self_in;
201 mp_obj_dict_t *other = mp_obj_new_dict(self->map.alloc);
202 other->map.used = self->map.used;
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +0300203 other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
204 other->map.table_is_fixed_array = 0;
John R. Lentond90b19e2014-01-06 18:11:20 +0000205 memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
206 return other;
207}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200208STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000209
Damien Georgeeae16442014-01-11 19:22:29 +0000210// this is a classmethod
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200211STATIC mp_obj_t dict_fromkeys(uint n_args, const mp_obj_t *args) {
Damien Georgeeae16442014-01-11 19:22:29 +0000212 assert(2 <= n_args && n_args <= 3);
Damien Georged17926d2014-03-30 13:35:08 +0100213 mp_obj_t iter = mp_getiter(args[1]);
Damien Georgeeae16442014-01-11 19:22:29 +0000214 mp_obj_t len = mp_obj_len_maybe(iter);
215 mp_obj_t value = mp_const_none;
216 mp_obj_t next = NULL;
217 mp_obj_dict_t *self = NULL;
218
219 if (n_args > 2) {
220 value = args[2];
221 }
222
223 if (len == MP_OBJ_NULL) {
224 /* object's type doesn't have a __len__ slot */
225 self = mp_obj_new_dict(0);
226 } else {
227 self = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
228 }
229
Damien Georged17926d2014-03-30 13:35:08 +0100230 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
Damien Georgeeae16442014-01-11 19:22:29 +0000231 mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
232 }
233
234 return self;
235}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200236STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
237STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, (const mp_obj_t)&dict_fromkeys_fun_obj);
Damien Georgeeae16442014-01-11 19:22:29 +0000238
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200239STATIC mp_obj_t dict_get_helper(mp_map_t *self, mp_obj_t key, mp_obj_t deflt, mp_map_lookup_kind_t lookup_kind) {
Damien George38a2da62014-01-08 17:33:12 +0000240 mp_map_elem_t *elem = mp_map_lookup(self, key, lookup_kind);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000241 mp_obj_t value;
242 if (elem == NULL || elem->value == NULL) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000243 if (deflt == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000244 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien Georgeea13f402014-04-05 18:32:08 +0100245 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000246 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000247 value = mp_const_none;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000248 }
249 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000250 value = deflt;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000251 }
252 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000253 value = elem->value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000254 }
Damien George38a2da62014-01-08 17:33:12 +0000255 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000256 elem->value = value;
257 }
258 return value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000259}
260
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200261STATIC mp_obj_t dict_get(uint n_args, const mp_obj_t *args) {
John R. Lentoncd088732014-01-06 18:53:16 +0000262 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000263 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lentoncd088732014-01-06 18:53:16 +0000264
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000265 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
266 args[1],
267 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000268 MP_MAP_LOOKUP);
John R. Lentoncd088732014-01-06 18:53:16 +0000269}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200270STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
John R. Lentoncd088732014-01-06 18:53:16 +0000271
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200272STATIC mp_obj_t dict_pop(uint n_args, const mp_obj_t *args) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000273 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000274 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000275
276 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
277 args[1],
278 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000279 MP_MAP_LOOKUP_REMOVE_IF_FOUND);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000280}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200281STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000282
283
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200284STATIC mp_obj_t dict_setdefault(uint n_args, const mp_obj_t *args) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000285 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000286 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000287
288 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
289 args[1],
290 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000291 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000292}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200293STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000294
John R. Lentonf77dce82014-01-06 20:08:52 +0000295
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200296STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000297 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lentonf77dce82014-01-06 20:08:52 +0000298 mp_obj_dict_t *self = self_in;
299 if (self->map.used == 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100300 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "popitem(): dictionary is empty"));
John R. Lentonf77dce82014-01-06 20:08:52 +0000301 }
302 mp_obj_dict_it_t *iter = mp_obj_new_dict_iterator(self, 0);
303
304 mp_map_elem_t *next = dict_it_iternext_elem(iter);
305 self->map.used--;
306 mp_obj_t items[] = {next->key, next->value};
307 next->key = NULL;
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000308 next->value = NULL;
John R. Lentonf77dce82014-01-06 20:08:52 +0000309 mp_obj_t tuple = mp_obj_new_tuple(2, items);
310
311 return tuple;
312}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200313STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
John R. Lentonf77dce82014-01-06 20:08:52 +0000314
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200315STATIC mp_obj_t dict_update(mp_obj_t self_in, mp_obj_t iterable) {
Damien George3e1a5c12014-03-29 13:43:38 +0000316 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton88f30432014-01-06 22:58:17 +0000317 mp_obj_dict_t *self = self_in;
318 /* TODO: check for the "keys" method */
Damien Georged17926d2014-03-30 13:35:08 +0100319 mp_obj_t iter = mp_getiter(iterable);
John R. Lenton88f30432014-01-06 22:58:17 +0000320 mp_obj_t next = NULL;
Damien Georged17926d2014-03-30 13:35:08 +0100321 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
322 mp_obj_t inneriter = mp_getiter(next);
323 mp_obj_t key = mp_iternext(inneriter);
324 mp_obj_t value = mp_iternext(inneriter);
325 mp_obj_t stop = mp_iternext(inneriter);
Damien George66eaf842014-03-26 19:27:58 +0000326 if (key == MP_OBJ_NULL
327 || value == MP_OBJ_NULL
328 || stop != MP_OBJ_NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100329 nlr_raise(mp_obj_new_exception_msg(
Damien Georgec5966122014-02-15 16:10:44 +0000330 &mp_type_ValueError,
John R. Lenton88f30432014-01-06 22:58:17 +0000331 "dictionary update sequence has the wrong length"));
332 } else {
Damien George38a2da62014-01-08 17:33:12 +0000333 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
John R. Lenton88f30432014-01-06 22:58:17 +0000334 }
335 }
336
337 return mp_const_none;
338}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200339STATIC MP_DEFINE_CONST_FUN_OBJ_2(dict_update_obj, dict_update);
John R. Lenton88f30432014-01-06 22:58:17 +0000340
John R. Lentonf77dce82014-01-06 20:08:52 +0000341
John R. Lentona41fe312014-01-06 17:17:02 +0000342/******************************************************************************/
John R. Lenton9ec3a872014-01-10 01:00:20 +0000343/* dict views */
344
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200345STATIC const mp_obj_type_t dict_view_type;
346STATIC const mp_obj_type_t dict_view_it_type;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000347
348typedef enum _mp_dict_view_kind_t {
349 MP_DICT_VIEW_ITEMS,
350 MP_DICT_VIEW_KEYS,
351 MP_DICT_VIEW_VALUES,
352} mp_dict_view_kind_t;
353
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200354STATIC char *mp_dict_view_names[] = {"dict_items", "dict_keys", "dict_values"};
John R. Lenton9ec3a872014-01-10 01:00:20 +0000355
356typedef struct _mp_obj_dict_view_it_t {
357 mp_obj_base_t base;
358 mp_dict_view_kind_t kind;
359 mp_obj_dict_it_t *iter;
360 machine_uint_t cur;
361} mp_obj_dict_view_it_t;
362
363typedef struct _mp_obj_dict_view_t {
364 mp_obj_base_t base;
365 mp_obj_dict_t *dict;
366 mp_dict_view_kind_t kind;
367} mp_obj_dict_view_t;
368
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200369STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000370 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_it_type));
371 mp_obj_dict_view_it_t *self = self_in;
372 mp_map_elem_t *next = dict_it_iternext_elem(self->iter);
373
374 if (next != NULL) {
375 switch (self->kind) {
Damien Georgeeae16442014-01-11 19:22:29 +0000376 case MP_DICT_VIEW_ITEMS:
377 {
378 mp_obj_t items[] = {next->key, next->value};
379 return mp_obj_new_tuple(2, items);
380 }
381 case MP_DICT_VIEW_KEYS:
382 return next->key;
383 case MP_DICT_VIEW_VALUES:
384 return next->value;
385 default:
386 assert(0); /* can't happen */
387 return mp_const_none;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000388 }
389 } else {
Damien George66eaf842014-03-26 19:27:58 +0000390 return MP_OBJ_NULL;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000391 }
392}
393
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200394STATIC const mp_obj_type_t dict_view_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000395 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000396 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300397 .getiter = mp_identity,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000398 .iternext = dict_view_it_iternext,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000399};
400
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200401STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000402 assert(MP_OBJ_IS_TYPE(view_in, &dict_view_type));
403 mp_obj_dict_view_t *view = view_in;
404 mp_obj_dict_view_it_t *o = m_new_obj(mp_obj_dict_view_it_t);
405 o->base.type = &dict_view_it_type;
406 o->kind = view->kind;
407 o->iter = mp_obj_new_dict_iterator(view->dict, 0);
408 return o;
409}
410
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200411STATIC void dict_view_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000412 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
413 mp_obj_dict_view_t *self = self_in;
414 bool first = true;
415 print(env, mp_dict_view_names[self->kind]);
416 print(env, "([");
417 mp_obj_t *self_iter = dict_view_getiter(self);
418 mp_obj_t *next = NULL;
Damien George66eaf842014-03-26 19:27:58 +0000419 while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_NULL) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000420 if (!first) {
421 print(env, ", ");
422 }
423 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200424 mp_obj_print_helper(print, env, next, PRINT_REPR);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000425 }
426 print(env, "])");
427}
428
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200429STATIC mp_obj_t dict_view_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
John R. Lentonc1bef212014-01-11 12:39:33 +0000430 /* only supported for the 'keys' kind until sets and dicts are refactored */
431 mp_obj_dict_view_t *o = lhs_in;
432 if (o->kind != MP_DICT_VIEW_KEYS) return NULL;
Damien Georged17926d2014-03-30 13:35:08 +0100433 if (op != MP_BINARY_OP_IN) return NULL;
John R. Lentonc1bef212014-01-11 12:39:33 +0000434 return dict_binary_op(op, o->dict, rhs_in);
435}
436
437
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200438STATIC const mp_obj_type_t dict_view_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000439 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000440 .name = MP_QSTR_dict_view,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000441 .print = dict_view_print,
John R. Lentonc1bef212014-01-11 12:39:33 +0000442 .binary_op = dict_view_binary_op,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000443 .getiter = dict_view_getiter,
444};
445
446mp_obj_t mp_obj_new_dict_view(mp_obj_dict_t *dict, mp_dict_view_kind_t kind) {
447 mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t);
448 o->base.type = &dict_view_type;
449 o->dict = dict;
450 o->kind = kind;
451 return o;
452}
453
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200454STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
Damien George3e1a5c12014-03-29 13:43:38 +0000455 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton9ec3a872014-01-10 01:00:20 +0000456 mp_obj_dict_t *self = self_in;
457 return mp_obj_new_dict_view(self, kind);
458}
459
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200460STATIC mp_obj_t dict_items(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000461 return dict_view(self_in, MP_DICT_VIEW_ITEMS);
462}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200463STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_items_obj, dict_items);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000464
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200465STATIC mp_obj_t dict_keys(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000466 return dict_view(self_in, MP_DICT_VIEW_KEYS);
467}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200468STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_keys_obj, dict_keys);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000469
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200470STATIC mp_obj_t dict_values(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000471 return dict_view(self_in, MP_DICT_VIEW_VALUES);
472}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200473STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000474
John R. Lenton4bee76e2014-01-10 11:25:03 +0000475/******************************************************************************/
Damien Georgeeae16442014-01-11 19:22:29 +0000476/* dict constructors & public C API */
John R. Lentona41fe312014-01-06 17:17:02 +0000477
Damien George9b196cd2014-03-26 21:47:19 +0000478STATIC const mp_map_elem_t dict_locals_dict_table[] = {
479 { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&dict_clear_obj },
480 { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&dict_copy_obj },
481 { MP_OBJ_NEW_QSTR(MP_QSTR_fromkeys), (mp_obj_t)&dict_fromkeys_obj },
482 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&dict_get_obj },
483 { MP_OBJ_NEW_QSTR(MP_QSTR_items), (mp_obj_t)&dict_items_obj },
484 { MP_OBJ_NEW_QSTR(MP_QSTR_keys), (mp_obj_t)&dict_keys_obj },
485 { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&dict_pop_obj },
486 { MP_OBJ_NEW_QSTR(MP_QSTR_popitem), (mp_obj_t)&dict_popitem_obj },
487 { MP_OBJ_NEW_QSTR(MP_QSTR_setdefault), (mp_obj_t)&dict_setdefault_obj },
488 { MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&dict_update_obj },
489 { MP_OBJ_NEW_QSTR(MP_QSTR_values), (mp_obj_t)&dict_values_obj },
Paul Sokolovsky12a04392014-04-11 05:24:44 +0300490 { MP_OBJ_NEW_QSTR(MP_QSTR___getitem__), (mp_obj_t)&dict_getitem_obj },
John R. Lentonbaa66542014-01-07 23:18:25 +0000491};
492
Damien George9b196cd2014-03-26 21:47:19 +0000493STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);
494
Damien George3e1a5c12014-03-29 13:43:38 +0000495const mp_obj_type_t mp_type_dict = {
Damien Georgec5966122014-02-15 16:10:44 +0000496 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000497 .name = MP_QSTR_dict,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200498 .print = dict_print,
499 .make_new = dict_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000500 .unary_op = dict_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200501 .binary_op = dict_binary_op,
Damien Georgef4c9b332014-04-08 21:32:29 +0100502 .store_item = dict_store_item,
John R. Lentona41fe312014-01-06 17:17:02 +0000503 .getiter = dict_getiter,
Damien George9b196cd2014-03-26 21:47:19 +0000504 .locals_dict = (mp_obj_t)&dict_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000505};
506
Damien George8b0535e2014-04-05 21:53:54 +0100507void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args) {
508 dict->base.type = &mp_type_dict;
509 mp_map_init(&dict->map, n_args);
510}
511
Damiend99b0522013-12-21 18:17:45 +0000512mp_obj_t mp_obj_new_dict(int n_args) {
513 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
Damien George8b0535e2014-04-05 21:53:54 +0100514 mp_obj_dict_init(o, n_args);
Damiend99b0522013-12-21 18:17:45 +0000515 return o;
516}
Damiendae7eb72013-12-29 22:32:51 +0000517
518uint mp_obj_dict_len(mp_obj_t self_in) {
John R. Lentoncd088732014-01-06 18:53:16 +0000519 return ((mp_obj_dict_t *)self_in)->map.used;
Damiendae7eb72013-12-29 22:32:51 +0000520}
521
522mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
Damien George3e1a5c12014-03-29 13:43:38 +0000523 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
Damiendae7eb72013-12-29 22:32:51 +0000524 mp_obj_dict_t *self = self_in;
Damien George38a2da62014-01-08 17:33:12 +0000525 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
Damiendae7eb72013-12-29 22:32:51 +0000526 return self_in;
527}
Damien George062478e2014-01-09 20:57:50 +0000528
Damien George66edc5d2014-04-05 13:25:13 +0100529mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key) {
530 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
531 mp_obj_dict_t *self = self_in;
532 dict_get_helper(&self->map, key, NULL, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
533 return self_in;
534}
535
Damien George062478e2014-01-09 20:57:50 +0000536mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000537 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
Damien George062478e2014-01-09 20:57:50 +0000538 mp_obj_dict_t *self = self_in;
539 return &self->map;
540}