blob: 1e8930eeea57acf59d2e8ff0cd82b59566e2dcfa [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
26/******************************************************************************/
27/* list */
28
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020029static 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 +000030 mp_obj_list_t *o = o_in;
31 print(env, "[");
32 for (int i = 0; i < o->len; i++) {
33 if (i > 0) {
34 print(env, ", ");
35 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020036 mp_obj_print_helper(print, env, o->items[i], PRINT_REPR);
Damiend99b0522013-12-21 18:17:45 +000037 }
38 print(env, "]");
39}
40
Damien George20006db2014-01-18 14:10:48 +000041static mp_obj_t list_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
42 // TODO check n_kw == 0
43
Damien George71c51812014-01-04 20:21:15 +000044 switch (n_args) {
45 case 0:
46 // return a new, empty list
Damien Georgeebde0b82014-01-19 00:47:40 +000047 return mp_obj_new_list(0, NULL);
Damien George71c51812014-01-04 20:21:15 +000048
49 case 1:
50 {
51 // make list from iterable
52 mp_obj_t iterable = rt_getiter(args[0]);
Damien Georgeebde0b82014-01-19 00:47:40 +000053 mp_obj_t list = mp_obj_new_list(0, NULL);
Damien George71c51812014-01-04 20:21:15 +000054 mp_obj_t item;
55 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
Damien Georgeebde0b82014-01-19 00:47:40 +000056 mp_obj_list_append(list, item);
Damien George71c51812014-01-04 20:21:15 +000057 }
58 return list;
59 }
60
61 default:
62 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));
63 }
ian-v5fd8fd22014-01-06 13:51:53 -080064 return NULL;
Damien George71c51812014-01-04 20:21:15 +000065}
66
Paul Sokolovsky1945e602014-01-12 02:01:00 +020067// Don't pass RT_COMPARE_OP_NOT_EQUAL here
68static bool list_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
69 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
70 if (!MP_OBJ_IS_TYPE(another_in, &list_type)) {
71 return false;
72 }
73 mp_obj_list_t *self = self_in;
74 mp_obj_list_t *another = another_in;
75 if (op == RT_COMPARE_OP_EQUAL && self->len != another->len) {
76 return false;
77 }
78
79 // Let's deal only with > & >=
80 if (op == RT_COMPARE_OP_LESS || op == RT_COMPARE_OP_LESS_EQUAL) {
81 mp_obj_t t = self;
82 self = another;
83 another = t;
84 if (op == RT_COMPARE_OP_LESS) {
85 op = RT_COMPARE_OP_MORE;
86 } else {
87 op = RT_COMPARE_OP_MORE_EQUAL;
88 }
89 }
90
91 int len = self->len < another->len ? self->len : another->len;
92 bool eq_status = true; // empty lists are equal
93 bool rel_status;
94 for (int i = 0; i < len; i++) {
95 eq_status = mp_obj_equal(self->items[i], another->items[i]);
96 if (op == RT_COMPARE_OP_EQUAL && !eq_status) {
97 return false;
98 }
99 rel_status = (rt_binary_op(op, self->items[i], another->items[i]) == mp_const_true);
100 if (!eq_status && !rel_status) {
101 return false;
102 }
103 }
104
105 // If we had tie in the last element...
106 if (eq_status) {
107 // ... and we have lists of different lengths...
108 if (self->len != another->len) {
109 if (self->len < another->len) {
110 // ... then longer list length wins (we deal only with >)
111 return false;
112 }
113 } else if (op == RT_COMPARE_OP_MORE) {
114 // Otherwise, if we have strict relation, equality means failure
115 return false;
116 }
117 }
118
119 return true;
120}
121
Damien George4e8dc8c2014-01-27 23:15:32 +0000122static mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
123 mp_obj_list_t *self = self_in;
124 switch (op) {
125 case RT_UNARY_OP_NOT: if (self->len == 0) { return mp_const_true; } else { return mp_const_false; }
126 default: return MP_OBJ_NULL; // op not supported for None
127 }
128}
129
Damiend99b0522013-12-21 18:17:45 +0000130static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
131 mp_obj_list_t *o = lhs;
132 switch (op) {
133 case RT_BINARY_OP_SUBSCR:
134 {
135 // list load
136 uint index = mp_get_index(o->base.type, o->len, rhs);
137 return o->items[index];
138 }
John R. Lenton4cb80582014-01-03 02:27:08 +0000139 case RT_BINARY_OP_ADD:
140 {
John R. Lenton81ad89c2014-01-03 02:32:40 +0000141 if (!MP_OBJ_IS_TYPE(rhs, &list_type)) {
142 return NULL;
143 }
144 mp_obj_list_t *p = rhs;
145 mp_obj_list_t *s = list_new(o->len + p->len);
John R. Lenton9bc56d92014-01-03 10:13:38 +0000146 memcpy(s->items, o->items, sizeof(mp_obj_t) * o->len);
147 memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len);
John R. Lenton81ad89c2014-01-03 02:32:40 +0000148 return s;
John R. Lenton4cb80582014-01-03 02:27:08 +0000149 }
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200150 case RT_BINARY_OP_INPLACE_ADD:
151 {
152 if (!MP_OBJ_IS_TYPE(rhs, &list_type)) {
153 return NULL;
154 }
155 list_extend(lhs, rhs);
156 return o;
157 }
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200158 case RT_BINARY_OP_MULTIPLY:
159 {
160 if (!MP_OBJ_IS_SMALL_INT(rhs)) {
161 return NULL;
162 }
163 int n = MP_OBJ_SMALL_INT_VALUE(rhs);
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200164 mp_obj_list_t *s = list_new(o->len * n);
165 mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200166 return s;
167 }
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200168 case RT_COMPARE_OP_EQUAL:
169 case RT_COMPARE_OP_LESS:
170 case RT_COMPARE_OP_LESS_EQUAL:
171 case RT_COMPARE_OP_MORE:
172 case RT_COMPARE_OP_MORE_EQUAL:
173 return MP_BOOL(list_cmp_helper(op, lhs, rhs));
174 case RT_COMPARE_OP_NOT_EQUAL:
175 return MP_BOOL(!list_cmp_helper(RT_COMPARE_OP_EQUAL, lhs, rhs));
176
Damiend99b0522013-12-21 18:17:45 +0000177 default:
178 // op not supported
179 return NULL;
180 }
181}
182
183static mp_obj_t list_getiter(mp_obj_t o_in) {
184 return mp_obj_new_list_iterator(o_in, 0);
185}
186
187mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
188 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
189 mp_obj_list_t *self = self_in;
190 if (self->len >= self->alloc) {
Damien732407f2013-12-29 19:33:23 +0000191 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
Damiend99b0522013-12-21 18:17:45 +0000192 self->alloc *= 2;
Damiend99b0522013-12-21 18:17:45 +0000193 }
194 self->items[self->len++] = arg;
195 return mp_const_none; // return None, as per CPython
196}
197
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200198static mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
199 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
200 assert(MP_OBJ_IS_TYPE(arg_in, &list_type));
201 mp_obj_list_t *self = self_in;
202 mp_obj_list_t *arg = arg_in;
203
204 if (self->len + arg->len > self->alloc) {
205 // TODO: use alloc policy for "4"
206 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + arg->len + 4);
207 self->alloc = self->len + arg->len + 4;
208 }
209
210 memcpy(self->items + self->len, arg->items, sizeof(mp_obj_t) * arg->len);
211 self->len += arg->len;
212 return mp_const_none; // return None, as per CPython
213}
214
Damien Georgea11ceca2014-01-19 16:02:09 +0000215static mp_obj_t list_pop(uint n_args, const mp_obj_t *args) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000216 assert(1 <= n_args && n_args <= 2);
217 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
218 mp_obj_list_t *self = args[0];
219 if (self->len == 0) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000220 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "pop from empty list"));
John R. Lenton25f417c2014-01-03 22:53:18 +0000221 }
222 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 +0000223 mp_obj_t ret = self->items[index];
224 self->len -= 1;
225 memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
John R. Lenton25f417c2014-01-03 22:53:18 +0000226 if (self->alloc > 2 * self->len) {
227 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
228 self->alloc /= 2;
229 }
Damiend99b0522013-12-21 18:17:45 +0000230 return ret;
231}
232
233// TODO make this conform to CPython's definition of sort
John R. Lentonc06763a2014-01-07 17:29:16 +0000234static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool reversed) {
235 int op = reversed ? RT_COMPARE_OP_MORE : RT_COMPARE_OP_LESS;
Damiend99b0522013-12-21 18:17:45 +0000236 while (head < tail) {
237 mp_obj_t *h = head - 1;
238 mp_obj_t *t = tail;
John R. Lentonc06763a2014-01-07 17:29:16 +0000239 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 +0000240 for (;;) {
John R. Lentonb8698fc2014-01-11 00:58:59 +0000241 do ++h; while (rt_binary_op(op, key_fn == NULL ? h[0] : rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
242 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 +0000243 if (h >= t) break;
244 mp_obj_t x = h[0];
245 h[0] = t[0];
246 t[0] = x;
247 }
248 mp_obj_t x = h[0];
249 h[0] = tail[0];
250 tail[0] = x;
John R. Lentonc06763a2014-01-07 17:29:16 +0000251 mp_quicksort(head, t, key_fn, reversed);
Damiend99b0522013-12-21 18:17:45 +0000252 head = h + 1;
253 }
254}
255
Damien George20006db2014-01-18 14:10:48 +0000256mp_obj_t mp_obj_list_sort(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
257 assert(n_args >= 1);
258 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
259 if (n_args > 1) {
John R. Lentonc06763a2014-01-07 17:29:16 +0000260 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
261 "list.sort takes no positional arguments"));
262 }
Damien George20006db2014-01-18 14:10:48 +0000263 mp_obj_list_t *self = args[0];
Damiend99b0522013-12-21 18:17:45 +0000264 if (self->len > 1) {
Damien George55baff42014-01-21 21:40:13 +0000265 mp_map_elem_t *keyfun = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(QSTR_FROM_STR_STATIC("key")), MP_MAP_LOOKUP);
266 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 +0000267 mp_quicksort(self->items, self->items + self->len - 1,
268 keyfun ? keyfun->value : NULL,
269 reverse && reverse->value ? rt_is_true(reverse->value) : false);
Damiend99b0522013-12-21 18:17:45 +0000270 }
271 return mp_const_none; // return None, as per CPython
272}
273
John R. Lenton069ded92014-01-03 23:22:53 +0000274static mp_obj_t list_clear(mp_obj_t self_in) {
275 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
276 mp_obj_list_t *self = self_in;
277 self->len = 0;
278 self->items = m_renew(mp_obj_t, self->items, self->alloc, 4);
279 self->alloc = 4;
280 return mp_const_none;
281}
282
John R. Lenton26c21162014-01-03 23:42:17 +0000283static mp_obj_t list_copy(mp_obj_t self_in) {
284 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
285 mp_obj_list_t *self = self_in;
286 return mp_obj_new_list(self->len, self->items);
287}
288
John R. Lentone241e8c2014-01-03 23:57:28 +0000289static mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
290 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
291 mp_obj_list_t *self = self_in;
292 int count = 0;
293 for (int i = 0; i < self->len; i++) {
294 if (mp_obj_equal(self->items[i], value)) {
295 count++;
296 }
297 }
298
299 return mp_obj_new_int(count);
300}
301
Damien Georgea11ceca2014-01-19 16:02:09 +0000302static mp_obj_t list_index(uint n_args, const mp_obj_t *args) {
John R. Lenton5d4a8212014-01-04 00:26:30 +0000303 assert(2 <= n_args && n_args <= 4);
304 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
305 mp_obj_list_t *self = args[0];
306 mp_obj_t *value = args[1];
John R. Lentonc5531622014-01-05 21:57:27 +0000307 uint start = 0;
308 uint stop = self->len;
John R. Lenton5d4a8212014-01-04 00:26:30 +0000309
John R. Lentonc5531622014-01-05 21:57:27 +0000310 if (n_args >= 3) {
311 start = mp_get_index(self->base.type, self->len, args[2]);
312 if (n_args >= 4) {
313 stop = mp_get_index(self->base.type, self->len, args[3]);
314 }
315 }
John R. Lenton5d4a8212014-01-04 00:26:30 +0000316
John R. Lentonc5531622014-01-05 21:57:27 +0000317 for (uint i = start; i < stop; i++) {
John R. Lenton5d4a8212014-01-04 00:26:30 +0000318 if (mp_obj_equal(self->items[i], value)) {
319 return mp_obj_new_int(i);
320 }
321 }
322
Damien Georgef0691f42014-01-05 13:44:06 +0000323 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "object not in list"));
John R. Lenton5d4a8212014-01-04 00:26:30 +0000324}
325
John R. Lenton45a87442014-01-04 01:15:01 +0000326static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
327 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
328 mp_obj_list_t *self = self_in;
329 // insert has its own strange index logic
330 int index = MP_OBJ_SMALL_INT_VALUE(idx);
331 if (index < 0) {
332 index += self->len;
333 }
334 if (index < 0) {
335 index = 0;
336 }
337 if (index > self->len) {
338 index = self->len;
339 }
340
341 mp_obj_list_append(self_in, mp_const_none);
342
343 for (int i = self->len-1; i > index; i--) {
344 self->items[i] = self->items[i-1];
345 }
346 self->items[index] = obj;
347
348 return mp_const_none;
349}
350
John R. Lenton49fb6e52014-01-04 01:56:53 +0000351static mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) {
352 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
353 mp_obj_t args[] = {self_in, value};
354 args[1] = list_index(2, args);
355 list_pop(2, args);
356
357 return mp_const_none;
358}
359
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000360static mp_obj_t list_reverse(mp_obj_t self_in) {
361 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
362 mp_obj_list_t *self = self_in;
363
364 int len = self->len;
365 for (int i = 0; i < len/2; i++) {
366 mp_obj_t *a = self->items[i];
367 self->items[i] = self->items[len-i-1];
368 self->items[len-i-1] = a;
369 }
370
371 return mp_const_none;
372}
373
Damiend99b0522013-12-21 18:17:45 +0000374static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200375static MP_DEFINE_CONST_FUN_OBJ_2(list_extend_obj, list_extend);
John R. Lenton069ded92014-01-03 23:22:53 +0000376static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
John R. Lenton26c21162014-01-03 23:42:17 +0000377static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
John R. Lentone241e8c2014-01-03 23:57:28 +0000378static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
John R. Lenton5d4a8212014-01-04 00:26:30 +0000379static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
John R. Lenton45a87442014-01-04 01:15:01 +0000380static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
John R. Lenton25f417c2014-01-03 22:53:18 +0000381static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
John R. Lenton49fb6e52014-01-04 01:56:53 +0000382static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000383static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
Damien George0f592032014-01-14 23:18:35 +0000384static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 0, mp_obj_list_sort);
Damiend99b0522013-12-21 18:17:45 +0000385
ian-va5a01df2014-01-06 14:14:11 -0800386static const mp_method_t list_type_methods[] = {
ian-v7a16fad2014-01-06 09:52:29 -0800387 { "append", &list_append_obj },
388 { "clear", &list_clear_obj },
389 { "copy", &list_copy_obj },
390 { "count", &list_count_obj },
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200391 { "extend", &list_extend_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800392 { "index", &list_index_obj },
ian-v5fd8fd22014-01-06 13:51:53 -0800393 { "insert", &list_insert_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800394 { "pop", &list_pop_obj },
395 { "remove", &list_remove_obj },
396 { "reverse", &list_reverse_obj },
397 { "sort", &list_sort_obj },
398 { NULL, NULL }, // end-of-list sentinel
399};
Damien George97209d32014-01-07 15:58:30 +0000400
Damiend99b0522013-12-21 18:17:45 +0000401const mp_obj_type_t list_type = {
John R. Lenton3391e192014-01-07 18:06:34 +0000402 { &mp_const_type },
403 "list",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200404 .print = list_print,
405 .make_new = list_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000406 .unary_op = list_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200407 .binary_op = list_binary_op,
408 .getiter = list_getiter,
ian-v7a16fad2014-01-06 09:52:29 -0800409 .methods = list_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000410};
411
412static mp_obj_list_t *list_new(uint n) {
413 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
414 o->base.type = &list_type;
415 o->alloc = n < 4 ? 4 : n;
416 o->len = n;
417 o->items = m_new(mp_obj_t, o->alloc);
418 return o;
419}
420
421mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
422 mp_obj_list_t *o = list_new(n);
423 for (int i = 0; i < n; i++) {
424 o->items[i] = items[i];
425 }
426 return o;
427}
428
Damiend99b0522013-12-21 18:17:45 +0000429void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
430 mp_obj_list_t *self = self_in;
431 *len = self->len;
432 *items = self->items;
433}
434
435void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
436 mp_obj_list_t *self = self_in;
437 uint i = mp_get_index(self->base.type, self->len, index);
438 self->items[i] = value;
439}
440
441/******************************************************************************/
442/* list iterator */
443
444typedef struct _mp_obj_list_it_t {
445 mp_obj_base_t base;
446 mp_obj_list_t *list;
447 machine_uint_t cur;
448} mp_obj_list_it_t;
449
450mp_obj_t list_it_iternext(mp_obj_t self_in) {
451 mp_obj_list_it_t *self = self_in;
452 if (self->cur < self->list->len) {
453 mp_obj_t o_out = self->list->items[self->cur];
454 self->cur += 1;
455 return o_out;
456 } else {
457 return mp_const_stop_iteration;
458 }
459}
460
461static const mp_obj_type_t list_it_type = {
John R. Lenton3391e192014-01-07 18:06:34 +0000462 { &mp_const_type },
463 "list_iterator",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200464 .iternext = list_it_iternext,
Damiend99b0522013-12-21 18:17:45 +0000465};
466
467mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
468 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
469 o->base.type = &list_it_type;
470 o->list = list;
471 o->cur = cur;
472 return o;
473}