blob: 0c55f524da39b7c5f8fa446e1edc9a4bfc2c8d7f [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#include <string.h>
2#include <assert.h>
3
4#include "nlr.h"
5#include "misc.h"
6#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00007#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +00008#include "obj.h"
9#include "runtime0.h"
10#include "runtime.h"
Paul Sokolovsky18bef252014-04-13 06:17:29 +030011#include "objlist.h"
Damiend99b0522013-12-21 18:17:45 +000012
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020013STATIC mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
14STATIC mp_obj_list_t *list_new(uint n);
15STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in);
Damien Georgef4c9b332014-04-08 21:32:29 +010016STATIC mp_obj_t list_pop(uint n_args, const mp_obj_t *args);
Damiend99b0522013-12-21 18:17:45 +000017
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +020018// TODO: Move to mpconfig.h
19#define LIST_MIN_ALLOC 4
20
Damiend99b0522013-12-21 18:17:45 +000021/******************************************************************************/
22/* list */
23
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020024STATIC 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 +000025 mp_obj_list_t *o = o_in;
26 print(env, "[");
27 for (int i = 0; i < o->len; i++) {
28 if (i > 0) {
29 print(env, ", ");
30 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020031 mp_obj_print_helper(print, env, o->items[i], PRINT_REPR);
Damiend99b0522013-12-21 18:17:45 +000032 }
33 print(env, "]");
34}
35
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +030036STATIC mp_obj_t list_extend_from_iter(mp_obj_t list, mp_obj_t iterable) {
37 mp_obj_t iter = mp_getiter(iterable);
38 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +010039 while ((item = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +030040 mp_obj_list_append(list, item);
41 }
42 return list;
43}
44
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020045STATIC 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 +000046 // TODO check n_kw == 0
47
Damien George71c51812014-01-04 20:21:15 +000048 switch (n_args) {
49 case 0:
50 // return a new, empty list
Damien Georgeebde0b82014-01-19 00:47:40 +000051 return mp_obj_new_list(0, NULL);
Damien George71c51812014-01-04 20:21:15 +000052
53 case 1:
54 {
55 // make list from iterable
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +030056 // TODO: optimize list/tuple
Damien Georgeebde0b82014-01-19 00:47:40 +000057 mp_obj_t list = mp_obj_new_list(0, NULL);
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +030058 return list_extend_from_iter(list, args[0]);
Damien George71c51812014-01-04 20:21:15 +000059 }
60
61 default:
Damien Georgeea13f402014-04-05 18:32:08 +010062 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 +000063 }
ian-v5fd8fd22014-01-06 13:51:53 -080064 return NULL;
Damien George71c51812014-01-04 20:21:15 +000065}
66
Damien Georged17926d2014-03-30 13:35:08 +010067// Don't pass MP_BINARY_OP_NOT_EQUAL here
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020068STATIC bool list_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
Damien George3e1a5c12014-03-29 13:43:38 +000069 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
70 if (!MP_OBJ_IS_TYPE(another_in, &mp_type_list)) {
Paul Sokolovsky1945e602014-01-12 02:01:00 +020071 return false;
72 }
73 mp_obj_list_t *self = self_in;
74 mp_obj_list_t *another = another_in;
Paul Sokolovsky1945e602014-01-12 02:01:00 +020075
Paul Sokolovsky1a996c42014-02-08 22:49:46 +020076 return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len);
Paul Sokolovsky1945e602014-01-12 02:01:00 +020077}
78
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020079STATIC mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
Damien George4e8dc8c2014-01-27 23:15:32 +000080 mp_obj_list_t *self = self_in;
81 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010082 case MP_UNARY_OP_BOOL: return MP_BOOL(self->len != 0);
83 case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
Damien Georgeea8d06c2014-04-17 23:19:36 +010084 default: return MP_OBJ_NOT_SUPPORTED;
Damien George4e8dc8c2014-01-27 23:15:32 +000085 }
86}
87
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020088STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damiend99b0522013-12-21 18:17:45 +000089 mp_obj_list_t *o = lhs;
90 switch (op) {
Damien George729f7b42014-04-17 22:10:53 +010091 case MP_BINARY_OP_ADD: {
Damien George3e1a5c12014-03-29 13:43:38 +000092 if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
John R. Lenton81ad89c2014-01-03 02:32:40 +000093 return NULL;
94 }
95 mp_obj_list_t *p = rhs;
96 mp_obj_list_t *s = list_new(o->len + p->len);
Paul Sokolovskyee4aaf72014-02-08 23:17:51 +020097 m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
John R. Lenton81ad89c2014-01-03 02:32:40 +000098 return s;
John R. Lenton4cb80582014-01-03 02:27:08 +000099 }
Damien George729f7b42014-04-17 22:10:53 +0100100 case MP_BINARY_OP_INPLACE_ADD: {
Damien George3e1a5c12014-03-29 13:43:38 +0000101 if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200102 return NULL;
103 }
104 list_extend(lhs, rhs);
105 return o;
106 }
Damien George8270e382014-04-03 11:00:54 +0000107 case MP_BINARY_OP_MULTIPLY: {
108 machine_int_t n;
109 if (!mp_obj_get_int_maybe(rhs, &n)) {
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200110 return NULL;
111 }
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200112 mp_obj_list_t *s = list_new(o->len * n);
113 mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200114 return s;
115 }
Damien Georged17926d2014-03-30 13:35:08 +0100116 case MP_BINARY_OP_EQUAL:
117 case MP_BINARY_OP_LESS:
118 case MP_BINARY_OP_LESS_EQUAL:
119 case MP_BINARY_OP_MORE:
120 case MP_BINARY_OP_MORE_EQUAL:
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200121 return MP_BOOL(list_cmp_helper(op, lhs, rhs));
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200122
Damiend99b0522013-12-21 18:17:45 +0000123 default:
Damien Georgeea8d06c2014-04-17 23:19:36 +0100124 return MP_OBJ_NOT_SUPPORTED;
Damiend99b0522013-12-21 18:17:45 +0000125 }
126}
127
Damien George729f7b42014-04-17 22:10:53 +0100128STATIC 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 +0100129 if (value == MP_OBJ_NULL) {
Damien George729f7b42014-04-17 22:10:53 +0100130 // delete
Damien Georgef4c9b332014-04-08 21:32:29 +0100131 mp_obj_t args[2] = {self_in, index};
132 list_pop(2, args);
Damien George729f7b42014-04-17 22:10:53 +0100133 return mp_const_none;
134 } else if (value == MP_OBJ_SENTINEL) {
135 // load
136 mp_obj_list_t *self = self_in;
137#if MICROPY_ENABLE_SLICE
138 if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
139 machine_uint_t start, stop;
140 if (!m_seq_get_fast_slice_indexes(self->len, index, &start, &stop)) {
141 assert(0);
142 }
143 mp_obj_list_t *res = list_new(stop - start);
144 m_seq_copy(res->items, self->items + start, res->len, mp_obj_t);
145 return res;
146 }
147#endif
148 uint index_val = mp_get_index(self->base.type, self->len, index, false);
149 return self->items[index_val];
Damien Georgef4c9b332014-04-08 21:32:29 +0100150 } else {
151 mp_obj_list_store(self_in, index, value);
Damien George729f7b42014-04-17 22:10:53 +0100152 return mp_const_none;
Damien Georgef4c9b332014-04-08 21:32:29 +0100153 }
Damien Georgef4c9b332014-04-08 21:32:29 +0100154}
155
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200156STATIC mp_obj_t list_getiter(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000157 return mp_obj_new_list_iterator(o_in, 0);
158}
159
160mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
Damien George3e1a5c12014-03-29 13:43:38 +0000161 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Damiend99b0522013-12-21 18:17:45 +0000162 mp_obj_list_t *self = self_in;
163 if (self->len >= self->alloc) {
Damien732407f2013-12-29 19:33:23 +0000164 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
Damiend99b0522013-12-21 18:17:45 +0000165 self->alloc *= 2;
Paul Sokolovskya2240672014-04-28 00:16:57 +0300166 mp_seq_clear(self->items, self->len + 1, self->alloc, sizeof(*self->items));
Damiend99b0522013-12-21 18:17:45 +0000167 }
168 self->items[self->len++] = arg;
169 return mp_const_none; // return None, as per CPython
170}
171
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200172STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000173 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +0300174 if (MP_OBJ_IS_TYPE(arg_in, &mp_type_list)) {
175 mp_obj_list_t *self = self_in;
176 mp_obj_list_t *arg = arg_in;
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200177
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +0300178 if (self->len + arg->len > self->alloc) {
179 // TODO: use alloc policy for "4"
180 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + arg->len + 4);
181 self->alloc = self->len + arg->len + 4;
Paul Sokolovskya2240672014-04-28 00:16:57 +0300182 mp_seq_clear(self->items, self->len + arg->len, self->alloc, sizeof(*self->items));
Paul Sokolovskyaa6666c2014-04-13 03:20:10 +0300183 }
184
185 memcpy(self->items + self->len, arg->items, sizeof(mp_obj_t) * arg->len);
186 self->len += arg->len;
187 } else {
188 list_extend_from_iter(self_in, arg_in);
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200189 }
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200190 return mp_const_none; // return None, as per CPython
191}
192
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200193STATIC mp_obj_t list_pop(uint n_args, const mp_obj_t *args) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000194 assert(1 <= n_args && n_args <= 2);
Damien George3e1a5c12014-03-29 13:43:38 +0000195 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
John R. Lenton25f417c2014-01-03 22:53:18 +0000196 mp_obj_list_t *self = args[0];
197 if (self->len == 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100198 nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "pop from empty list"));
John R. Lenton25f417c2014-01-03 22:53:18 +0000199 }
Paul Sokolovsky48bf6b32014-04-27 23:07:37 +0300200 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 +0000201 mp_obj_t ret = self->items[index];
202 self->len -= 1;
203 memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
Paul Sokolovskya2240672014-04-28 00:16:57 +0300204 // Clear stale pointer from slot which just got freed to prevent GC issues
205 self->items[self->len] = MP_OBJ_NULL;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200206 if (self->alloc > LIST_MIN_ALLOC && self->alloc > 2 * self->len) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000207 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
208 self->alloc /= 2;
209 }
Damiend99b0522013-12-21 18:17:45 +0000210 return ret;
211}
212
213// TODO make this conform to CPython's definition of sort
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200214STATIC 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 +0100215 int op = reversed ? MP_BINARY_OP_MORE : MP_BINARY_OP_LESS;
Damiend99b0522013-12-21 18:17:45 +0000216 while (head < tail) {
217 mp_obj_t *h = head - 1;
218 mp_obj_t *t = tail;
Damien Georged17926d2014-03-30 13:35:08 +0100219 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 +0000220 for (;;) {
Damien Georged17926d2014-03-30 13:35:08 +0100221 do ++h; while (mp_binary_op(op, key_fn == NULL ? h[0] : mp_call_function_1(key_fn, h[0]), v) == mp_const_true);
222 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 +0000223 if (h >= t) break;
224 mp_obj_t x = h[0];
225 h[0] = t[0];
226 t[0] = x;
227 }
228 mp_obj_t x = h[0];
229 h[0] = tail[0];
230 tail[0] = x;
John R. Lentonc06763a2014-01-07 17:29:16 +0000231 mp_quicksort(head, t, key_fn, reversed);
Damiend99b0522013-12-21 18:17:45 +0000232 head = h + 1;
233 }
234}
235
Damien George20006db2014-01-18 14:10:48 +0000236mp_obj_t mp_obj_list_sort(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
237 assert(n_args >= 1);
Damien George3e1a5c12014-03-29 13:43:38 +0000238 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
Damien George20006db2014-01-18 14:10:48 +0000239 if (n_args > 1) {
Damien Georgeea13f402014-04-05 18:32:08 +0100240 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lentonc06763a2014-01-07 17:29:16 +0000241 "list.sort takes no positional arguments"));
242 }
Damien George20006db2014-01-18 14:10:48 +0000243 mp_obj_list_t *self = args[0];
Damiend99b0522013-12-21 18:17:45 +0000244 if (self->len > 1) {
Damien George7d0bfbe2014-02-08 19:01:47 +0000245 mp_map_elem_t *keyfun = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_key), MP_MAP_LOOKUP);
246 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 +0000247 mp_quicksort(self->items, self->items + self->len - 1,
248 keyfun ? keyfun->value : NULL,
Damien Georged17926d2014-03-30 13:35:08 +0100249 reverse && reverse->value ? mp_obj_is_true(reverse->value) : false);
Damiend99b0522013-12-21 18:17:45 +0000250 }
251 return mp_const_none; // return None, as per CPython
252}
253
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200254STATIC mp_obj_t list_clear(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000255 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton069ded92014-01-03 23:22:53 +0000256 mp_obj_list_t *self = self_in;
257 self->len = 0;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200258 self->items = m_renew(mp_obj_t, self->items, self->alloc, LIST_MIN_ALLOC);
259 self->alloc = LIST_MIN_ALLOC;
Paul Sokolovskya2240672014-04-28 00:16:57 +0300260 mp_seq_clear(self->items, 0, self->alloc, sizeof(*self->items));
John R. Lenton069ded92014-01-03 23:22:53 +0000261 return mp_const_none;
262}
263
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200264STATIC mp_obj_t list_copy(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000265 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton26c21162014-01-03 23:42:17 +0000266 mp_obj_list_t *self = self_in;
267 return mp_obj_new_list(self->len, self->items);
268}
269
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200270STATIC mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
Damien George3e1a5c12014-03-29 13:43:38 +0000271 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lentone241e8c2014-01-03 23:57:28 +0000272 mp_obj_list_t *self = self_in;
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200273 return mp_seq_count_obj(self->items, self->len, value);
John R. Lentone241e8c2014-01-03 23:57:28 +0000274}
275
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200276STATIC mp_obj_t list_index(uint n_args, const mp_obj_t *args) {
John R. Lenton5d4a8212014-01-04 00:26:30 +0000277 assert(2 <= n_args && n_args <= 4);
Damien George3e1a5c12014-03-29 13:43:38 +0000278 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
John R. Lenton5d4a8212014-01-04 00:26:30 +0000279 mp_obj_list_t *self = args[0];
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200280 return mp_seq_index_obj(self->items, self->len, n_args, args);
John R. Lenton5d4a8212014-01-04 00:26:30 +0000281}
282
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200283STATIC 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 +0000284 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton45a87442014-01-04 01:15:01 +0000285 mp_obj_list_t *self = self_in;
286 // insert has its own strange index logic
287 int index = MP_OBJ_SMALL_INT_VALUE(idx);
288 if (index < 0) {
289 index += self->len;
290 }
291 if (index < 0) {
292 index = 0;
293 }
294 if (index > self->len) {
295 index = self->len;
296 }
297
298 mp_obj_list_append(self_in, mp_const_none);
299
300 for (int i = self->len-1; i > index; i--) {
301 self->items[i] = self->items[i-1];
302 }
303 self->items[index] = obj;
304
305 return mp_const_none;
306}
307
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200308STATIC mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) {
Damien George3e1a5c12014-03-29 13:43:38 +0000309 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton49fb6e52014-01-04 01:56:53 +0000310 mp_obj_t args[] = {self_in, value};
311 args[1] = list_index(2, args);
312 list_pop(2, args);
313
314 return mp_const_none;
315}
316
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200317STATIC mp_obj_t list_reverse(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. Lenton6e1e98f2014-01-04 02:10:29 +0000319 mp_obj_list_t *self = self_in;
320
321 int len = self->len;
322 for (int i = 0; i < len/2; i++) {
323 mp_obj_t *a = self->items[i];
324 self->items[i] = self->items[len-i-1];
325 self->items[len-i-1] = a;
326 }
327
328 return mp_const_none;
329}
330
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200331STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
332STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_extend_obj, list_extend);
333STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
334STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
335STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
336STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
337STATIC MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
338STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
339STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
340STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
341STATIC MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 0, mp_obj_list_sort);
Damiend99b0522013-12-21 18:17:45 +0000342
Damien George9b196cd2014-03-26 21:47:19 +0000343STATIC const mp_map_elem_t list_locals_dict_table[] = {
344 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&list_append_obj },
345 { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&list_clear_obj },
346 { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&list_copy_obj },
347 { MP_OBJ_NEW_QSTR(MP_QSTR_count), (mp_obj_t)&list_count_obj },
348 { MP_OBJ_NEW_QSTR(MP_QSTR_extend), (mp_obj_t)&list_extend_obj },
349 { MP_OBJ_NEW_QSTR(MP_QSTR_index), (mp_obj_t)&list_index_obj },
350 { MP_OBJ_NEW_QSTR(MP_QSTR_insert), (mp_obj_t)&list_insert_obj },
351 { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&list_pop_obj },
352 { MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&list_remove_obj },
353 { MP_OBJ_NEW_QSTR(MP_QSTR_reverse), (mp_obj_t)&list_reverse_obj },
354 { MP_OBJ_NEW_QSTR(MP_QSTR_sort), (mp_obj_t)&list_sort_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800355};
Damien George97209d32014-01-07 15:58:30 +0000356
Damien George9b196cd2014-03-26 21:47:19 +0000357STATIC MP_DEFINE_CONST_DICT(list_locals_dict, list_locals_dict_table);
358
Damien George3e1a5c12014-03-29 13:43:38 +0000359const mp_obj_type_t mp_type_list = {
Damien Georgec5966122014-02-15 16:10:44 +0000360 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000361 .name = MP_QSTR_list,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200362 .print = list_print,
363 .make_new = list_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000364 .unary_op = list_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200365 .binary_op = list_binary_op,
Damien George729f7b42014-04-17 22:10:53 +0100366 .subscr = list_subscr,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200367 .getiter = list_getiter,
Damien George9b196cd2014-03-26 21:47:19 +0000368 .locals_dict = (mp_obj_t)&list_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000369};
370
Paul Sokolovsky18bef252014-04-13 06:17:29 +0300371void mp_obj_list_init(mp_obj_list_t *o, uint n) {
Damien George3e1a5c12014-03-29 13:43:38 +0000372 o->base.type = &mp_type_list;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200373 o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
Damiend99b0522013-12-21 18:17:45 +0000374 o->len = n;
375 o->items = m_new(mp_obj_t, o->alloc);
Paul Sokolovskya2240672014-04-28 00:16:57 +0300376 mp_seq_clear(o->items, n, o->alloc, sizeof(*o->items));
Paul Sokolovsky18bef252014-04-13 06:17:29 +0300377}
378
379STATIC mp_obj_list_t *list_new(uint n) {
380 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
381 mp_obj_list_init(o, n);
Damiend99b0522013-12-21 18:17:45 +0000382 return o;
383}
384
385mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
386 mp_obj_list_t *o = list_new(n);
Paul Sokolovskye5a15cb2014-02-04 09:44:10 +0200387 if (items != NULL) {
388 for (int i = 0; i < n; i++) {
389 o->items[i] = items[i];
390 }
Damiend99b0522013-12-21 18:17:45 +0000391 }
392 return o;
393}
394
Damiend99b0522013-12-21 18:17:45 +0000395void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
396 mp_obj_list_t *self = self_in;
397 *len = self->len;
398 *items = self->items;
399}
400
Damien George495d7812014-04-08 17:51:47 +0100401void mp_obj_list_set_len(mp_obj_t self_in, uint len) {
402 // trust that the caller knows what it's doing
403 // TODO realloc if len got much smaller than alloc
404 mp_obj_list_t *self = self_in;
405 self->len = len;
406}
407
Damiend99b0522013-12-21 18:17:45 +0000408void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
409 mp_obj_list_t *self = self_in;
xbe9e1e8cd2014-03-12 22:57:16 -0700410 uint i = mp_get_index(self->base.type, self->len, index, false);
Damiend99b0522013-12-21 18:17:45 +0000411 self->items[i] = value;
412}
413
414/******************************************************************************/
415/* list iterator */
416
417typedef struct _mp_obj_list_it_t {
418 mp_obj_base_t base;
419 mp_obj_list_t *list;
420 machine_uint_t cur;
421} mp_obj_list_it_t;
422
423mp_obj_t list_it_iternext(mp_obj_t self_in) {
424 mp_obj_list_it_t *self = self_in;
425 if (self->cur < self->list->len) {
426 mp_obj_t o_out = self->list->items[self->cur];
427 self->cur += 1;
428 return o_out;
429 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100430 return MP_OBJ_STOP_ITERATION;
Damiend99b0522013-12-21 18:17:45 +0000431 }
432}
433
Damien George3e1a5c12014-03-29 13:43:38 +0000434STATIC const mp_obj_type_t mp_type_list_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000435 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000436 .name = MP_QSTR_iterator,
Paul Sokolovskyf7eaf602014-03-30 22:00:12 +0300437 .getiter = mp_identity,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200438 .iternext = list_it_iternext,
Damiend99b0522013-12-21 18:17:45 +0000439};
440
441mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
442 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000443 o->base.type = &mp_type_list_it;
Damiend99b0522013-12-21 18:17:45 +0000444 o->list = list;
445 o->cur = cur;
446 return o;
447}