blob: 1553a83b46039d971219c8abba50c14559fea59f [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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/runtime.h"
31#include "py/builtin.h"
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020032#include "py/objtype.h"
33
Damien George999cedb2015-11-27 17:01:44 +000034#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 +000035
Damien George4b72b3a2016-01-03 14:21:40 +000036STATIC 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 +000037
Damien George8a9b9992014-09-17 15:53:03 +010038// This is a helper function to iterate through a dictionary. The state of
39// the iteration is held in *cur and should be initialised with zero for the
40// first call. Will return NULL when no more elements are available.
Damien George1ea2f7a2017-02-16 16:15:04 +110041STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, size_t *cur) {
42 size_t max = dict->map.alloc;
Damien George8a9b9992014-09-17 15:53:03 +010043 mp_map_t *map = &dict->map;
44
Damien George1ea2f7a2017-02-16 16:15:04 +110045 for (size_t i = *cur; i < max; i++) {
Damien George8a9b9992014-09-17 15:53:03 +010046 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
47 *cur = i + 1;
48 return &(map->table[i]);
49 }
50 }
51
52 return NULL;
53}
54
Damien George7f9d1d62015-04-09 23:56:15 +010055STATIC 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 +000056 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damiend99b0522013-12-21 18:17:45 +000057 bool first = true;
Damien George612045f2014-09-17 22:56:34 +010058 if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
59 kind = PRINT_REPR;
60 }
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020061 if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict) {
Damien George044c4732015-04-11 13:03:37 +010062 mp_printf(print, "%q(", self->base.type->name);
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020063 }
Damien George7f9d1d62015-04-09 23:56:15 +010064 mp_print_str(print, "{");
Damien George1ea2f7a2017-02-16 16:15:04 +110065 size_t cur = 0;
John R. Lentona41fe312014-01-06 17:17:02 +000066 mp_map_elem_t *next = NULL;
Damien George8a9b9992014-09-17 15:53:03 +010067 while ((next = dict_iter_next(self, &cur)) != NULL) {
John R. Lentona41fe312014-01-06 17:17:02 +000068 if (!first) {
Damien George7f9d1d62015-04-09 23:56:15 +010069 mp_print_str(print, ", ");
Damiend99b0522013-12-21 18:17:45 +000070 }
John R. Lentona41fe312014-01-06 17:17:02 +000071 first = false;
Damien George7f9d1d62015-04-09 23:56:15 +010072 mp_obj_print_helper(print, next->key, kind);
73 mp_print_str(print, ": ");
74 mp_obj_print_helper(print, next->value, kind);
Damiend99b0522013-12-21 18:17:45 +000075 }
Damien George7f9d1d62015-04-09 23:56:15 +010076 mp_print_str(print, "}");
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020077 if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict) {
Damien George7f9d1d62015-04-09 23:56:15 +010078 mp_print_str(print, ")");
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020079 }
Damiend99b0522013-12-21 18:17:45 +000080}
81
Damien George5b3f0b72016-01-03 15:55:55 +000082STATIC 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 +020083 mp_obj_t dict_out = mp_obj_new_dict(0);
Damien George999cedb2015-11-27 17:01:44 +000084 mp_obj_dict_t *dict = MP_OBJ_TO_PTR(dict_out);
Damien George5b3f0b72016-01-03 15:55:55 +000085 dict->base.type = type;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020086 #if MICROPY_PY_COLLECTIONS_ORDEREDDICT
Damien George5b3f0b72016-01-03 15:55:55 +000087 if (type == &mp_type_ordereddict) {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +020088 dict->map.is_ordered = 1;
89 }
90 #endif
Damien Georgebcb6ca42014-06-03 12:53:44 +010091 if (n_args > 0 || n_kw > 0) {
Paul Sokolovsky0090c712015-03-26 12:28:31 +020092 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 +010093 mp_map_t kwargs;
94 mp_map_init_fixed_table(&kwargs, n_kw, args + n_args);
95 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 +010096 }
Paul Sokolovsky0090c712015-03-26 12:28:31 +020097 return dict_out;
Damien George71c51812014-01-04 20:21:15 +000098}
99
Damien George58321dd2017-08-29 13:04:01 +1000100STATIC mp_obj_t dict_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000101 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damien George4e8dc8c2014-01-27 23:15:32 +0000102 switch (op) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300103 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->map.used != 0);
Damien Georgebb4c6f32014-07-31 10:49:14 +0100104 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->map.used);
Paul Sokolovskybfc20922017-08-11 09:42:39 +0300105 #if MICROPY_PY_SYS_GETSIZEOF
106 case MP_UNARY_OP_SIZEOF: {
107 size_t sz = sizeof(*self) + sizeof(*self->map.table) * self->map.alloc;
108 return MP_OBJ_NEW_SMALL_INT(sz);
109 }
110 #endif
Damien George6ac5dce2014-05-21 19:42:43 +0100111 default: return MP_OBJ_NULL; // op not supported
Damien George4e8dc8c2014-01-27 23:15:32 +0000112 }
113}
114
Damien George58321dd2017-08-29 13:04:01 +1000115STATIC mp_obj_t dict_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien George999cedb2015-11-27 17:01:44 +0000116 mp_obj_dict_t *o = MP_OBJ_TO_PTR(lhs_in);
Damiend99b0522013-12-21 18:17:45 +0000117 switch (op) {
Damien George729f7b42014-04-17 22:10:53 +0100118 case MP_BINARY_OP_IN: {
John R. Lentonc1bef212014-01-11 12:39:33 +0000119 mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300120 return mp_obj_new_bool(elem != NULL);
John R. Lentonc1bef212014-01-11 12:39:33 +0000121 }
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300122 case MP_BINARY_OP_EQUAL: {
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200123 #if MICROPY_PY_COLLECTIONS_ORDEREDDICT
124 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 -0700125 // Iterate through both dictionaries simultaneously and compare keys and values.
126 mp_obj_dict_t *rhs = MP_OBJ_TO_PTR(rhs_in);
Damien George1ea2f7a2017-02-16 16:15:04 +1100127 size_t c1 = 0, c2 = 0;
Mark Anthony Palomer31310532016-06-05 18:48:25 -0700128 mp_map_elem_t *e1 = dict_iter_next(o, &c1), *e2 = dict_iter_next(rhs, &c2);
129 for (; e1 != NULL && e2 != NULL; e1 = dict_iter_next(o, &c1), e2 = dict_iter_next(rhs, &c2)) {
130 if (!mp_obj_equal(e1->key, e2->key) || !mp_obj_equal(e1->value, e2->value)) {
131 return mp_const_false;
132 }
133 }
134 return e1 == NULL && e2 == NULL ? mp_const_true : mp_const_false;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200135 } else
136 #endif
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300137 if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_dict)) {
Damien George999cedb2015-11-27 17:01:44 +0000138 mp_obj_dict_t *rhs = MP_OBJ_TO_PTR(rhs_in);
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300139 if (o->map.used != rhs->map.used) {
140 return mp_const_false;
141 }
142
Damien George1ea2f7a2017-02-16 16:15:04 +1100143 size_t cur = 0;
Damien George8a9b9992014-09-17 15:53:03 +0100144 mp_map_elem_t *next = NULL;
145 while ((next = dict_iter_next(o, &cur)) != NULL) {
146 mp_map_elem_t *elem = mp_map_lookup(&rhs->map, next->key, MP_MAP_LOOKUP);
147 if (elem == NULL || !mp_obj_equal(next->value, elem->value)) {
148 return mp_const_false;
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300149 }
150 }
151 return mp_const_true;
Damien Georgee22d76e2014-04-11 10:52:06 +0000152 } else {
153 // dict is not equal to instance of any other type
154 return mp_const_false;
Paul Sokolovsky7cf057a2014-04-06 21:20:52 +0300155 }
156 }
Damiend99b0522013-12-21 18:17:45 +0000157 default:
158 // op not supported
Damien George6ac5dce2014-05-21 19:42:43 +0100159 return MP_OBJ_NULL;
Damiend99b0522013-12-21 18:17:45 +0000160 }
161}
162
Paul Sokolovsky75ce9252014-06-05 20:02:15 +0300163// TODO: Make sure this is inlined in dict_subscr() below.
164mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index) {
Damien George999cedb2015-11-27 17:01:44 +0000165 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky75ce9252014-06-05 20:02:15 +0300166 mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
167 if (elem == NULL) {
Damien George2750a7b2016-10-17 12:00:19 +1100168 nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, index));
Paul Sokolovsky75ce9252014-06-05 20:02:15 +0300169 } else {
170 return elem->value;
171 }
172}
173
Damien George729f7b42014-04-17 22:10:53 +0100174STATIC 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 +0100175 if (value == MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100176 // delete
Damien Georgef4c9b332014-04-08 21:32:29 +0100177 mp_obj_dict_delete(self_in, index);
Damien George729f7b42014-04-17 22:10:53 +0100178 return mp_const_none;
179 } else if (value == MP_OBJ_SENTINEL) {
180 // load
Damien George999cedb2015-11-27 17:01:44 +0000181 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damien George729f7b42014-04-17 22:10:53 +0100182 mp_map_elem_t *elem = mp_map_lookup(&self->map, index, MP_MAP_LOOKUP);
183 if (elem == NULL) {
Damien George2750a7b2016-10-17 12:00:19 +1100184 nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, index));
Damien George729f7b42014-04-17 22:10:53 +0100185 } else {
186 return elem->value;
187 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100188 } else {
Damien George729f7b42014-04-17 22:10:53 +0100189 // store
Damien Georgef4c9b332014-04-08 21:32:29 +0100190 mp_obj_dict_store(self_in, index, value);
Damien George729f7b42014-04-17 22:10:53 +0100191 return mp_const_none;
Damien Georgef4c9b332014-04-08 21:32:29 +0100192 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100193}
John R. Lentona41fe312014-01-06 17:17:02 +0000194
195/******************************************************************************/
196/* dict iterator */
197
198typedef struct _mp_obj_dict_it_t {
199 mp_obj_base_t base;
Damien George8212d972016-01-03 16:27:55 +0000200 mp_fun_1_t iternext;
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200201 mp_obj_t dict;
Damien George1ea2f7a2017-02-16 16:15:04 +1100202 size_t cur;
John R. Lentona41fe312014-01-06 17:17:02 +0000203} mp_obj_dict_it_t;
204
Damien George8a9b9992014-09-17 15:53:03 +0100205STATIC mp_obj_t dict_it_iternext(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000206 mp_obj_dict_it_t *self = MP_OBJ_TO_PTR(self_in);
207 mp_map_elem_t *next = dict_iter_next(MP_OBJ_TO_PTR(self->dict), &self->cur);
John R. Lentona41fe312014-01-06 17:17:02 +0000208
Damien George8a9b9992014-09-17 15:53:03 +0100209 if (next == NULL) {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100210 return MP_OBJ_STOP_ITERATION;
Damien George8a9b9992014-09-17 15:53:03 +0100211 } else {
212 return next->key;
John R. Lentona41fe312014-01-06 17:17:02 +0000213 }
214}
215
Damien Georgeae8d8672016-01-09 23:14:54 +0000216STATIC mp_obj_t dict_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
217 assert(sizeof(mp_obj_dict_it_t) <= sizeof(mp_obj_iter_buf_t));
218 mp_obj_dict_it_t *o = (mp_obj_dict_it_t*)iter_buf;
Damien George8212d972016-01-03 16:27:55 +0000219 o->base.type = &mp_type_polymorph_iter;
220 o->iternext = dict_it_iternext;
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200221 o->dict = self_in;
Damien George8a9b9992014-09-17 15:53:03 +0100222 o->cur = 0;
Damien George999cedb2015-11-27 17:01:44 +0000223 return MP_OBJ_FROM_PTR(o);
John R. Lentona41fe312014-01-06 17:17:02 +0000224}
225
John R. Lentona41fe312014-01-06 17:17:02 +0000226/******************************************************************************/
227/* dict methods */
228
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200229STATIC mp_obj_t dict_clear(mp_obj_t self_in) {
Paul Sokolovsky83e0eba2016-08-12 22:01:11 +0300230 mp_check_self(MP_OBJ_IS_DICT_TYPE(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000231 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000232
233 mp_map_clear(&self->map);
234
235 return mp_const_none;
236}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200237STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_clear_obj, dict_clear);
John R. Lenton7d21d512014-01-06 17:54:04 +0000238
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200239STATIC mp_obj_t dict_copy(mp_obj_t self_in) {
Paul Sokolovsky83e0eba2016-08-12 22:01:11 +0300240 mp_check_self(MP_OBJ_IS_DICT_TYPE(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000241 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200242 mp_obj_t other_out = mp_obj_new_dict(self->map.alloc);
Damien George999cedb2015-11-27 17:01:44 +0000243 mp_obj_dict_t *other = MP_OBJ_TO_PTR(other_out);
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200244 other->base.type = self->base.type;
John R. Lentond90b19e2014-01-06 18:11:20 +0000245 other->map.used = self->map.used;
Paul Sokolovsky5fedd0c2014-04-06 21:00:58 +0300246 other->map.all_keys_are_qstrs = self->map.all_keys_are_qstrs;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200247 other->map.is_fixed = 0;
248 other->map.is_ordered = self->map.is_ordered;
John R. Lentond90b19e2014-01-06 18:11:20 +0000249 memcpy(other->map.table, self->map.table, self->map.alloc * sizeof(mp_map_elem_t));
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200250 return other_out;
John R. Lentond90b19e2014-01-06 18:11:20 +0000251}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200252STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy);
John R. Lenton4ce6cea2014-01-06 17:38:47 +0000253
Damien Georgeeae16442014-01-11 19:22:29 +0000254// this is a classmethod
Damien George4b72b3a2016-01-03 14:21:40 +0000255STATIC mp_obj_t dict_fromkeys(size_t n_args, const mp_obj_t *args) {
Damien Georgee6003f42017-02-13 15:44:31 +1100256 mp_obj_t iter = mp_getiter(args[1], NULL);
Damien Georgeeae16442014-01-11 19:22:29 +0000257 mp_obj_t value = mp_const_none;
Damien George83229d32015-11-20 14:09:20 +0000258 mp_obj_t next = MP_OBJ_NULL;
Damien Georgeeae16442014-01-11 19:22:29 +0000259
260 if (n_args > 2) {
261 value = args[2];
262 }
263
Damien Georgea3edeb92016-10-17 11:58:57 +1100264 // optimisation to allocate result based on len of argument
265 mp_obj_t self_out;
266 mp_obj_t len = mp_obj_len_maybe(args[1]);
Damien Georgeeae16442014-01-11 19:22:29 +0000267 if (len == MP_OBJ_NULL) {
268 /* object's type doesn't have a __len__ slot */
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200269 self_out = mp_obj_new_dict(0);
Damien Georgeeae16442014-01-11 19:22:29 +0000270 } else {
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200271 self_out = mp_obj_new_dict(MP_OBJ_SMALL_INT_VALUE(len));
Damien Georgeeae16442014-01-11 19:22:29 +0000272 }
273
Damien George999cedb2015-11-27 17:01:44 +0000274 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_out);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100275 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien Georgeeae16442014-01-11 19:22:29 +0000276 mp_map_lookup(&self->map, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
277 }
278
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200279 return self_out;
Damien Georgeeae16442014-01-11 19:22:29 +0000280}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200281STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_fromkeys_fun_obj, 2, 3, dict_fromkeys);
Damien Georgecbf76742015-11-27 13:38:15 +0000282STATIC MP_DEFINE_CONST_CLASSMETHOD_OBJ(dict_fromkeys_obj, MP_ROM_PTR(&dict_fromkeys_fun_obj));
Damien Georgeeae16442014-01-11 19:22:29 +0000283
Damien George8b84b8a2017-07-04 23:24:59 +1000284STATIC mp_obj_t dict_get_helper(size_t n_args, const mp_obj_t *args, mp_map_lookup_kind_t lookup_kind) {
285 mp_check_self(MP_OBJ_IS_DICT_TYPE(args[0]));
286 mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]);
287 mp_map_elem_t *elem = mp_map_lookup(&self->map, args[1], lookup_kind);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000288 mp_obj_t value;
Damien George8a9b9992014-09-17 15:53:03 +0100289 if (elem == NULL || elem->value == MP_OBJ_NULL) {
Damien George8b84b8a2017-07-04 23:24:59 +1000290 if (n_args == 2) {
Damien George38a2da62014-01-08 17:33:12 +0000291 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
Damien George8b84b8a2017-07-04 23:24:59 +1000292 nlr_raise(mp_obj_new_exception_arg1(&mp_type_KeyError, args[1]));
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000293 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000294 value = mp_const_none;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000295 }
296 } else {
Damien George8b84b8a2017-07-04 23:24:59 +1000297 value = args[2];
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000298 }
Damien George8a9b9992014-09-17 15:53:03 +0100299 if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
300 elem->value = value;
301 }
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000302 } else {
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000303 value = elem->value;
Damien George8a9b9992014-09-17 15:53:03 +0100304 if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
305 elem->value = MP_OBJ_NULL; // so that GC can collect the deleted value
306 }
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000307 }
308 return value;
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000309}
310
Damien George4b72b3a2016-01-03 14:21:40 +0000311STATIC mp_obj_t dict_get(size_t n_args, const mp_obj_t *args) {
Damien George8b84b8a2017-07-04 23:24:59 +1000312 return dict_get_helper(n_args, args, MP_MAP_LOOKUP);
John R. Lentoncd088732014-01-06 18:53:16 +0000313}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200314STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_get_obj, 2, 3, dict_get);
John R. Lentoncd088732014-01-06 18:53:16 +0000315
Damien George4b72b3a2016-01-03 14:21:40 +0000316STATIC mp_obj_t dict_pop(size_t n_args, const mp_obj_t *args) {
Damien George8b84b8a2017-07-04 23:24:59 +1000317 return dict_get_helper(n_args, args, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000318}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200319STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_pop_obj, 2, 3, dict_pop);
John R. Lenton0fcbaa42014-01-06 19:48:34 +0000320
Damien George4b72b3a2016-01-03 14:21:40 +0000321STATIC mp_obj_t dict_setdefault(size_t n_args, const mp_obj_t *args) {
Damien George8b84b8a2017-07-04 23:24:59 +1000322 return dict_get_helper(n_args, args, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000323}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200324STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(dict_setdefault_obj, 2, 3, dict_setdefault);
John R. Lentonbe8fe5b2014-01-06 20:25:51 +0000325
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200326STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
Paul Sokolovsky83e0eba2016-08-12 22:01:11 +0300327 mp_check_self(MP_OBJ_IS_DICT_TYPE(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000328 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damien George1ea2f7a2017-02-16 16:15:04 +1100329 size_t cur = 0;
Damien George8a9b9992014-09-17 15:53:03 +0100330 mp_map_elem_t *next = dict_iter_next(self, &cur);
331 if (next == NULL) {
Damien George7d0d7212016-10-17 12:17:37 +1100332 mp_raise_msg(&mp_type_KeyError, "popitem(): dictionary is empty");
John R. Lentonf77dce82014-01-06 20:08:52 +0000333 }
John R. Lentonf77dce82014-01-06 20:08:52 +0000334 self->map.used--;
335 mp_obj_t items[] = {next->key, next->value};
Damien George8a9b9992014-09-17 15:53:03 +0100336 next->key = MP_OBJ_SENTINEL; // must mark key as sentinel to indicate that it was deleted
337 next->value = MP_OBJ_NULL;
John R. Lentonf77dce82014-01-06 20:08:52 +0000338 mp_obj_t tuple = mp_obj_new_tuple(2, items);
339
340 return tuple;
341}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200342STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_popitem_obj, dict_popitem);
John R. Lentonf77dce82014-01-06 20:08:52 +0000343
Damien George4b72b3a2016-01-03 14:21:40 +0000344STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
Paul Sokolovsky83e0eba2016-08-12 22:01:11 +0300345 mp_check_self(MP_OBJ_IS_DICT_TYPE(args[0]));
Damien George999cedb2015-11-27 17:01:44 +0000346 mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]);
Damien Georgebcb6ca42014-06-03 12:53:44 +0100347
348 mp_arg_check_num(n_args, kwargs->used, 1, 2, true);
349
350 if (n_args == 2) {
351 // given a positional argument
352
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200353 if (MP_OBJ_IS_DICT_TYPE(args[1])) {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100354 // update from other dictionary (make sure other is not self)
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200355 if (args[1] != args[0]) {
Damien George1ea2f7a2017-02-16 16:15:04 +1100356 size_t cur = 0;
Damien Georgebcb6ca42014-06-03 12:53:44 +0100357 mp_map_elem_t *elem = NULL;
Damien George999cedb2015-11-27 17:01:44 +0000358 while ((elem = dict_iter_next((mp_obj_dict_t*)MP_OBJ_TO_PTR(args[1]), &cur)) != NULL) {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100359 mp_map_lookup(&self->map, elem->key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = elem->value;
360 }
361 }
John R. Lenton88f30432014-01-06 22:58:17 +0000362 } else {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100363 // update from a generic iterable of pairs
Damien Georgee6003f42017-02-13 15:44:31 +1100364 mp_obj_t iter = mp_getiter(args[1], NULL);
Damien George83229d32015-11-20 14:09:20 +0000365 mp_obj_t next = MP_OBJ_NULL;
Damien Georgebcb6ca42014-06-03 12:53:44 +0100366 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien Georgee6003f42017-02-13 15:44:31 +1100367 mp_obj_t inneriter = mp_getiter(next, NULL);
Damien Georgebcb6ca42014-06-03 12:53:44 +0100368 mp_obj_t key = mp_iternext(inneriter);
369 mp_obj_t value = mp_iternext(inneriter);
370 mp_obj_t stop = mp_iternext(inneriter);
371 if (key == MP_OBJ_STOP_ITERATION
372 || value == MP_OBJ_STOP_ITERATION
373 || stop != MP_OBJ_STOP_ITERATION) {
Damien George18c059f2017-03-29 12:36:46 +1100374 mp_raise_ValueError("dict update sequence has wrong length");
Damien Georgebcb6ca42014-06-03 12:53:44 +0100375 } else {
376 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
377 }
378 }
379 }
380 }
381
382 // update the dict with any keyword args
Damien George1ea2f7a2017-02-16 16:15:04 +1100383 for (size_t i = 0; i < kwargs->alloc; i++) {
Damien Georgebcb6ca42014-06-03 12:53:44 +0100384 if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
385 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 +0000386 }
387 }
388
389 return mp_const_none;
390}
Damien Georgebcb6ca42014-06-03 12:53:44 +0100391STATIC MP_DEFINE_CONST_FUN_OBJ_KW(dict_update_obj, 1, dict_update);
John R. Lenton88f30432014-01-06 22:58:17 +0000392
John R. Lentonf77dce82014-01-06 20:08:52 +0000393
John R. Lentona41fe312014-01-06 17:17:02 +0000394/******************************************************************************/
John R. Lenton9ec3a872014-01-10 01:00:20 +0000395/* dict views */
396
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200397STATIC const mp_obj_type_t dict_view_type;
398STATIC const mp_obj_type_t dict_view_it_type;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000399
400typedef enum _mp_dict_view_kind_t {
401 MP_DICT_VIEW_ITEMS,
402 MP_DICT_VIEW_KEYS,
403 MP_DICT_VIEW_VALUES,
404} mp_dict_view_kind_t;
405
Damien George3ff16ff2016-05-20 12:38:15 +0100406STATIC const char *const mp_dict_view_names[] = {"dict_items", "dict_keys", "dict_values"};
John R. Lenton9ec3a872014-01-10 01:00:20 +0000407
408typedef struct _mp_obj_dict_view_it_t {
409 mp_obj_base_t base;
410 mp_dict_view_kind_t kind;
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200411 mp_obj_t dict;
Damien George1ea2f7a2017-02-16 16:15:04 +1100412 size_t cur;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000413} mp_obj_dict_view_it_t;
414
415typedef struct _mp_obj_dict_view_t {
416 mp_obj_base_t base;
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200417 mp_obj_t dict;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000418 mp_dict_view_kind_t kind;
419} mp_obj_dict_view_t;
420
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200421STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
Paul Sokolovsky83e0eba2016-08-12 22:01:11 +0300422 mp_check_self(MP_OBJ_IS_TYPE(self_in, &dict_view_it_type));
Damien George999cedb2015-11-27 17:01:44 +0000423 mp_obj_dict_view_it_t *self = MP_OBJ_TO_PTR(self_in);
424 mp_map_elem_t *next = dict_iter_next(MP_OBJ_TO_PTR(self->dict), &self->cur);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000425
Damien George8a9b9992014-09-17 15:53:03 +0100426 if (next == NULL) {
427 return MP_OBJ_STOP_ITERATION;
428 } else {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000429 switch (self->kind) {
Damien Georgeeae16442014-01-11 19:22:29 +0000430 case MP_DICT_VIEW_ITEMS:
Damien Georged2d64f02015-01-14 21:32:42 +0000431 default: {
Damien Georgeeae16442014-01-11 19:22:29 +0000432 mp_obj_t items[] = {next->key, next->value};
433 return mp_obj_new_tuple(2, items);
434 }
435 case MP_DICT_VIEW_KEYS:
436 return next->key;
437 case MP_DICT_VIEW_VALUES:
438 return next->value;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000439 }
John R. Lenton9ec3a872014-01-10 01:00:20 +0000440 }
441}
442
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200443STATIC const mp_obj_type_t dict_view_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000444 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000445 .name = MP_QSTR_iterator,
Damien Georgeae8d8672016-01-09 23:14:54 +0000446 .getiter = mp_identity_getiter,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000447 .iternext = dict_view_it_iternext,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000448};
449
Damien Georgeae8d8672016-01-09 23:14:54 +0000450STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in, mp_obj_iter_buf_t *iter_buf) {
451 assert(sizeof(mp_obj_dict_view_it_t) <= sizeof(mp_obj_iter_buf_t));
Paul Sokolovsky83e0eba2016-08-12 22:01:11 +0300452 mp_check_self(MP_OBJ_IS_TYPE(view_in, &dict_view_type));
Damien George999cedb2015-11-27 17:01:44 +0000453 mp_obj_dict_view_t *view = MP_OBJ_TO_PTR(view_in);
Damien Georgeae8d8672016-01-09 23:14:54 +0000454 mp_obj_dict_view_it_t *o = (mp_obj_dict_view_it_t*)iter_buf;
John R. Lenton9ec3a872014-01-10 01:00:20 +0000455 o->base.type = &dict_view_it_type;
456 o->kind = view->kind;
Damien George8a9b9992014-09-17 15:53:03 +0100457 o->dict = view->dict;
458 o->cur = 0;
Damien George999cedb2015-11-27 17:01:44 +0000459 return MP_OBJ_FROM_PTR(o);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000460}
461
Damien George7f9d1d62015-04-09 23:56:15 +0100462STATIC 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 +0000463 (void)kind;
Paul Sokolovsky83e0eba2016-08-12 22:01:11 +0300464 mp_check_self(MP_OBJ_IS_TYPE(self_in, &dict_view_type));
Damien George999cedb2015-11-27 17:01:44 +0000465 mp_obj_dict_view_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000466 bool first = true;
Damien George7f9d1d62015-04-09 23:56:15 +0100467 mp_print_str(print, mp_dict_view_names[self->kind]);
468 mp_print_str(print, "([");
Damien Georgeae8d8672016-01-09 23:14:54 +0000469 mp_obj_iter_buf_t iter_buf;
470 mp_obj_t self_iter = dict_view_getiter(self_in, &iter_buf);
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200471 mp_obj_t next = MP_OBJ_NULL;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100472 while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000473 if (!first) {
Damien George7f9d1d62015-04-09 23:56:15 +0100474 mp_print_str(print, ", ");
John R. Lenton9ec3a872014-01-10 01:00:20 +0000475 }
476 first = false;
Damien George7f9d1d62015-04-09 23:56:15 +0100477 mp_obj_print_helper(print, next, PRINT_REPR);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000478 }
Damien George7f9d1d62015-04-09 23:56:15 +0100479 mp_print_str(print, "])");
John R. Lenton9ec3a872014-01-10 01:00:20 +0000480}
481
Damien George58321dd2017-08-29 13:04:01 +1000482STATIC mp_obj_t dict_view_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien George6ac5dce2014-05-21 19:42:43 +0100483 // only supported for the 'keys' kind until sets and dicts are refactored
Damien George999cedb2015-11-27 17:01:44 +0000484 mp_obj_dict_view_t *o = MP_OBJ_TO_PTR(lhs_in);
Damien Georged0a5bf32014-05-10 13:55:11 +0100485 if (o->kind != MP_DICT_VIEW_KEYS) {
Damien George6ac5dce2014-05-21 19:42:43 +0100486 return MP_OBJ_NULL; // op not supported
Damien Georged0a5bf32014-05-10 13:55:11 +0100487 }
488 if (op != MP_BINARY_OP_IN) {
Damien George6ac5dce2014-05-21 19:42:43 +0100489 return MP_OBJ_NULL; // op not supported
Damien Georged0a5bf32014-05-10 13:55:11 +0100490 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000491 return dict_binary_op(op, o->dict, rhs_in);
492}
493
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200494STATIC const mp_obj_type_t dict_view_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000495 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000496 .name = MP_QSTR_dict_view,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000497 .print = dict_view_print,
John R. Lentonc1bef212014-01-11 12:39:33 +0000498 .binary_op = dict_view_binary_op,
John R. Lenton9ec3a872014-01-10 01:00:20 +0000499 .getiter = dict_view_getiter,
500};
501
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200502STATIC 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 +0000503 mp_obj_dict_view_t *o = m_new_obj(mp_obj_dict_view_t);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000504 o->base.type = &dict_view_type;
505 o->dict = dict;
506 o->kind = kind;
Damien George999cedb2015-11-27 17:01:44 +0000507 return MP_OBJ_FROM_PTR(o);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000508}
509
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200510STATIC mp_obj_t dict_view(mp_obj_t self_in, mp_dict_view_kind_t kind) {
Paul Sokolovsky83e0eba2016-08-12 22:01:11 +0300511 mp_check_self(MP_OBJ_IS_DICT_TYPE(self_in));
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200512 return mp_obj_new_dict_view(self_in, kind);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000513}
514
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200515STATIC mp_obj_t dict_items(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000516 return dict_view(self_in, MP_DICT_VIEW_ITEMS);
517}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200518STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_items_obj, dict_items);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000519
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200520STATIC mp_obj_t dict_keys(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000521 return dict_view(self_in, MP_DICT_VIEW_KEYS);
522}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200523STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_keys_obj, dict_keys);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000524
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200525STATIC mp_obj_t dict_values(mp_obj_t self_in) {
John R. Lenton9ec3a872014-01-10 01:00:20 +0000526 return dict_view(self_in, MP_DICT_VIEW_VALUES);
527}
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200528STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_values_obj, dict_values);
John R. Lenton9ec3a872014-01-10 01:00:20 +0000529
John R. Lenton4bee76e2014-01-10 11:25:03 +0000530/******************************************************************************/
Damien Georgeeae16442014-01-11 19:22:29 +0000531/* dict constructors & public C API */
John R. Lentona41fe312014-01-06 17:17:02 +0000532
Damien Georgecbf76742015-11-27 13:38:15 +0000533STATIC const mp_rom_map_elem_t dict_locals_dict_table[] = {
534 { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&dict_clear_obj) },
535 { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&dict_copy_obj) },
536 { MP_ROM_QSTR(MP_QSTR_fromkeys), MP_ROM_PTR(&dict_fromkeys_obj) },
537 { MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&dict_get_obj) },
538 { MP_ROM_QSTR(MP_QSTR_items), MP_ROM_PTR(&dict_items_obj) },
539 { MP_ROM_QSTR(MP_QSTR_keys), MP_ROM_PTR(&dict_keys_obj) },
540 { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&dict_pop_obj) },
541 { MP_ROM_QSTR(MP_QSTR_popitem), MP_ROM_PTR(&dict_popitem_obj) },
542 { MP_ROM_QSTR(MP_QSTR_setdefault), MP_ROM_PTR(&dict_setdefault_obj) },
543 { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&dict_update_obj) },
544 { MP_ROM_QSTR(MP_QSTR_values), MP_ROM_PTR(&dict_values_obj) },
545 { MP_ROM_QSTR(MP_QSTR___getitem__), MP_ROM_PTR(&mp_op_getitem_obj) },
546 { MP_ROM_QSTR(MP_QSTR___setitem__), MP_ROM_PTR(&mp_op_setitem_obj) },
547 { MP_ROM_QSTR(MP_QSTR___delitem__), MP_ROM_PTR(&mp_op_delitem_obj) },
John R. Lentonbaa66542014-01-07 23:18:25 +0000548};
549
Damien George9b196cd2014-03-26 21:47:19 +0000550STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);
551
Damien George3e1a5c12014-03-29 13:43:38 +0000552const mp_obj_type_t mp_type_dict = {
Damien Georgec5966122014-02-15 16:10:44 +0000553 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000554 .name = MP_QSTR_dict,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200555 .print = dict_print,
556 .make_new = dict_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000557 .unary_op = dict_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200558 .binary_op = dict_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100559 .subscr = dict_subscr,
John R. Lentona41fe312014-01-06 17:17:02 +0000560 .getiter = dict_getiter,
Damien George999cedb2015-11-27 17:01:44 +0000561 .locals_dict = (mp_obj_dict_t*)&dict_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000562};
563
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200564#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200565const mp_obj_type_t mp_type_ordereddict = {
566 { &mp_type_type },
567 .name = MP_QSTR_OrderedDict,
568 .print = dict_print,
569 .make_new = dict_make_new,
570 .unary_op = dict_unary_op,
571 .binary_op = dict_binary_op,
572 .subscr = dict_subscr,
573 .getiter = dict_getiter,
Damien George816413e2017-04-06 12:09:01 +1000574 .parent = &mp_type_dict,
Damien George999cedb2015-11-27 17:01:44 +0000575 .locals_dict = (mp_obj_dict_t*)&dict_locals_dict,
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200576};
577#endif
578
Damien George1ea2f7a2017-02-16 16:15:04 +1100579void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args) {
Damien George8b0535e2014-04-05 21:53:54 +0100580 dict->base.type = &mp_type_dict;
581 mp_map_init(&dict->map, n_args);
582}
583
Damien George1ea2f7a2017-02-16 16:15:04 +1100584mp_obj_t mp_obj_new_dict(size_t n_args) {
Damien George999cedb2015-11-27 17:01:44 +0000585 mp_obj_dict_t *o = m_new_obj(mp_obj_dict_t);
Damien George8b0535e2014-04-05 21:53:54 +0100586 mp_obj_dict_init(o, n_args);
Damien George999cedb2015-11-27 17:01:44 +0000587 return MP_OBJ_FROM_PTR(o);
Damiend99b0522013-12-21 18:17:45 +0000588}
Damiendae7eb72013-12-29 22:32:51 +0000589
Damien George1ea2f7a2017-02-16 16:15:04 +1100590size_t mp_obj_dict_len(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000591 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovsky0090c712015-03-26 12:28:31 +0200592 return self->map.used;
Damiendae7eb72013-12-29 22:32:51 +0000593}
594
595mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value) {
Paul Sokolovsky83e0eba2016-08-12 22:01:11 +0300596 mp_check_self(MP_OBJ_IS_DICT_TYPE(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000597 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damien George38a2da62014-01-08 17:33:12 +0000598 mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
Damiendae7eb72013-12-29 22:32:51 +0000599 return self_in;
600}
Damien George062478e2014-01-09 20:57:50 +0000601
Damien George66edc5d2014-04-05 13:25:13 +0100602mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key) {
Damien George8b84b8a2017-07-04 23:24:59 +1000603 mp_obj_t args[2] = {self_in, key};
604 dict_get_helper(2, args, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
Damien George66edc5d2014-04-05 13:25:13 +0100605 return self_in;
606}
607
Damien George062478e2014-01-09 20:57:50 +0000608mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in) {
Paul Sokolovsky83e0eba2016-08-12 22:01:11 +0300609 mp_check_self(MP_OBJ_IS_DICT_TYPE(self_in));
Damien George999cedb2015-11-27 17:01:44 +0000610 mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
Damien George062478e2014-01-09 20:57:50 +0000611 return &self->map;
612}