blob: 8a0a08772a4d9c4d3cece89d0ea4788945646735 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damiend99b0522013-12-21 18:17:45 +000028#include <string.h>
29#include <assert.h>
30
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030031#include "mpconfig.h"
Damiend99b0522013-12-21 18:17:45 +000032#include "nlr.h"
33#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000034#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000035#include "obj.h"
Paul Sokolovskybe019ce2014-04-11 07:01:15 +030036#include "objtuple.h"
Damiend99b0522013-12-21 18:17:45 +000037#include "runtime0.h"
38#include "runtime.h"
Paul Sokolovsky68e7c512014-04-13 11:53:15 +030039#include "builtin.h"
Damiend99b0522013-12-21 18:17:45 +000040
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020041STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur);
42STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in);
Damien Georgebcb6ca42014-06-03 12:53:44 +010043STATIC mp_obj_t dict_update(uint n_args, const mp_obj_t *args, mp_map_t *kwargs);
John R. Lentona41fe312014-01-06 17:17:02 +000044
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020045STATIC 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 +000046 mp_obj_dict_t *self = self_in;
47 bool first = true;
48 print(env, "{");
John R. Lentona41fe312014-01-06 17:17:02 +000049 mp_obj_t *dict_iter = mp_obj_new_dict_iterator(self, 0);
50 mp_map_elem_t *next = NULL;
Damien Georgeea8d06c2014-04-17 23:19:36 +010051 while ((next = dict_it_iternext_elem(dict_iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lentona41fe312014-01-06 17:17:02 +000052 if (!first) {
53 print(env, ", ");
Damiend99b0522013-12-21 18:17:45 +000054 }
John R. Lentona41fe312014-01-06 17:17:02 +000055 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020056 mp_obj_print_helper(print, env, next->key, PRINT_REPR);
John R. Lentona41fe312014-01-06 17:17:02 +000057 print(env, ": ");
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020058 mp_obj_print_helper(print, env, next->value, PRINT_REPR);
Damiend99b0522013-12-21 18:17:45 +000059 }
60 print(env, "}");
61}
62
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020063STATIC mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien Georgebcb6ca42014-06-03 12:53:44 +010064 mp_obj_t dict = mp_obj_new_dict(0);
65 if (n_args > 0 || n_kw > 0) {
66 mp_obj_t args2[2] = {dict, args[0]}; // args[0] is always valid, even if it's not a positional arg
67 mp_map_t kwargs;
68 mp_map_init_fixed_table(&kwargs, n_kw, args + n_args);
69 dict_update(n_args + 1, args2, &kwargs); // dict_update will check that n_args + 1 == 1 or 2
Damien Georged7aadcf2014-04-04 15:08:00 +010070 }
Damien Georged7aadcf2014-04-04 15:08:00 +010071 return dict;
Damien George71c51812014-01-04 20:21:15 +000072}
73
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020074STATIC mp_obj_t dict_unary_op(int op, mp_obj_t self_in) {
Damien George4e8dc8c2014-01-27 23:15:32 +000075 mp_obj_dict_t *self = self_in;
76 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010077 case MP_UNARY_OP_BOOL: return MP_BOOL(self->map.used != 0);
78 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT((machine_int_t)self->map.used);
Damien George6ac5dce2014-05-21 19:42:43 +010079 default: return MP_OBJ_NULL; // op not supported
Damien George4e8dc8c2014-01-27 23:15:32 +000080 }
81}
82
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020083STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damiend99b0522013-12-21 18:17:45 +000084 mp_obj_dict_t *o = lhs_in;
85 switch (op) {
Damien George729f7b42014-04-17 22:10:53 +010086 case MP_BINARY_OP_IN: {
John R. Lentonc1bef212014-01-11 12:39:33 +000087 mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
Damien George9aa2a522014-02-01 23:04:09 +000088 return MP_BOOL(elem != NULL);
John R. Lentonc1bef212014-01-11 12:39:33 +000089 }
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +030090 case MP_BINARY_OP_EQUAL: {
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +030091 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;
Damien Georgee22d76e2014-04-11 10:52:06 +0000109 } else {
110 // dict is not equal to instance of any other type
111 return mp_const_false;
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300112 }
113 }
Damiend99b0522013-12-21 18:17:45 +0000114 default:
115 // op not supported
Damien George6ac5dce2014-05-21 19:42:43 +0100116 return MP_OBJ_NULL;
Damiend99b0522013-12-21 18:17:45 +0000117 }
118}
119
Damien George729f7b42014-04-17 22:10:53 +0100120STATIC mp_obj_t dict_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100121 if (value == MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100122 // delete
Damien Georgef4c9b332014-04-08 21:32:29 +0100123 mp_obj_dict_delete(self_in, index);
Damien George729f7b42014-04-17 22:10:53 +0100124 return mp_const_none;
125 } else if (value == MP_OBJ_SENTINEL) {
126 // load
127 mp_obj_dict_t *self = self_in;
128 mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
129 if (elem == NULL) {
130 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
131 } else {
132 return elem->value;
133 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100134 } else {
Damien George729f7b42014-04-17 22:10:53 +0100135 // store
Damien Georgef4c9b332014-04-08 21:32:29 +0100136 mp_obj_dict_store(self_in, index, value);
Damien George729f7b42014-04-17 22:10:53 +0100137 return mp_const_none;
Damien Georgef4c9b332014-04-08 21:32:29 +0100138 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100139}
John R. Lentona41fe312014-01-06 17:17:02 +0000140
141/******************************************************************************/
142/* dict iterator */
143
144typedef struct _mp_obj_dict_it_t {
145 mp_obj_base_t base;
146 mp_obj_dict_t *dict;
147 machine_uint_t cur;
148} mp_obj_dict_it_t;
149
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200150STATIC mp_map_elem_t *dict_it_iternext_elem(mp_obj_t self_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000151 mp_obj_dict_it_t *self = self_in;
152 machine_uint_t max = self->dict->map.alloc;
Damien George8b0535e2014-04-05 21:53:54 +0100153 mp_map_t *map = &self->dict->map;
John R. Lentona41fe312014-01-06 17:17:02 +0000154
155 for (int i = self->cur; i < max; i++) {
Damien George8b0535e2014-04-05 21:53:54 +0100156 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
John R. Lentona41fe312014-01-06 17:17:02 +0000157 self->cur = i + 1;
Damien George8b0535e2014-04-05 21:53:54 +0100158 return &(map->table[i]);
John R. Lentona41fe312014-01-06 17:17:02 +0000159 }
160 }
161
Damien Georgeea8d06c2014-04-17 23:19:36 +0100162 return MP_OBJ_STOP_ITERATION;
John R. Lentona41fe312014-01-06 17:17:02 +0000163}
164
165mp_obj_t dict_it_iternext(mp_obj_t self_in) {
166 mp_map_elem_t *next = dict_it_iternext_elem(self_in);
167
Damien Georgeea8d06c2014-04-17 23:19:36 +0100168 if (next != MP_OBJ_STOP_ITERATION) {
John R. Lentona41fe312014-01-06 17:17:02 +0000169 return next->key;
170 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100171 return MP_OBJ_STOP_ITERATION;
John R. Lentona41fe312014-01-06 17:17:02 +0000172 }
173}
174
Damien George3e1a5c12014-03-29 13:43:38 +0000175STATIC const mp_obj_type_t mp_type_dict_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000176 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000177 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300178 .getiter = mp_identity,
John R. Lentona41fe312014-01-06 17:17:02 +0000179 .iternext = dict_it_iternext,
John R. Lentona41fe312014-01-06 17:17:02 +0000180};
181
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200182STATIC mp_obj_t mp_obj_new_dict_iterator(mp_obj_dict_t *dict, int cur) {
John R. Lentona41fe312014-01-06 17:17:02 +0000183 mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000184 o->base.type = &mp_type_dict_it;
John R. Lentona41fe312014-01-06 17:17:02 +0000185 o->dict = dict;
186 o->cur = cur;
187 return o;
188}
189
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200190STATIC mp_obj_t dict_getiter(mp_obj_t o_in) {
John R. Lentona41fe312014-01-06 17:17:02 +0000191 return mp_obj_new_dict_iterator(o_in, 0);
192}
193
194/******************************************************************************/
195/* dict methods */
196
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200197STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000198 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000199 mp_obj_dict_t *self = self_in;
200
201 mp_map_clear(&self->map);
202
203 return mp_const_none;
204}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200205STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
John R. Lenton7d21d512014-01-06 17:54:04 +0000206
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200207STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000208 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lentond90b19e2014-01-06 18:11:20 +0000209 mp_obj_dict_t *self = self_in;
210 mp_obj_dict_t *other = mp_obj_new_dict(self->map.alloc);
211 other->map.used = self->map.used;
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +0300212 other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
213 other->map.table_is_fixed_array = 0;
John R. Lentond90b19e2014-01-06 18:11:20 +0000214 memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
215 return other;
216}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200217STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000218
Damien Georgeeae16442014-01-11 19:22:29 +0000219// this is a classmethod
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200220STATIC mp_obj_t dict_fromkeys(uint n_args, const mp_obj_t *args) {
Damien Georgeeae16442014-01-11 19:22:29 +0000221 assert(2 <= n_args && n_args <= 3);
Damien Georged17926d2014-03-30 13:35:08 +0100222 mp_obj_t iter = mp_getiter(args[1]);
Damien Georgeeae16442014-01-11 19:22:29 +0000223 mp_obj_t len = mp_obj_len_maybe(iter);
224 mp_obj_t value = mp_const_none;
225 mp_obj_t next = NULL;
226 mp_obj_dict_t *self = NULL;
227
228 if (n_args > 2) {
229 value = args[2];
230 }
231
232 if (len == MP_OBJ_NULL) {
233 /* object's type doesn't have a __len__ slot */
234 self = mp_obj_new_dict(0);
235 } else {
236 self = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
237 }
238
Damien Georgeea8d06c2014-04-17 23:19:36 +0100239 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien Georgeeae16442014-01-11 19:22:29 +0000240 mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
241 }
242
243 return self;
244}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200245STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
246STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, (const mp_obj_t)&dict_fromkeys_fun_obj);
Damien Georgeeae16442014-01-11 19:22:29 +0000247
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200248STATIC 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 +0000249 mp_map_elem_t *elem = mp_map_lookup(self, key, lookup_kind);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000250 mp_obj_t value;
251 if (elem == NULL || elem->value == NULL) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000252 if (deflt == NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000253 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien Georgeea13f402014-04-05 18:32:08 +0100254 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000255 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000256 value = mp_const_none;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000257 }
258 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000259 value = deflt;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000260 }
261 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000262 value = elem->value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000263 }
Damien George38a2da62014-01-08 17:33:12 +0000264 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000265 elem->value = value;
266 }
267 return value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000268}
269
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200270STATIC mp_obj_t dict_get(uint n_args, const mp_obj_t *args) {
John R. Lentoncd088732014-01-06 18:53:16 +0000271 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000272 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lentoncd088732014-01-06 18:53:16 +0000273
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000274 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
275 args[1],
276 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000277 MP_MAP_LOOKUP);
John R. Lentoncd088732014-01-06 18:53:16 +0000278}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200279STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
John R. Lentoncd088732014-01-06 18:53:16 +0000280
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200281STATIC mp_obj_t dict_pop(uint n_args, const mp_obj_t *args) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000282 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000283 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000284
285 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
286 args[1],
287 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000288 MP_MAP_LOOKUP_REMOVE_IF_FOUND);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000289}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200290STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000291
292
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200293STATIC mp_obj_t dict_setdefault(uint n_args, const mp_obj_t *args) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000294 assert(2 <= n_args && n_args <= 3);
Damien George3e1a5c12014-03-29 13:43:38 +0000295 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000296
297 return dict_get_helper(&((mp_obj_dict_t *)args[0])->map,
298 args[1],
299 n_args == 3 ? args[2] : NULL,
Damien George38a2da62014-01-08 17:33:12 +0000300 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000301}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200302STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000303
John R. Lentonf77dce82014-01-06 20:08:52 +0000304
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200305STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000306 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lentonf77dce82014-01-06 20:08:52 +0000307 mp_obj_dict_t *self = self_in;
308 if (self->map.used == 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100309 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "popitem(): dictionary is empty"));
John R. Lentonf77dce82014-01-06 20:08:52 +0000310 }
311 mp_obj_dict_it_t *iter = mp_obj_new_dict_iterator(self, 0);
312
313 mp_map_elem_t *next = dict_it_iternext_elem(iter);
314 self->map.used--;
315 mp_obj_t items[] = {next->key, next->value};
316 next->key = NULL;
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000317 next->value = NULL;
John R. Lentonf77dce82014-01-06 20:08:52 +0000318 mp_obj_t tuple = mp_obj_new_tuple(2, items);
319
320 return tuple;
321}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200322STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
John R. Lentonf77dce82014-01-06 20:08:52 +0000323
Damien Georgebcb6ca42014-06-03 12:53:44 +0100324STATIC mp_obj_t dict_update(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
325 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_dict));
326 mp_obj_dict_t *self = args[0];
327
328 mp_arg_check_num(n_args, kwargs->used, 1, 2, true);
329
330 if (n_args == 2) {
331 // given a positional argument
332
333 if (MP_OBJ_IS_TYPE(args[1], &mp_type_dict)) {
334 // update from other dictionary (make sure other is not self)
335 if (args[1] != self) {
336 // TODO don't allocate heap object for this iterator
337 mp_obj_t *dict_iter = mp_obj_new_dict_iterator(args[1], 0);
338 mp_map_elem_t *elem = NULL;
339 while ((elem = dict_it_iternext_elem(dict_iter)) != MP_OBJ_STOP_ITERATION) {
340 mp_map_lookup(&self->map, elem->key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = elem->value;
341 }
342 }
John R. Lenton88f30432014-01-06 22:58:17 +0000343 } else {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100344 // update from a generic iterable of pairs
345 mp_obj_t iter = mp_getiter(args[1]);
346 mp_obj_t next = NULL;
347 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
348 mp_obj_t inneriter = mp_getiter(next);
349 mp_obj_t key = mp_iternext(inneriter);
350 mp_obj_t value = mp_iternext(inneriter);
351 mp_obj_t stop = mp_iternext(inneriter);
352 if (key == MP_OBJ_STOP_ITERATION
353 || value == MP_OBJ_STOP_ITERATION
354 || stop != MP_OBJ_STOP_ITERATION) {
355 nlr_raise(mp_obj_new_exception_msg(
356 &mp_type_ValueError,
357 "dictionary update sequence has the wrong length"));
358 } else {
359 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
360 }
361 }
362 }
363 }
364
365 // update the dict with any keyword args
366 for (machine_uint_t i = 0; i < kwargs->alloc; i++) {
367 if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
368 mp_map_lookup(&self->map, kwargs->table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = kwargs->table[i].value;
John R. Lenton88f30432014-01-06 22:58:17 +0000369 }
370 }
371
372 return mp_const_none;
373}
Damien Georgebcb6ca42014-06-03 12:53:44 +0100374STATIC MP_DEFINE_CONST_FUN_OBJ_KW(dict_update_obj, 1, dict_update);
John R. Lenton88f30432014-01-06 22:58:17 +0000375
John R. Lentonf77dce82014-01-06 20:08:52 +0000376
John R. Lentona41fe312014-01-06 17:17:02 +0000377/******************************************************************************/
John R. Lenton9ec3a872014-01-10 01:00:20 +0000378/* dict views */
379
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200380STATIC const mp_obj_type_t dict_view_type;
381STATIC const mp_obj_type_t dict_view_it_type;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000382
383typedef enum _mp_dict_view_kind_t {
384 MP_DICT_VIEW_ITEMS,
385 MP_DICT_VIEW_KEYS,
386 MP_DICT_VIEW_VALUES,
387} mp_dict_view_kind_t;
388
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200389STATIC char *mp_dict_view_names[] = {"dict_items", "dict_keys", "dict_values"};
John R. Lenton9ec3a872014-01-10 01:00:20 +0000390
391typedef struct _mp_obj_dict_view_it_t {
392 mp_obj_base_t base;
393 mp_dict_view_kind_t kind;
394 mp_obj_dict_it_t *iter;
395 machine_uint_t cur;
396} mp_obj_dict_view_it_t;
397
398typedef struct _mp_obj_dict_view_t {
399 mp_obj_base_t base;
400 mp_obj_dict_t *dict;
401 mp_dict_view_kind_t kind;
402} mp_obj_dict_view_t;
403
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200404STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000405 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_it_type));
406 mp_obj_dict_view_it_t *self = self_in;
407 mp_map_elem_t *next = dict_it_iternext_elem(self->iter);
408
Damien Georgeea8d06c2014-04-17 23:19:36 +0100409 if (next != MP_OBJ_STOP_ITERATION) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000410 switch (self->kind) {
Damien Georgeeae16442014-01-11 19:22:29 +0000411 case MP_DICT_VIEW_ITEMS:
412 {
413 mp_obj_t items[] = {next->key, next->value};
414 return mp_obj_new_tuple(2, items);
415 }
416 case MP_DICT_VIEW_KEYS:
417 return next->key;
418 case MP_DICT_VIEW_VALUES:
419 return next->value;
420 default:
421 assert(0); /* can't happen */
422 return mp_const_none;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000423 }
424 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100425 return MP_OBJ_STOP_ITERATION;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000426 }
427}
428
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200429STATIC const mp_obj_type_t dict_view_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000430 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000431 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300432 .getiter = mp_identity,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000433 .iternext = dict_view_it_iternext,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000434};
435
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200436STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000437 assert(MP_OBJ_IS_TYPE(view_in, &dict_view_type));
438 mp_obj_dict_view_t *view = view_in;
439 mp_obj_dict_view_it_t *o = m_new_obj(mp_obj_dict_view_it_t);
440 o->base.type = &dict_view_it_type;
441 o->kind = view->kind;
442 o->iter = mp_obj_new_dict_iterator(view->dict, 0);
443 return o;
444}
445
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200446STATIC 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 +0000447 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
448 mp_obj_dict_view_t *self = self_in;
449 bool first = true;
450 print(env, mp_dict_view_names[self->kind]);
451 print(env, "([");
452 mp_obj_t *self_iter = dict_view_getiter(self);
453 mp_obj_t *next = NULL;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100454 while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000455 if (!first) {
456 print(env, ", ");
457 }
458 first = false;
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200459 mp_obj_print_helper(print, env, next, PRINT_REPR);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000460 }
461 print(env, "])");
462}
463
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200464STATIC mp_obj_t dict_view_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien George6ac5dce2014-05-21 19:42:43 +0100465 // only supported for the 'keys' kind until sets and dicts are refactored
John R. Lentonc1bef212014-01-11 12:39:33 +0000466 mp_obj_dict_view_t *o = lhs_in;
Damien Georged0a5bf32014-05-10 13:55:11 +0100467 if (o->kind != MP_DICT_VIEW_KEYS) {
Damien George6ac5dce2014-05-21 19:42:43 +0100468 return MP_OBJ_NULL; // op not supported
Damien Georged0a5bf32014-05-10 13:55:11 +0100469 }
470 if (op != MP_BINARY_OP_IN) {
Damien George6ac5dce2014-05-21 19:42:43 +0100471 return MP_OBJ_NULL; // op not supported
Damien Georged0a5bf32014-05-10 13:55:11 +0100472 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000473 return dict_binary_op(op, o->dict, rhs_in);
474}
475
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200476STATIC const mp_obj_type_t dict_view_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000477 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000478 .name = MP_QSTR_dict_view,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000479 .print = dict_view_print,
John R. Lentonc1bef212014-01-11 12:39:33 +0000480 .binary_op = dict_view_binary_op,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000481 .getiter = dict_view_getiter,
482};
483
484mp_obj_t mp_obj_new_dict_view(mp_obj_dict_t *dict, mp_dict_view_kind_t kind) {
485 mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t);
486 o->base.type = &dict_view_type;
487 o->dict = dict;
488 o->kind = kind;
489 return o;
490}
491
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200492STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
Damien George3e1a5c12014-03-29 13:43:38 +0000493 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
John R. Lenton9ec3a872014-01-10 01:00:20 +0000494 mp_obj_dict_t *self = self_in;
495 return mp_obj_new_dict_view(self, kind);
496}
497
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200498STATIC mp_obj_t dict_items(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000499 return dict_view(self_in, MP_DICT_VIEW_ITEMS);
500}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200501STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_items_obj, dict_items);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000502
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200503STATIC mp_obj_t dict_keys(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000504 return dict_view(self_in, MP_DICT_VIEW_KEYS);
505}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200506STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_keys_obj, dict_keys);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000507
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200508STATIC mp_obj_t dict_values(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000509 return dict_view(self_in, MP_DICT_VIEW_VALUES);
510}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200511STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000512
John R. Lenton4bee76e2014-01-10 11:25:03 +0000513/******************************************************************************/
Damien Georgeeae16442014-01-11 19:22:29 +0000514/* dict constructors & public C API */
John R. Lentona41fe312014-01-06 17:17:02 +0000515
Damien George9b196cd2014-03-26 21:47:19 +0000516STATIC const mp_map_elem_t dict_locals_dict_table[] = {
517 { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&dict_clear_obj },
518 { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&dict_copy_obj },
519 { MP_OBJ_NEW_QSTR(MP_QSTR_fromkeys), (mp_obj_t)&dict_fromkeys_obj },
520 { MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&dict_get_obj },
521 { MP_OBJ_NEW_QSTR(MP_QSTR_items), (mp_obj_t)&dict_items_obj },
522 { MP_OBJ_NEW_QSTR(MP_QSTR_keys), (mp_obj_t)&dict_keys_obj },
523 { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&dict_pop_obj },
524 { MP_OBJ_NEW_QSTR(MP_QSTR_popitem), (mp_obj_t)&dict_popitem_obj },
525 { MP_OBJ_NEW_QSTR(MP_QSTR_setdefault), (mp_obj_t)&dict_setdefault_obj },
526 { MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&dict_update_obj },
527 { MP_OBJ_NEW_QSTR(MP_QSTR_values), (mp_obj_t)&dict_values_obj },
Paul Sokolovsky68e7c512014-04-13 11:53:15 +0300528 { MP_OBJ_NEW_QSTR(MP_QSTR___getitem__), (mp_obj_t)&mp_op_getitem_obj },
Paul Sokolovskycd94b382014-04-13 23:41:49 +0300529 { MP_OBJ_NEW_QSTR(MP_QSTR___setitem__), (mp_obj_t)&mp_op_setitem_obj },
Paul Sokolovsky14de1142014-04-13 23:55:59 +0300530 { MP_OBJ_NEW_QSTR(MP_QSTR___delitem__), (mp_obj_t)&mp_op_delitem_obj },
John R. Lentonbaa66542014-01-07 23:18:25 +0000531};
532
Damien George9b196cd2014-03-26 21:47:19 +0000533STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);
534
Damien George3e1a5c12014-03-29 13:43:38 +0000535const mp_obj_type_t mp_type_dict = {
Damien Georgec5966122014-02-15 16:10:44 +0000536 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000537 .name = MP_QSTR_dict,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200538 .print = dict_print,
539 .make_new = dict_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000540 .unary_op = dict_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200541 .binary_op = dict_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100542 .subscr = dict_subscr,
John R. Lentona41fe312014-01-06 17:17:02 +0000543 .getiter = dict_getiter,
Damien George9b196cd2014-03-26 21:47:19 +0000544 .locals_dict = (mp_obj_t)&dict_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000545};
546
Damien George8b0535e2014-04-05 21:53:54 +0100547void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args) {
548 dict->base.type = &mp_type_dict;
549 mp_map_init(&dict->map, n_args);
550}
551
Damiend99b0522013-12-21 18:17:45 +0000552mp_obj_t mp_obj_new_dict(int n_args) {
553 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
Damien George8b0535e2014-04-05 21:53:54 +0100554 mp_obj_dict_init(o, n_args);
Damiend99b0522013-12-21 18:17:45 +0000555 return o;
556}
Damiendae7eb72013-12-29 22:32:51 +0000557
558uint mp_obj_dict_len(mp_obj_t self_in) {
John R. Lentoncd088732014-01-06 18:53:16 +0000559 return ((mp_obj_dict_t *)self_in)->map.used;
Damiendae7eb72013-12-29 22:32:51 +0000560}
561
562mp_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 +0000563 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
Damiendae7eb72013-12-29 22:32:51 +0000564 mp_obj_dict_t *self = self_in;
Damien George38a2da62014-01-08 17:33:12 +0000565 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
Damiendae7eb72013-12-29 22:32:51 +0000566 return self_in;
567}
Damien George062478e2014-01-09 20:57:50 +0000568
Damien George66edc5d2014-04-05 13:25:13 +0100569mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key) {
570 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
571 mp_obj_dict_t *self = self_in;
572 dict_get_helper(&self->map, key, NULL, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
573 return self_in;
574}
575
Damien George062478e2014-01-09 20:57:50 +0000576mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000577 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_dict));
Damien George062478e2014-01-09 20:57:50 +0000578 mp_obj_dict_t *self = self_in;
579 return &self->map;
580}