blob: 29d0c92c52ba2b6502d1a59ce83ec835f0f620d7 [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;
Paul Sokolovsky1945e602014-01-12 02:01:00 +020078
Paul Sokolovsky1a996c42014-02-08 22:49:46 +020079 return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len);
Paul Sokolovsky1945e602014-01-12 02:01:00 +020080}
81
Damien George4e8dc8c2014-01-27 23:15:32 +000082static mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
83 mp_obj_list_t *self = self_in;
84 switch (op) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +020085 case RT_UNARY_OP_BOOL: return MP_BOOL(self->len != 0);
86 case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
Damien George4e8dc8c2014-01-27 23:15:32 +000087 default: return MP_OBJ_NULL; // op not supported for None
88 }
89}
90
Damiend99b0522013-12-21 18:17:45 +000091static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
92 mp_obj_list_t *o = lhs;
93 switch (op) {
94 case RT_BINARY_OP_SUBSCR:
95 {
Paul Sokolovsky13cfabd2014-02-02 03:32:55 +020096#if MICROPY_ENABLE_SLICE
97 if (MP_OBJ_IS_TYPE(rhs, &slice_type)) {
98 machine_uint_t start, stop;
Paul Sokolovskyea2509d2014-02-02 08:57:05 +020099 if (!m_seq_get_fast_slice_indexes(o->len, rhs, &start, &stop)) {
100 assert(0);
101 }
Paul Sokolovsky13cfabd2014-02-02 03:32:55 +0200102 mp_obj_list_t *res = list_new(stop - start);
103 m_seq_copy(res->items, o->items + start, res->len, mp_obj_t);
104 return res;
105 }
106#endif
Damiend99b0522013-12-21 18:17:45 +0000107 uint index = mp_get_index(o->base.type, o->len, rhs);
108 return o->items[index];
109 }
John R. Lenton4cb80582014-01-03 02:27:08 +0000110 case RT_BINARY_OP_ADD:
111 {
John R. Lenton81ad89c2014-01-03 02:32:40 +0000112 if (!MP_OBJ_IS_TYPE(rhs, &list_type)) {
113 return NULL;
114 }
115 mp_obj_list_t *p = rhs;
116 mp_obj_list_t *s = list_new(o->len + p->len);
John R. Lenton9bc56d92014-01-03 10:13:38 +0000117 memcpy(s->items, o->items, sizeof(mp_obj_t) * o->len);
118 memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len);
John R. Lenton81ad89c2014-01-03 02:32:40 +0000119 return s;
John R. Lenton4cb80582014-01-03 02:27:08 +0000120 }
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200121 case RT_BINARY_OP_INPLACE_ADD:
122 {
123 if (!MP_OBJ_IS_TYPE(rhs, &list_type)) {
124 return NULL;
125 }
126 list_extend(lhs, rhs);
127 return o;
128 }
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200129 case RT_BINARY_OP_MULTIPLY:
130 {
131 if (!MP_OBJ_IS_SMALL_INT(rhs)) {
132 return NULL;
133 }
134 int n = MP_OBJ_SMALL_INT_VALUE(rhs);
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200135 mp_obj_list_t *s = list_new(o->len * n);
136 mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200137 return s;
138 }
Damien George9aa2a522014-02-01 23:04:09 +0000139 case RT_BINARY_OP_EQUAL:
140 case RT_BINARY_OP_LESS:
141 case RT_BINARY_OP_LESS_EQUAL:
142 case RT_BINARY_OP_MORE:
143 case RT_BINARY_OP_MORE_EQUAL:
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200144 return MP_BOOL(list_cmp_helper(op, lhs, rhs));
Damien George9aa2a522014-02-01 23:04:09 +0000145 case RT_BINARY_OP_NOT_EQUAL:
146 return MP_BOOL(!list_cmp_helper(RT_BINARY_OP_EQUAL, lhs, rhs));
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200147
Damiend99b0522013-12-21 18:17:45 +0000148 default:
149 // op not supported
150 return NULL;
151 }
152}
153
154static mp_obj_t list_getiter(mp_obj_t o_in) {
155 return mp_obj_new_list_iterator(o_in, 0);
156}
157
158mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
159 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
160 mp_obj_list_t *self = self_in;
161 if (self->len >= self->alloc) {
Damien732407f2013-12-29 19:33:23 +0000162 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200163 assert(self->items);
Damiend99b0522013-12-21 18:17:45 +0000164 self->alloc *= 2;
Damiend99b0522013-12-21 18:17:45 +0000165 }
166 self->items[self->len++] = arg;
167 return mp_const_none; // return None, as per CPython
168}
169
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200170static mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
171 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
172 assert(MP_OBJ_IS_TYPE(arg_in, &list_type));
173 mp_obj_list_t *self = self_in;
174 mp_obj_list_t *arg = arg_in;
175
176 if (self->len + arg->len > self->alloc) {
177 // TODO: use alloc policy for "4"
178 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + arg->len + 4);
179 self->alloc = self->len + arg->len + 4;
180 }
181
182 memcpy(self->items + self->len, arg->items, sizeof(mp_obj_t) * arg->len);
183 self->len += arg->len;
184 return mp_const_none; // return None, as per CPython
185}
186
Damien Georgea11ceca2014-01-19 16:02:09 +0000187static mp_obj_t list_pop(uint n_args, const mp_obj_t *args) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000188 assert(1 <= n_args && n_args <= 2);
189 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
190 mp_obj_list_t *self = args[0];
191 if (self->len == 0) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000192 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "pop from empty list"));
John R. Lenton25f417c2014-01-03 22:53:18 +0000193 }
194 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 +0000195 mp_obj_t ret = self->items[index];
196 self->len -= 1;
197 memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200198 if (self->alloc > LIST_MIN_ALLOC && self->alloc > 2 * self->len) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000199 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
200 self->alloc /= 2;
201 }
Damiend99b0522013-12-21 18:17:45 +0000202 return ret;
203}
204
205// TODO make this conform to CPython's definition of sort
John R. Lentonc06763a2014-01-07 17:29:16 +0000206static 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 +0000207 int op = reversed ? RT_BINARY_OP_MORE : RT_BINARY_OP_LESS;
Damiend99b0522013-12-21 18:17:45 +0000208 while (head < tail) {
209 mp_obj_t *h = head - 1;
210 mp_obj_t *t = tail;
John R. Lentonc06763a2014-01-07 17:29:16 +0000211 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 +0000212 for (;;) {
John R. Lentonb8698fc2014-01-11 00:58:59 +0000213 do ++h; while (rt_binary_op(op, key_fn == NULL ? h[0] : rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
214 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 +0000215 if (h >= t) break;
216 mp_obj_t x = h[0];
217 h[0] = t[0];
218 t[0] = x;
219 }
220 mp_obj_t x = h[0];
221 h[0] = tail[0];
222 tail[0] = x;
John R. Lentonc06763a2014-01-07 17:29:16 +0000223 mp_quicksort(head, t, key_fn, reversed);
Damiend99b0522013-12-21 18:17:45 +0000224 head = h + 1;
225 }
226}
227
Damien George20006db2014-01-18 14:10:48 +0000228mp_obj_t mp_obj_list_sort(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
229 assert(n_args >= 1);
230 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
231 if (n_args > 1) {
John R. Lentonc06763a2014-01-07 17:29:16 +0000232 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
233 "list.sort takes no positional arguments"));
234 }
Damien George20006db2014-01-18 14:10:48 +0000235 mp_obj_list_t *self = args[0];
Damiend99b0522013-12-21 18:17:45 +0000236 if (self->len > 1) {
Damien George55baff42014-01-21 21:40:13 +0000237 mp_map_elem_t *keyfun = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(QSTR_FROM_STR_STATIC("key")), MP_MAP_LOOKUP);
238 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 +0000239 mp_quicksort(self->items, self->items + self->len - 1,
240 keyfun ? keyfun->value : NULL,
241 reverse && reverse->value ? rt_is_true(reverse->value) : false);
Damiend99b0522013-12-21 18:17:45 +0000242 }
243 return mp_const_none; // return None, as per CPython
244}
245
John R. Lenton069ded92014-01-03 23:22:53 +0000246static mp_obj_t list_clear(mp_obj_t self_in) {
247 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
248 mp_obj_list_t *self = self_in;
249 self->len = 0;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200250 self->items = m_renew(mp_obj_t, self->items, self->alloc, LIST_MIN_ALLOC);
251 self->alloc = LIST_MIN_ALLOC;
John R. Lenton069ded92014-01-03 23:22:53 +0000252 return mp_const_none;
253}
254
John R. Lenton26c21162014-01-03 23:42:17 +0000255static mp_obj_t list_copy(mp_obj_t self_in) {
256 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
257 mp_obj_list_t *self = self_in;
258 return mp_obj_new_list(self->len, self->items);
259}
260
John R. Lentone241e8c2014-01-03 23:57:28 +0000261static mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
262 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
263 mp_obj_list_t *self = self_in;
264 int count = 0;
265 for (int i = 0; i < self->len; i++) {
266 if (mp_obj_equal(self->items[i], value)) {
267 count++;
268 }
269 }
270
271 return mp_obj_new_int(count);
272}
273
Damien Georgea11ceca2014-01-19 16:02:09 +0000274static mp_obj_t list_index(uint n_args, const mp_obj_t *args) {
John R. Lenton5d4a8212014-01-04 00:26:30 +0000275 assert(2 <= n_args && n_args <= 4);
276 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
277 mp_obj_list_t *self = args[0];
278 mp_obj_t *value = args[1];
John R. Lentonc5531622014-01-05 21:57:27 +0000279 uint start = 0;
280 uint stop = self->len;
John R. Lenton5d4a8212014-01-04 00:26:30 +0000281
John R. Lentonc5531622014-01-05 21:57:27 +0000282 if (n_args >= 3) {
283 start = mp_get_index(self->base.type, self->len, args[2]);
284 if (n_args >= 4) {
285 stop = mp_get_index(self->base.type, self->len, args[3]);
286 }
287 }
John R. Lenton5d4a8212014-01-04 00:26:30 +0000288
John R. Lentonc5531622014-01-05 21:57:27 +0000289 for (uint i = start; i < stop; i++) {
John R. Lenton5d4a8212014-01-04 00:26:30 +0000290 if (mp_obj_equal(self->items[i], value)) {
291 return mp_obj_new_int(i);
292 }
293 }
294
Damien Georgef0691f42014-01-05 13:44:06 +0000295 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "object not in list"));
John R. Lenton5d4a8212014-01-04 00:26:30 +0000296}
297
John R. Lenton45a87442014-01-04 01:15:01 +0000298static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
299 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
300 mp_obj_list_t *self = self_in;
301 // insert has its own strange index logic
302 int index = MP_OBJ_SMALL_INT_VALUE(idx);
303 if (index < 0) {
304 index += self->len;
305 }
306 if (index < 0) {
307 index = 0;
308 }
309 if (index > self->len) {
310 index = self->len;
311 }
312
313 mp_obj_list_append(self_in, mp_const_none);
314
315 for (int i = self->len-1; i > index; i--) {
316 self->items[i] = self->items[i-1];
317 }
318 self->items[index] = obj;
319
320 return mp_const_none;
321}
322
John R. Lenton49fb6e52014-01-04 01:56:53 +0000323static mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) {
324 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
325 mp_obj_t args[] = {self_in, value};
326 args[1] = list_index(2, args);
327 list_pop(2, args);
328
329 return mp_const_none;
330}
331
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000332static mp_obj_t list_reverse(mp_obj_t self_in) {
333 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
334 mp_obj_list_t *self = self_in;
335
336 int len = self->len;
337 for (int i = 0; i < len/2; i++) {
338 mp_obj_t *a = self->items[i];
339 self->items[i] = self->items[len-i-1];
340 self->items[len-i-1] = a;
341 }
342
343 return mp_const_none;
344}
345
Damiend99b0522013-12-21 18:17:45 +0000346static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200347static MP_DEFINE_CONST_FUN_OBJ_2(list_extend_obj, list_extend);
John R. Lenton069ded92014-01-03 23:22:53 +0000348static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
John R. Lenton26c21162014-01-03 23:42:17 +0000349static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
John R. Lentone241e8c2014-01-03 23:57:28 +0000350static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
John R. Lenton5d4a8212014-01-04 00:26:30 +0000351static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
John R. Lenton45a87442014-01-04 01:15:01 +0000352static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
John R. Lenton25f417c2014-01-03 22:53:18 +0000353static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
John R. Lenton49fb6e52014-01-04 01:56:53 +0000354static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000355static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
Damien George0f592032014-01-14 23:18:35 +0000356static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 0, mp_obj_list_sort);
Damiend99b0522013-12-21 18:17:45 +0000357
ian-va5a01df2014-01-06 14:14:11 -0800358static const mp_method_t list_type_methods[] = {
ian-v7a16fad2014-01-06 09:52:29 -0800359 { "append", &list_append_obj },
360 { "clear", &list_clear_obj },
361 { "copy", &list_copy_obj },
362 { "count", &list_count_obj },
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200363 { "extend", &list_extend_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800364 { "index", &list_index_obj },
ian-v5fd8fd22014-01-06 13:51:53 -0800365 { "insert", &list_insert_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800366 { "pop", &list_pop_obj },
367 { "remove", &list_remove_obj },
368 { "reverse", &list_reverse_obj },
369 { "sort", &list_sort_obj },
370 { NULL, NULL }, // end-of-list sentinel
371};
Damien George97209d32014-01-07 15:58:30 +0000372
Damiend99b0522013-12-21 18:17:45 +0000373const mp_obj_type_t list_type = {
John R. Lenton3391e192014-01-07 18:06:34 +0000374 { &mp_const_type },
375 "list",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200376 .print = list_print,
377 .make_new = list_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000378 .unary_op = list_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200379 .binary_op = list_binary_op,
380 .getiter = list_getiter,
ian-v7a16fad2014-01-06 09:52:29 -0800381 .methods = list_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000382};
383
384static mp_obj_list_t *list_new(uint n) {
385 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
386 o->base.type = &list_type;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200387 o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
Damiend99b0522013-12-21 18:17:45 +0000388 o->len = n;
389 o->items = m_new(mp_obj_t, o->alloc);
390 return o;
391}
392
393mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
394 mp_obj_list_t *o = list_new(n);
Paul Sokolovskye5a15cb2014-02-04 09:44:10 +0200395 if (items != NULL) {
396 for (int i = 0; i < n; i++) {
397 o->items[i] = items[i];
398 }
Damiend99b0522013-12-21 18:17:45 +0000399 }
400 return o;
401}
402
Damiend99b0522013-12-21 18:17:45 +0000403void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
404 mp_obj_list_t *self = self_in;
405 *len = self->len;
406 *items = self->items;
407}
408
409void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
410 mp_obj_list_t *self = self_in;
411 uint i = mp_get_index(self->base.type, self->len, index);
412 self->items[i] = value;
413}
414
415/******************************************************************************/
416/* list iterator */
417
418typedef struct _mp_obj_list_it_t {
419 mp_obj_base_t base;
420 mp_obj_list_t *list;
421 machine_uint_t cur;
422} mp_obj_list_it_t;
423
424mp_obj_t list_it_iternext(mp_obj_t self_in) {
425 mp_obj_list_it_t *self = self_in;
426 if (self->cur < self->list->len) {
427 mp_obj_t o_out = self->list->items[self->cur];
428 self->cur += 1;
429 return o_out;
430 } else {
431 return mp_const_stop_iteration;
432 }
433}
434
435static const mp_obj_type_t list_it_type = {
John R. Lenton3391e192014-01-07 18:06:34 +0000436 { &mp_const_type },
437 "list_iterator",
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);
443 o->base.type = &list_it_type;
444 o->list = list;
445 o->cur = cur;
446 return o;
447}