blob: 3855c36e6a5b3372e1c4d188ef1c69bdbf4f82fd [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 }
Damiend99b0522013-12-21 18:17:45 +000089 default:
90 // op not supported
91 return NULL;
92 }
93}
94
John R. Lentona41fe312014-01-06 17:17:02 +000095
96/******************************************************************************/
97/* dict iterator */
98
99typedef struct _mp_obj_dict_it_t {
100 mp_obj_base_t base;
101 mp_obj_dict_t *dict;
102 machine_uint_t cur;
103} mp_obj_dict_it_t;
104
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200105STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000106 mp_obj_dict_it_t *self = self_in;
107 machine_uint_t max = self->dict->map.alloc;
Damien George8b0535e2014-04-05 21:53:54 +0100108 mp_map_t *map = &self->dict->map;
John R. Lentona41fe312014-01-06 17:17:02 +0000109
110 for (int i = self->cur; i < max; i++) {
Damien George8b0535e2014-04-05 21:53:54 +0100111 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
John R. Lentona41fe312014-01-06 17:17:02 +0000112 self->cur = i + 1;
Damien George8b0535e2014-04-05 21:53:54 +0100113 return &(map->table[i]);
John R. Lentona41fe312014-01-06 17:17:02 +0000114 }
115 }
116
117 return NULL;
118}
119
120mp_obj_t dict_it_iternext(mp_obj_t self_in) {
121 mp_map_elem_t *next = dict_it_iternext_elem(self_in);
122
123 if (next != NULL) {
124 return next->key;
125 } else {
Damien George66eaf842014-03-26 19:27:58 +0000126 return MP_OBJ_NULL;
John R. Lentona41fe312014-01-06 17:17:02 +0000127 }
128}
129
Damien George3e1a5c12014-03-29 13:43:38 +0000130STATIC const mp_obj_type_t mp_type_dict_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000131 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000132 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300133 .getiter = mp_identity,
John R. Lentona41fe312014-01-06 17:17:02 +0000134 .iternext = dict_it_iternext,
John R. Lentona41fe312014-01-06 17:17:02 +0000135};
136
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200137STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur) {
John R. Lentona41fe312014-01-06 17:17:02 +0000138 mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000139 o->base.type = &mp_type_dict_it;
John R. Lentona41fe312014-01-06 17:17:02 +0000140 o->dict = dict;
141 o->cur = cur;
142 return o;
143}
144
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200145STATIC mp_obj_t dict_getiter(mp_obj_t o_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000146 return mp_obj_new_dict_iterator(o_in, 0);
147}
148
149/******************************************************************************/
150/* dict methods */
151
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200152STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000153 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000154 mp_obj_dict_t *self = self_in;
155
156 mp_map_clear(&self->map);
157
158 return mp_const_none;
159}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200160STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
John R. Lenton7d21d512014-01-06 17:54:04 +0000161
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200162STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000163 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lentond90b19e2014-01-06 18:11:20 +0000164 mp_obj_dict_t *self = self_in;
165 mp_obj_dict_t *other = mp_obj_new_dict(self->map.alloc);
166 other->map.used = self->map.used;
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +0300167 other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
168 other->map.table_is_fixed_array = 0;
John R. Lentond90b19e2014-01-06 18:11:20 +0000169 memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
170 return other;
171}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200172STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000173
Damien Georgeeae16442014-01-11 19:22:29 +0000174// this is a classmethod
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200175STATIC mp_obj_t dict_fromkeys(uint n_args, const mp_obj_t *args) {
Damien Georgeeae16442014-01-11 19:22:29 +0000176 assert(2 <= n_args && n_args <= 3);
Damien Georged17926d2014-03-30 13:35:08 +0100177 mp_obj_t iter = mp_getiter(args[1]);
Damien Georgeeae16442014-01-11 19:22:29 +0000178 mp_obj_t len = mp_obj_len_maybe(iter);
179 mp_obj_t value = mp_const_none;
180 mp_obj_t next = NULL;
181 mp_obj_dict_t *self = NULL;
182
183 if (n_args > 2) {
184 value = args[2];
185 }
186
187 if (len == MP_OBJ_NULL) {
188 /* object's type doesn't have a __len__ slot */
189 self = mp_obj_new_dict(0);
190 } else {
191 self = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
192 }
193
Damien Georged17926d2014-03-30 13:35:08 +0100194 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
Damien Georgeeae16442014-01-11 19:22:29 +0000195 mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
196 }
197
198 return self;
199}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200200STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
201STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, (const mp_obj_t)&dict_fromkeys_fun_obj);
Damien Georgeeae16442014-01-11 19:22:29 +0000202
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200203STATIC 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 +0000204 mp_map_elem_t *elem = mp_map_lookup(self, key, lookup_kind);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000205 mp_obj_t value;
206 if (elem == NULL || elem->value == NULL) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000207 if (deflt == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000208 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien Georgeea13f402014-04-05 18:32:08 +0100209 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000210 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000211 value = mp_const_none;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000212 }
213 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000214 value = deflt;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000215 }
216 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000217 value = elem->value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000218 }
Damien George38a2da62014-01-08 17:33:12 +0000219 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000220 elem->value = value;
221 }
222 return value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000223}
224
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200225STATIC mp_obj_t dict_get(uint n_args, const mp_obj_t *args) {
John R. Lentoncd088732014-01-06 18:53:16 +0000226 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000227 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lentoncd088732014-01-06 18:53:16 +0000228
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000229 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
230 args[1],
231 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000232 MP_MAP_LOOKUP);
John R. Lentoncd088732014-01-06 18:53:16 +0000233}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200234STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
John R. Lentoncd088732014-01-06 18:53:16 +0000235
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200236STATIC mp_obj_t dict_pop(uint n_args, const mp_obj_t *args) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000237 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000238 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000239
240 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
241 args[1],
242 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000243 MP_MAP_LOOKUP_REMOVE_IF_FOUND);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000244}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200245STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000246
247
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200248STATIC mp_obj_t dict_setdefault(uint n_args, const mp_obj_t *args) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000249 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000250 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000251
252 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
253 args[1],
254 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000255 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000256}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200257STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000258
John R. Lentonf77dce82014-01-06 20:08:52 +0000259
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200260STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000261 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lentonf77dce82014-01-06 20:08:52 +0000262 mp_obj_dict_t *self = self_in;
263 if (self->map.used == 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100264 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "popitem(): dictionary is empty"));
John R. Lentonf77dce82014-01-06 20:08:52 +0000265 }
266 mp_obj_dict_it_t *iter = mp_obj_new_dict_iterator(self, 0);
267
268 mp_map_elem_t *next = dict_it_iternext_elem(iter);
269 self->map.used--;
270 mp_obj_t items[] = {next->key, next->value};
271 next->key = NULL;
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000272 next->value = NULL;
John R. Lentonf77dce82014-01-06 20:08:52 +0000273 mp_obj_t tuple = mp_obj_new_tuple(2, items);
274
275 return tuple;
276}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200277STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
John R. Lentonf77dce82014-01-06 20:08:52 +0000278
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200279STATIC mp_obj_t dict_update(mp_obj_t self_in, mp_obj_t iterable) {
Damien George3e1a5c12014-03-29 13:43:38 +0000280 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton88f30432014-01-06 22:58:17 +0000281 mp_obj_dict_t *self = self_in;
282 /* TODO: check for the "keys" method */
Damien Georged17926d2014-03-30 13:35:08 +0100283 mp_obj_t iter = mp_getiter(iterable);
John R. Lenton88f30432014-01-06 22:58:17 +0000284 mp_obj_t next = NULL;
Damien Georged17926d2014-03-30 13:35:08 +0100285 while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
286 mp_obj_t inneriter = mp_getiter(next);
287 mp_obj_t key = mp_iternext(inneriter);
288 mp_obj_t value = mp_iternext(inneriter);
289 mp_obj_t stop = mp_iternext(inneriter);
Damien George66eaf842014-03-26 19:27:58 +0000290 if (key == MP_OBJ_NULL
291 || value == MP_OBJ_NULL
292 || stop != MP_OBJ_NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100293 nlr_raise(mp_obj_new_exception_msg(
Damien Georgec5966122014-02-15 16:10:44 +0000294 &mp_type_ValueError,
John R. Lenton88f30432014-01-06 22:58:17 +0000295 "dictionary update sequence has the wrong length"));
296 } else {
Damien George38a2da62014-01-08 17:33:12 +0000297 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
John R. Lenton88f30432014-01-06 22:58:17 +0000298 }
299 }
300
301 return mp_const_none;
302}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200303STATIC MP_DEFINE_CONST_FUN_OBJ_2(dict_update_obj, dict_update);
John R. Lenton88f30432014-01-06 22:58:17 +0000304
John R. Lentonf77dce82014-01-06 20:08:52 +0000305
John R. Lentona41fe312014-01-06 17:17:02 +0000306/******************************************************************************/
John R. Lenton9ec3a872014-01-10 01:00:20 +0000307/* dict views */
308
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200309STATIC const mp_obj_type_t dict_view_type;
310STATIC const mp_obj_type_t dict_view_it_type;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000311
312typedef enum _mp_dict_view_kind_t {
313 MP_DICT_VIEW_ITEMS,
314 MP_DICT_VIEW_KEYS,
315 MP_DICT_VIEW_VALUES,
316} mp_dict_view_kind_t;
317
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200318STATIC char *mp_dict_view_names[] = {"dict_items", "dict_keys", "dict_values"};
John R. Lenton9ec3a872014-01-10 01:00:20 +0000319
320typedef struct _mp_obj_dict_view_it_t {
321 mp_obj_base_t base;
322 mp_dict_view_kind_t kind;
323 mp_obj_dict_it_t *iter;
324 machine_uint_t cur;
325} mp_obj_dict_view_it_t;
326
327typedef struct _mp_obj_dict_view_t {
328 mp_obj_base_t base;
329 mp_obj_dict_t *dict;
330 mp_dict_view_kind_t kind;
331} mp_obj_dict_view_t;
332
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200333STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000334 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_it_type));
335 mp_obj_dict_view_it_t *self = self_in;
336 mp_map_elem_t *next = dict_it_iternext_elem(self->iter);
337
338 if (next != NULL) {
339 switch (self->kind) {
Damien Georgeeae16442014-01-11 19:22:29 +0000340 case MP_DICT_VIEW_ITEMS:
341 {
342 mp_obj_t items[] = {next->key, next->value};
343 return mp_obj_new_tuple(2, items);
344 }
345 case MP_DICT_VIEW_KEYS:
346 return next->key;
347 case MP_DICT_VIEW_VALUES:
348 return next->value;
349 default:
350 assert(0); /* can't happen */
351 return mp_const_none;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000352 }
353 } else {
Damien George66eaf842014-03-26 19:27:58 +0000354 return MP_OBJ_NULL;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000355 }
356}
357
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200358STATIC const mp_obj_type_t dict_view_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000359 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000360 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300361 .getiter = mp_identity,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000362 .iternext = dict_view_it_iternext,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000363};
364
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200365STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000366 assert(MP_OBJ_IS_TYPE(view_in, &dict_view_type));
367 mp_obj_dict_view_t *view = view_in;
368 mp_obj_dict_view_it_t *o = m_new_obj(mp_obj_dict_view_it_t);
369 o->base.type = &dict_view_it_type;
370 o->kind = view->kind;
371 o->iter = mp_obj_new_dict_iterator(view->dict, 0);
372 return o;
373}
374
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200375STATIC 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 +0000376 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
377 mp_obj_dict_view_t *self = self_in;
378 bool first = true;
379 print(env, mp_dict_view_names[self->kind]);
380 print(env, "([");
381 mp_obj_t *self_iter = dict_view_getiter(self);
382 mp_obj_t *next = NULL;
Damien George66eaf842014-03-26 19:27:58 +0000383 while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_NULL) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000384 if (!first) {
385 print(env, ", ");
386 }
387 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200388 mp_obj_print_helper(print, env, next, PRINT_REPR);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000389 }
390 print(env, "])");
391}
392
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200393STATIC 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 +0000394 /* only supported for the 'keys' kind until sets and dicts are refactored */
395 mp_obj_dict_view_t *o = lhs_in;
396 if (o->kind != MP_DICT_VIEW_KEYS) return NULL;
Damien Georged17926d2014-03-30 13:35:08 +0100397 if (op != MP_BINARY_OP_IN) return NULL;
John R. Lentonc1bef212014-01-11 12:39:33 +0000398 return dict_binary_op(op, o->dict, rhs_in);
399}
400
401
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200402STATIC const mp_obj_type_t dict_view_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000403 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000404 .name = MP_QSTR_dict_view,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000405 .print = dict_view_print,
John R. Lentonc1bef212014-01-11 12:39:33 +0000406 .binary_op = dict_view_binary_op,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000407 .getiter = dict_view_getiter,
408};
409
410mp_obj_t mp_obj_new_dict_view(mp_obj_dict_t *dict, mp_dict_view_kind_t kind) {
411 mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t);
412 o->base.type = &dict_view_type;
413 o->dict = dict;
414 o->kind = kind;
415 return o;
416}
417
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200418STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
Damien George3e1a5c12014-03-29 13:43:38 +0000419 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton9ec3a872014-01-10 01:00:20 +0000420 mp_obj_dict_t *self = self_in;
421 return mp_obj_new_dict_view(self, kind);
422}
423
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200424STATIC mp_obj_t dict_items(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000425 return dict_view(self_in, MP_DICT_VIEW_ITEMS);
426}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200427STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_items_obj, dict_items);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000428
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200429STATIC mp_obj_t dict_keys(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000430 return dict_view(self_in, MP_DICT_VIEW_KEYS);
431}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200432STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_keys_obj, dict_keys);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000433
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200434STATIC mp_obj_t dict_values(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000435 return dict_view(self_in, MP_DICT_VIEW_VALUES);
436}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200437STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000438
John R. Lenton4bee76e2014-01-10 11:25:03 +0000439/******************************************************************************/
Damien Georgeeae16442014-01-11 19:22:29 +0000440/* dict constructors & public C API */
John R. Lentona41fe312014-01-06 17:17:02 +0000441
Damien George9b196cd2014-03-26 21:47:19 +0000442STATIC const mp_map_elem_t dict_locals_dict_table[] = {
443 { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&dict_clear_obj },
444 { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&dict_copy_obj },
445 { MP_OBJ_NEW_QSTR(MP_QSTR_fromkeys), (mp_obj_t)&dict_fromkeys_obj },
446 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&dict_get_obj },
447 { MP_OBJ_NEW_QSTR(MP_QSTR_items), (mp_obj_t)&dict_items_obj },
448 { MP_OBJ_NEW_QSTR(MP_QSTR_keys), (mp_obj_t)&dict_keys_obj },
449 { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&dict_pop_obj },
450 { MP_OBJ_NEW_QSTR(MP_QSTR_popitem), (mp_obj_t)&dict_popitem_obj },
451 { MP_OBJ_NEW_QSTR(MP_QSTR_setdefault), (mp_obj_t)&dict_setdefault_obj },
452 { MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&dict_update_obj },
453 { MP_OBJ_NEW_QSTR(MP_QSTR_values), (mp_obj_t)&dict_values_obj },
John R. Lentonbaa66542014-01-07 23:18:25 +0000454};
455
Damien George9b196cd2014-03-26 21:47:19 +0000456STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);
457
Damien George3e1a5c12014-03-29 13:43:38 +0000458const mp_obj_type_t mp_type_dict = {
Damien Georgec5966122014-02-15 16:10:44 +0000459 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000460 .name = MP_QSTR_dict,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200461 .print = dict_print,
462 .make_new = dict_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000463 .unary_op = dict_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200464 .binary_op = dict_binary_op,
John R. Lentona41fe312014-01-06 17:17:02 +0000465 .getiter = dict_getiter,
Damien George9b196cd2014-03-26 21:47:19 +0000466 .locals_dict = (mp_obj_t)&dict_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000467};
468
Damien George8b0535e2014-04-05 21:53:54 +0100469void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args) {
470 dict->base.type = &mp_type_dict;
471 mp_map_init(&dict->map, n_args);
472}
473
Damiend99b0522013-12-21 18:17:45 +0000474mp_obj_t mp_obj_new_dict(int n_args) {
475 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
Damien George8b0535e2014-04-05 21:53:54 +0100476 mp_obj_dict_init(o, n_args);
Damiend99b0522013-12-21 18:17:45 +0000477 return o;
478}
Damiendae7eb72013-12-29 22:32:51 +0000479
480uint mp_obj_dict_len(mp_obj_t self_in) {
John R. Lentoncd088732014-01-06 18:53:16 +0000481 return ((mp_obj_dict_t *)self_in)->map.used;
Damiendae7eb72013-12-29 22:32:51 +0000482}
483
484mp_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 +0000485 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
Damiendae7eb72013-12-29 22:32:51 +0000486 mp_obj_dict_t *self = self_in;
Damien George38a2da62014-01-08 17:33:12 +0000487 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
Damiendae7eb72013-12-29 22:32:51 +0000488 return self_in;
489}
Damien George062478e2014-01-09 20:57:50 +0000490
Damien George66edc5d2014-04-05 13:25:13 +0100491mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key) {
492 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
493 mp_obj_dict_t *self = self_in;
494 dict_get_helper(&self->map, key, NULL, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
495 return self_in;
496}
497
Damien George062478e2014-01-09 20:57:50 +0000498mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000499 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
Damien George062478e2014-01-09 20:57:50 +0000500 mp_obj_dict_t *self = self_in;
501 return &self->map;
502}