blob: a6fbe4e423c58075f23f63c9617d323280998ef0 [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
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020022STATIC mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
23STATIC mp_obj_list_t *list_new(uint n);
24STATIC 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 Sokolovskyd5df6cd2014-02-12 18:15:40 +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
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020044STATIC 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 +000045 // 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:
Damien Georgec5966122014-02-15 16:10:44 +000065 nlr_jump(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 +000066 }
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 Sokolovskyd5df6cd2014-02-12 18:15:40 +020071STATIC bool list_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
Paul Sokolovsky1945e602014-01-12 02:01:00 +020072 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
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020082STATIC mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
Damien George4e8dc8c2014-01-27 23:15:32 +000083 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
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020091STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damiend99b0522013-12-21 18:17:45 +000092 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);
Paul Sokolovskyee4aaf72014-02-08 23:17:51 +0200117 m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
John R. Lenton81ad89c2014-01-03 02:32:40 +0000118 return s;
John R. Lenton4cb80582014-01-03 02:27:08 +0000119 }
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200120 case RT_BINARY_OP_INPLACE_ADD:
121 {
122 if (!MP_OBJ_IS_TYPE(rhs, &list_type)) {
123 return NULL;
124 }
125 list_extend(lhs, rhs);
126 return o;
127 }
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200128 case RT_BINARY_OP_MULTIPLY:
129 {
130 if (!MP_OBJ_IS_SMALL_INT(rhs)) {
131 return NULL;
132 }
133 int n = MP_OBJ_SMALL_INT_VALUE(rhs);
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200134 mp_obj_list_t *s = list_new(o->len * n);
135 mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200136 return s;
137 }
Damien George9aa2a522014-02-01 23:04:09 +0000138 case RT_BINARY_OP_EQUAL:
139 case RT_BINARY_OP_LESS:
140 case RT_BINARY_OP_LESS_EQUAL:
141 case RT_BINARY_OP_MORE:
142 case RT_BINARY_OP_MORE_EQUAL:
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200143 return MP_BOOL(list_cmp_helper(op, lhs, rhs));
Damien George9aa2a522014-02-01 23:04:09 +0000144 case RT_BINARY_OP_NOT_EQUAL:
145 return MP_BOOL(!list_cmp_helper(RT_BINARY_OP_EQUAL, lhs, rhs));
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200146
Damiend99b0522013-12-21 18:17:45 +0000147 default:
148 // op not supported
149 return NULL;
150 }
151}
152
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200153STATIC mp_obj_t list_getiter(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000154 return mp_obj_new_list_iterator(o_in, 0);
155}
156
157mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
158 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
159 mp_obj_list_t *self = self_in;
160 if (self->len >= self->alloc) {
Damien732407f2013-12-29 19:33:23 +0000161 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200162 assert(self->items);
Damiend99b0522013-12-21 18:17:45 +0000163 self->alloc *= 2;
Damiend99b0522013-12-21 18:17:45 +0000164 }
165 self->items[self->len++] = arg;
166 return mp_const_none; // return None, as per CPython
167}
168
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200169STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200170 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
171 assert(MP_OBJ_IS_TYPE(arg_in, &list_type));
172 mp_obj_list_t *self = self_in;
173 mp_obj_list_t *arg = arg_in;
174
175 if (self->len + arg->len > self->alloc) {
176 // TODO: use alloc policy for "4"
177 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + arg->len + 4);
178 self->alloc = self->len + arg->len + 4;
179 }
180
181 memcpy(self->items + self->len, arg->items, sizeof(mp_obj_t) * arg->len);
182 self->len += arg->len;
183 return mp_const_none; // return None, as per CPython
184}
185
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200186STATIC mp_obj_t list_pop(uint n_args, const mp_obj_t *args) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000187 assert(1 <= n_args && n_args <= 2);
188 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
189 mp_obj_list_t *self = args[0];
190 if (self->len == 0) {
Damien Georgec5966122014-02-15 16:10:44 +0000191 nlr_jump(mp_obj_new_exception_msg(&mp_type_IndexError, "pop from empty list"));
John R. Lenton25f417c2014-01-03 22:53:18 +0000192 }
193 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 +0000194 mp_obj_t ret = self->items[index];
195 self->len -= 1;
196 memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200197 if (self->alloc > LIST_MIN_ALLOC && self->alloc > 2 * self->len) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000198 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
199 self->alloc /= 2;
200 }
Damiend99b0522013-12-21 18:17:45 +0000201 return ret;
202}
203
204// TODO make this conform to CPython's definition of sort
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200205STATIC 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 +0000206 int op = reversed ? RT_BINARY_OP_MORE : RT_BINARY_OP_LESS;
Damiend99b0522013-12-21 18:17:45 +0000207 while (head < tail) {
208 mp_obj_t *h = head - 1;
209 mp_obj_t *t = tail;
John R. Lentonc06763a2014-01-07 17:29:16 +0000210 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 +0000211 for (;;) {
John R. Lentonb8698fc2014-01-11 00:58:59 +0000212 do ++h; while (rt_binary_op(op, key_fn == NULL ? h[0] : rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
213 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 +0000214 if (h >= t) break;
215 mp_obj_t x = h[0];
216 h[0] = t[0];
217 t[0] = x;
218 }
219 mp_obj_t x = h[0];
220 h[0] = tail[0];
221 tail[0] = x;
John R. Lentonc06763a2014-01-07 17:29:16 +0000222 mp_quicksort(head, t, key_fn, reversed);
Damiend99b0522013-12-21 18:17:45 +0000223 head = h + 1;
224 }
225}
226
Damien George20006db2014-01-18 14:10:48 +0000227mp_obj_t mp_obj_list_sort(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
228 assert(n_args >= 1);
229 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
230 if (n_args > 1) {
Damien Georgec5966122014-02-15 16:10:44 +0000231 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lentonc06763a2014-01-07 17:29:16 +0000232 "list.sort takes no positional arguments"));
233 }
Damien George20006db2014-01-18 14:10:48 +0000234 mp_obj_list_t *self = args[0];
Damiend99b0522013-12-21 18:17:45 +0000235 if (self->len > 1) {
Damien George7d0bfbe2014-02-08 19:01:47 +0000236 mp_map_elem_t *keyfun = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_key), MP_MAP_LOOKUP);
237 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 +0000238 mp_quicksort(self->items, self->items + self->len - 1,
239 keyfun ? keyfun->value : NULL,
240 reverse && reverse->value ? rt_is_true(reverse->value) : false);
Damiend99b0522013-12-21 18:17:45 +0000241 }
242 return mp_const_none; // return None, as per CPython
243}
244
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200245STATIC mp_obj_t list_clear(mp_obj_t self_in) {
John R. Lenton069ded92014-01-03 23:22:53 +0000246 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
247 mp_obj_list_t *self = self_in;
248 self->len = 0;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200249 self->items = m_renew(mp_obj_t, self->items, self->alloc, LIST_MIN_ALLOC);
250 self->alloc = LIST_MIN_ALLOC;
John R. Lenton069ded92014-01-03 23:22:53 +0000251 return mp_const_none;
252}
253
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200254STATIC mp_obj_t list_copy(mp_obj_t self_in) {
John R. Lenton26c21162014-01-03 23:42:17 +0000255 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
256 mp_obj_list_t *self = self_in;
257 return mp_obj_new_list(self->len, self->items);
258}
259
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200260STATIC mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
John R. Lentone241e8c2014-01-03 23:57:28 +0000261 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
262 mp_obj_list_t *self = self_in;
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200263 return mp_seq_count_obj(self->items, self->len, value);
John R. Lentone241e8c2014-01-03 23:57:28 +0000264}
265
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200266STATIC mp_obj_t list_index(uint n_args, const mp_obj_t *args) {
John R. Lenton5d4a8212014-01-04 00:26:30 +0000267 assert(2 <= n_args && n_args <= 4);
268 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
269 mp_obj_list_t *self = args[0];
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200270 return mp_seq_index_obj(self->items, self->len, n_args, args);
John R. Lenton5d4a8212014-01-04 00:26:30 +0000271}
272
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200273STATIC mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
John R. Lenton45a87442014-01-04 01:15:01 +0000274 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
275 mp_obj_list_t *self = self_in;
276 // insert has its own strange index logic
277 int index = MP_OBJ_SMALL_INT_VALUE(idx);
278 if (index < 0) {
279 index += self->len;
280 }
281 if (index < 0) {
282 index = 0;
283 }
284 if (index > self->len) {
285 index = self->len;
286 }
287
288 mp_obj_list_append(self_in, mp_const_none);
289
290 for (int i = self->len-1; i > index; i--) {
291 self->items[i] = self->items[i-1];
292 }
293 self->items[index] = obj;
294
295 return mp_const_none;
296}
297
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200298STATIC mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) {
John R. Lenton49fb6e52014-01-04 01:56:53 +0000299 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
300 mp_obj_t args[] = {self_in, value};
301 args[1] = list_index(2, args);
302 list_pop(2, args);
303
304 return mp_const_none;
305}
306
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200307STATIC mp_obj_t list_reverse(mp_obj_t self_in) {
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000308 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
309 mp_obj_list_t *self = self_in;
310
311 int len = self->len;
312 for (int i = 0; i < len/2; i++) {
313 mp_obj_t *a = self->items[i];
314 self->items[i] = self->items[len-i-1];
315 self->items[len-i-1] = a;
316 }
317
318 return mp_const_none;
319}
320
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200321STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
322STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_extend_obj, list_extend);
323STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
324STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
325STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
326STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
327STATIC MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
328STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
329STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
330STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
331STATIC MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 0, mp_obj_list_sort);
Damiend99b0522013-12-21 18:17:45 +0000332
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200333STATIC const mp_method_t list_type_methods[] = {
ian-v7a16fad2014-01-06 09:52:29 -0800334 { "append", &list_append_obj },
335 { "clear", &list_clear_obj },
336 { "copy", &list_copy_obj },
337 { "count", &list_count_obj },
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200338 { "extend", &list_extend_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800339 { "index", &list_index_obj },
ian-v5fd8fd22014-01-06 13:51:53 -0800340 { "insert", &list_insert_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800341 { "pop", &list_pop_obj },
342 { "remove", &list_remove_obj },
343 { "reverse", &list_reverse_obj },
344 { "sort", &list_sort_obj },
345 { NULL, NULL }, // end-of-list sentinel
346};
Damien George97209d32014-01-07 15:58:30 +0000347
Damiend99b0522013-12-21 18:17:45 +0000348const mp_obj_type_t list_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000349 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000350 .name = MP_QSTR_list,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200351 .print = list_print,
352 .make_new = list_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000353 .unary_op = list_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200354 .binary_op = list_binary_op,
355 .getiter = list_getiter,
ian-v7a16fad2014-01-06 09:52:29 -0800356 .methods = list_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000357};
358
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200359STATIC mp_obj_list_t *list_new(uint n) {
Damiend99b0522013-12-21 18:17:45 +0000360 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
361 o->base.type = &list_type;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200362 o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
Damiend99b0522013-12-21 18:17:45 +0000363 o->len = n;
364 o->items = m_new(mp_obj_t, o->alloc);
365 return o;
366}
367
368mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
369 mp_obj_list_t *o = list_new(n);
Paul Sokolovskye5a15cb2014-02-04 09:44:10 +0200370 if (items != NULL) {
371 for (int i = 0; i < n; i++) {
372 o->items[i] = items[i];
373 }
Damiend99b0522013-12-21 18:17:45 +0000374 }
375 return o;
376}
377
Damiend99b0522013-12-21 18:17:45 +0000378void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
379 mp_obj_list_t *self = self_in;
380 *len = self->len;
381 *items = self->items;
382}
383
384void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
385 mp_obj_list_t *self = self_in;
386 uint i = mp_get_index(self->base.type, self->len, index);
387 self->items[i] = value;
388}
389
390/******************************************************************************/
391/* list iterator */
392
393typedef struct _mp_obj_list_it_t {
394 mp_obj_base_t base;
395 mp_obj_list_t *list;
396 machine_uint_t cur;
397} mp_obj_list_it_t;
398
399mp_obj_t list_it_iternext(mp_obj_t self_in) {
400 mp_obj_list_it_t *self = self_in;
401 if (self->cur < self->list->len) {
402 mp_obj_t o_out = self->list->items[self->cur];
403 self->cur += 1;
404 return o_out;
405 } else {
406 return mp_const_stop_iteration;
407 }
408}
409
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200410STATIC const mp_obj_type_t list_it_type = {
Damien Georgec5966122014-02-15 16:10:44 +0000411 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000412 .name = MP_QSTR_iterator,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200413 .iternext = list_it_iternext,
Damiend99b0522013-12-21 18:17:45 +0000414};
415
416mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
417 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
418 o->base.type = &list_it_type;
419 o->list = list;
420 o->cur = cur;
421 return o;
422}