blob: 91d5b75e2b9f1e880844c19f7a4f08e04d9f7206 [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
Damiend99b0522013-12-21 18:17:45 +000027#include <string.h>
28#include <assert.h>
29
Damien George51dfcb42015-01-01 20:27:54 +000030#include "py/nlr.h"
31#include "py/obj.h"
32#include "py/runtime0.h"
33#include "py/runtime.h"
34#include "py/builtin.h"
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020035#include "py/objtype.h"
36
Damien George999cedb2015-11-27 17:01:44 +000037#define MP_OBJ_IS_DICT_TYPE(o) (MP_OBJ_IS_OBJ(o) && ((mp_obj_base_t*)MP_OBJ_TO_PTR(o))->type->make_new == dict_make_new)
Damiend99b0522013-12-21 18:17:45 +000038
Damien George4b72b3a2016-01-03 14:21:40 +000039STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
John R. Lentona41fe312014-01-06 17:17:02 +000040
Damien George8a9b9992014-09-17 15:53:03 +010041// This is a helper function to iterate through a dictionary. The state of
42// the iteration is held in *cur and should be initialised with zero for the
43// first call. Will return NULL when no more elements are available.
44STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, mp_uint_t *cur) {
45 mp_uint_t max = dict->map.alloc;
46 mp_map_t *map = &dict->map;
47
48 for (mp_uint_t i = *cur; i < max; i++) {
49 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
50 *cur = i + 1;
51 return &(map->table[i]);
52 }
53 }
54
55 return NULL;
56}
57
Damien George7f9d1d62015-04-09 23:56:15 +010058STATIC void dict_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
Damien George999cedb2015-11-27 17:01:44 +000059 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damiend99b0522013-12-21 18:17:45 +000060 bool first = true;
Damien George612045f2014-09-17 22:56:34 +010061 if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
62 kind = PRINT_REPR;
63 }
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020064 if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict) {
Damien George044c4732015-04-11 13:03:37 +010065 mp_printf(print, "%q(", self->base.type->name);
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020066 }
Damien George7f9d1d62015-04-09 23:56:15 +010067 mp_print_str(print, "{");
Damien George8a9b9992014-09-17 15:53:03 +010068 mp_uint_t cur = 0;
John R. Lentona41fe312014-01-06 17:17:02 +000069 mp_map_elem_t *next = NULL;
Damien George8a9b9992014-09-17 15:53:03 +010070 while ((next = dict_iter_next(self, &cur)) != NULL) {
John R. Lentona41fe312014-01-06 17:17:02 +000071 if (!first) {
Damien George7f9d1d62015-04-09 23:56:15 +010072 mp_print_str(print, ", ");
Damiend99b0522013-12-21 18:17:45 +000073 }
John R. Lentona41fe312014-01-06 17:17:02 +000074 first = false;
Damien George7f9d1d62015-04-09 23:56:15 +010075 mp_obj_print_helper(print, next->key, kind);
76 mp_print_str(print, ": ");
77 mp_obj_print_helper(print, next->value, kind);
Damiend99b0522013-12-21 18:17:45 +000078 }
Damien George7f9d1d62015-04-09 23:56:15 +010079 mp_print_str(print, "}");
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020080 if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict) {
Damien George7f9d1d62015-04-09 23:56:15 +010081 mp_print_str(print, ")");
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020082 }
Damiend99b0522013-12-21 18:17:45 +000083}
84
Damien George5b3f0b72016-01-03 15:55:55 +000085STATIC mp_obj_t dict_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Paul Sokolovsky0090c712015-03-26 12:28:31 +020086 mp_obj_t dict_out = mp_obj_new_dict(0);
Damien George999cedb2015-11-27 17:01:44 +000087 mp_obj_dict_t *dict = MP_OBJ_TO_PTR(dict_out);
Damien George5b3f0b72016-01-03 15:55:55 +000088 dict->base.type = type;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020089 #if MICROPY_PY_COLLECTIONS_ORDEREDDICT
Damien George5b3f0b72016-01-03 15:55:55 +000090 if (type == &mp_type_ordereddict) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020091 dict->map.is_ordered = 1;
92 }
93 #endif
Damien Georgebcb6ca42014-06-03 12:53:44 +010094 if (n_args > 0 || n_kw > 0) {
Paul Sokolovsky0090c712015-03-26 12:28:31 +020095 mp_obj_t args2[2] = {dict_out, args[0]}; // args[0] is always valid, even if it's not a positional arg
Damien Georgebcb6ca42014-06-03 12:53:44 +010096 mp_map_t kwargs;
97 mp_map_init_fixed_table(&kwargs, n_kw, args + n_args);
98 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 +010099 }
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200100 return dict_out;
Damien George71c51812014-01-04 20:21:15 +0000101}
102
Damien Georgeecc88e92014-08-30 00:35:11 +0100103STATIC mp_obj_t dict_unary_op(mp_uint_t op, mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000104 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damien George4e8dc8c2014-01-27 23:15:32 +0000105 switch (op) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300106 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->map.used != 0);
Damien Georgebb4c6f32014-07-31 10:49:14 +0100107 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->map.used);
Damien George6ac5dce2014-05-21 19:42:43 +0100108 default: return MP_OBJ_NULL; // op not supported
Damien George4e8dc8c2014-01-27 23:15:32 +0000109 }
110}
111
Damien Georgeecc88e92014-08-30 00:35:11 +0100112STATIC mp_obj_t dict_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien George999cedb2015-11-27 17:01:44 +0000113 mp_obj_dict_t *o = MP_OBJ_TO_PTR(lhs_in);
Damiend99b0522013-12-21 18:17:45 +0000114 switch (op) {
Damien George729f7b42014-04-17 22:10:53 +0100115 case MP_BINARY_OP_IN: {
John R. Lentonc1bef212014-01-11 12:39:33 +0000116 mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300117 return mp_obj_new_bool(elem != NULL);
John R. Lentonc1bef212014-01-11 12:39:33 +0000118 }
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300119 case MP_BINARY_OP_EQUAL: {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200120 #if MICROPY_PY_COLLECTIONS_ORDEREDDICT
121 if (MP_UNLIKELY(MP_OBJ_IS_TYPE(lhs_in, &mp_type_ordereddict) && MP_OBJ_IS_TYPE(rhs_in, &mp_type_ordereddict))) {
Mark Anthony Palomer31310532016-06-05 18:48:25 -0700122 // Iterate through both dictionaries simultaneously and compare keys and values.
123 mp_obj_dict_t *rhs = MP_OBJ_TO_PTR(rhs_in);
124 mp_uint_t c1 = 0, c2 = 0;
125 mp_map_elem_t *e1 = dict_iter_next(o, &c1), *e2 = dict_iter_next(rhs, &c2);
126 for (; e1 != NULL && e2 != NULL; e1 = dict_iter_next(o, &c1), e2 = dict_iter_next(rhs, &c2)) {
127 if (!mp_obj_equal(e1->key, e2->key) || !mp_obj_equal(e1->value, e2->value)) {
128 return mp_const_false;
129 }
130 }
131 return e1 == NULL && e2 == NULL ? mp_const_true : mp_const_false;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200132 } else
133 #endif
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300134 if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_dict)) {
Damien George999cedb2015-11-27 17:01:44 +0000135 mp_obj_dict_t *rhs = MP_OBJ_TO_PTR(rhs_in);
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300136 if (o->map.used != rhs->map.used) {
137 return mp_const_false;
138 }
139
Damien George8a9b9992014-09-17 15:53:03 +0100140 mp_uint_t cur = 0;
141 mp_map_elem_t *next = NULL;
142 while ((next = dict_iter_next(o, &cur)) != NULL) {
143 mp_map_elem_t *elem = mp_map_lookup(&rhs->map, next->key, MP_MAP_LOOKUP);
144 if (elem == NULL || !mp_obj_equal(next->value, elem->value)) {
145 return mp_const_false;
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300146 }
147 }
148 return mp_const_true;
Damien Georgee22d76e2014-04-11 10:52:06 +0000149 } else {
150 // dict is not equal to instance of any other type
151 return mp_const_false;
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300152 }
153 }
Damiend99b0522013-12-21 18:17:45 +0000154 default:
155 // op not supported
Damien George6ac5dce2014-05-21 19:42:43 +0100156 return MP_OBJ_NULL;
Damiend99b0522013-12-21 18:17:45 +0000157 }
158}
159
Paul Sokolovsky75ce9252014-06-05 20:02:15 +0300160// TODO: Make sure this is inlined in dict_subscr() below.
161mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index) {
Damien George999cedb2015-11-27 17:01:44 +0000162 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky75ce9252014-06-05 20:02:15 +0300163 mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
164 if (elem == NULL) {
165 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
166 } else {
167 return elem->value;
168 }
169}
170
Damien George729f7b42014-04-17 22:10:53 +0100171STATIC 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 +0100172 if (value == MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100173 // delete
Damien Georgef4c9b332014-04-08 21:32:29 +0100174 mp_obj_dict_delete(self_in, index);
Damien George729f7b42014-04-17 22:10:53 +0100175 return mp_const_none;
176 } else if (value == MP_OBJ_SENTINEL) {
177 // load
Damien George999cedb2015-11-27 17:01:44 +0000178 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damien George729f7b42014-04-17 22:10:53 +0100179 mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
180 if (elem == NULL) {
181 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
182 } else {
183 return elem->value;
184 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100185 } else {
Damien George729f7b42014-04-17 22:10:53 +0100186 // store
Damien Georgef4c9b332014-04-08 21:32:29 +0100187 mp_obj_dict_store(self_in, index, value);
Damien George729f7b42014-04-17 22:10:53 +0100188 return mp_const_none;
Damien Georgef4c9b332014-04-08 21:32:29 +0100189 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100190}
John R. Lentona41fe312014-01-06 17:17:02 +0000191
192/******************************************************************************/
193/* dict iterator */
194
195typedef struct _mp_obj_dict_it_t {
196 mp_obj_base_t base;
Damien George8212d972016-01-03 16:27:55 +0000197 mp_fun_1_t iternext;
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200198 mp_obj_t dict;
Damien George40f3c022014-07-03 13:25:24 +0100199 mp_uint_t cur;
John R. Lentona41fe312014-01-06 17:17:02 +0000200} mp_obj_dict_it_t;
201
Damien George8a9b9992014-09-17 15:53:03 +0100202STATIC mp_obj_t dict_it_iternext(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000203 mp_obj_dict_it_t *self = MP_OBJ_TO_PTR(self_in);
204 mp_map_elem_t *next = dict_iter_next(MP_OBJ_TO_PTR(self->dict), &self->cur);
John R. Lentona41fe312014-01-06 17:17:02 +0000205
Damien George8a9b9992014-09-17 15:53:03 +0100206 if (next == NULL) {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100207 return MP_OBJ_STOP_ITERATION;
Damien George8a9b9992014-09-17 15:53:03 +0100208 } else {
209 return next->key;
John R. Lentona41fe312014-01-06 17:17:02 +0000210 }
211}
212
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200213STATIC mp_obj_t dict_getiter(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000214 mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t);
Damien George8212d972016-01-03 16:27:55 +0000215 o->base.type = &mp_type_polymorph_iter;
216 o->iternext = dict_it_iternext;
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200217 o->dict = self_in;
Damien George8a9b9992014-09-17 15:53:03 +0100218 o->cur = 0;
Damien George999cedb2015-11-27 17:01:44 +0000219 return MP_OBJ_FROM_PTR(o);
John R. Lentona41fe312014-01-06 17:17:02 +0000220}
221
John R. Lentona41fe312014-01-06 17:17:02 +0000222/******************************************************************************/
223/* dict methods */
224
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200225STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200226 assert(MP_OBJ_IS_DICT_TYPE(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000227 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000228
229 mp_map_clear(&self->map);
230
231 return mp_const_none;
232}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200233STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
John R. Lenton7d21d512014-01-06 17:54:04 +0000234
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200235STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200236 assert(MP_OBJ_IS_DICT_TYPE(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000237 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200238 mp_obj_t other_out = mp_obj_new_dict(self->map.alloc);
Damien George999cedb2015-11-27 17:01:44 +0000239 mp_obj_dict_t *other = MP_OBJ_TO_PTR(other_out);
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200240 other->base.type = self->base.type;
John R. Lentond90b19e2014-01-06 18:11:20 +0000241 other->map.used = self->map.used;
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +0300242 other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200243 other->map.is_fixed = 0;
244 other->map.is_ordered = self->map.is_ordered;
John R. Lentond90b19e2014-01-06 18:11:20 +0000245 memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200246 return other_out;
John R. Lentond90b19e2014-01-06 18:11:20 +0000247}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200248STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000249
Damien Georgeeae16442014-01-11 19:22:29 +0000250// this is a classmethod
Damien George4b72b3a2016-01-03 14:21:40 +0000251STATIC mp_obj_t dict_fromkeys(size_t n_args, const mp_obj_t *args) {
Damien Georgeeae16442014-01-11 19:22:29 +0000252 assert(2 <= n_args && n_args <= 3);
Damien Georged17926d2014-03-30 13:35:08 +0100253 mp_obj_t iter = mp_getiter(args[1]);
Damien Georgeeae16442014-01-11 19:22:29 +0000254 mp_obj_t len = mp_obj_len_maybe(iter);
255 mp_obj_t value = mp_const_none;
Damien George83229d32015-11-20 14:09:20 +0000256 mp_obj_t next = MP_OBJ_NULL;
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200257 mp_obj_t self_out;
Damien Georgeeae16442014-01-11 19:22:29 +0000258
259 if (n_args > 2) {
260 value = args[2];
261 }
262
263 if (len == MP_OBJ_NULL) {
264 /* object's type doesn't have a __len__ slot */
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200265 self_out = mp_obj_new_dict(0);
Damien Georgeeae16442014-01-11 19:22:29 +0000266 } else {
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200267 self_out = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
Damien Georgeeae16442014-01-11 19:22:29 +0000268 }
269
Damien George999cedb2015-11-27 17:01:44 +0000270 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_out);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100271 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien Georgeeae16442014-01-11 19:22:29 +0000272 mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
273 }
274
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200275 return self_out;
Damien Georgeeae16442014-01-11 19:22:29 +0000276}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200277STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
Damien Georgecbf76742015-11-27 13:38:15 +0000278STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, MP_ROM_PTR(&dict_fromkeys_fun_obj));
Damien Georgeeae16442014-01-11 19:22:29 +0000279
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200280STATIC 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 +0000281 mp_map_elem_t *elem = mp_map_lookup(self, key, lookup_kind);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000282 mp_obj_t value;
Damien George8a9b9992014-09-17 15:53:03 +0100283 if (elem == NULL || elem->value == MP_OBJ_NULL) {
284 if (deflt == MP_OBJ_NULL) {
Damien George38a2da62014-01-08 17:33:12 +0000285 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien Georgeea13f402014-04-05 18:32:08 +0100286 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "<value>"));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000287 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000288 value = mp_const_none;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000289 }
290 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000291 value = deflt;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000292 }
Damien George8a9b9992014-09-17 15:53:03 +0100293 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
294 elem->value = value;
295 }
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000296 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000297 value = elem->value;
Damien George8a9b9992014-09-17 15:53:03 +0100298 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
299 elem->value = MP_OBJ_NULL; // so that GC can collect the deleted value
300 }
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000301 }
302 return value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000303}
304
Damien George4b72b3a2016-01-03 14:21:40 +0000305STATIC mp_obj_t dict_get(size_t n_args, const mp_obj_t *args) {
John R. Lentoncd088732014-01-06 18:53:16 +0000306 assert(2 <= n_args && n_args <= 3);
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200307 assert(MP_OBJ_IS_DICT_TYPE(args[0]));
Damien George999cedb2015-11-27 17:01:44 +0000308 mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]);
John R. Lentoncd088732014-01-06 18:53:16 +0000309
Damien George999cedb2015-11-27 17:01:44 +0000310 return dict_get_helper(&self->map,
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000311 args[1],
Damien George8a9b9992014-09-17 15:53:03 +0100312 n_args == 3 ? args[2] : MP_OBJ_NULL,
Damien George38a2da62014-01-08 17:33:12 +0000313 MP_MAP_LOOKUP);
John R. Lentoncd088732014-01-06 18:53:16 +0000314}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200315STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
John R. Lentoncd088732014-01-06 18:53:16 +0000316
Damien George4b72b3a2016-01-03 14:21:40 +0000317STATIC mp_obj_t dict_pop(size_t n_args, const mp_obj_t *args) {
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000318 assert(2 <= n_args && n_args <= 3);
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200319 assert(MP_OBJ_IS_DICT_TYPE(args[0]));
Damien George999cedb2015-11-27 17:01:44 +0000320 mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000321
Damien George999cedb2015-11-27 17:01:44 +0000322 return dict_get_helper(&self->map,
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000323 args[1],
Damien George8a9b9992014-09-17 15:53:03 +0100324 n_args == 3 ? args[2] : MP_OBJ_NULL,
Damien George38a2da62014-01-08 17:33:12 +0000325 MP_MAP_LOOKUP_REMOVE_IF_FOUND);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000326}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200327STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000328
329
Damien George4b72b3a2016-01-03 14:21:40 +0000330STATIC mp_obj_t dict_setdefault(size_t n_args, const mp_obj_t *args) {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000331 assert(2 <= n_args && n_args <= 3);
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200332 assert(MP_OBJ_IS_DICT_TYPE(args[0]));
Damien George999cedb2015-11-27 17:01:44 +0000333 mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000334
Damien George999cedb2015-11-27 17:01:44 +0000335 return dict_get_helper(&self->map,
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000336 args[1],
Damien George8a9b9992014-09-17 15:53:03 +0100337 n_args == 3 ? args[2] : MP_OBJ_NULL,
Damien George38a2da62014-01-08 17:33:12 +0000338 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000339}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200340STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000341
John R. Lentonf77dce82014-01-06 20:08:52 +0000342
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200343STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200344 assert(MP_OBJ_IS_DICT_TYPE(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000345 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damien George8a9b9992014-09-17 15:53:03 +0100346 mp_uint_t cur = 0;
347 mp_map_elem_t *next = dict_iter_next(self, &cur);
348 if (next == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100349 nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "popitem(): dictionary is empty"));
John R. Lentonf77dce82014-01-06 20:08:52 +0000350 }
John R. Lentonf77dce82014-01-06 20:08:52 +0000351 self->map.used--;
352 mp_obj_t items[] = {next->key, next->value};
Damien George8a9b9992014-09-17 15:53:03 +0100353 next->key = MP_OBJ_SENTINEL; // must mark key as sentinel to indicate that it was deleted
354 next->value = MP_OBJ_NULL;
John R. Lentonf77dce82014-01-06 20:08:52 +0000355 mp_obj_t tuple = mp_obj_new_tuple(2, items);
356
357 return tuple;
358}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200359STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
John R. Lentonf77dce82014-01-06 20:08:52 +0000360
Damien George4b72b3a2016-01-03 14:21:40 +0000361STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200362 assert(MP_OBJ_IS_DICT_TYPE(args[0]));
Damien George999cedb2015-11-27 17:01:44 +0000363 mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]);
Damien Georgebcb6ca42014-06-03 12:53:44 +0100364
365 mp_arg_check_num(n_args, kwargs->used, 1, 2, true);
366
367 if (n_args == 2) {
368 // given a positional argument
369
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200370 if (MP_OBJ_IS_DICT_TYPE(args[1])) {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100371 // update from other dictionary (make sure other is not self)
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200372 if (args[1] != args[0]) {
Damien George8a9b9992014-09-17 15:53:03 +0100373 mp_uint_t cur = 0;
Damien Georgebcb6ca42014-06-03 12:53:44 +0100374 mp_map_elem_t *elem = NULL;
Damien George999cedb2015-11-27 17:01:44 +0000375 while ((elem = dict_iter_next((mp_obj_dict_t*)MP_OBJ_TO_PTR(args[1]), &cur)) != NULL) {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100376 mp_map_lookup(&self->map, elem->key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = elem->value;
377 }
378 }
John R. Lenton88f30432014-01-06 22:58:17 +0000379 } else {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100380 // update from a generic iterable of pairs
381 mp_obj_t iter = mp_getiter(args[1]);
Damien George83229d32015-11-20 14:09:20 +0000382 mp_obj_t next = MP_OBJ_NULL;
Damien Georgebcb6ca42014-06-03 12:53:44 +0100383 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
384 mp_obj_t inneriter = mp_getiter(next);
385 mp_obj_t key = mp_iternext(inneriter);
386 mp_obj_t value = mp_iternext(inneriter);
387 mp_obj_t stop = mp_iternext(inneriter);
388 if (key == MP_OBJ_STOP_ITERATION
389 || value == MP_OBJ_STOP_ITERATION
390 || stop != MP_OBJ_STOP_ITERATION) {
391 nlr_raise(mp_obj_new_exception_msg(
392 &mp_type_ValueError,
393 "dictionary update sequence has the wrong length"));
394 } else {
395 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
396 }
397 }
398 }
399 }
400
401 // update the dict with any keyword args
Damien George40f3c022014-07-03 13:25:24 +0100402 for (mp_uint_t i = 0; i < kwargs->alloc; i++) {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100403 if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
404 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 +0000405 }
406 }
407
408 return mp_const_none;
409}
Damien Georgebcb6ca42014-06-03 12:53:44 +0100410STATIC MP_DEFINE_CONST_FUN_OBJ_KW(dict_update_obj, 1, dict_update);
John R. Lenton88f30432014-01-06 22:58:17 +0000411
John R. Lentonf77dce82014-01-06 20:08:52 +0000412
John R. Lentona41fe312014-01-06 17:17:02 +0000413/******************************************************************************/
John R. Lenton9ec3a872014-01-10 01:00:20 +0000414/* dict views */
415
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200416STATIC const mp_obj_type_t dict_view_type;
417STATIC const mp_obj_type_t dict_view_it_type;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000418
419typedef enum _mp_dict_view_kind_t {
420 MP_DICT_VIEW_ITEMS,
421 MP_DICT_VIEW_KEYS,
422 MP_DICT_VIEW_VALUES,
423} mp_dict_view_kind_t;
424
Damien George3ff16ff2016-05-20 12:38:15 +0100425STATIC const char *const mp_dict_view_names[] = {"dict_items", "dict_keys", "dict_values"};
John R. Lenton9ec3a872014-01-10 01:00:20 +0000426
427typedef struct _mp_obj_dict_view_it_t {
428 mp_obj_base_t base;
429 mp_dict_view_kind_t kind;
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200430 mp_obj_t dict;
Damien George40f3c022014-07-03 13:25:24 +0100431 mp_uint_t cur;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000432} mp_obj_dict_view_it_t;
433
434typedef struct _mp_obj_dict_view_t {
435 mp_obj_base_t base;
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200436 mp_obj_t dict;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000437 mp_dict_view_kind_t kind;
438} mp_obj_dict_view_t;
439
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200440STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000441 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_it_type));
Damien George999cedb2015-11-27 17:01:44 +0000442 mp_obj_dict_view_it_t *self = MP_OBJ_TO_PTR(self_in);
443 mp_map_elem_t *next = dict_iter_next(MP_OBJ_TO_PTR(self->dict), &self->cur);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000444
Damien George8a9b9992014-09-17 15:53:03 +0100445 if (next == NULL) {
446 return MP_OBJ_STOP_ITERATION;
447 } else {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000448 switch (self->kind) {
Damien Georgeeae16442014-01-11 19:22:29 +0000449 case MP_DICT_VIEW_ITEMS:
Damien Georged2d64f02015-01-14 21:32:42 +0000450 default: {
Damien Georgeeae16442014-01-11 19:22:29 +0000451 mp_obj_t items[] = {next->key, next->value};
452 return mp_obj_new_tuple(2, items);
453 }
454 case MP_DICT_VIEW_KEYS:
455 return next->key;
456 case MP_DICT_VIEW_VALUES:
457 return next->value;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000458 }
John R. Lenton9ec3a872014-01-10 01:00:20 +0000459 }
460}
461
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200462STATIC const mp_obj_type_t dict_view_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000463 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000464 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300465 .getiter = mp_identity,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000466 .iternext = dict_view_it_iternext,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000467};
468
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200469STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000470 assert(MP_OBJ_IS_TYPE(view_in, &dict_view_type));
Damien George999cedb2015-11-27 17:01:44 +0000471 mp_obj_dict_view_t *view = MP_OBJ_TO_PTR(view_in);
472 mp_obj_dict_view_it_t *o = m_new_obj(mp_obj_dict_view_it_t);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000473 o->base.type = &dict_view_it_type;
474 o->kind = view->kind;
Damien George8a9b9992014-09-17 15:53:03 +0100475 o->dict = view->dict;
476 o->cur = 0;
Damien George999cedb2015-11-27 17:01:44 +0000477 return MP_OBJ_FROM_PTR(o);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000478}
479
Damien George7f9d1d62015-04-09 23:56:15 +0100480STATIC void dict_view_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000481 (void)kind;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000482 assert(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
Damien George999cedb2015-11-27 17:01:44 +0000483 mp_obj_dict_view_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000484 bool first = true;
Damien George7f9d1d62015-04-09 23:56:15 +0100485 mp_print_str(print, mp_dict_view_names[self->kind]);
486 mp_print_str(print, "([");
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200487 mp_obj_t self_iter = dict_view_getiter(self_in);
488 mp_obj_t next = MP_OBJ_NULL;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100489 while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000490 if (!first) {
Damien George7f9d1d62015-04-09 23:56:15 +0100491 mp_print_str(print, ", ");
John R. Lenton9ec3a872014-01-10 01:00:20 +0000492 }
493 first = false;
Damien George7f9d1d62015-04-09 23:56:15 +0100494 mp_obj_print_helper(print, next, PRINT_REPR);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000495 }
Damien George7f9d1d62015-04-09 23:56:15 +0100496 mp_print_str(print, "])");
John R. Lenton9ec3a872014-01-10 01:00:20 +0000497}
498
Damien Georgeecc88e92014-08-30 00:35:11 +0100499STATIC mp_obj_t dict_view_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien George6ac5dce2014-05-21 19:42:43 +0100500 // only supported for the 'keys' kind until sets and dicts are refactored
Damien George999cedb2015-11-27 17:01:44 +0000501 mp_obj_dict_view_t *o = MP_OBJ_TO_PTR(lhs_in);
Damien Georged0a5bf32014-05-10 13:55:11 +0100502 if (o->kind != MP_DICT_VIEW_KEYS) {
Damien George6ac5dce2014-05-21 19:42:43 +0100503 return MP_OBJ_NULL; // op not supported
Damien Georged0a5bf32014-05-10 13:55:11 +0100504 }
505 if (op != MP_BINARY_OP_IN) {
Damien George6ac5dce2014-05-21 19:42:43 +0100506 return MP_OBJ_NULL; // op not supported
Damien Georged0a5bf32014-05-10 13:55:11 +0100507 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000508 return dict_binary_op(op, o->dict, rhs_in);
509}
510
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200511STATIC const mp_obj_type_t dict_view_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000512 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000513 .name = MP_QSTR_dict_view,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000514 .print = dict_view_print,
John R. Lentonc1bef212014-01-11 12:39:33 +0000515 .binary_op = dict_view_binary_op,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000516 .getiter = dict_view_getiter,
517};
518
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200519STATIC mp_obj_t mp_obj_new_dict_view(mp_obj_t dict, mp_dict_view_kind_t kind) {
Damien George999cedb2015-11-27 17:01:44 +0000520 mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000521 o->base.type = &dict_view_type;
522 o->dict = dict;
523 o->kind = kind;
Damien George999cedb2015-11-27 17:01:44 +0000524 return MP_OBJ_FROM_PTR(o);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000525}
526
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200527STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200528 assert(MP_OBJ_IS_DICT_TYPE(self_in));
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200529 return mp_obj_new_dict_view(self_in, kind);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000530}
531
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200532STATIC mp_obj_t dict_items(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000533 return dict_view(self_in, MP_DICT_VIEW_ITEMS);
534}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200535STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_items_obj, dict_items);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000536
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200537STATIC mp_obj_t dict_keys(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000538 return dict_view(self_in, MP_DICT_VIEW_KEYS);
539}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200540STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_keys_obj, dict_keys);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000541
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200542STATIC mp_obj_t dict_values(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000543 return dict_view(self_in, MP_DICT_VIEW_VALUES);
544}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200545STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000546
John R. Lenton4bee76e2014-01-10 11:25:03 +0000547/******************************************************************************/
Damien Georgeeae16442014-01-11 19:22:29 +0000548/* dict constructors & public C API */
John R. Lentona41fe312014-01-06 17:17:02 +0000549
Damien Georgecbf76742015-11-27 13:38:15 +0000550STATIC const mp_rom_map_elem_t dict_locals_dict_table[] = {
551 { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&dict_clear_obj) },
552 { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&dict_copy_obj) },
553 { MP_ROM_QSTR(MP_QSTR_fromkeys), MP_ROM_PTR(&dict_fromkeys_obj) },
554 { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&dict_get_obj) },
555 { MP_ROM_QSTR(MP_QSTR_items), MP_ROM_PTR(&dict_items_obj) },
556 { MP_ROM_QSTR(MP_QSTR_keys), MP_ROM_PTR(&dict_keys_obj) },
557 { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&dict_pop_obj) },
558 { MP_ROM_QSTR(MP_QSTR_popitem), MP_ROM_PTR(&dict_popitem_obj) },
559 { MP_ROM_QSTR(MP_QSTR_setdefault), MP_ROM_PTR(&dict_setdefault_obj) },
560 { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&dict_update_obj) },
561 { MP_ROM_QSTR(MP_QSTR_values), MP_ROM_PTR(&dict_values_obj) },
562 { MP_ROM_QSTR(MP_QSTR___getitem__), MP_ROM_PTR(&mp_op_getitem_obj) },
563 { MP_ROM_QSTR(MP_QSTR___setitem__), MP_ROM_PTR(&mp_op_setitem_obj) },
564 { MP_ROM_QSTR(MP_QSTR___delitem__), MP_ROM_PTR(&mp_op_delitem_obj) },
John R. Lentonbaa66542014-01-07 23:18:25 +0000565};
566
Damien George9b196cd2014-03-26 21:47:19 +0000567STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);
568
Damien George3e1a5c12014-03-29 13:43:38 +0000569const mp_obj_type_t mp_type_dict = {
Damien Georgec5966122014-02-15 16:10:44 +0000570 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000571 .name = MP_QSTR_dict,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200572 .print = dict_print,
573 .make_new = dict_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000574 .unary_op = dict_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200575 .binary_op = dict_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100576 .subscr = dict_subscr,
John R. Lentona41fe312014-01-06 17:17:02 +0000577 .getiter = dict_getiter,
Damien George999cedb2015-11-27 17:01:44 +0000578 .locals_dict = (mp_obj_dict_t*)&dict_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000579};
580
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200581#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
Damien Georgecbf76742015-11-27 13:38:15 +0000582STATIC const mp_rom_obj_tuple_t ordereddict_base_tuple = {{&mp_type_tuple}, 1, {MP_ROM_PTR(&mp_type_dict)}};
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200583
584const mp_obj_type_t mp_type_ordereddict = {
585 { &mp_type_type },
586 .name = MP_QSTR_OrderedDict,
587 .print = dict_print,
588 .make_new = dict_make_new,
589 .unary_op = dict_unary_op,
590 .binary_op = dict_binary_op,
591 .subscr = dict_subscr,
592 .getiter = dict_getiter,
Damien George999cedb2015-11-27 17:01:44 +0000593 .bases_tuple = (mp_obj_tuple_t*)(mp_rom_obj_tuple_t*)&ordereddict_base_tuple,
594 .locals_dict = (mp_obj_dict_t*)&dict_locals_dict,
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200595};
596#endif
597
Damien George93965e72014-08-30 13:23:35 +0100598void mp_obj_dict_init(mp_obj_dict_t *dict, mp_uint_t n_args) {
Damien George8b0535e2014-04-05 21:53:54 +0100599 dict->base.type = &mp_type_dict;
600 mp_map_init(&dict->map, n_args);
601}
602
Damien George93965e72014-08-30 13:23:35 +0100603mp_obj_t mp_obj_new_dict(mp_uint_t n_args) {
Damien George999cedb2015-11-27 17:01:44 +0000604 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
Damien George8b0535e2014-04-05 21:53:54 +0100605 mp_obj_dict_init(o, n_args);
Damien George999cedb2015-11-27 17:01:44 +0000606 return MP_OBJ_FROM_PTR(o);
Damiend99b0522013-12-21 18:17:45 +0000607}
Damiendae7eb72013-12-29 22:32:51 +0000608
Damien George93965e72014-08-30 13:23:35 +0100609mp_uint_t mp_obj_dict_len(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000610 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200611 return self->map.used;
Damiendae7eb72013-12-29 22:32:51 +0000612}
613
614mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200615 assert(MP_OBJ_IS_DICT_TYPE(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000616 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damien George38a2da62014-01-08 17:33:12 +0000617 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
Damiendae7eb72013-12-29 22:32:51 +0000618 return self_in;
619}
Damien George062478e2014-01-09 20:57:50 +0000620
Damien George66edc5d2014-04-05 13:25:13 +0100621mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200622 assert(MP_OBJ_IS_DICT_TYPE(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000623 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damien George8a9b9992014-09-17 15:53:03 +0100624 dict_get_helper(&self->map, key, MP_OBJ_NULL, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
Damien George66edc5d2014-04-05 13:25:13 +0100625 return self_in;
626}
627
Damien George062478e2014-01-09 20:57:50 +0000628mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200629 assert(MP_OBJ_IS_DICT_TYPE(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000630 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damien George062478e2014-01-09 20:57:50 +0000631 return &self->map;
632}