blob: 27b2a913d860b71ef0b5731fbe55589997853216 [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"
John R. Lentonc06763a2014-01-07 17:29:16 +00009#include "map.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "runtime0.h"
11#include "runtime.h"
12
13typedef struct _mp_obj_list_t {
14 mp_obj_base_t base;
15 machine_uint_t alloc;
16 machine_uint_t len;
17 mp_obj_t *items;
18} mp_obj_list_t;
19
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020020STATIC mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
21STATIC mp_obj_list_t *list_new(uint n);
22STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in);
Damiend99b0522013-12-21 18:17:45 +000023
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +020024// TODO: Move to mpconfig.h
25#define LIST_MIN_ALLOC 4
26
Damiend99b0522013-12-21 18:17:45 +000027/******************************************************************************/
28/* list */
29
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020030STATIC 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 +000031 mp_obj_list_t *o = o_in;
32 print(env, "[");
33 for (int i = 0; i < o->len; i++) {
34 if (i > 0) {
35 print(env, ", ");
36 }
Paul Sokolovsky76d982e2014-01-13 19:19:16 +020037 mp_obj_print_helper(print, env, o->items[i], PRINT_REPR);
Damiend99b0522013-12-21 18:17:45 +000038 }
39 print(env, "]");
40}
41
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020042STATIC 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 +000043 // TODO check n_kw == 0
44
Damien George71c51812014-01-04 20:21:15 +000045 switch (n_args) {
46 case 0:
47 // return a new, empty list
Damien Georgeebde0b82014-01-19 00:47:40 +000048 return mp_obj_new_list(0, NULL);
Damien George71c51812014-01-04 20:21:15 +000049
50 case 1:
51 {
52 // make list from iterable
53 mp_obj_t iterable = rt_getiter(args[0]);
Damien Georgeebde0b82014-01-19 00:47:40 +000054 mp_obj_t list = mp_obj_new_list(0, NULL);
Damien George71c51812014-01-04 20:21:15 +000055 mp_obj_t item;
Damien George66eaf842014-03-26 19:27:58 +000056 while ((item = rt_iternext(iterable)) != MP_OBJ_NULL) {
Damien Georgeebde0b82014-01-19 00:47:40 +000057 mp_obj_list_append(list, item);
Damien George71c51812014-01-04 20:21:15 +000058 }
59 return list;
60 }
61
62 default:
Damien Georgec5966122014-02-15 16:10:44 +000063 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 +000064 }
ian-v5fd8fd22014-01-06 13:51:53 -080065 return NULL;
Damien George71c51812014-01-04 20:21:15 +000066}
67
Damien George9aa2a522014-02-01 23:04:09 +000068// Don't pass RT_BINARY_OP_NOT_EQUAL here
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020069STATIC bool list_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
Damien George3e1a5c12014-03-29 13:43:38 +000070 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
71 if (!MP_OBJ_IS_TYPE(another_in, &mp_type_list)) {
Paul Sokolovsky1945e602014-01-12 02:01:00 +020072 return false;
73 }
74 mp_obj_list_t *self = self_in;
75 mp_obj_list_t *another = another_in;
Paul Sokolovsky1945e602014-01-12 02:01:00 +020076
Paul Sokolovsky1a996c42014-02-08 22:49:46 +020077 return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len);
Paul Sokolovsky1945e602014-01-12 02:01:00 +020078}
79
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020080STATIC mp_obj_t list_unary_op(int op, mp_obj_t self_in) {
Damien George4e8dc8c2014-01-27 23:15:32 +000081 mp_obj_list_t *self = self_in;
82 switch (op) {
Paul Sokolovskyc1d9bbc2014-01-30 04:37:19 +020083 case RT_UNARY_OP_BOOL: return MP_BOOL(self->len != 0);
84 case RT_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
Damien George4e8dc8c2014-01-27 23:15:32 +000085 default: return MP_OBJ_NULL; // op not supported for None
86 }
87}
88
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020089STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damiend99b0522013-12-21 18:17:45 +000090 mp_obj_list_t *o = lhs;
91 switch (op) {
92 case RT_BINARY_OP_SUBSCR:
93 {
Paul Sokolovsky13cfabd2014-02-02 03:32:55 +020094#if MICROPY_ENABLE_SLICE
Damien George3e1a5c12014-03-29 13:43:38 +000095 if (MP_OBJ_IS_TYPE(rhs, &mp_type_slice)) {
Paul Sokolovsky13cfabd2014-02-02 03:32:55 +020096 machine_uint_t start, stop;
Paul Sokolovskyea2509d2014-02-02 08:57:05 +020097 if (!m_seq_get_fast_slice_indexes(o->len, rhs, &start, &stop)) {
98 assert(0);
99 }
Paul Sokolovsky13cfabd2014-02-02 03:32:55 +0200100 mp_obj_list_t *res = list_new(stop - start);
101 m_seq_copy(res->items, o->items + start, res->len, mp_obj_t);
102 return res;
103 }
104#endif
xbe9e1e8cd2014-03-12 22:57:16 -0700105 uint index = mp_get_index(o->base.type, o->len, rhs, false);
Damiend99b0522013-12-21 18:17:45 +0000106 return o->items[index];
107 }
John R. Lenton4cb80582014-01-03 02:27:08 +0000108 case RT_BINARY_OP_ADD:
109 {
Damien George3e1a5c12014-03-29 13:43:38 +0000110 if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
John R. Lenton81ad89c2014-01-03 02:32:40 +0000111 return NULL;
112 }
113 mp_obj_list_t *p = rhs;
114 mp_obj_list_t *s = list_new(o->len + p->len);
Paul Sokolovskyee4aaf72014-02-08 23:17:51 +0200115 m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
John R. Lenton81ad89c2014-01-03 02:32:40 +0000116 return s;
John R. Lenton4cb80582014-01-03 02:27:08 +0000117 }
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200118 case RT_BINARY_OP_INPLACE_ADD:
119 {
Damien George3e1a5c12014-03-29 13:43:38 +0000120 if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200121 return NULL;
122 }
123 list_extend(lhs, rhs);
124 return o;
125 }
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200126 case RT_BINARY_OP_MULTIPLY:
127 {
128 if (!MP_OBJ_IS_SMALL_INT(rhs)) {
129 return NULL;
130 }
131 int n = MP_OBJ_SMALL_INT_VALUE(rhs);
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200132 mp_obj_list_t *s = list_new(o->len * n);
133 mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
Paul Sokolovsky074d3b52014-01-10 18:12:25 +0200134 return s;
135 }
Damien George9aa2a522014-02-01 23:04:09 +0000136 case RT_BINARY_OP_EQUAL:
137 case RT_BINARY_OP_LESS:
138 case RT_BINARY_OP_LESS_EQUAL:
139 case RT_BINARY_OP_MORE:
140 case RT_BINARY_OP_MORE_EQUAL:
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200141 return MP_BOOL(list_cmp_helper(op, lhs, rhs));
Damien George9aa2a522014-02-01 23:04:09 +0000142 case RT_BINARY_OP_NOT_EQUAL:
143 return MP_BOOL(!list_cmp_helper(RT_BINARY_OP_EQUAL, lhs, rhs));
Paul Sokolovsky1945e602014-01-12 02:01:00 +0200144
Damiend99b0522013-12-21 18:17:45 +0000145 default:
146 // op not supported
147 return NULL;
148 }
149}
150
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200151STATIC mp_obj_t list_getiter(mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +0000152 return mp_obj_new_list_iterator(o_in, 0);
153}
154
155mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
Damien George3e1a5c12014-03-29 13:43:38 +0000156 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
Damiend99b0522013-12-21 18:17:45 +0000157 mp_obj_list_t *self = self_in;
158 if (self->len >= self->alloc) {
Damien732407f2013-12-29 19:33:23 +0000159 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200160 assert(self->items);
Damiend99b0522013-12-21 18:17:45 +0000161 self->alloc *= 2;
Damiend99b0522013-12-21 18:17:45 +0000162 }
163 self->items[self->len++] = arg;
164 return mp_const_none; // return None, as per CPython
165}
166
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200167STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000168 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
169 assert(MP_OBJ_IS_TYPE(arg_in, &mp_type_list));
Paul Sokolovskyc698d262014-01-12 00:51:05 +0200170 mp_obj_list_t *self = self_in;
171 mp_obj_list_t *arg = arg_in;
172
173 if (self->len + arg->len > self->alloc) {
174 // TODO: use alloc policy for "4"
175 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->len + arg->len + 4);
176 self->alloc = self->len + arg->len + 4;
177 }
178
179 memcpy(self->items + self->len, arg->items, sizeof(mp_obj_t) * arg->len);
180 self->len += arg->len;
181 return mp_const_none; // return None, as per CPython
182}
183
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200184STATIC mp_obj_t list_pop(uint n_args, const mp_obj_t *args) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000185 assert(1 <= n_args && n_args <= 2);
Damien George3e1a5c12014-03-29 13:43:38 +0000186 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
John R. Lenton25f417c2014-01-03 22:53:18 +0000187 mp_obj_list_t *self = args[0];
188 if (self->len == 0) {
Damien Georgec5966122014-02-15 16:10:44 +0000189 nlr_jump(mp_obj_new_exception_msg(&mp_type_IndexError, "pop from empty list"));
John R. Lenton25f417c2014-01-03 22:53:18 +0000190 }
xbe9e1e8cd2014-03-12 22:57:16 -0700191 uint index = mp_get_index(self->base.type, self->len, n_args == 1 ? mp_obj_new_int(-1) : args[1], false);
Damiend99b0522013-12-21 18:17:45 +0000192 mp_obj_t ret = self->items[index];
193 self->len -= 1;
194 memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200195 if (self->alloc > LIST_MIN_ALLOC && self->alloc > 2 * self->len) {
John R. Lenton25f417c2014-01-03 22:53:18 +0000196 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
197 self->alloc /= 2;
198 }
Damiend99b0522013-12-21 18:17:45 +0000199 return ret;
200}
201
202// TODO make this conform to CPython's definition of sort
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200203STATIC 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 +0000204 int op = reversed ? RT_BINARY_OP_MORE : RT_BINARY_OP_LESS;
Damiend99b0522013-12-21 18:17:45 +0000205 while (head < tail) {
206 mp_obj_t *h = head - 1;
207 mp_obj_t *t = tail;
John R. Lentonc06763a2014-01-07 17:29:16 +0000208 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 +0000209 for (;;) {
John R. Lentonb8698fc2014-01-11 00:58:59 +0000210 do ++h; while (rt_binary_op(op, key_fn == NULL ? h[0] : rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
211 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 +0000212 if (h >= t) break;
213 mp_obj_t x = h[0];
214 h[0] = t[0];
215 t[0] = x;
216 }
217 mp_obj_t x = h[0];
218 h[0] = tail[0];
219 tail[0] = x;
John R. Lentonc06763a2014-01-07 17:29:16 +0000220 mp_quicksort(head, t, key_fn, reversed);
Damiend99b0522013-12-21 18:17:45 +0000221 head = h + 1;
222 }
223}
224
Damien George20006db2014-01-18 14:10:48 +0000225mp_obj_t mp_obj_list_sort(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
226 assert(n_args >= 1);
Damien George3e1a5c12014-03-29 13:43:38 +0000227 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
Damien George20006db2014-01-18 14:10:48 +0000228 if (n_args > 1) {
Damien Georgec5966122014-02-15 16:10:44 +0000229 nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError,
John R. Lentonc06763a2014-01-07 17:29:16 +0000230 "list.sort takes no positional arguments"));
231 }
Damien George20006db2014-01-18 14:10:48 +0000232 mp_obj_list_t *self = args[0];
Damiend99b0522013-12-21 18:17:45 +0000233 if (self->len > 1) {
Damien George7d0bfbe2014-02-08 19:01:47 +0000234 mp_map_elem_t *keyfun = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(MP_QSTR_key), MP_MAP_LOOKUP);
235 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 +0000236 mp_quicksort(self->items, self->items + self->len - 1,
237 keyfun ? keyfun->value : NULL,
238 reverse && reverse->value ? rt_is_true(reverse->value) : false);
Damiend99b0522013-12-21 18:17:45 +0000239 }
240 return mp_const_none; // return None, as per CPython
241}
242
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200243STATIC mp_obj_t list_clear(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000244 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton069ded92014-01-03 23:22:53 +0000245 mp_obj_list_t *self = self_in;
246 self->len = 0;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200247 self->items = m_renew(mp_obj_t, self->items, self->alloc, LIST_MIN_ALLOC);
248 self->alloc = LIST_MIN_ALLOC;
John R. Lenton069ded92014-01-03 23:22:53 +0000249 return mp_const_none;
250}
251
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200252STATIC mp_obj_t list_copy(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000253 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton26c21162014-01-03 23:42:17 +0000254 mp_obj_list_t *self = self_in;
255 return mp_obj_new_list(self->len, self->items);
256}
257
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200258STATIC mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
Damien George3e1a5c12014-03-29 13:43:38 +0000259 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lentone241e8c2014-01-03 23:57:28 +0000260 mp_obj_list_t *self = self_in;
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200261 return mp_seq_count_obj(self->items, self->len, value);
John R. Lentone241e8c2014-01-03 23:57:28 +0000262}
263
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200264STATIC mp_obj_t list_index(uint n_args, const mp_obj_t *args) {
John R. Lenton5d4a8212014-01-04 00:26:30 +0000265 assert(2 <= n_args && n_args <= 4);
Damien George3e1a5c12014-03-29 13:43:38 +0000266 assert(MP_OBJ_IS_TYPE(args[0], &mp_type_list));
John R. Lenton5d4a8212014-01-04 00:26:30 +0000267 mp_obj_list_t *self = args[0];
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200268 return mp_seq_index_obj(self->items, self->len, n_args, args);
John R. Lenton5d4a8212014-01-04 00:26:30 +0000269}
270
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200271STATIC 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 +0000272 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton45a87442014-01-04 01:15:01 +0000273 mp_obj_list_t *self = self_in;
274 // insert has its own strange index logic
275 int index = MP_OBJ_SMALL_INT_VALUE(idx);
276 if (index < 0) {
277 index += self->len;
278 }
279 if (index < 0) {
280 index = 0;
281 }
282 if (index > self->len) {
283 index = self->len;
284 }
285
286 mp_obj_list_append(self_in, mp_const_none);
287
288 for (int i = self->len-1; i > index; i--) {
289 self->items[i] = self->items[i-1];
290 }
291 self->items[index] = obj;
292
293 return mp_const_none;
294}
295
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200296STATIC mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) {
Damien George3e1a5c12014-03-29 13:43:38 +0000297 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton49fb6e52014-01-04 01:56:53 +0000298 mp_obj_t args[] = {self_in, value};
299 args[1] = list_index(2, args);
300 list_pop(2, args);
301
302 return mp_const_none;
303}
304
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200305STATIC mp_obj_t list_reverse(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +0000306 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000307 mp_obj_list_t *self = self_in;
308
309 int len = self->len;
310 for (int i = 0; i < len/2; i++) {
311 mp_obj_t *a = self->items[i];
312 self->items[i] = self->items[len-i-1];
313 self->items[len-i-1] = a;
314 }
315
316 return mp_const_none;
317}
318
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200319STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
320STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_extend_obj, list_extend);
321STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
322STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
323STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
324STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
325STATIC MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
326STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
327STATIC MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
328STATIC MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
329STATIC MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, 0, mp_obj_list_sort);
Damiend99b0522013-12-21 18:17:45 +0000330
Damien George9b196cd2014-03-26 21:47:19 +0000331STATIC const mp_map_elem_t list_locals_dict_table[] = {
332 { MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&list_append_obj },
333 { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&list_clear_obj },
334 { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&list_copy_obj },
335 { MP_OBJ_NEW_QSTR(MP_QSTR_count), (mp_obj_t)&list_count_obj },
336 { MP_OBJ_NEW_QSTR(MP_QSTR_extend), (mp_obj_t)&list_extend_obj },
337 { MP_OBJ_NEW_QSTR(MP_QSTR_index), (mp_obj_t)&list_index_obj },
338 { MP_OBJ_NEW_QSTR(MP_QSTR_insert), (mp_obj_t)&list_insert_obj },
339 { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&list_pop_obj },
340 { MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&list_remove_obj },
341 { MP_OBJ_NEW_QSTR(MP_QSTR_reverse), (mp_obj_t)&list_reverse_obj },
342 { MP_OBJ_NEW_QSTR(MP_QSTR_sort), (mp_obj_t)&list_sort_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800343};
Damien George97209d32014-01-07 15:58:30 +0000344
Damien George9b196cd2014-03-26 21:47:19 +0000345STATIC MP_DEFINE_CONST_DICT(list_locals_dict, list_locals_dict_table);
346
Damien George3e1a5c12014-03-29 13:43:38 +0000347const mp_obj_type_t mp_type_list = {
Damien Georgec5966122014-02-15 16:10:44 +0000348 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000349 .name = MP_QSTR_list,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200350 .print = list_print,
351 .make_new = list_make_new,
Damien George4e8dc8c2014-01-27 23:15:32 +0000352 .unary_op = list_unary_op,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200353 .binary_op = list_binary_op,
354 .getiter = list_getiter,
Damien George9b196cd2014-03-26 21:47:19 +0000355 .locals_dict = (mp_obj_t)&list_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000356};
357
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200358STATIC mp_obj_list_t *list_new(uint n) {
Damiend99b0522013-12-21 18:17:45 +0000359 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000360 o->base.type = &mp_type_list;
Paul Sokolovskyddf1aa92014-01-27 01:06:23 +0200361 o->alloc = n < LIST_MIN_ALLOC ? LIST_MIN_ALLOC : n;
Damiend99b0522013-12-21 18:17:45 +0000362 o->len = n;
363 o->items = m_new(mp_obj_t, o->alloc);
364 return o;
365}
366
367mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
368 mp_obj_list_t *o = list_new(n);
Paul Sokolovskye5a15cb2014-02-04 09:44:10 +0200369 if (items != NULL) {
370 for (int i = 0; i < n; i++) {
371 o->items[i] = items[i];
372 }
Damiend99b0522013-12-21 18:17:45 +0000373 }
374 return o;
375}
376
Damiend99b0522013-12-21 18:17:45 +0000377void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
378 mp_obj_list_t *self = self_in;
379 *len = self->len;
380 *items = self->items;
381}
382
383void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
384 mp_obj_list_t *self = self_in;
xbe9e1e8cd2014-03-12 22:57:16 -0700385 uint i = mp_get_index(self->base.type, self->len, index, false);
Damiend99b0522013-12-21 18:17:45 +0000386 self->items[i] = value;
387}
388
389/******************************************************************************/
390/* list iterator */
391
392typedef struct _mp_obj_list_it_t {
393 mp_obj_base_t base;
394 mp_obj_list_t *list;
395 machine_uint_t cur;
396} mp_obj_list_it_t;
397
398mp_obj_t list_it_iternext(mp_obj_t self_in) {
399 mp_obj_list_it_t *self = self_in;
400 if (self->cur < self->list->len) {
401 mp_obj_t o_out = self->list->items[self->cur];
402 self->cur += 1;
403 return o_out;
404 } else {
Damien George66eaf842014-03-26 19:27:58 +0000405 return MP_OBJ_NULL;
Damiend99b0522013-12-21 18:17:45 +0000406 }
407}
408
Damien George3e1a5c12014-03-29 13:43:38 +0000409STATIC const mp_obj_type_t mp_type_list_it = {
Damien Georgec5966122014-02-15 16:10:44 +0000410 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000411 .name = MP_QSTR_iterator,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200412 .iternext = list_it_iternext,
Damiend99b0522013-12-21 18:17:45 +0000413};
414
415mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
416 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000417 o->base.type = &mp_type_list_it;
Damiend99b0522013-12-21 18:17:45 +0000418 o->list = list;
419 o->cur = cur;
420 return o;
421}