blob: 9d8caee33981eb29e4029b6a0bc7b647e60a8a2c [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
Damien Georgedfc0bac2014-01-07 23:18:54 +0000146static mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs) {
John R. Lentonc06763a2014-01-07 17:29:16 +0000147 mp_obj_t *args_items = NULL;
Damien Georgedfc0bac2014-01-07 23:18:54 +0000148 uint args_len = 0;
John R. Lentonc06763a2014-01-07 17:29:16 +0000149
150 assert(MP_OBJ_IS_TYPE(args, &tuple_type));
151 mp_obj_tuple_get(args, &args_len, &args_items);
152 assert(args_len >= 1);
153 if (args_len > 1) {
154 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
155 "list.sort takes no positional arguments"));
156 }
157 mp_obj_list_t *self = args_items[0];
Damiend99b0522013-12-21 18:17:45 +0000158 if (self->len > 1) {
Damien George38a2da62014-01-08 17:33:12 +0000159 mp_map_elem_t *keyfun = mp_map_lookup(kwargs, MP_OBJ_NEW_QSTR(qstr_from_str_static("key")), MP_MAP_LOOKUP);
160 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 +0000161 mp_quicksort(self->items, self->items + self->len - 1,
162 keyfun ? keyfun->value : NULL,
163 reverse && reverse->value ? rt_is_true(reverse->value) : false);
Damiend99b0522013-12-21 18:17:45 +0000164 }
165 return mp_const_none; // return None, as per CPython
166}
167
John R. Lenton069ded92014-01-03 23:22:53 +0000168static mp_obj_t list_clear(mp_obj_t self_in) {
169 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
170 mp_obj_list_t *self = self_in;
171 self->len = 0;
172 self->items = m_renew(mp_obj_t, self->items, self->alloc, 4);
173 self->alloc = 4;
174 return mp_const_none;
175}
176
John R. Lenton26c21162014-01-03 23:42:17 +0000177static mp_obj_t list_copy(mp_obj_t self_in) {
178 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
179 mp_obj_list_t *self = self_in;
180 return mp_obj_new_list(self->len, self->items);
181}
182
John R. Lentone241e8c2014-01-03 23:57:28 +0000183static mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
184 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
185 mp_obj_list_t *self = self_in;
186 int count = 0;
187 for (int i = 0; i < self->len; i++) {
188 if (mp_obj_equal(self->items[i], value)) {
189 count++;
190 }
191 }
192
193 return mp_obj_new_int(count);
194}
195
John R. Lenton5d4a8212014-01-04 00:26:30 +0000196static mp_obj_t list_index(int n_args, const mp_obj_t *args) {
197 assert(2 <= n_args && n_args <= 4);
198 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
199 mp_obj_list_t *self = args[0];
200 mp_obj_t *value = args[1];
John R. Lentonc5531622014-01-05 21:57:27 +0000201 uint start = 0;
202 uint stop = self->len;
John R. Lenton5d4a8212014-01-04 00:26:30 +0000203
John R. Lentonc5531622014-01-05 21:57:27 +0000204 if (n_args >= 3) {
205 start = mp_get_index(self->base.type, self->len, args[2]);
206 if (n_args >= 4) {
207 stop = mp_get_index(self->base.type, self->len, args[3]);
208 }
209 }
John R. Lenton5d4a8212014-01-04 00:26:30 +0000210
John R. Lentonc5531622014-01-05 21:57:27 +0000211 for (uint i = start; i < stop; i++) {
John R. Lenton5d4a8212014-01-04 00:26:30 +0000212 if (mp_obj_equal(self->items[i], value)) {
213 return mp_obj_new_int(i);
214 }
215 }
216
Damien Georgef0691f42014-01-05 13:44:06 +0000217 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "object not in list"));
John R. Lenton5d4a8212014-01-04 00:26:30 +0000218}
219
John R. Lenton45a87442014-01-04 01:15:01 +0000220static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
221 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
222 mp_obj_list_t *self = self_in;
223 // insert has its own strange index logic
224 int index = MP_OBJ_SMALL_INT_VALUE(idx);
225 if (index < 0) {
226 index += self->len;
227 }
228 if (index < 0) {
229 index = 0;
230 }
231 if (index > self->len) {
232 index = self->len;
233 }
234
235 mp_obj_list_append(self_in, mp_const_none);
236
237 for (int i = self->len-1; i > index; i--) {
238 self->items[i] = self->items[i-1];
239 }
240 self->items[index] = obj;
241
242 return mp_const_none;
243}
244
John R. Lenton49fb6e52014-01-04 01:56:53 +0000245static mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) {
246 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
247 mp_obj_t args[] = {self_in, value};
248 args[1] = list_index(2, args);
249 list_pop(2, args);
250
251 return mp_const_none;
252}
253
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000254static mp_obj_t list_reverse(mp_obj_t self_in) {
255 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
256 mp_obj_list_t *self = self_in;
257
258 int len = self->len;
259 for (int i = 0; i < len/2; i++) {
260 mp_obj_t *a = self->items[i];
261 self->items[i] = self->items[len-i-1];
262 self->items[len-i-1] = a;
263 }
264
265 return mp_const_none;
266}
267
Damiend99b0522013-12-21 18:17:45 +0000268static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
John R. Lenton069ded92014-01-03 23:22:53 +0000269static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
John R. Lenton26c21162014-01-03 23:42:17 +0000270static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
John R. Lentone241e8c2014-01-03 23:57:28 +0000271static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
John R. Lenton5d4a8212014-01-04 00:26:30 +0000272static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
John R. Lenton45a87442014-01-04 01:15:01 +0000273static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
John R. Lenton25f417c2014-01-03 22:53:18 +0000274static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
John R. Lenton49fb6e52014-01-04 01:56:53 +0000275static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000276static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
John R. Lentonc06763a2014-01-07 17:29:16 +0000277static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, list_sort);
Damiend99b0522013-12-21 18:17:45 +0000278
ian-va5a01df2014-01-06 14:14:11 -0800279static const mp_method_t list_type_methods[] = {
ian-v7a16fad2014-01-06 09:52:29 -0800280 { "append", &list_append_obj },
281 { "clear", &list_clear_obj },
282 { "copy", &list_copy_obj },
283 { "count", &list_count_obj },
284 { "index", &list_index_obj },
ian-v5fd8fd22014-01-06 13:51:53 -0800285 { "insert", &list_insert_obj },
ian-v7a16fad2014-01-06 09:52:29 -0800286 { "pop", &list_pop_obj },
287 { "remove", &list_remove_obj },
288 { "reverse", &list_reverse_obj },
289 { "sort", &list_sort_obj },
290 { NULL, NULL }, // end-of-list sentinel
291};
Damien George97209d32014-01-07 15:58:30 +0000292
Damiend99b0522013-12-21 18:17:45 +0000293const mp_obj_type_t list_type = {
John R. Lenton3391e192014-01-07 18:06:34 +0000294 { &mp_const_type },
295 "list",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200296 .print = list_print,
297 .make_new = list_make_new,
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200298 .binary_op = list_binary_op,
299 .getiter = list_getiter,
ian-v7a16fad2014-01-06 09:52:29 -0800300 .methods = list_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000301};
302
303static mp_obj_list_t *list_new(uint n) {
304 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
305 o->base.type = &list_type;
306 o->alloc = n < 4 ? 4 : n;
307 o->len = n;
308 o->items = m_new(mp_obj_t, o->alloc);
309 return o;
310}
311
312mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
313 mp_obj_list_t *o = list_new(n);
314 for (int i = 0; i < n; i++) {
315 o->items[i] = items[i];
316 }
317 return o;
318}
319
320mp_obj_t mp_obj_new_list_reverse(uint n, mp_obj_t *items) {
321 mp_obj_list_t *o = list_new(n);
322 for (int i = 0; i < n; i++) {
323 o->items[i] = items[n - i - 1];
324 }
325 return o;
326}
327
328void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
329 mp_obj_list_t *self = self_in;
330 *len = self->len;
331 *items = self->items;
332}
333
334void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
335 mp_obj_list_t *self = self_in;
336 uint i = mp_get_index(self->base.type, self->len, index);
337 self->items[i] = value;
338}
339
340/******************************************************************************/
341/* list iterator */
342
343typedef struct _mp_obj_list_it_t {
344 mp_obj_base_t base;
345 mp_obj_list_t *list;
346 machine_uint_t cur;
347} mp_obj_list_it_t;
348
349mp_obj_t list_it_iternext(mp_obj_t self_in) {
350 mp_obj_list_it_t *self = self_in;
351 if (self->cur < self->list->len) {
352 mp_obj_t o_out = self->list->items[self->cur];
353 self->cur += 1;
354 return o_out;
355 } else {
356 return mp_const_stop_iteration;
357 }
358}
359
360static const mp_obj_type_t list_it_type = {
John R. Lenton3391e192014-01-07 18:06:34 +0000361 { &mp_const_type },
362 "list_iterator",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200363 .iternext = list_it_iternext,
Damiend99b0522013-12-21 18:17:45 +0000364};
365
366mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
367 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
368 o->base.type = &list_it_type;
369 o->list = list;
370 o->cur = cur;
371 return o;
372}