blob: 5162fa09ff6302c45c8e834516194656f764fa5e [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 Georgeeb7bfcb2014-01-04 15:57:35 +00009#include "mpqstr.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);
Damiend99b0522013-12-21 18:17:45 +000024
25/******************************************************************************/
26/* list */
27
28static void list_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
29 mp_obj_list_t *o = o_in;
30 print(env, "[");
31 for (int i = 0; i < o->len; i++) {
32 if (i > 0) {
33 print(env, ", ");
34 }
35 mp_obj_print_helper(print, env, o->items[i]);
36 }
37 print(env, "]");
38}
39
Damien George71c51812014-01-04 20:21:15 +000040static mp_obj_t list_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
41 switch (n_args) {
42 case 0:
43 // return a new, empty list
44 return rt_build_list(0, NULL);
45
46 case 1:
47 {
48 // make list from iterable
49 mp_obj_t iterable = rt_getiter(args[0]);
50 mp_obj_t list = rt_build_list(0, NULL);
51 mp_obj_t item;
52 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
53 rt_list_append(list, item);
54 }
55 return list;
56 }
57
58 default:
59 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));
60 }
ian-v5fd8fd22014-01-06 13:51:53 -080061 return NULL;
Damien George71c51812014-01-04 20:21:15 +000062}
63
Damiend99b0522013-12-21 18:17:45 +000064static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
65 mp_obj_list_t *o = lhs;
66 switch (op) {
67 case RT_BINARY_OP_SUBSCR:
68 {
69 // list load
70 uint index = mp_get_index(o->base.type, o->len, rhs);
71 return o->items[index];
72 }
John R. Lenton4cb80582014-01-03 02:27:08 +000073 case RT_BINARY_OP_ADD:
74 {
John R. Lenton81ad89c2014-01-03 02:32:40 +000075 if (!MP_OBJ_IS_TYPE(rhs, &list_type)) {
76 return NULL;
77 }
78 mp_obj_list_t *p = rhs;
79 mp_obj_list_t *s = list_new(o->len + p->len);
John R. Lenton9bc56d92014-01-03 10:13:38 +000080 memcpy(s->items, o->items, sizeof(mp_obj_t) * o->len);
81 memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len);
John R. Lenton81ad89c2014-01-03 02:32:40 +000082 return s;
John R. Lenton4cb80582014-01-03 02:27:08 +000083 }
Damiend99b0522013-12-21 18:17:45 +000084 default:
85 // op not supported
86 return NULL;
87 }
88}
89
90static mp_obj_t list_getiter(mp_obj_t o_in) {
91 return mp_obj_new_list_iterator(o_in, 0);
92}
93
94mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
95 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
96 mp_obj_list_t *self = self_in;
97 if (self->len >= self->alloc) {
Damien732407f2013-12-29 19:33:23 +000098 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
Damiend99b0522013-12-21 18:17:45 +000099 self->alloc *= 2;
Damiend99b0522013-12-21 18:17:45 +0000100 }
101 self->items[self->len++] = arg;
102 return mp_const_none; // return None, as per CPython
103}
104
John R. Lenton25f417c2014-01-03 22:53:18 +0000105static mp_obj_t list_pop(int n_args, const mp_obj_t *args) {
106 assert(1 <= n_args && n_args <= 2);
107 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
108 mp_obj_list_t *self = args[0];
109 if (self->len == 0) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000110 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "pop from empty list"));
John R. Lenton25f417c2014-01-03 22:53:18 +0000111 }
112 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 +0000113 mp_obj_t ret = self->items[index];
114 self->len -= 1;
115 memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
John R. Lenton25f417c2014-01-03 22:53:18 +0000116 if (self->alloc > 2 * self->len) {
117 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
118 self->alloc /= 2;
119 }
Damiend99b0522013-12-21 18:17:45 +0000120 return ret;
121}
122
123// TODO make this conform to CPython's definition of sort
John R. Lentonc06763a2014-01-07 17:29:16 +0000124static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool reversed) {
125 int op = reversed ? RT_COMPARE_OP_MORE : RT_COMPARE_OP_LESS;
Damiend99b0522013-12-21 18:17:45 +0000126 while (head < tail) {
127 mp_obj_t *h = head - 1;
128 mp_obj_t *t = tail;
John R. Lentonc06763a2014-01-07 17:29:16 +0000129 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 +0000130 for (;;) {
John R. Lentonc06763a2014-01-07 17:29:16 +0000131 do ++h; while (rt_compare_op(op, key_fn == NULL ? h[0] : rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
132 do --t; while (h < t && rt_compare_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 +0000133 if (h >= t) break;
134 mp_obj_t x = h[0];
135 h[0] = t[0];
136 t[0] = x;
137 }
138 mp_obj_t x = h[0];
139 h[0] = tail[0];
140 tail[0] = x;
John R. Lentonc06763a2014-01-07 17:29:16 +0000141 mp_quicksort(head, t, key_fn, reversed);
Damiend99b0522013-12-21 18:17:45 +0000142 head = h + 1;
143 }
144}
145
John R. Lentonc06763a2014-01-07 17:29:16 +0000146static mp_obj_t list_sort(mp_obj_t *args, mp_map_t *kwargs) {
147 mp_obj_t *args_items = NULL;
148 machine_uint_t args_len = 0;
149 qstr key_idx = qstr_from_str_static("key");
150 qstr reverse_idx = qstr_from_str_static("reverse");
151
152 assert(MP_OBJ_IS_TYPE(args, &tuple_type));
153 mp_obj_tuple_get(args, &args_len, &args_items);
154 assert(args_len >= 1);
155 if (args_len > 1) {
156 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
157 "list.sort takes no positional arguments"));
158 }
159 mp_obj_list_t *self = args_items[0];
Damiend99b0522013-12-21 18:17:45 +0000160 if (self->len > 1) {
John R. Lentonc06763a2014-01-07 17:29:16 +0000161 mp_map_elem_t *keyfun = mp_qstr_map_lookup(kwargs, key_idx, false);
162 mp_map_elem_t *reverse = mp_qstr_map_lookup(kwargs, reverse_idx, false);
163 mp_quicksort(self->items, self->items + self->len - 1,
164 keyfun ? keyfun->value : NULL,
165 reverse && reverse->value ? rt_is_true(reverse->value) : false);
Damiend99b0522013-12-21 18:17:45 +0000166 }
167 return mp_const_none; // return None, as per CPython
168}
169
John R. Lenton069ded92014-01-03 23:22:53 +0000170static mp_obj_t list_clear(mp_obj_t self_in) {
171 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
172 mp_obj_list_t *self = self_in;
173 self->len = 0;
174 self->items = m_renew(mp_obj_t, self->items, self->alloc, 4);
175 self->alloc = 4;
176 return mp_const_none;
177}
178
John R. Lenton26c21162014-01-03 23:42:17 +0000179static mp_obj_t list_copy(mp_obj_t self_in) {
180 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
181 mp_obj_list_t *self = self_in;
182 return mp_obj_new_list(self->len, self->items);
183}
184
John R. Lentone241e8c2014-01-03 23:57:28 +0000185static mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
186 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
187 mp_obj_list_t *self = self_in;
188 int count = 0;
189 for (int i = 0; i < self->len; i++) {
190 if (mp_obj_equal(self->items[i], value)) {
191 count++;
192 }
193 }
194
195 return mp_obj_new_int(count);
196}
197
John R. Lenton5d4a8212014-01-04 00:26:30 +0000198static mp_obj_t list_index(int n_args, const mp_obj_t *args) {
199 assert(2 <= n_args && n_args <= 4);
200 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
201 mp_obj_list_t *self = args[0];
202 mp_obj_t *value = args[1];
John R. Lentonc5531622014-01-05 21:57:27 +0000203 uint start = 0;
204 uint stop = self->len;
John R. Lenton5d4a8212014-01-04 00:26:30 +0000205
John R. Lentonc5531622014-01-05 21:57:27 +0000206 if (n_args >= 3) {
207 start = mp_get_index(self->base.type, self->len, args[2]);
208 if (n_args >= 4) {
209 stop = mp_get_index(self->base.type, self->len, args[3]);
210 }
211 }
John R. Lenton5d4a8212014-01-04 00:26:30 +0000212
John R. Lentonc5531622014-01-05 21:57:27 +0000213 for (uint i = start; i < stop; i++) {
John R. Lenton5d4a8212014-01-04 00:26:30 +0000214 if (mp_obj_equal(self->items[i], value)) {
215 return mp_obj_new_int(i);
216 }
217 }
218
Damien Georgef0691f42014-01-05 13:44:06 +0000219 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "object not in list"));
John R. Lenton5d4a8212014-01-04 00:26:30 +0000220}
221
John R. Lenton45a87442014-01-04 01:15:01 +0000222static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
223 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
224 mp_obj_list_t *self = self_in;
225 // insert has its own strange index logic
226 int index = MP_OBJ_SMALL_INT_VALUE(idx);
227 if (index < 0) {
228 index += self->len;
229 }
230 if (index < 0) {
231 index = 0;
232 }
233 if (index > self->len) {
234 index = self->len;
235 }
236
237 mp_obj_list_append(self_in, mp_const_none);
238
239 for (int i = self->len-1; i > index; i--) {
240 self->items[i] = self->items[i-1];
241 }
242 self->items[index] = obj;
243
244 return mp_const_none;
245}
246
John R. Lenton49fb6e52014-01-04 01:56:53 +0000247static mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) {
248 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
249 mp_obj_t args[] = {self_in, value};
250 args[1] = list_index(2, args);
251 list_pop(2, args);
252
253 return mp_const_none;
254}
255
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000256static mp_obj_t list_reverse(mp_obj_t self_in) {
257 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
258 mp_obj_list_t *self = self_in;
259
260 int len = self->len;
261 for (int i = 0; i < len/2; i++) {
262 mp_obj_t *a = self->items[i];
263 self->items[i] = self->items[len-i-1];
264 self->items[len-i-1] = a;
265 }
266
267 return mp_const_none;
268}
269
Damiend99b0522013-12-21 18:17:45 +0000270static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
John R. Lenton069ded92014-01-03 23:22:53 +0000271static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
John R. Lenton26c21162014-01-03 23:42:17 +0000272static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
John R. Lentone241e8c2014-01-03 23:57:28 +0000273static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
John R. Lenton5d4a8212014-01-04 00:26:30 +0000274static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
John R. Lenton45a87442014-01-04 01:15:01 +0000275static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
John R. Lenton25f417c2014-01-03 22:53:18 +0000276static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
John R. Lenton49fb6e52014-01-04 01:56:53 +0000277static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000278static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
John R. Lentonc06763a2014-01-07 17:29:16 +0000279static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, list_sort);
Damiend99b0522013-12-21 18:17:45 +0000280
ian-va5a01df2014-01-06 14:14:11 -0800281static const mp_method_t list_type_methods[] = {
ian-v7a16fad2014-01-06 09:52:29 -0800282 { "append", &list_append_obj },
283 { "clear", &list_clear_obj },
284 { "copy", &list_copy_obj },
285 { "count", &list_count_obj },
286 { "index", &list_index_obj },
ian-v5fd8fd22014-01-06 13:51:53 -0800287 { "insert", &list_insert_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800288 { "pop", &list_pop_obj },
289 { "remove", &list_remove_obj },
290 { "reverse", &list_reverse_obj },
291 { "sort", &list_sort_obj },
292 { NULL, NULL }, // end-of-list sentinel
293};
Damien George97209d32014-01-07 15:58:30 +0000294
Damiend99b0522013-12-21 18:17:45 +0000295const mp_obj_type_t list_type = {
John R. Lenton3391e192014-01-07 18:06:34 +0000296 { &mp_const_type },
297 "list",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200298 .print = list_print,
299 .make_new = list_make_new,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200300 .binary_op = list_binary_op,
301 .getiter = list_getiter,
ian-v7a16fad2014-01-06 09:52:29 -0800302 .methods = list_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000303};
304
305static mp_obj_list_t *list_new(uint n) {
306 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
307 o->base.type = &list_type;
308 o->alloc = n < 4 ? 4 : n;
309 o->len = n;
310 o->items = m_new(mp_obj_t, o->alloc);
311 return o;
312}
313
314mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
315 mp_obj_list_t *o = list_new(n);
316 for (int i = 0; i < n; i++) {
317 o->items[i] = items[i];
318 }
319 return o;
320}
321
322mp_obj_t mp_obj_new_list_reverse(uint n, mp_obj_t *items) {
323 mp_obj_list_t *o = list_new(n);
324 for (int i = 0; i < n; i++) {
325 o->items[i] = items[n - i - 1];
326 }
327 return o;
328}
329
330void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
331 mp_obj_list_t *self = self_in;
332 *len = self->len;
333 *items = self->items;
334}
335
336void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
337 mp_obj_list_t *self = self_in;
338 uint i = mp_get_index(self->base.type, self->len, index);
339 self->items[i] = value;
340}
341
342/******************************************************************************/
343/* list iterator */
344
345typedef struct _mp_obj_list_it_t {
346 mp_obj_base_t base;
347 mp_obj_list_t *list;
348 machine_uint_t cur;
349} mp_obj_list_it_t;
350
351mp_obj_t list_it_iternext(mp_obj_t self_in) {
352 mp_obj_list_it_t *self = self_in;
353 if (self->cur < self->list->len) {
354 mp_obj_t o_out = self->list->items[self->cur];
355 self->cur += 1;
356 return o_out;
357 } else {
358 return mp_const_stop_iteration;
359 }
360}
361
362static const mp_obj_type_t list_it_type = {
John R. Lenton3391e192014-01-07 18:06:34 +0000363 { &mp_const_type },
364 "list_iterator",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200365 .iternext = list_it_iternext,
Damiend99b0522013-12-21 18:17:45 +0000366};
367
368mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
369 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
370 o->base.type = &list_it_type;
371 o->list = list;
372 o->cur = cur;
373 return o;
374}