blob: 32ec64fa44f3b1e9c1e122aa8919c75aa95c9857 [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 George9c4cbe22014-08-30 14:04:14 +010036STATIC mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, mp_uint_t cur);
37STATIC mp_obj_list_t *list_new(mp_uint_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 Georgeecc88e92014-08-30 00:35:11 +010039STATIC mp_obj_t list_pop(mp_uint_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
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020047STATIC void list_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +000048 mp_obj_list_t *o = o_in;
Damien George612045f2014-09-17 22:56:34 +010049 if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
50 kind = PRINT_REPR;
51 }
Damiend99b0522013-12-21 18:17:45 +000052 print(env, "[");
Damien George9c4cbe22014-08-30 14:04:14 +010053 for (mp_uint_t i = 0; i < o->len; i++) {
Damiend99b0522013-12-21 18:17:45 +000054 if (i > 0) {
55 print(env, ", ");
56 }
Damien George612045f2014-09-17 22:56:34 +010057 mp_obj_print_helper(print, env, o->items[i], kind);
Damiend99b0522013-12-21 18:17:45 +000058 }
59 print(env, "]");
60}
61
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +030062STATIC mp_obj_t list_extend_from_iter(mp_obj_t list, mp_obj_t iterable) {
63 mp_obj_t iter = mp_getiter(iterable);
64 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 Georgeecc88e92014-08-30 00:35:11 +010071STATIC mp_obj_t list_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_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) {
Damien George3e1a5c12014-03-29 13:43:38 +000092 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
93 if (!MP_OBJ_IS_TYPE(another_in, &mp_type_list)) {
Paul Sokolovsky1945e602014-01-12 02:01:00 +020094 return false;
95 }
96 mp_obj_list_t *self = self_in;
97 mp_obj_list_t *another = 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 George4e8dc8c2014-01-27 23:15:32 +0000103 mp_obj_list_t *self = self_in;
104 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100105 case MP_UNARY_OP_BOOL: return MP_BOOL(self->len != 0);
106 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) {
Damiend99b0522013-12-21 18:17:45 +0000112 mp_obj_list_t *o = lhs;
113 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 }
118 mp_obj_list_t *p = rhs;
119 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);
John R. Lenton81ad89c2014-01-03 02:32:40 +0000121 return s;
John R. Lenton4cb80582014-01-03 02:27:08 +0000122 }
Damien George729f7b42014-04-17 22:10:53 +0100123 case MP_BINARY_OP_INPLACE_ADD: {
Damien George3e1a5c12014-03-29 13:43:38 +0000124 if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
Damien George6ac5dce2014-05-21 19:42:43 +0100125 return MP_OBJ_NULL; // op not supported
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200126 }
127 list_extend(lhs, rhs);
128 return o;
129 }
Damien George8270e382014-04-03 11:00:54 +0000130 case MP_BINARY_OP_MULTIPLY: {
Damien George40f3c022014-07-03 13:25:24 +0100131 mp_int_t n;
Damien George8270e382014-04-03 11:00:54 +0000132 if (!mp_obj_get_int_maybe(rhs, &n)) {
Damien George6ac5dce2014-05-21 19:42:43 +0100133 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200134 }
Damien George9b7a8ee2014-08-13 13:22:24 +0100135 if (n < 0) {
136 n = 0;
137 }
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200138 mp_obj_list_t *s = list_new(o->len * n);
139 mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200140 return s;
141 }
Damien Georged17926d2014-03-30 13:35:08 +0100142 case MP_BINARY_OP_EQUAL:
143 case MP_BINARY_OP_LESS:
144 case MP_BINARY_OP_LESS_EQUAL:
145 case MP_BINARY_OP_MORE:
146 case MP_BINARY_OP_MORE_EQUAL:
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200147 return MP_BOOL(list_cmp_helper(op, lhs, rhs));
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200148
Damiend99b0522013-12-21 18:17:45 +0000149 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100150 return MP_OBJ_NULL; // op not supported
Damiend99b0522013-12-21 18:17:45 +0000151 }
152}
153
Damien George729f7b42014-04-17 22:10:53 +0100154STATIC 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 +0100155 if (value == MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100156 // delete
Damien Georgefb510b32014-06-01 13:32:54 +0100157#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky3f8d34c2014-05-10 23:03:30 +0300158 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
159 mp_obj_list_t *self = self_in;
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300160 mp_bound_slice_t slice;
161 if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
Paul Sokolovsky3f8d34c2014-05-10 23:03:30 +0300162 assert(0);
163 }
164
Damien George42f3de92014-10-03 17:44:14 +0000165 mp_int_t len_adj = slice.start - slice.stop;
Paul Sokolovsky3f8d34c2014-05-10 23:03:30 +0300166 //printf("Len adj: %d\n", len_adj);
167 assert(len_adj <= 0);
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200168 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 +0300169 // Clear "freed" elements at the end of list
170 mp_seq_clear(self->items, self->len + len_adj, self->len, sizeof(*self->items));
171 self->len += len_adj;
172 return mp_const_none;
173 }
174#endif
Damien Georgef4c9b332014-04-08 21:32:29 +0100175 mp_obj_t args[2] = {self_in, index};
176 list_pop(2, args);
Damien George729f7b42014-04-17 22:10:53 +0100177 return mp_const_none;
178 } else if (value == MP_OBJ_SENTINEL) {
179 // load
180 mp_obj_list_t *self = self_in;
Damien Georgefb510b32014-06-01 13:32:54 +0100181#if MICROPY_PY_BUILTINS_SLICE
Damien George729f7b42014-04-17 22:10:53 +0100182 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300183 mp_bound_slice_t slice;
184 if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300185 return mp_seq_extract_slice(self->len, self->items, &slice);
Damien George729f7b42014-04-17 22:10:53 +0100186 }
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300187 mp_obj_list_t *res = list_new(slice.stop - slice.start);
188 mp_seq_copy(res->items, self->items + slice.start, res->len, mp_obj_t);
Damien George729f7b42014-04-17 22:10:53 +0100189 return res;
190 }
191#endif
Damien George9c4cbe22014-08-30 14:04:14 +0100192 mp_uint_t index_val = mp_get_index(self->base.type, self->len, index, false);
Damien George729f7b42014-04-17 22:10:53 +0100193 return self->items[index_val];
Damien Georgef4c9b332014-04-08 21:32:29 +0100194 } else {
Damien Georgefb510b32014-06-01 13:32:54 +0100195#if MICROPY_PY_BUILTINS_SLICE
Paul Sokolovsky94d82462014-05-10 22:23:00 +0300196 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
197 mp_obj_list_t *self = self_in;
198 assert(MP_OBJ_IS_TYPE(value, &mp_type_list));
199 mp_obj_list_t *slice = value;
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300200 mp_bound_slice_t slice_out;
201 if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice_out)) {
Paul Sokolovsky94d82462014-05-10 22:23:00 +0300202 assert(0);
203 }
Damien George42f3de92014-10-03 17:44:14 +0000204 mp_int_t len_adj = slice->len - (slice_out.stop - slice_out.start);
Paul Sokolovsky94d82462014-05-10 22:23:00 +0300205 //printf("Len adj: %d\n", len_adj);
Paul Sokolovsky2705f4c2014-05-25 02:36:12 +0300206 if (len_adj > 0) {
207 if (self->len + len_adj > self->alloc) {
208 // TODO: Might optimize memory copies here by checking if block can
209 // be grown inplace or not
210 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + len_adj);
211 self->alloc = self->len + len_adj;
212 }
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300213 mp_seq_replace_slice_grow_inplace(self->items, self->len,
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200214 slice_out.start, slice_out.stop, slice->items, slice->len, len_adj, sizeof(*self->items));
Paul Sokolovsky2705f4c2014-05-25 02:36:12 +0300215 } else {
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300216 mp_seq_replace_slice_no_grow(self->items, self->len,
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200217 slice_out.start, slice_out.stop, slice->items, slice->len, sizeof(*self->items));
Paul Sokolovsky2705f4c2014-05-25 02:36:12 +0300218 // Clear "freed" elements at the end of list
219 mp_seq_clear(self->items, self->len + len_adj, self->len, sizeof(*self->items));
220 // TODO: apply allocation policy re: alloc_size
221 }
Paul Sokolovsky94d82462014-05-10 22:23:00 +0300222 self->len += len_adj;
Paul Sokolovsky94d82462014-05-10 22:23:00 +0300223 return mp_const_none;
224 }
225#endif
Damien Georgef4c9b332014-04-08 21:32:29 +0100226 mp_obj_list_store(self_in, index, value);
Damien George729f7b42014-04-17 22:10:53 +0100227 return mp_const_none;
Damien Georgef4c9b332014-04-08 21:32:29 +0100228 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100229}
230
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200231STATIC mp_obj_t list_getiter(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000232 return mp_obj_new_list_iterator(o_in, 0);
233}
234
235mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
Damien George3e1a5c12014-03-29 13:43:38 +0000236 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Damiend99b0522013-12-21 18:17:45 +0000237 mp_obj_list_t *self = self_in;
238 if (self->len >= self->alloc) {
Damien732407f2013-12-29 19:33:23 +0000239 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
Damiend99b0522013-12-21 18:17:45 +0000240 self->alloc *= 2;
Paul Sokolovskya2240672014-04-28 00:16:57 +0300241 mp_seq_clear(self->items, self->len + 1, self->alloc, sizeof(*self->items));
Damiend99b0522013-12-21 18:17:45 +0000242 }
243 self->items[self->len++] = arg;
244 return mp_const_none; // return None, as per CPython
245}
246
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200247STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000248 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +0300249 if (MP_OBJ_IS_TYPE(arg_in, &mp_type_list)) {
250 mp_obj_list_t *self = self_in;
251 mp_obj_list_t *arg = arg_in;
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200252
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +0300253 if (self->len + arg->len > self->alloc) {
254 // TODO: use alloc policy for "4"
255 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + arg->len + 4);
256 self->alloc = self->len + arg->len + 4;
Paul Sokolovskya2240672014-04-28 00:16:57 +0300257 mp_seq_clear(self->items, self->len + arg->len, self->alloc, sizeof(*self->items));
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +0300258 }
259
260 memcpy(self->items + self->len, arg->items, sizeof(mp_obj_t) * arg->len);
261 self->len += arg->len;
262 } else {
263 list_extend_from_iter(self_in, arg_in);
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200264 }
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200265 return mp_const_none; // return None, as per CPython
266}
267
Damien Georgeecc88e92014-08-30 00:35:11 +0100268STATIC mp_obj_t list_pop(mp_uint_t n_args, const mp_obj_t *args) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000269 assert(1 <= n_args && n_args <= 2);
Damien George3e1a5c12014-03-29 13:43:38 +0000270 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
John R. Lenton25f417c2014-01-03 22:53:18 +0000271 mp_obj_list_t *self = args[0];
272 if (self->len == 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100273 nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "pop from empty list"));
John R. Lenton25f417c2014-01-03 22:53:18 +0000274 }
Damien George9c4cbe22014-08-30 14:04:14 +0100275 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 +0000276 mp_obj_t ret = self->items[index];
277 self->len -= 1;
Damien George17ae2392014-08-29 21:07:54 +0100278 memmove(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300279 // Clear stale pointer from slot which just got freed to prevent GC issues
280 self->items[self->len] = MP_OBJ_NULL;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200281 if (self->alloc > LIST_MIN_ALLOC && self->alloc > 2 * self->len) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000282 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
283 self->alloc /= 2;
284 }
Damiend99b0522013-12-21 18:17:45 +0000285 return ret;
286}
287
Damien George744e7672015-02-02 15:14:22 +0000288STATIC void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, mp_obj_t binop_less_result) {
289 MP_STACK_CHECK();
Damiend99b0522013-12-21 18:17:45 +0000290 while (head < tail) {
291 mp_obj_t *h = head - 1;
292 mp_obj_t *t = tail;
Damien George744e7672015-02-02 15:14:22 +0000293 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 +0000294 for (;;) {
Damien George744e7672015-02-02 15:14:22 +0000295 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);
296 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 +0000297 if (h >= t) break;
298 mp_obj_t x = h[0];
299 h[0] = t[0];
300 t[0] = x;
301 }
302 mp_obj_t x = h[0];
303 h[0] = tail[0];
304 tail[0] = x;
Damien George744e7672015-02-02 15:14:22 +0000305 // do the smaller recursive call first, to keep stack within O(log(N))
306 if (t - head < tail - h - 1) {
307 mp_quicksort(head, t, key_fn, binop_less_result);
308 head = h + 1;
309 } else {
310 mp_quicksort(h + 1, tail, key_fn, binop_less_result);
311 tail = t;
312 }
Damiend99b0522013-12-21 18:17:45 +0000313 }
314}
315
Damien George744e7672015-02-02 15:14:22 +0000316// TODO Python defines sort to be stable but ours is not
317mp_obj_t mp_obj_list_sort(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
318 static const mp_arg_t allowed_args[] = {
319 { MP_QSTR_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
320 { MP_QSTR_reverse, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
321 };
322
323 // parse args
324 mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
325 mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
326
327 mp_obj_list_t *self = pos_args[0];
328 assert(MP_OBJ_IS_TYPE(self, &mp_type_list));
329
Damiend99b0522013-12-21 18:17:45 +0000330 if (self->len > 1) {
John R. Lentonc06763a2014-01-07 17:29:16 +0000331 mp_quicksort(self->items, self->items + self->len - 1,
Damien George744e7672015-02-02 15:14:22 +0000332 args[0].u_obj == mp_const_none ? MP_OBJ_NULL : args[0].u_obj,
333 args[1].u_bool ? mp_const_false : mp_const_true);
Damiend99b0522013-12-21 18:17:45 +0000334 }
Damien George744e7672015-02-02 15:14:22 +0000335
336 return mp_const_none;
Damiend99b0522013-12-21 18:17:45 +0000337}
338
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200339STATIC mp_obj_t list_clear(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000340 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton069ded92014-01-03 23:22:53 +0000341 mp_obj_list_t *self = self_in;
342 self->len = 0;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200343 self->items = m_renew(mp_obj_t, self->items, self->alloc, LIST_MIN_ALLOC);
344 self->alloc = LIST_MIN_ALLOC;
Paul Sokolovskya2240672014-04-28 00:16:57 +0300345 mp_seq_clear(self->items, 0, self->alloc, sizeof(*self->items));
John R. Lenton069ded92014-01-03 23:22:53 +0000346 return mp_const_none;
347}
348
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200349STATIC mp_obj_t list_copy(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000350 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton26c21162014-01-03 23:42:17 +0000351 mp_obj_list_t *self = self_in;
352 return mp_obj_new_list(self->len, self->items);
353}
354
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200355STATIC mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
Damien George3e1a5c12014-03-29 13:43:38 +0000356 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lentone241e8c2014-01-03 23:57:28 +0000357 mp_obj_list_t *self = self_in;
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200358 return mp_seq_count_obj(self->items, self->len, value);
John R. Lentone241e8c2014-01-03 23:57:28 +0000359}
360
Damien Georgeecc88e92014-08-30 00:35:11 +0100361STATIC mp_obj_t list_index(mp_uint_t n_args, const mp_obj_t *args) {
John R. Lenton5d4a8212014-01-04 00:26:30 +0000362 assert(2 <= n_args && n_args <= 4);
Damien George3e1a5c12014-03-29 13:43:38 +0000363 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
John R. Lenton5d4a8212014-01-04 00:26:30 +0000364 mp_obj_list_t *self = args[0];
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200365 return mp_seq_index_obj(self->items, self->len, n_args, args);
John R. Lenton5d4a8212014-01-04 00:26:30 +0000366}
367
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200368STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
Damien George3e1a5c12014-03-29 13:43:38 +0000369 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton45a87442014-01-04 01:15:01 +0000370 mp_obj_list_t *self = self_in;
371 // insert has its own strange index logic
Damien Georgeecc88e92014-08-30 00:35:11 +0100372 mp_int_t index = MP_OBJ_SMALL_INT_VALUE(idx);
John R. Lenton45a87442014-01-04 01:15:01 +0000373 if (index < 0) {
374 index += self->len;
375 }
376 if (index < 0) {
377 index = 0;
378 }
Damien George963a5a32015-01-16 17:47:07 +0000379 if ((mp_uint_t)index > self->len) {
John R. Lenton45a87442014-01-04 01:15:01 +0000380 index = self->len;
381 }
382
383 mp_obj_list_append(self_in, mp_const_none);
384
Damien Georgeecc88e92014-08-30 00:35:11 +0100385 for (mp_int_t i = self->len-1; i > index; i--) {
John R. Lenton45a87442014-01-04 01:15:01 +0000386 self->items[i] = self->items[i-1];
387 }
388 self->items[index] = obj;
389
390 return mp_const_none;
391}
392
Damien Georgeeff359e2015-02-21 14:47:02 +0000393mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value) {
Damien George3e1a5c12014-03-29 13:43:38 +0000394 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton49fb6e52014-01-04 01:56:53 +0000395 mp_obj_t args[] = {self_in, value};
396 args[1] = list_index(2, args);
397 list_pop(2, args);
398
399 return mp_const_none;
400}
401
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200402STATIC mp_obj_t list_reverse(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000403 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000404 mp_obj_list_t *self = self_in;
405
Damien Georgeecc88e92014-08-30 00:35:11 +0100406 mp_int_t len = self->len;
407 for (mp_int_t i = 0; i < len/2; i++) {
Paul Sokolovsky69922c62015-03-20 23:24:25 +0200408 mp_obj_t a = self->items[i];
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000409 self->items[i] = self->items[len-i-1];
410 self->items[len-i-1] = a;
411 }
412
413 return mp_const_none;
414}
415
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200416STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
417STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_extend_obj, list_extend);
418STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
419STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
420STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
421STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
422STATIC MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
423STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
Damien Georgeeff359e2015-02-21 14:47:02 +0000424STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, mp_obj_list_remove);
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200425STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
Damien George744e7672015-02-02 15:14:22 +0000426STATIC MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 1, mp_obj_list_sort);
Damiend99b0522013-12-21 18:17:45 +0000427
Damien George9b196cd2014-03-26 21:47:19 +0000428STATIC const mp_map_elem_t list_locals_dict_table[] = {
429 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&list_append_obj },
430 { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&list_clear_obj },
431 { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&list_copy_obj },
432 { MP_OBJ_NEW_QSTR(MP_QSTR_count), (mp_obj_t)&list_count_obj },
433 { MP_OBJ_NEW_QSTR(MP_QSTR_extend), (mp_obj_t)&list_extend_obj },
434 { MP_OBJ_NEW_QSTR(MP_QSTR_index), (mp_obj_t)&list_index_obj },
435 { MP_OBJ_NEW_QSTR(MP_QSTR_insert), (mp_obj_t)&list_insert_obj },
436 { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&list_pop_obj },
437 { MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&list_remove_obj },
438 { MP_OBJ_NEW_QSTR(MP_QSTR_reverse), (mp_obj_t)&list_reverse_obj },
439 { MP_OBJ_NEW_QSTR(MP_QSTR_sort), (mp_obj_t)&list_sort_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800440};
Damien George97209d32014-01-07 15:58:30 +0000441
Damien George9b196cd2014-03-26 21:47:19 +0000442STATIC MP_DEFINE_CONST_DICT(list_locals_dict, list_locals_dict_table);
443
Damien George3e1a5c12014-03-29 13:43:38 +0000444const mp_obj_type_t mp_type_list = {
Damien Georgec5966122014-02-15 16:10:44 +0000445 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000446 .name = MP_QSTR_list,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200447 .print = list_print,
448 .make_new = list_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000449 .unary_op = list_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200450 .binary_op = list_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100451 .subscr = list_subscr,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200452 .getiter = list_getiter,
Damien George9b196cd2014-03-26 21:47:19 +0000453 .locals_dict = (mp_obj_t)&list_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000454};
455
Damien George9c4cbe22014-08-30 14:04:14 +0100456void mp_obj_list_init(mp_obj_list_t *o, mp_uint_t n) {
Damien George3e1a5c12014-03-29 13:43:38 +0000457 o->base.type = &mp_type_list;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200458 o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
Damiend99b0522013-12-21 18:17:45 +0000459 o->len = n;
460 o->items = m_new(mp_obj_t, o->alloc);
Paul Sokolovskya2240672014-04-28 00:16:57 +0300461 mp_seq_clear(o->items, n, o->alloc, sizeof(*o->items));
Paul Sokolovsky18bef252014-04-13 06:17:29 +0300462}
463
Damien George9c4cbe22014-08-30 14:04:14 +0100464STATIC mp_obj_list_t *list_new(mp_uint_t n) {
Paul Sokolovsky18bef252014-04-13 06:17:29 +0300465 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
466 mp_obj_list_init(o, n);
Damiend99b0522013-12-21 18:17:45 +0000467 return o;
468}
469
Damien George9c4cbe22014-08-30 14:04:14 +0100470mp_obj_t mp_obj_new_list(mp_uint_t n, mp_obj_t *items) {
Damiend99b0522013-12-21 18:17:45 +0000471 mp_obj_list_t *o = list_new(n);
Paul Sokolovskye5a15cb2014-02-04 09:44:10 +0200472 if (items != NULL) {
Damien George9c4cbe22014-08-30 14:04:14 +0100473 for (mp_uint_t i = 0; i < n; i++) {
Paul Sokolovskye5a15cb2014-02-04 09:44:10 +0200474 o->items[i] = items[i];
475 }
Damiend99b0522013-12-21 18:17:45 +0000476 }
477 return o;
478}
479
Damien George9c4cbe22014-08-30 14:04:14 +0100480void mp_obj_list_get(mp_obj_t self_in, mp_uint_t *len, mp_obj_t **items) {
Damiend99b0522013-12-21 18:17:45 +0000481 mp_obj_list_t *self = self_in;
482 *len = self->len;
483 *items = self->items;
484}
485
Damien George9c4cbe22014-08-30 14:04:14 +0100486void mp_obj_list_set_len(mp_obj_t self_in, mp_uint_t len) {
Damien George495d7812014-04-08 17:51:47 +0100487 // trust that the caller knows what it's doing
488 // TODO realloc if len got much smaller than alloc
489 mp_obj_list_t *self = self_in;
490 self->len = len;
491}
492
Damiend99b0522013-12-21 18:17:45 +0000493void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
494 mp_obj_list_t *self = self_in;
Damien George9c4cbe22014-08-30 14:04:14 +0100495 mp_uint_t i = mp_get_index(self->base.type, self->len, index, false);
Damiend99b0522013-12-21 18:17:45 +0000496 self->items[i] = value;
497}
498
499/******************************************************************************/
500/* list iterator */
501
502typedef struct _mp_obj_list_it_t {
503 mp_obj_base_t base;
504 mp_obj_list_t *list;
Damien George40f3c022014-07-03 13:25:24 +0100505 mp_uint_t cur;
Damiend99b0522013-12-21 18:17:45 +0000506} mp_obj_list_it_t;
507
Damien George969a6b32014-12-10 22:07:04 +0000508STATIC mp_obj_t list_it_iternext(mp_obj_t self_in) {
Damiend99b0522013-12-21 18:17:45 +0000509 mp_obj_list_it_t *self = self_in;
510 if (self->cur < self->list->len) {
511 mp_obj_t o_out = self->list->items[self->cur];
512 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 George3e1a5c12014-03-29 13:43:38 +0000519STATIC const mp_obj_type_t mp_type_list_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000520 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000521 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300522 .getiter = mp_identity,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200523 .iternext = list_it_iternext,
Damiend99b0522013-12-21 18:17:45 +0000524};
525
Damien George9c4cbe22014-08-30 14:04:14 +0100526mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, mp_uint_t cur) {
Damiend99b0522013-12-21 18:17:45 +0000527 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000528 o->base.type = &mp_type_list_it;
Damiend99b0522013-12-21 18:17:45 +0000529 o->list = list;
530 o->cur = cur;
531 return o;
532}