blob: aa857e41c285fb584d3c16d24c9066e29ca9fc34 [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#include <stdlib.h>
2#include <stdint.h>
3#include <string.h>
4#include <assert.h>
5
6#include "nlr.h"
7#include "misc.h"
8#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00009#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "obj.h"
John R. Lentonc06763a2014-01-07 17:29:16 +000011#include "map.h"
Damiend99b0522013-12-21 18:17:45 +000012#include "runtime0.h"
13#include "runtime.h"
14
15typedef struct _mp_obj_list_t {
16 mp_obj_base_t base;
17 machine_uint_t alloc;
18 machine_uint_t len;
19 mp_obj_t *items;
20} mp_obj_list_t;
21
22static mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
John R. Lenton4cb80582014-01-03 02:27:08 +000023static mp_obj_list_t *list_new(uint n);
Paul Sokolovskyc698d262014-01-12 00:51:05 +020024static mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in);
Damiend99b0522013-12-21 18:17:45 +000025
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +020026// TODO: Move to mpconfig.h
27#define LIST_MIN_ALLOC 4
28
Damiend99b0522013-12-21 18:17:45 +000029/******************************************************************************/
30/* list */
31
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020032static 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 +000033 mp_obj_list_t *o = o_in;
34 print(env, "[");
35 for (int i = 0; i < o->len; i++) {
36 if (i > 0) {
37 print(env, ", ");
38 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020039 mp_obj_print_helper(print, env, o->items[i], PRINT_REPR);
Damiend99b0522013-12-21 18:17:45 +000040 }
41 print(env, "]");
42}
43
Damien George20006db2014-01-18 14:10:48 +000044static mp_obj_t list_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
45 // TODO check n_kw == 0
46
Damien George71c51812014-01-04 20:21:15 +000047 switch (n_args) {
48 case 0:
49 // return a new, empty list
Damien Georgeebde0b82014-01-19 00:47:40 +000050 return mp_obj_new_list(0, NULL);
Damien George71c51812014-01-04 20:21:15 +000051
52 case 1:
53 {
54 // make list from iterable
55 mp_obj_t iterable = rt_getiter(args[0]);
Damien Georgeebde0b82014-01-19 00:47:40 +000056 mp_obj_t list = mp_obj_new_list(0, NULL);
Damien George71c51812014-01-04 20:21:15 +000057 mp_obj_t item;
58 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
Damien Georgeebde0b82014-01-19 00:47:40 +000059 mp_obj_list_append(list, item);
Damien George71c51812014-01-04 20:21:15 +000060 }
61 return list;
62 }
63
64 default:
65 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "list takes at most 1 argument, %d given", (void*)(machine_int_t)n_args));
66 }
ian-v5fd8fd22014-01-06 13:51:53 -080067 return NULL;
Damien George71c51812014-01-04 20:21:15 +000068}
69
Damien George9aa2a522014-02-01 23:04:09 +000070// Don't pass RT_BINARY_OP_NOT_EQUAL here
Paul Sokolovsky1945e602014-01-12 02:01:00 +020071static bool list_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
72 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
73 if (!MP_OBJ_IS_TYPE(another_in, &list_type)) {
74 return false;
75 }
76 mp_obj_list_t *self = self_in;
77 mp_obj_list_t *another = another_in;
Damien George9aa2a522014-02-01 23:04:09 +000078 if (op == RT_BINARY_OP_EQUAL && self->len != another->len) {
Paul Sokolovsky1945e602014-01-12 02:01:00 +020079 return false;
80 }
81
82 // Let's deal only with > & >=
Damien George9aa2a522014-02-01 23:04:09 +000083 if (op == RT_BINARY_OP_LESS || op == RT_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky1945e602014-01-12 02:01:00 +020084 mp_obj_t t = self;
85 self = another;
86 another = t;
Damien George9aa2a522014-02-01 23:04:09 +000087 if (op == RT_BINARY_OP_LESS) {
88 op = RT_BINARY_OP_MORE;
Paul Sokolovsky1945e602014-01-12 02:01:00 +020089 } else {
Damien George9aa2a522014-02-01 23:04:09 +000090 op = RT_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky1945e602014-01-12 02:01:00 +020091 }
92 }
93
94 int len = self->len < another->len ? self->len : another->len;
95 bool eq_status = true; // empty lists are equal
96 bool rel_status;
97 for (int i = 0; i < len; i++) {
98 eq_status = mp_obj_equal(self->items[i], another->items[i]);
Damien George9aa2a522014-02-01 23:04:09 +000099 if (op == RT_BINARY_OP_EQUAL && !eq_status) {
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200100 return false;
101 }
102 rel_status = (rt_binary_op(op, self->items[i], another->items[i]) == mp_const_true);
103 if (!eq_status && !rel_status) {
104 return false;
105 }
106 }
107
108 // If we had tie in the last element...
109 if (eq_status) {
110 // ... and we have lists of different lengths...
111 if (self->len != another->len) {
112 if (self->len < another->len) {
113 // ... then longer list length wins (we deal only with >)
114 return false;
115 }
Damien George9aa2a522014-02-01 23:04:09 +0000116 } else if (op == RT_BINARY_OP_MORE) {
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200117 // Otherwise, if we have strict relation, equality means failure
118 return false;
119 }
120 }
121
122 return true;
123}
124
Damien George4e8dc8c2014-01-27 23:15:32 +0000125static mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
126 mp_obj_list_t *self = self_in;
127 switch (op) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +0200128 case RT_UNARY_OP_BOOL: return MP_BOOL(self->len != 0);
129 case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
Damien George4e8dc8c2014-01-27 23:15:32 +0000130 default: return MP_OBJ_NULL; // op not supported for None
131 }
132}
133
Damiend99b0522013-12-21 18:17:45 +0000134static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
135 mp_obj_list_t *o = lhs;
136 switch (op) {
137 case RT_BINARY_OP_SUBSCR:
138 {
Paul Sokolovsky13cfabd2014-02-02 03:32:55 +0200139#if MICROPY_ENABLE_SLICE
140 if (MP_OBJ_IS_TYPE(rhs, &slice_type)) {
141 machine_uint_t start, stop;
Paul Sokolovskyea2509d2014-02-02 08:57:05 +0200142 if (!m_seq_get_fast_slice_indexes(o->len, rhs, &start, &stop)) {
143 assert(0);
144 }
Paul Sokolovsky13cfabd2014-02-02 03:32:55 +0200145 mp_obj_list_t *res = list_new(stop - start);
146 m_seq_copy(res->items, o->items + start, res->len, mp_obj_t);
147 return res;
148 }
149#endif
Damiend99b0522013-12-21 18:17:45 +0000150 uint index = mp_get_index(o->base.type, o->len, rhs);
151 return o->items[index];
152 }
John R. Lenton4cb80582014-01-03 02:27:08 +0000153 case RT_BINARY_OP_ADD:
154 {
John R. Lenton81ad89c2014-01-03 02:32:40 +0000155 if (!MP_OBJ_IS_TYPE(rhs, &list_type)) {
156 return NULL;
157 }
158 mp_obj_list_t *p = rhs;
159 mp_obj_list_t *s = list_new(o->len + p->len);
John R. Lenton9bc56d92014-01-03 10:13:38 +0000160 memcpy(s->items, o->items, sizeof(mp_obj_t) * o->len);
161 memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len);
John R. Lenton81ad89c2014-01-03 02:32:40 +0000162 return s;
John R. Lenton4cb80582014-01-03 02:27:08 +0000163 }
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200164 case RT_BINARY_OP_INPLACE_ADD:
165 {
166 if (!MP_OBJ_IS_TYPE(rhs, &list_type)) {
167 return NULL;
168 }
169 list_extend(lhs, rhs);
170 return o;
171 }
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200172 case RT_BINARY_OP_MULTIPLY:
173 {
174 if (!MP_OBJ_IS_SMALL_INT(rhs)) {
175 return NULL;
176 }
177 int n = MP_OBJ_SMALL_INT_VALUE(rhs);
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200178 mp_obj_list_t *s = list_new(o->len * n);
179 mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200180 return s;
181 }
Damien George9aa2a522014-02-01 23:04:09 +0000182 case RT_BINARY_OP_EQUAL:
183 case RT_BINARY_OP_LESS:
184 case RT_BINARY_OP_LESS_EQUAL:
185 case RT_BINARY_OP_MORE:
186 case RT_BINARY_OP_MORE_EQUAL:
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200187 return MP_BOOL(list_cmp_helper(op, lhs, rhs));
Damien George9aa2a522014-02-01 23:04:09 +0000188 case RT_BINARY_OP_NOT_EQUAL:
189 return MP_BOOL(!list_cmp_helper(RT_BINARY_OP_EQUAL, lhs, rhs));
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200190
Damiend99b0522013-12-21 18:17:45 +0000191 default:
192 // op not supported
193 return NULL;
194 }
195}
196
197static mp_obj_t list_getiter(mp_obj_t o_in) {
198 return mp_obj_new_list_iterator(o_in, 0);
199}
200
201mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
202 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
203 mp_obj_list_t *self = self_in;
204 if (self->len >= self->alloc) {
Damien732407f2013-12-29 19:33:23 +0000205 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200206 assert(self->items);
Damiend99b0522013-12-21 18:17:45 +0000207 self->alloc *= 2;
Damiend99b0522013-12-21 18:17:45 +0000208 }
209 self->items[self->len++] = arg;
210 return mp_const_none; // return None, as per CPython
211}
212
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200213static mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
214 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
215 assert(MP_OBJ_IS_TYPE(arg_in, &list_type));
216 mp_obj_list_t *self = self_in;
217 mp_obj_list_t *arg = arg_in;
218
219 if (self->len + arg->len > self->alloc) {
220 // TODO: use alloc policy for "4"
221 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + arg->len + 4);
222 self->alloc = self->len + arg->len + 4;
223 }
224
225 memcpy(self->items + self->len, arg->items, sizeof(mp_obj_t) * arg->len);
226 self->len += arg->len;
227 return mp_const_none; // return None, as per CPython
228}
229
Damien Georgea11ceca2014-01-19 16:02:09 +0000230static mp_obj_t list_pop(uint n_args, const mp_obj_t *args) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000231 assert(1 <= n_args && n_args <= 2);
232 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
233 mp_obj_list_t *self = args[0];
234 if (self->len == 0) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000235 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "pop from empty list"));
John R. Lenton25f417c2014-01-03 22:53:18 +0000236 }
237 uint index = mp_get_index(self->base.type, self->len, n_args == 1 ? mp_obj_new_int(-1) : args[1]);
Damiend99b0522013-12-21 18:17:45 +0000238 mp_obj_t ret = self->items[index];
239 self->len -= 1;
240 memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200241 if (self->alloc > LIST_MIN_ALLOC && self->alloc > 2 * self->len) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000242 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
243 self->alloc /= 2;
244 }
Damiend99b0522013-12-21 18:17:45 +0000245 return ret;
246}
247
248// TODO make this conform to CPython's definition of sort
John R. Lentonc06763a2014-01-07 17:29:16 +0000249static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool reversed) {
Damien George9aa2a522014-02-01 23:04:09 +0000250 int op = reversed ? RT_BINARY_OP_MORE : RT_BINARY_OP_LESS;
Damiend99b0522013-12-21 18:17:45 +0000251 while (head < tail) {
252 mp_obj_t *h = head - 1;
253 mp_obj_t *t = tail;
John R. Lentonc06763a2014-01-07 17:29:16 +0000254 mp_obj_t v = key_fn == NULL ? tail[0] : rt_call_function_1(key_fn, tail[0]); // get pivot using key_fn
Damiend99b0522013-12-21 18:17:45 +0000255 for (;;) {
John R. Lentonb8698fc2014-01-11 00:58:59 +0000256 do ++h; while (rt_binary_op(op, key_fn == NULL ? h[0] : rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
257 do --t; while (h < t && rt_binary_op(op, v, key_fn == NULL ? t[0] : rt_call_function_1(key_fn, t[0])) == mp_const_true);
Damiend99b0522013-12-21 18:17:45 +0000258 if (h >= t) break;
259 mp_obj_t x = h[0];
260 h[0] = t[0];
261 t[0] = x;
262 }
263 mp_obj_t x = h[0];
264 h[0] = tail[0];
265 tail[0] = x;
John R. Lentonc06763a2014-01-07 17:29:16 +0000266 mp_quicksort(head, t, key_fn, reversed);
Damiend99b0522013-12-21 18:17:45 +0000267 head = h + 1;
268 }
269}
270
Damien George20006db2014-01-18 14:10:48 +0000271mp_obj_t mp_obj_list_sort(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
272 assert(n_args >= 1);
273 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
274 if (n_args > 1) {
John R. Lentonc06763a2014-01-07 17:29:16 +0000275 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
276 "list.sort takes no positional arguments"));
277 }
Damien George20006db2014-01-18 14:10:48 +0000278 mp_obj_list_t *self = args[0];
Damiend99b0522013-12-21 18:17:45 +0000279 if (self->len > 1) {
Damien George55baff42014-01-21 21:40:13 +0000280 mp_map_elem_t *keyfun = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(QSTR_FROM_STR_STATIC("key")), MP_MAP_LOOKUP);
281 mp_map_elem_t *reverse = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(QSTR_FROM_STR_STATIC("reverse")), MP_MAP_LOOKUP);
John R. Lentonc06763a2014-01-07 17:29:16 +0000282 mp_quicksort(self->items, self->items + self->len - 1,
283 keyfun ? keyfun->value : NULL,
284 reverse && reverse->value ? rt_is_true(reverse->value) : false);
Damiend99b0522013-12-21 18:17:45 +0000285 }
286 return mp_const_none; // return None, as per CPython
287}
288
John R. Lenton069ded92014-01-03 23:22:53 +0000289static mp_obj_t list_clear(mp_obj_t self_in) {
290 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
291 mp_obj_list_t *self = self_in;
292 self->len = 0;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200293 self->items = m_renew(mp_obj_t, self->items, self->alloc, LIST_MIN_ALLOC);
294 self->alloc = LIST_MIN_ALLOC;
John R. Lenton069ded92014-01-03 23:22:53 +0000295 return mp_const_none;
296}
297
John R. Lenton26c21162014-01-03 23:42:17 +0000298static mp_obj_t list_copy(mp_obj_t self_in) {
299 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
300 mp_obj_list_t *self = self_in;
301 return mp_obj_new_list(self->len, self->items);
302}
303
John R. Lentone241e8c2014-01-03 23:57:28 +0000304static mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
305 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
306 mp_obj_list_t *self = self_in;
307 int count = 0;
308 for (int i = 0; i < self->len; i++) {
309 if (mp_obj_equal(self->items[i], value)) {
310 count++;
311 }
312 }
313
314 return mp_obj_new_int(count);
315}
316
Damien Georgea11ceca2014-01-19 16:02:09 +0000317static mp_obj_t list_index(uint n_args, const mp_obj_t *args) {
John R. Lenton5d4a8212014-01-04 00:26:30 +0000318 assert(2 <= n_args && n_args <= 4);
319 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
320 mp_obj_list_t *self = args[0];
321 mp_obj_t *value = args[1];
John R. Lentonc5531622014-01-05 21:57:27 +0000322 uint start = 0;
323 uint stop = self->len;
John R. Lenton5d4a8212014-01-04 00:26:30 +0000324
John R. Lentonc5531622014-01-05 21:57:27 +0000325 if (n_args >= 3) {
326 start = mp_get_index(self->base.type, self->len, args[2]);
327 if (n_args >= 4) {
328 stop = mp_get_index(self->base.type, self->len, args[3]);
329 }
330 }
John R. Lenton5d4a8212014-01-04 00:26:30 +0000331
John R. Lentonc5531622014-01-05 21:57:27 +0000332 for (uint i = start; i < stop; i++) {
John R. Lenton5d4a8212014-01-04 00:26:30 +0000333 if (mp_obj_equal(self->items[i], value)) {
334 return mp_obj_new_int(i);
335 }
336 }
337
Damien Georgef0691f42014-01-05 13:44:06 +0000338 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "object not in list"));
John R. Lenton5d4a8212014-01-04 00:26:30 +0000339}
340
John R. Lenton45a87442014-01-04 01:15:01 +0000341static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
342 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
343 mp_obj_list_t *self = self_in;
344 // insert has its own strange index logic
345 int index = MP_OBJ_SMALL_INT_VALUE(idx);
346 if (index < 0) {
347 index += self->len;
348 }
349 if (index < 0) {
350 index = 0;
351 }
352 if (index > self->len) {
353 index = self->len;
354 }
355
356 mp_obj_list_append(self_in, mp_const_none);
357
358 for (int i = self->len-1; i > index; i--) {
359 self->items[i] = self->items[i-1];
360 }
361 self->items[index] = obj;
362
363 return mp_const_none;
364}
365
John R. Lenton49fb6e52014-01-04 01:56:53 +0000366static mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) {
367 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
368 mp_obj_t args[] = {self_in, value};
369 args[1] = list_index(2, args);
370 list_pop(2, args);
371
372 return mp_const_none;
373}
374
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000375static mp_obj_t list_reverse(mp_obj_t self_in) {
376 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
377 mp_obj_list_t *self = self_in;
378
379 int len = self->len;
380 for (int i = 0; i < len/2; i++) {
381 mp_obj_t *a = self->items[i];
382 self->items[i] = self->items[len-i-1];
383 self->items[len-i-1] = a;
384 }
385
386 return mp_const_none;
387}
388
Damiend99b0522013-12-21 18:17:45 +0000389static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200390static MP_DEFINE_CONST_FUN_OBJ_2(list_extend_obj, list_extend);
John R. Lenton069ded92014-01-03 23:22:53 +0000391static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
John R. Lenton26c21162014-01-03 23:42:17 +0000392static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
John R. Lentone241e8c2014-01-03 23:57:28 +0000393static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
John R. Lenton5d4a8212014-01-04 00:26:30 +0000394static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
John R. Lenton45a87442014-01-04 01:15:01 +0000395static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
John R. Lenton25f417c2014-01-03 22:53:18 +0000396static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
John R. Lenton49fb6e52014-01-04 01:56:53 +0000397static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000398static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
Damien George0f592032014-01-14 23:18:35 +0000399static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 0, mp_obj_list_sort);
Damiend99b0522013-12-21 18:17:45 +0000400
ian-va5a01df2014-01-06 14:14:11 -0800401static const mp_method_t list_type_methods[] = {
ian-v7a16fad2014-01-06 09:52:29 -0800402 { "append", &list_append_obj },
403 { "clear", &list_clear_obj },
404 { "copy", &list_copy_obj },
405 { "count", &list_count_obj },
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200406 { "extend", &list_extend_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800407 { "index", &list_index_obj },
ian-v5fd8fd22014-01-06 13:51:53 -0800408 { "insert", &list_insert_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800409 { "pop", &list_pop_obj },
410 { "remove", &list_remove_obj },
411 { "reverse", &list_reverse_obj },
412 { "sort", &list_sort_obj },
413 { NULL, NULL }, // end-of-list sentinel
414};
Damien George97209d32014-01-07 15:58:30 +0000415
Damiend99b0522013-12-21 18:17:45 +0000416const mp_obj_type_t list_type = {
John R. Lenton3391e192014-01-07 18:06:34 +0000417 { &mp_const_type },
418 "list",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200419 .print = list_print,
420 .make_new = list_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000421 .unary_op = list_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200422 .binary_op = list_binary_op,
423 .getiter = list_getiter,
ian-v7a16fad2014-01-06 09:52:29 -0800424 .methods = list_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000425};
426
427static mp_obj_list_t *list_new(uint n) {
428 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
429 o->base.type = &list_type;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200430 o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
Damiend99b0522013-12-21 18:17:45 +0000431 o->len = n;
432 o->items = m_new(mp_obj_t, o->alloc);
433 return o;
434}
435
436mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
437 mp_obj_list_t *o = list_new(n);
Paul Sokolovskye5a15cb2014-02-04 09:44:10 +0200438 if (items != NULL) {
439 for (int i = 0; i < n; i++) {
440 o->items[i] = items[i];
441 }
Damiend99b0522013-12-21 18:17:45 +0000442 }
443 return o;
444}
445
Damiend99b0522013-12-21 18:17:45 +0000446void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
447 mp_obj_list_t *self = self_in;
448 *len = self->len;
449 *items = self->items;
450}
451
452void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
453 mp_obj_list_t *self = self_in;
454 uint i = mp_get_index(self->base.type, self->len, index);
455 self->items[i] = value;
456}
457
458/******************************************************************************/
459/* list iterator */
460
461typedef struct _mp_obj_list_it_t {
462 mp_obj_base_t base;
463 mp_obj_list_t *list;
464 machine_uint_t cur;
465} mp_obj_list_it_t;
466
467mp_obj_t list_it_iternext(mp_obj_t self_in) {
468 mp_obj_list_it_t *self = self_in;
469 if (self->cur < self->list->len) {
470 mp_obj_t o_out = self->list->items[self->cur];
471 self->cur += 1;
472 return o_out;
473 } else {
474 return mp_const_stop_iteration;
475 }
476}
477
478static const mp_obj_type_t list_it_type = {
John R. Lenton3391e192014-01-07 18:06:34 +0000479 { &mp_const_type },
480 "list_iterator",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200481 .iternext = list_it_iternext,
Damiend99b0522013-12-21 18:17:45 +0000482};
483
484mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
485 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
486 o->base.type = &list_it_type;
487 o->list = list;
488 o->cur = cur;
489 return o;
490}