blob: 9e30ebb4aa0df38a548691f43f7b9b84f3339611 [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
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030030#include "mpconfig.h"
Damiend99b0522013-12-21 18:17:45 +000031#include "nlr.h"
32#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000033#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000034#include "obj.h"
35#include "runtime0.h"
36#include "runtime.h"
Paul Sokolovsky18bef252014-04-13 06:17:29 +030037#include "objlist.h"
Damiend99b0522013-12-21 18:17:45 +000038
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020039STATIC mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
40STATIC mp_obj_list_t *list_new(uint n);
41STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in);
Damien Georgef4c9b332014-04-08 21:32:29 +010042STATIC mp_obj_t list_pop(uint n_args, const mp_obj_t *args);
Damiend99b0522013-12-21 18:17:45 +000043
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +020044// TODO: Move to mpconfig.h
45#define LIST_MIN_ALLOC 4
46
Damiend99b0522013-12-21 18:17:45 +000047/******************************************************************************/
48/* list */
49
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020050STATIC 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 +000051 mp_obj_list_t *o = o_in;
52 print(env, "[");
53 for (int i = 0; i < o->len; i++) {
54 if (i > 0) {
55 print(env, ", ");
56 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020057 mp_obj_print_helper(print, env, o->items[i], PRINT_REPR);
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
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020071STATIC mp_obj_t list_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George20006db2014-01-18 14:10:48 +000072 // TODO check n_kw == 0
73
Damien George71c51812014-01-04 20:21:15 +000074 switch (n_args) {
75 case 0:
76 // return a new, empty list
Damien Georgeebde0b82014-01-19 00:47:40 +000077 return mp_obj_new_list(0, NULL);
Damien George71c51812014-01-04 20:21:15 +000078
79 case 1:
80 {
81 // make list from iterable
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +030082 // TODO: optimize list/tuple
Damien Georgeebde0b82014-01-19 00:47:40 +000083 mp_obj_t list = mp_obj_new_list(0, NULL);
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +030084 return list_extend_from_iter(list, args[0]);
Damien George71c51812014-01-04 20:21:15 +000085 }
86
87 default:
Damien Georgeea13f402014-04-05 18:32:08 +010088 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "list takes at most 1 argument, %d given", n_args));
Damien George71c51812014-01-04 20:21:15 +000089 }
90}
91
Damien Georged17926d2014-03-30 13:35:08 +010092// Don't pass MP_BINARY_OP_NOT_EQUAL here
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020093STATIC bool list_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
Damien George3e1a5c12014-03-29 13:43:38 +000094 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
95 if (!MP_OBJ_IS_TYPE(another_in, &mp_type_list)) {
Paul Sokolovsky1945e602014-01-12 02:01:00 +020096 return false;
97 }
98 mp_obj_list_t *self = self_in;
99 mp_obj_list_t *another = another_in;
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200100
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200101 return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len);
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200102}
103
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200104STATIC mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
Damien George4e8dc8c2014-01-27 23:15:32 +0000105 mp_obj_list_t *self = self_in;
106 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100107 case MP_UNARY_OP_BOOL: return MP_BOOL(self->len != 0);
108 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100109 default: return MP_OBJ_NOT_SUPPORTED;
Damien George4e8dc8c2014-01-27 23:15:32 +0000110 }
111}
112
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200113STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damiend99b0522013-12-21 18:17:45 +0000114 mp_obj_list_t *o = lhs;
115 switch (op) {
Damien George729f7b42014-04-17 22:10:53 +0100116 case MP_BINARY_OP_ADD: {
Damien George3e1a5c12014-03-29 13:43:38 +0000117 if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
Damien Georged0a5bf32014-05-10 13:55:11 +0100118 return MP_OBJ_NOT_SUPPORTED;
John R. Lenton81ad89c2014-01-03 02:32:40 +0000119 }
120 mp_obj_list_t *p = rhs;
121 mp_obj_list_t *s = list_new(o->len + p->len);
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300122 mp_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
John R. Lenton81ad89c2014-01-03 02:32:40 +0000123 return s;
John R. Lenton4cb80582014-01-03 02:27:08 +0000124 }
Damien George729f7b42014-04-17 22:10:53 +0100125 case MP_BINARY_OP_INPLACE_ADD: {
Damien George3e1a5c12014-03-29 13:43:38 +0000126 if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
Damien Georged0a5bf32014-05-10 13:55:11 +0100127 return MP_OBJ_NOT_SUPPORTED;
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200128 }
129 list_extend(lhs, rhs);
130 return o;
131 }
Damien George8270e382014-04-03 11:00:54 +0000132 case MP_BINARY_OP_MULTIPLY: {
133 machine_int_t n;
134 if (!mp_obj_get_int_maybe(rhs, &n)) {
Damien Georged0a5bf32014-05-10 13:55:11 +0100135 return MP_OBJ_NOT_SUPPORTED;
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200136 }
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200137 mp_obj_list_t *s = list_new(o->len * n);
138 mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200139 return s;
140 }
Damien Georged17926d2014-03-30 13:35:08 +0100141 case MP_BINARY_OP_EQUAL:
142 case MP_BINARY_OP_LESS:
143 case MP_BINARY_OP_LESS_EQUAL:
144 case MP_BINARY_OP_MORE:
145 case MP_BINARY_OP_MORE_EQUAL:
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200146 return MP_BOOL(list_cmp_helper(op, lhs, rhs));
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200147
Damiend99b0522013-12-21 18:17:45 +0000148 default:
Damien Georgeea8d06c2014-04-17 23:19:36 +0100149 return MP_OBJ_NOT_SUPPORTED;
Damiend99b0522013-12-21 18:17:45 +0000150 }
151}
152
Damien George729f7b42014-04-17 22:10:53 +0100153STATIC 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 +0100154 if (value == MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100155 // delete
Paul Sokolovsky3f8d34c2014-05-10 23:03:30 +0300156#if MICROPY_ENABLE_SLICE
157 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
158 mp_obj_list_t *self = self_in;
159 machine_uint_t start, stop;
160 if (!mp_seq_get_fast_slice_indexes(self->len, index, &start, &stop)) {
161 assert(0);
162 }
163
164 int len_adj = start - stop;
165 //printf("Len adj: %d\n", len_adj);
166 assert(len_adj <= 0);
167 mp_seq_replace_slice_no_grow(self->items, self->len, start, stop, self->items/*NULL*/, 0, mp_obj_t);
168 // Clear "freed" elements at the end of list
169 mp_seq_clear(self->items, self->len + len_adj, self->len, sizeof(*self->items));
170 self->len += len_adj;
171 return mp_const_none;
172 }
173#endif
Damien Georgef4c9b332014-04-08 21:32:29 +0100174 mp_obj_t args[2] = {self_in, index};
175 list_pop(2, args);
Damien George729f7b42014-04-17 22:10:53 +0100176 return mp_const_none;
177 } else if (value == MP_OBJ_SENTINEL) {
178 // load
179 mp_obj_list_t *self = self_in;
180#if MICROPY_ENABLE_SLICE
181 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
182 machine_uint_t start, stop;
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300183 if (!mp_seq_get_fast_slice_indexes(self->len, index, &start, &stop)) {
Damien George729f7b42014-04-17 22:10:53 +0100184 assert(0);
185 }
186 mp_obj_list_t *res = list_new(stop - start);
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300187 mp_seq_copy(res->items, self->items + start, res->len, mp_obj_t);
Damien George729f7b42014-04-17 22:10:53 +0100188 return res;
189 }
190#endif
191 uint index_val = mp_get_index(self->base.type, self->len, index, false);
192 return self->items[index_val];
Damien Georgef4c9b332014-04-08 21:32:29 +0100193 } else {
Paul Sokolovsky94d82462014-05-10 22:23:00 +0300194#if MICROPY_ENABLE_SLICE
195 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
196 mp_obj_list_t *self = self_in;
197 assert(MP_OBJ_IS_TYPE(value, &mp_type_list));
198 mp_obj_list_t *slice = value;
199 machine_uint_t start, stop;
200 if (!mp_seq_get_fast_slice_indexes(self->len, index, &start, &stop)) {
201 assert(0);
202 }
203 int len_adj = slice->len - (stop - start);
204 //printf("Len adj: %d\n", len_adj);
205 assert(len_adj <= 0);
206 mp_seq_replace_slice_no_grow(self->items, self->len, start, stop, slice->items, slice->len, mp_obj_t);
207 // Clear "freed" elements at the end of list
208 mp_seq_clear(self->items, self->len + len_adj, self->len, sizeof(*self->items));
209 self->len += len_adj;
210 // TODO: apply allocation policy re: alloc_size
211 return mp_const_none;
212 }
213#endif
Damien Georgef4c9b332014-04-08 21:32:29 +0100214 mp_obj_list_store(self_in, index, value);
Damien George729f7b42014-04-17 22:10:53 +0100215 return mp_const_none;
Damien Georgef4c9b332014-04-08 21:32:29 +0100216 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100217}
218
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200219STATIC mp_obj_t list_getiter(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000220 return mp_obj_new_list_iterator(o_in, 0);
221}
222
223mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
Damien George3e1a5c12014-03-29 13:43:38 +0000224 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Damiend99b0522013-12-21 18:17:45 +0000225 mp_obj_list_t *self = self_in;
226 if (self->len >= self->alloc) {
Damien732407f2013-12-29 19:33:23 +0000227 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
Damiend99b0522013-12-21 18:17:45 +0000228 self->alloc *= 2;
Paul Sokolovskya2240672014-04-28 00:16:57 +0300229 mp_seq_clear(self->items, self->len + 1, self->alloc, sizeof(*self->items));
Damiend99b0522013-12-21 18:17:45 +0000230 }
231 self->items[self->len++] = arg;
232 return mp_const_none; // return None, as per CPython
233}
234
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200235STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000236 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +0300237 if (MP_OBJ_IS_TYPE(arg_in, &mp_type_list)) {
238 mp_obj_list_t *self = self_in;
239 mp_obj_list_t *arg = arg_in;
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200240
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +0300241 if (self->len + arg->len > self->alloc) {
242 // TODO: use alloc policy for "4"
243 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + arg->len + 4);
244 self->alloc = self->len + arg->len + 4;
Paul Sokolovskya2240672014-04-28 00:16:57 +0300245 mp_seq_clear(self->items, self->len + arg->len, self->alloc, sizeof(*self->items));
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +0300246 }
247
248 memcpy(self->items + self->len, arg->items, sizeof(mp_obj_t) * arg->len);
249 self->len += arg->len;
250 } else {
251 list_extend_from_iter(self_in, arg_in);
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200252 }
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200253 return mp_const_none; // return None, as per CPython
254}
255
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200256STATIC mp_obj_t list_pop(uint n_args, const mp_obj_t *args) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000257 assert(1 <= n_args && n_args <= 2);
Damien George3e1a5c12014-03-29 13:43:38 +0000258 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
John R. Lenton25f417c2014-01-03 22:53:18 +0000259 mp_obj_list_t *self = args[0];
260 if (self->len == 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100261 nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "pop from empty list"));
John R. Lenton25f417c2014-01-03 22:53:18 +0000262 }
Paul Sokolovsky48bf6b32014-04-27 23:07:37 +0300263 uint 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 +0000264 mp_obj_t ret = self->items[index];
265 self->len -= 1;
266 memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300267 // Clear stale pointer from slot which just got freed to prevent GC issues
268 self->items[self->len] = MP_OBJ_NULL;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200269 if (self->alloc > LIST_MIN_ALLOC && self->alloc > 2 * self->len) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000270 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
271 self->alloc /= 2;
272 }
Damiend99b0522013-12-21 18:17:45 +0000273 return ret;
274}
275
276// TODO make this conform to CPython's definition of sort
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200277STATIC void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool reversed) {
Damien Georged17926d2014-03-30 13:35:08 +0100278 int op = reversed ? MP_BINARY_OP_MORE : MP_BINARY_OP_LESS;
Damiend99b0522013-12-21 18:17:45 +0000279 while (head < tail) {
280 mp_obj_t *h = head - 1;
281 mp_obj_t *t = tail;
Damien Georged17926d2014-03-30 13:35:08 +0100282 mp_obj_t v = key_fn == NULL ? tail[0] : mp_call_function_1(key_fn, tail[0]); // get pivot using key_fn
Damiend99b0522013-12-21 18:17:45 +0000283 for (;;) {
Damien Georged17926d2014-03-30 13:35:08 +0100284 do ++h; while (mp_binary_op(op, key_fn == NULL ? h[0] : mp_call_function_1(key_fn, h[0]), v) == mp_const_true);
285 do --t; while (h < t && mp_binary_op(op, v, key_fn == NULL ? t[0] : mp_call_function_1(key_fn, t[0])) == mp_const_true);
Damiend99b0522013-12-21 18:17:45 +0000286 if (h >= t) break;
287 mp_obj_t x = h[0];
288 h[0] = t[0];
289 t[0] = x;
290 }
291 mp_obj_t x = h[0];
292 h[0] = tail[0];
293 tail[0] = x;
John R. Lentonc06763a2014-01-07 17:29:16 +0000294 mp_quicksort(head, t, key_fn, reversed);
Damiend99b0522013-12-21 18:17:45 +0000295 head = h + 1;
296 }
297}
298
Damien George20006db2014-01-18 14:10:48 +0000299mp_obj_t mp_obj_list_sort(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
300 assert(n_args >= 1);
Damien George3e1a5c12014-03-29 13:43:38 +0000301 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
Damien George20006db2014-01-18 14:10:48 +0000302 if (n_args > 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100303 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lentonc06763a2014-01-07 17:29:16 +0000304 "list.sort takes no positional arguments"));
305 }
Damien George20006db2014-01-18 14:10:48 +0000306 mp_obj_list_t *self = args[0];
Damiend99b0522013-12-21 18:17:45 +0000307 if (self->len > 1) {
Damien George7d0bfbe2014-02-08 19:01:47 +0000308 mp_map_elem_t *keyfun = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_key), MP_MAP_LOOKUP);
309 mp_map_elem_t *reverse = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_reverse), MP_MAP_LOOKUP);
John R. Lentonc06763a2014-01-07 17:29:16 +0000310 mp_quicksort(self->items, self->items + self->len - 1,
311 keyfun ? keyfun->value : NULL,
Damien Georged17926d2014-03-30 13:35:08 +0100312 reverse && reverse->value ? mp_obj_is_true(reverse->value) : false);
Damiend99b0522013-12-21 18:17:45 +0000313 }
314 return mp_const_none; // return None, as per CPython
315}
316
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200317STATIC mp_obj_t list_clear(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000318 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton069ded92014-01-03 23:22:53 +0000319 mp_obj_list_t *self = self_in;
320 self->len = 0;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200321 self->items = m_renew(mp_obj_t, self->items, self->alloc, LIST_MIN_ALLOC);
322 self->alloc = LIST_MIN_ALLOC;
Paul Sokolovskya2240672014-04-28 00:16:57 +0300323 mp_seq_clear(self->items, 0, self->alloc, sizeof(*self->items));
John R. Lenton069ded92014-01-03 23:22:53 +0000324 return mp_const_none;
325}
326
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200327STATIC mp_obj_t list_copy(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000328 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton26c21162014-01-03 23:42:17 +0000329 mp_obj_list_t *self = self_in;
330 return mp_obj_new_list(self->len, self->items);
331}
332
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200333STATIC mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
Damien George3e1a5c12014-03-29 13:43:38 +0000334 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lentone241e8c2014-01-03 23:57:28 +0000335 mp_obj_list_t *self = self_in;
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200336 return mp_seq_count_obj(self->items, self->len, value);
John R. Lentone241e8c2014-01-03 23:57:28 +0000337}
338
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200339STATIC mp_obj_t list_index(uint n_args, const mp_obj_t *args) {
John R. Lenton5d4a8212014-01-04 00:26:30 +0000340 assert(2 <= n_args && n_args <= 4);
Damien George3e1a5c12014-03-29 13:43:38 +0000341 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
John R. Lenton5d4a8212014-01-04 00:26:30 +0000342 mp_obj_list_t *self = args[0];
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200343 return mp_seq_index_obj(self->items, self->len, n_args, args);
John R. Lenton5d4a8212014-01-04 00:26:30 +0000344}
345
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200346STATIC 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 +0000347 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton45a87442014-01-04 01:15:01 +0000348 mp_obj_list_t *self = self_in;
349 // insert has its own strange index logic
350 int index = MP_OBJ_SMALL_INT_VALUE(idx);
351 if (index < 0) {
352 index += self->len;
353 }
354 if (index < 0) {
355 index = 0;
356 }
357 if (index > self->len) {
358 index = self->len;
359 }
360
361 mp_obj_list_append(self_in, mp_const_none);
362
363 for (int i = self->len-1; i > index; i--) {
364 self->items[i] = self->items[i-1];
365 }
366 self->items[index] = obj;
367
368 return mp_const_none;
369}
370
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200371STATIC mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) {
Damien George3e1a5c12014-03-29 13:43:38 +0000372 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton49fb6e52014-01-04 01:56:53 +0000373 mp_obj_t args[] = {self_in, value};
374 args[1] = list_index(2, args);
375 list_pop(2, args);
376
377 return mp_const_none;
378}
379
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200380STATIC mp_obj_t list_reverse(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000381 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000382 mp_obj_list_t *self = self_in;
383
384 int len = self->len;
385 for (int i = 0; i < len/2; i++) {
386 mp_obj_t *a = self->items[i];
387 self->items[i] = self->items[len-i-1];
388 self->items[len-i-1] = a;
389 }
390
391 return mp_const_none;
392}
393
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200394STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
395STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_extend_obj, list_extend);
396STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
397STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
398STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
399STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
400STATIC MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
401STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
402STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
403STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
404STATIC MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 0, mp_obj_list_sort);
Damiend99b0522013-12-21 18:17:45 +0000405
Damien George9b196cd2014-03-26 21:47:19 +0000406STATIC const mp_map_elem_t list_locals_dict_table[] = {
407 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&list_append_obj },
408 { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&list_clear_obj },
409 { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&list_copy_obj },
410 { MP_OBJ_NEW_QSTR(MP_QSTR_count), (mp_obj_t)&list_count_obj },
411 { MP_OBJ_NEW_QSTR(MP_QSTR_extend), (mp_obj_t)&list_extend_obj },
412 { MP_OBJ_NEW_QSTR(MP_QSTR_index), (mp_obj_t)&list_index_obj },
413 { MP_OBJ_NEW_QSTR(MP_QSTR_insert), (mp_obj_t)&list_insert_obj },
414 { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&list_pop_obj },
415 { MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&list_remove_obj },
416 { MP_OBJ_NEW_QSTR(MP_QSTR_reverse), (mp_obj_t)&list_reverse_obj },
417 { MP_OBJ_NEW_QSTR(MP_QSTR_sort), (mp_obj_t)&list_sort_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800418};
Damien George97209d32014-01-07 15:58:30 +0000419
Damien George9b196cd2014-03-26 21:47:19 +0000420STATIC MP_DEFINE_CONST_DICT(list_locals_dict, list_locals_dict_table);
421
Damien George3e1a5c12014-03-29 13:43:38 +0000422const mp_obj_type_t mp_type_list = {
Damien Georgec5966122014-02-15 16:10:44 +0000423 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000424 .name = MP_QSTR_list,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200425 .print = list_print,
426 .make_new = list_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000427 .unary_op = list_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200428 .binary_op = list_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100429 .subscr = list_subscr,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200430 .getiter = list_getiter,
Damien George9b196cd2014-03-26 21:47:19 +0000431 .locals_dict = (mp_obj_t)&list_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000432};
433
Paul Sokolovsky18bef252014-04-13 06:17:29 +0300434void mp_obj_list_init(mp_obj_list_t *o, uint n) {
Damien George3e1a5c12014-03-29 13:43:38 +0000435 o->base.type = &mp_type_list;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200436 o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
Damiend99b0522013-12-21 18:17:45 +0000437 o->len = n;
438 o->items = m_new(mp_obj_t, o->alloc);
Paul Sokolovskya2240672014-04-28 00:16:57 +0300439 mp_seq_clear(o->items, n, o->alloc, sizeof(*o->items));
Paul Sokolovsky18bef252014-04-13 06:17:29 +0300440}
441
442STATIC mp_obj_list_t *list_new(uint n) {
443 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
444 mp_obj_list_init(o, n);
Damiend99b0522013-12-21 18:17:45 +0000445 return o;
446}
447
448mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
449 mp_obj_list_t *o = list_new(n);
Paul Sokolovskye5a15cb2014-02-04 09:44:10 +0200450 if (items != NULL) {
451 for (int i = 0; i < n; i++) {
452 o->items[i] = items[i];
453 }
Damiend99b0522013-12-21 18:17:45 +0000454 }
455 return o;
456}
457
Damiend99b0522013-12-21 18:17:45 +0000458void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
459 mp_obj_list_t *self = self_in;
460 *len = self->len;
461 *items = self->items;
462}
463
Damien George495d7812014-04-08 17:51:47 +0100464void mp_obj_list_set_len(mp_obj_t self_in, uint len) {
465 // trust that the caller knows what it's doing
466 // TODO realloc if len got much smaller than alloc
467 mp_obj_list_t *self = self_in;
468 self->len = len;
469}
470
Damiend99b0522013-12-21 18:17:45 +0000471void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
472 mp_obj_list_t *self = self_in;
xbe9e1e8cd2014-03-12 22:57:16 -0700473 uint i = mp_get_index(self->base.type, self->len, index, false);
Damiend99b0522013-12-21 18:17:45 +0000474 self->items[i] = value;
475}
476
477/******************************************************************************/
478/* list iterator */
479
480typedef struct _mp_obj_list_it_t {
481 mp_obj_base_t base;
482 mp_obj_list_t *list;
483 machine_uint_t cur;
484} mp_obj_list_it_t;
485
486mp_obj_t list_it_iternext(mp_obj_t self_in) {
487 mp_obj_list_it_t *self = self_in;
488 if (self->cur < self->list->len) {
489 mp_obj_t o_out = self->list->items[self->cur];
490 self->cur += 1;
491 return o_out;
492 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100493 return MP_OBJ_STOP_ITERATION;
Damiend99b0522013-12-21 18:17:45 +0000494 }
495}
496
Damien George3e1a5c12014-03-29 13:43:38 +0000497STATIC const mp_obj_type_t mp_type_list_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000498 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000499 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300500 .getiter = mp_identity,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200501 .iternext = list_it_iternext,
Damiend99b0522013-12-21 18:17:45 +0000502};
503
504mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
505 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000506 o->base.type = &mp_type_list_it;
Damiend99b0522013-12-21 18:17:45 +0000507 o->list = list;
508 o->cur = cur;
509 return o;
510}