blob: ba7898415db854c0dfdf9a3f5d8d8d382fde01a6 [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/objlist.h"
32#include "py/runtime0.h"
33#include "py/runtime.h"
Damien George744e7672015-02-02 15:14:22 +000034#include "py/stackctrl.h"
Damiend99b0522013-12-21 18:17:45 +000035
Damien Georgeae8d8672016-01-09 23:14:54 +000036STATIC mp_obj_t mp_obj_new_list_iterator(mp_obj_t list, size_t cur, mp_obj_iter_buf_t *iter_buf);
Damien George58d9eeb2017-02-16 16:12:41 +110037STATIC mp_obj_list_t *list_new(size_t n);
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020038STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in);
Damien George4b72b3a2016-01-03 14:21:40 +000039STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args);
Damiend99b0522013-12-21 18:17:45 +000040
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +020041// TODO: Move to mpconfig.h
42#define LIST_MIN_ALLOC 4
43
Damiend99b0522013-12-21 18:17:45 +000044/******************************************************************************/
45/* list */
46
Damien George7f9d1d62015-04-09 23:56:15 +010047STATIC void list_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
Damien George999cedb2015-11-27 17:01:44 +000048 mp_obj_list_t *o = MP_OBJ_TO_PTR(o_in);
Damien George612045f2014-09-17 22:56:34 +010049 if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
50 kind = PRINT_REPR;
51 }
Damien George7f9d1d62015-04-09 23:56:15 +010052 mp_print_str(print, "[");
Damien George58d9eeb2017-02-16 16:12:41 +110053 for (size_t i = 0; i < o->len; i++) {
Damiend99b0522013-12-21 18:17:45 +000054 if (i > 0) {
Damien George7f9d1d62015-04-09 23:56:15 +010055 mp_print_str(print, ", ");
Damiend99b0522013-12-21 18:17:45 +000056 }
Damien George7f9d1d62015-04-09 23:56:15 +010057 mp_obj_print_helper(print, o->items[i], kind);
Damiend99b0522013-12-21 18:17:45 +000058 }
Damien George7f9d1d62015-04-09 23:56:15 +010059 mp_print_str(print, "]");
Damiend99b0522013-12-21 18:17:45 +000060}
61
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +030062STATIC mp_obj_t list_extend_from_iter(mp_obj_t list, mp_obj_t iterable) {
Damien Georgee6003f42017-02-13 15:44:31 +110063 mp_obj_t iter = mp_getiter(iterable, NULL);
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +030064 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +010065 while ((item = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +030066 mp_obj_list_append(list, item);
67 }
68 return list;
69}
70
Damien George5b3f0b72016-01-03 15:55:55 +000071STATIC mp_obj_t list_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000072 (void)type_in;
Damien Georgeee7a8802014-05-11 18:37:21 +010073 mp_arg_check_num(n_args, n_kw, 0, 1, false);
Damien George20006db2014-01-18 14:10:48 +000074
Damien George71c51812014-01-04 20:21:15 +000075 switch (n_args) {
76 case 0:
77 // return a new, empty list
Damien Georgeebde0b82014-01-19 00:47:40 +000078 return mp_obj_new_list(0, NULL);
Damien George71c51812014-01-04 20:21:15 +000079
80 case 1:
Damien Georgeee7a8802014-05-11 18:37:21 +010081 default: {
Damien George71c51812014-01-04 20:21:15 +000082 // make list from iterable
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +030083 // TODO: optimize list/tuple
Damien Georgeebde0b82014-01-19 00:47:40 +000084 mp_obj_t list = mp_obj_new_list(0, NULL);
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +030085 return list_extend_from_iter(list, args[0]);
Damien George71c51812014-01-04 20:21:15 +000086 }
Damien George71c51812014-01-04 20:21:15 +000087 }
88}
89
Damien Georged17926d2014-03-30 13:35:08 +010090// Don't pass MP_BINARY_OP_NOT_EQUAL here
Damien Georgeecc88e92014-08-30 00:35:11 +010091STATIC bool list_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t another_in) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +030092 mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Damien George3e1a5c12014-03-29 13:43:38 +000093 if (!MP_OBJ_IS_TYPE(another_in, &mp_type_list)) {
Paul Sokolovsky1945e602014-01-12 02:01:00 +020094 return false;
95 }
Damien George999cedb2015-11-27 17:01:44 +000096 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
97 mp_obj_list_t *another = MP_OBJ_TO_PTR(another_in);
Paul Sokolovsky1945e602014-01-12 02:01:00 +020098
Paul Sokolovsky1a996c42014-02-08 22:49:46 +020099 return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len);
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200100}
101
Damien Georgeecc88e92014-08-30 00:35:11 +0100102STATIC mp_obj_t list_unary_op(mp_uint_t op, mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000103 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
Damien George4e8dc8c2014-01-27 23:15:32 +0000104 switch (op) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300105 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->len != 0);
Damien Georged17926d2014-03-30 13:35:08 +0100106 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
Damien George6ac5dce2014-05-21 19:42:43 +0100107 default: return MP_OBJ_NULL; // op not supported
Damien George4e8dc8c2014-01-27 23:15:32 +0000108 }
109}
110
Damien Georgeecc88e92014-08-30 00:35:11 +0100111STATIC mp_obj_t list_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) {
Damien George999cedb2015-11-27 17:01:44 +0000112 mp_obj_list_t *o = MP_OBJ_TO_PTR(lhs);
Damiend99b0522013-12-21 18:17:45 +0000113 switch (op) {
Damien George729f7b42014-04-17 22:10:53 +0100114 case MP_BINARY_OP_ADD: {
Damien George3e1a5c12014-03-29 13:43:38 +0000115 if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
Damien George6ac5dce2014-05-21 19:42:43 +0100116 return MP_OBJ_NULL; // op not supported
John R. Lenton81ad89c2014-01-03 02:32:40 +0000117 }
Damien George999cedb2015-11-27 17:01:44 +0000118 mp_obj_list_t *p = MP_OBJ_TO_PTR(rhs);
John R. Lenton81ad89c2014-01-03 02:32:40 +0000119 mp_obj_list_t *s = list_new(o->len + p->len);
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300120 mp_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
Damien George999cedb2015-11-27 17:01:44 +0000121 return MP_OBJ_FROM_PTR(s);
John R. Lenton4cb80582014-01-03 02:27:08 +0000122 }
Damien George729f7b42014-04-17 22:10:53 +0100123 case MP_BINARY_OP_INPLACE_ADD: {
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200124 list_extend(lhs, rhs);
Paul Sokolovsky3d598252015-03-25 09:25:41 +0200125 return lhs;
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200126 }
Damien George8270e382014-04-03 11:00:54 +0000127 case MP_BINARY_OP_MULTIPLY: {
Damien George40f3c022014-07-03 13:25:24 +0100128 mp_int_t n;
Damien George8270e382014-04-03 11:00:54 +0000129 if (!mp_obj_get_int_maybe(rhs, &n)) {
Damien George6ac5dce2014-05-21 19:42:43 +0100130 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200131 }
Damien George9b7a8ee2014-08-13 13:22:24 +0100132 if (n < 0) {
133 n = 0;
134 }
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200135 mp_obj_list_t *s = list_new(o->len * n);
136 mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
Damien George999cedb2015-11-27 17:01:44 +0000137 return MP_OBJ_FROM_PTR(s);
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200138 }
Damien Georged17926d2014-03-30 13:35:08 +0100139 case MP_BINARY_OP_EQUAL:
140 case MP_BINARY_OP_LESS:
141 case MP_BINARY_OP_LESS_EQUAL:
142 case MP_BINARY_OP_MORE:
143 case MP_BINARY_OP_MORE_EQUAL:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300144 return mp_obj_new_bool(list_cmp_helper(op, lhs, rhs));
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200145
Damiend99b0522013-12-21 18:17:45 +0000146 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100147 return MP_OBJ_NULL; // op not supported
Damiend99b0522013-12-21 18:17:45 +0000148 }
149}
150
Damien George729f7b42014-04-17 22:10:53 +0100151STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
Damien Georgef4c9b332014-04-08 21:32:29 +0100152 if (value == MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100153 // delete
Damien Georgefb510b32014-06-01 13:32:54 +0100154#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky3f8d34c2014-05-10 23:03:30 +0300155 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
Damien George999cedb2015-11-27 17:01:44 +0000156 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300157 mp_bound_slice_t slice;
158 if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300159 mp_not_implemented("");
Paul Sokolovsky3f8d34c2014-05-10 23:03:30 +0300160 }
161
Damien George42f3de92014-10-03 17:44:14 +0000162 mp_int_t len_adj = slice.start - slice.stop;
Paul Sokolovsky3f8d34c2014-05-10 23:03:30 +0300163 //printf("Len adj: %d\n", len_adj);
164 assert(len_adj <= 0);
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200165 mp_seq_replace_slice_no_grow(self->items, self->len, slice.start, slice.stop, self->items/*NULL*/, 0, sizeof(*self->items));
Paul Sokolovsky3f8d34c2014-05-10 23:03:30 +0300166 // Clear "freed" elements at the end of list
167 mp_seq_clear(self->items, self->len + len_adj, self->len, sizeof(*self->items));
168 self->len += len_adj;
169 return mp_const_none;
170 }
171#endif
Damien Georgef4c9b332014-04-08 21:32:29 +0100172 mp_obj_t args[2] = {self_in, index};
173 list_pop(2, args);
Damien George729f7b42014-04-17 22:10:53 +0100174 return mp_const_none;
175 } else if (value == MP_OBJ_SENTINEL) {
176 // load
Damien George999cedb2015-11-27 17:01:44 +0000177 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georgefb510b32014-06-01 13:32:54 +0100178#if MICROPY_PY_BUILTINS_SLICE
Damien George729f7b42014-04-17 22:10:53 +0100179 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300180 mp_bound_slice_t slice;
181 if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300182 return mp_seq_extract_slice(self->len, self->items, &slice);
Damien George729f7b42014-04-17 22:10:53 +0100183 }
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300184 mp_obj_list_t *res = list_new(slice.stop - slice.start);
185 mp_seq_copy(res->items, self->items + slice.start, res->len, mp_obj_t);
Damien George999cedb2015-11-27 17:01:44 +0000186 return MP_OBJ_FROM_PTR(res);
Damien George729f7b42014-04-17 22:10:53 +0100187 }
188#endif
Damien George9c4cbe22014-08-30 14:04:14 +0100189 mp_uint_t index_val = mp_get_index(self->base.type, self->len, index, false);
Damien George729f7b42014-04-17 22:10:53 +0100190 return self->items[index_val];
Damien Georgef4c9b332014-04-08 21:32:29 +0100191 } else {
Damien Georgefb510b32014-06-01 13:32:54 +0100192#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky94d82462014-05-10 22:23:00 +0300193 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
Damien George999cedb2015-11-27 17:01:44 +0000194 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
Damien George89267882017-02-20 15:09:59 +1100195 mp_uint_t value_len; mp_obj_t *value_items;
196 mp_obj_get_array(value, &value_len, &value_items);
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300197 mp_bound_slice_t slice_out;
198 if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice_out)) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300199 mp_not_implemented("");
Paul Sokolovsky94d82462014-05-10 22:23:00 +0300200 }
Damien George89267882017-02-20 15:09:59 +1100201 mp_int_t len_adj = value_len - (slice_out.stop - slice_out.start);
Paul Sokolovsky94d82462014-05-10 22:23:00 +0300202 //printf("Len adj: %d\n", len_adj);
Paul Sokolovsky2705f4c2014-05-25 02:36:12 +0300203 if (len_adj > 0) {
204 if (self->len + len_adj > self->alloc) {
205 // TODO: Might optimize memory copies here by checking if block can
206 // be grown inplace or not
207 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + len_adj);
208 self->alloc = self->len + len_adj;
209 }
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300210 mp_seq_replace_slice_grow_inplace(self->items, self->len,
Damien George89267882017-02-20 15:09:59 +1100211 slice_out.start, slice_out.stop, value_items, value_len, len_adj, sizeof(*self->items));
Paul Sokolovsky2705f4c2014-05-25 02:36:12 +0300212 } else {
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300213 mp_seq_replace_slice_no_grow(self->items, self->len,
Damien George89267882017-02-20 15:09:59 +1100214 slice_out.start, slice_out.stop, value_items, value_len, sizeof(*self->items));
Paul Sokolovsky2705f4c2014-05-25 02:36:12 +0300215 // Clear "freed" elements at the end of list
216 mp_seq_clear(self->items, self->len + len_adj, self->len, sizeof(*self->items));
217 // TODO: apply allocation policy re: alloc_size
218 }
Paul Sokolovsky94d82462014-05-10 22:23:00 +0300219 self->len += len_adj;
Paul Sokolovsky94d82462014-05-10 22:23:00 +0300220 return mp_const_none;
221 }
222#endif
Damien Georgef4c9b332014-04-08 21:32:29 +0100223 mp_obj_list_store(self_in, index, value);
Damien George729f7b42014-04-17 22:10:53 +0100224 return mp_const_none;
Damien Georgef4c9b332014-04-08 21:32:29 +0100225 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100226}
227
Damien Georgeae8d8672016-01-09 23:14:54 +0000228STATIC mp_obj_t list_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
229 return mp_obj_new_list_iterator(o_in, 0, iter_buf);
Damiend99b0522013-12-21 18:17:45 +0000230}
231
232mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300233 mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Damien George999cedb2015-11-27 17:01:44 +0000234 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
Damiend99b0522013-12-21 18:17:45 +0000235 if (self->len >= self->alloc) {
Damien732407f2013-12-29 19:33:23 +0000236 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
Damiend99b0522013-12-21 18:17:45 +0000237 self->alloc *= 2;
Paul Sokolovskya2240672014-04-28 00:16:57 +0300238 mp_seq_clear(self->items, self->len + 1, self->alloc, sizeof(*self->items));
Damiend99b0522013-12-21 18:17:45 +0000239 }
240 self->items[self->len++] = arg;
241 return mp_const_none; // return None, as per CPython
242}
243
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200244STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300245 mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +0300246 if (MP_OBJ_IS_TYPE(arg_in, &mp_type_list)) {
Damien George999cedb2015-11-27 17:01:44 +0000247 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
248 mp_obj_list_t *arg = MP_OBJ_TO_PTR(arg_in);
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200249
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +0300250 if (self->len + arg->len > self->alloc) {
251 // TODO: use alloc policy for "4"
252 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + arg->len + 4);
253 self->alloc = self->len + arg->len + 4;
Paul Sokolovskya2240672014-04-28 00:16:57 +0300254 mp_seq_clear(self->items, self->len + arg->len, self->alloc, sizeof(*self->items));
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +0300255 }
256
257 memcpy(self->items + self->len, arg->items, sizeof(mp_obj_t) * arg->len);
258 self->len += arg->len;
259 } else {
260 list_extend_from_iter(self_in, arg_in);
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200261 }
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200262 return mp_const_none; // return None, as per CPython
263}
264
Damien George4b72b3a2016-01-03 14:21:40 +0000265STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300266 mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
Damien George999cedb2015-11-27 17:01:44 +0000267 mp_obj_list_t *self = MP_OBJ_TO_PTR(args[0]);
John R. Lenton25f417c2014-01-03 22:53:18 +0000268 if (self->len == 0) {
Damien George7d0d7212016-10-17 12:17:37 +1100269 mp_raise_msg(&mp_type_IndexError, "pop from empty list");
John R. Lenton25f417c2014-01-03 22:53:18 +0000270 }
Damien George9c4cbe22014-08-30 14:04:14 +0100271 mp_uint_t index = mp_get_index(self->base.type, self->len, n_args == 1 ? MP_OBJ_NEW_SMALL_INT(-1) : args[1], false);
Damiend99b0522013-12-21 18:17:45 +0000272 mp_obj_t ret = self->items[index];
273 self->len -= 1;
Damien George17ae2392014-08-29 21:07:54 +0100274 memmove(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300275 // Clear stale pointer from slot which just got freed to prevent GC issues
276 self->items[self->len] = MP_OBJ_NULL;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200277 if (self->alloc > LIST_MIN_ALLOC && self->alloc > 2 * self->len) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000278 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
279 self->alloc /= 2;
280 }
Damiend99b0522013-12-21 18:17:45 +0000281 return ret;
282}
283
Damien George744e7672015-02-02 15:14:22 +0000284STATIC void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, mp_obj_t binop_less_result) {
285 MP_STACK_CHECK();
Damiend99b0522013-12-21 18:17:45 +0000286 while (head < tail) {
287 mp_obj_t *h = head - 1;
288 mp_obj_t *t = tail;
Damien George744e7672015-02-02 15:14:22 +0000289 mp_obj_t v = key_fn == MP_OBJ_NULL ? tail[0] : mp_call_function_1(key_fn, tail[0]); // get pivot using key_fn
Damiend99b0522013-12-21 18:17:45 +0000290 for (;;) {
Damien George744e7672015-02-02 15:14:22 +0000291 do ++h; while (h < t && mp_binary_op(MP_BINARY_OP_LESS, key_fn == MP_OBJ_NULL ? h[0] : mp_call_function_1(key_fn, h[0]), v) == binop_less_result);
292 do --t; while (h < t && mp_binary_op(MP_BINARY_OP_LESS, v, key_fn == MP_OBJ_NULL ? t[0] : mp_call_function_1(key_fn, t[0])) == binop_less_result);
Damiend99b0522013-12-21 18:17:45 +0000293 if (h >= t) break;
294 mp_obj_t x = h[0];
295 h[0] = t[0];
296 t[0] = x;
297 }
298 mp_obj_t x = h[0];
299 h[0] = tail[0];
300 tail[0] = x;
Damien George744e7672015-02-02 15:14:22 +0000301 // do the smaller recursive call first, to keep stack within O(log(N))
302 if (t - head < tail - h - 1) {
303 mp_quicksort(head, t, key_fn, binop_less_result);
304 head = h + 1;
305 } else {
306 mp_quicksort(h + 1, tail, key_fn, binop_less_result);
307 tail = t;
308 }
Damiend99b0522013-12-21 18:17:45 +0000309 }
310}
311
Damien George744e7672015-02-02 15:14:22 +0000312// TODO Python defines sort to be stable but ours is not
Damien George4b72b3a2016-01-03 14:21:40 +0000313mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
Damien George744e7672015-02-02 15:14:22 +0000314 static const mp_arg_t allowed_args[] = {
Damien Georgecbf76742015-11-27 13:38:15 +0000315 { MP_QSTR_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },
Damien George744e7672015-02-02 15:14:22 +0000316 { MP_QSTR_reverse, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
317 };
318
319 // parse args
Damien George22d85ec2016-01-13 15:47:56 +0000320 struct {
321 mp_arg_val_t key, reverse;
322 } args;
323 mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
324 MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args);
Damien George744e7672015-02-02 15:14:22 +0000325
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300326 mp_check_self(MP_OBJ_IS_TYPE(pos_args[0], &mp_type_list));
Damien George999cedb2015-11-27 17:01:44 +0000327 mp_obj_list_t *self = MP_OBJ_TO_PTR(pos_args[0]);
Damien George744e7672015-02-02 15:14:22 +0000328
Damiend99b0522013-12-21 18:17:45 +0000329 if (self->len > 1) {
John R. Lentonc06763a2014-01-07 17:29:16 +0000330 mp_quicksort(self->items, self->items + self->len - 1,
Damien George22d85ec2016-01-13 15:47:56 +0000331 args.key.u_obj == mp_const_none ? MP_OBJ_NULL : args.key.u_obj,
332 args.reverse.u_bool ? mp_const_false : mp_const_true);
Damiend99b0522013-12-21 18:17:45 +0000333 }
Damien George744e7672015-02-02 15:14:22 +0000334
335 return mp_const_none;
Damiend99b0522013-12-21 18:17:45 +0000336}
337
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200338STATIC mp_obj_t list_clear(mp_obj_t self_in) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300339 mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Damien George999cedb2015-11-27 17:01:44 +0000340 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton069ded92014-01-03 23:22:53 +0000341 self->len = 0;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200342 self->items = m_renew(mp_obj_t, self->items, self->alloc, LIST_MIN_ALLOC);
343 self->alloc = LIST_MIN_ALLOC;
Paul Sokolovskya2240672014-04-28 00:16:57 +0300344 mp_seq_clear(self->items, 0, self->alloc, sizeof(*self->items));
John R. Lenton069ded92014-01-03 23:22:53 +0000345 return mp_const_none;
346}
347
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200348STATIC mp_obj_t list_copy(mp_obj_t self_in) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300349 mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Damien George999cedb2015-11-27 17:01:44 +0000350 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton26c21162014-01-03 23:42:17 +0000351 return mp_obj_new_list(self->len, self->items);
352}
353
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200354STATIC mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300355 mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Damien George999cedb2015-11-27 17:01:44 +0000356 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200357 return mp_seq_count_obj(self->items, self->len, value);
John R. Lentone241e8c2014-01-03 23:57:28 +0000358}
359
Damien George4b72b3a2016-01-03 14:21:40 +0000360STATIC mp_obj_t list_index(size_t n_args, const mp_obj_t *args) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300361 mp_check_self(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
Damien George999cedb2015-11-27 17:01:44 +0000362 mp_obj_list_t *self = MP_OBJ_TO_PTR(args[0]);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200363 return mp_seq_index_obj(self->items, self->len, n_args, args);
John R. Lenton5d4a8212014-01-04 00:26:30 +0000364}
365
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200366STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300367 mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Damien George999cedb2015-11-27 17:01:44 +0000368 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton45a87442014-01-04 01:15:01 +0000369 // insert has its own strange index logic
Damien Georgeecc88e92014-08-30 00:35:11 +0100370 mp_int_t index = MP_OBJ_SMALL_INT_VALUE(idx);
John R. Lenton45a87442014-01-04 01:15:01 +0000371 if (index < 0) {
372 index += self->len;
373 }
374 if (index < 0) {
375 index = 0;
376 }
Damien George58d9eeb2017-02-16 16:12:41 +1100377 if ((size_t)index > self->len) {
John R. Lenton45a87442014-01-04 01:15:01 +0000378 index = self->len;
379 }
380
381 mp_obj_list_append(self_in, mp_const_none);
382
Damien Georgeecc88e92014-08-30 00:35:11 +0100383 for (mp_int_t i = self->len-1; i > index; i--) {
John R. Lenton45a87442014-01-04 01:15:01 +0000384 self->items[i] = self->items[i-1];
385 }
386 self->items[index] = obj;
387
388 return mp_const_none;
389}
390
Damien Georgeeff359e2015-02-21 14:47:02 +0000391mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300392 mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton49fb6e52014-01-04 01:56:53 +0000393 mp_obj_t args[] = {self_in, value};
394 args[1] = list_index(2, args);
395 list_pop(2, args);
396
397 return mp_const_none;
398}
399
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200400STATIC mp_obj_t list_reverse(mp_obj_t self_in) {
Paul Sokolovskyc4a80042016-08-12 22:06:47 +0300401 mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Damien George999cedb2015-11-27 17:01:44 +0000402 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000403
Damien Georgeecc88e92014-08-30 00:35:11 +0100404 mp_int_t len = self->len;
405 for (mp_int_t i = 0; i < len/2; i++) {
Paul Sokolovsky69922c62015-03-20 23:24:25 +0200406 mp_obj_t a = self->items[i];
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000407 self->items[i] = self->items[len-i-1];
408 self->items[len-i-1] = a;
409 }
410
411 return mp_const_none;
412}
413
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200414STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
415STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_extend_obj, list_extend);
416STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
417STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
418STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
419STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
420STATIC MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
421STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
Damien Georgeeff359e2015-02-21 14:47:02 +0000422STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, mp_obj_list_remove);
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200423STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
Damien George744e7672015-02-02 15:14:22 +0000424STATIC MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 1, mp_obj_list_sort);
Damiend99b0522013-12-21 18:17:45 +0000425
Damien Georgecbf76742015-11-27 13:38:15 +0000426STATIC const mp_rom_map_elem_t list_locals_dict_table[] = {
427 { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&list_append_obj) },
428 { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&list_clear_obj) },
429 { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&list_copy_obj) },
430 { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&list_count_obj) },
431 { MP_ROM_QSTR(MP_QSTR_extend), MP_ROM_PTR(&list_extend_obj) },
432 { MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&list_index_obj) },
433 { MP_ROM_QSTR(MP_QSTR_insert), MP_ROM_PTR(&list_insert_obj) },
434 { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&list_pop_obj) },
435 { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&list_remove_obj) },
436 { MP_ROM_QSTR(MP_QSTR_reverse), MP_ROM_PTR(&list_reverse_obj) },
437 { MP_ROM_QSTR(MP_QSTR_sort), MP_ROM_PTR(&list_sort_obj) },
ian-v7a16fad2014-01-06 09:52:29 -0800438};
Damien George97209d32014-01-07 15:58:30 +0000439
Damien George9b196cd2014-03-26 21:47:19 +0000440STATIC MP_DEFINE_CONST_DICT(list_locals_dict, list_locals_dict_table);
441
Damien George3e1a5c12014-03-29 13:43:38 +0000442const mp_obj_type_t mp_type_list = {
Damien Georgec5966122014-02-15 16:10:44 +0000443 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000444 .name = MP_QSTR_list,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200445 .print = list_print,
446 .make_new = list_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000447 .unary_op = list_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200448 .binary_op = list_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100449 .subscr = list_subscr,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200450 .getiter = list_getiter,
Damien George999cedb2015-11-27 17:01:44 +0000451 .locals_dict = (mp_obj_dict_t*)&list_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000452};
453
Damien George58d9eeb2017-02-16 16:12:41 +1100454void mp_obj_list_init(mp_obj_list_t *o, size_t n) {
Damien George3e1a5c12014-03-29 13:43:38 +0000455 o->base.type = &mp_type_list;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200456 o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
Damiend99b0522013-12-21 18:17:45 +0000457 o->len = n;
458 o->items = m_new(mp_obj_t, o->alloc);
Paul Sokolovskya2240672014-04-28 00:16:57 +0300459 mp_seq_clear(o->items, n, o->alloc, sizeof(*o->items));
Paul Sokolovsky18bef252014-04-13 06:17:29 +0300460}
461
Damien George58d9eeb2017-02-16 16:12:41 +1100462STATIC mp_obj_list_t *list_new(size_t n) {
Damien George999cedb2015-11-27 17:01:44 +0000463 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
Paul Sokolovsky18bef252014-04-13 06:17:29 +0300464 mp_obj_list_init(o, n);
Damiend99b0522013-12-21 18:17:45 +0000465 return o;
466}
467
Damien George58d9eeb2017-02-16 16:12:41 +1100468mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items) {
Damiend99b0522013-12-21 18:17:45 +0000469 mp_obj_list_t *o = list_new(n);
Paul Sokolovskye5a15cb2014-02-04 09:44:10 +0200470 if (items != NULL) {
Damien George58d9eeb2017-02-16 16:12:41 +1100471 for (size_t i = 0; i < n; i++) {
Paul Sokolovskye5a15cb2014-02-04 09:44:10 +0200472 o->items[i] = items[i];
473 }
Damiend99b0522013-12-21 18:17:45 +0000474 }
Damien George999cedb2015-11-27 17:01:44 +0000475 return MP_OBJ_FROM_PTR(o);
Damiend99b0522013-12-21 18:17:45 +0000476}
477
Damien George9c4cbe22014-08-30 14:04:14 +0100478void mp_obj_list_get(mp_obj_t self_in, mp_uint_t *len, mp_obj_t **items) {
Damien George999cedb2015-11-27 17:01:44 +0000479 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
Damiend99b0522013-12-21 18:17:45 +0000480 *len = self->len;
481 *items = self->items;
482}
483
Damien George58d9eeb2017-02-16 16:12:41 +1100484void mp_obj_list_set_len(mp_obj_t self_in, size_t len) {
Damien George495d7812014-04-08 17:51:47 +0100485 // trust that the caller knows what it's doing
486 // TODO realloc if len got much smaller than alloc
Damien George999cedb2015-11-27 17:01:44 +0000487 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
Damien George495d7812014-04-08 17:51:47 +0100488 self->len = len;
489}
490
Damiend99b0522013-12-21 18:17:45 +0000491void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
Damien George999cedb2015-11-27 17:01:44 +0000492 mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
Damien George9c4cbe22014-08-30 14:04:14 +0100493 mp_uint_t i = mp_get_index(self->base.type, self->len, index, false);
Damiend99b0522013-12-21 18:17:45 +0000494 self->items[i] = value;
495}
496
497/******************************************************************************/
498/* list iterator */
499
500typedef struct _mp_obj_list_it_t {
501 mp_obj_base_t base;
Damien George8212d972016-01-03 16:27:55 +0000502 mp_fun_1_t iternext;
Paul Sokolovsky3d598252015-03-25 09:25:41 +0200503 mp_obj_t list;
Damien George58d9eeb2017-02-16 16:12:41 +1100504 size_t cur;
Damiend99b0522013-12-21 18:17:45 +0000505} mp_obj_list_it_t;
506
Damien George969a6b32014-12-10 22:07:04 +0000507STATIC mp_obj_t list_it_iternext(mp_obj_t self_in) {
Damien George999cedb2015-11-27 17:01:44 +0000508 mp_obj_list_it_t *self = MP_OBJ_TO_PTR(self_in);
509 mp_obj_list_t *list = MP_OBJ_TO_PTR(self->list);
Paul Sokolovsky3d598252015-03-25 09:25:41 +0200510 if (self->cur < list->len) {
511 mp_obj_t o_out = list->items[self->cur];
Damiend99b0522013-12-21 18:17:45 +0000512 self->cur += 1;
513 return o_out;
514 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100515 return MP_OBJ_STOP_ITERATION;
Damiend99b0522013-12-21 18:17:45 +0000516 }
517}
518
Damien Georgeae8d8672016-01-09 23:14:54 +0000519mp_obj_t mp_obj_new_list_iterator(mp_obj_t list, size_t cur, mp_obj_iter_buf_t *iter_buf) {
520 assert(sizeof(mp_obj_list_it_t) <= sizeof(mp_obj_iter_buf_t));
521 mp_obj_list_it_t *o = (mp_obj_list_it_t*)iter_buf;
Damien George8212d972016-01-03 16:27:55 +0000522 o->base.type = &mp_type_polymorph_iter;
523 o->iternext = list_it_iternext;
Damiend99b0522013-12-21 18:17:45 +0000524 o->list = list;
525 o->cur = cur;
Damien George999cedb2015-11-27 17:01:44 +0000526 return MP_OBJ_FROM_PTR(o);
Damiend99b0522013-12-21 18:17:45 +0000527}