blob: 3e5a15fceb55ac443370c0341cd3abd641bd86ea [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"
11#include "runtime0.h"
12#include "runtime.h"
13
14typedef struct _mp_obj_list_t {
15 mp_obj_base_t base;
16 machine_uint_t alloc;
17 machine_uint_t len;
18 mp_obj_t *items;
19} mp_obj_list_t;
20
21static mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
John R. Lenton4cb80582014-01-03 02:27:08 +000022static mp_obj_list_t *list_new(uint n);
Damiend99b0522013-12-21 18:17:45 +000023
24/******************************************************************************/
25/* list */
26
27static void list_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
28 mp_obj_list_t *o = o_in;
29 print(env, "[");
30 for (int i = 0; i < o->len; i++) {
31 if (i > 0) {
32 print(env, ", ");
33 }
34 mp_obj_print_helper(print, env, o->items[i]);
35 }
36 print(env, "]");
37}
38
Damien George71c51812014-01-04 20:21:15 +000039static mp_obj_t list_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
40 switch (n_args) {
41 case 0:
42 // return a new, empty list
43 return rt_build_list(0, NULL);
44
45 case 1:
46 {
47 // make list from iterable
48 mp_obj_t iterable = rt_getiter(args[0]);
49 mp_obj_t list = rt_build_list(0, NULL);
50 mp_obj_t item;
51 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
52 rt_list_append(list, item);
53 }
54 return list;
55 }
56
57 default:
58 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));
59 }
60}
61
Damiend99b0522013-12-21 18:17:45 +000062static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
63 mp_obj_list_t *o = lhs;
64 switch (op) {
65 case RT_BINARY_OP_SUBSCR:
66 {
67 // list load
68 uint index = mp_get_index(o->base.type, o->len, rhs);
69 return o->items[index];
70 }
John R. Lenton4cb80582014-01-03 02:27:08 +000071 case RT_BINARY_OP_ADD:
72 {
John R. Lenton81ad89c2014-01-03 02:32:40 +000073 if (!MP_OBJ_IS_TYPE(rhs, &list_type)) {
74 return NULL;
75 }
76 mp_obj_list_t *p = rhs;
77 mp_obj_list_t *s = list_new(o->len + p->len);
John R. Lenton9bc56d92014-01-03 10:13:38 +000078 memcpy(s->items, o->items, sizeof(mp_obj_t) * o->len);
79 memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len);
John R. Lenton81ad89c2014-01-03 02:32:40 +000080 return s;
John R. Lenton4cb80582014-01-03 02:27:08 +000081 }
Damiend99b0522013-12-21 18:17:45 +000082 default:
83 // op not supported
84 return NULL;
85 }
86}
87
88static mp_obj_t list_getiter(mp_obj_t o_in) {
89 return mp_obj_new_list_iterator(o_in, 0);
90}
91
92mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
93 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
94 mp_obj_list_t *self = self_in;
95 if (self->len >= self->alloc) {
Damien732407f2013-12-29 19:33:23 +000096 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
Damiend99b0522013-12-21 18:17:45 +000097 self->alloc *= 2;
Damiend99b0522013-12-21 18:17:45 +000098 }
99 self->items[self->len++] = arg;
100 return mp_const_none; // return None, as per CPython
101}
102
John R. Lenton25f417c2014-01-03 22:53:18 +0000103static mp_obj_t list_pop(int n_args, const mp_obj_t *args) {
104 assert(1 <= n_args && n_args <= 2);
105 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
106 mp_obj_list_t *self = args[0];
107 if (self->len == 0) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000108 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "pop from empty list"));
John R. Lenton25f417c2014-01-03 22:53:18 +0000109 }
110 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 +0000111 mp_obj_t ret = self->items[index];
112 self->len -= 1;
113 memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
John R. Lenton25f417c2014-01-03 22:53:18 +0000114 if (self->alloc > 2 * self->len) {
115 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
116 self->alloc /= 2;
117 }
Damiend99b0522013-12-21 18:17:45 +0000118 return ret;
119}
120
121// TODO make this conform to CPython's definition of sort
122static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn) {
123 while (head < tail) {
124 mp_obj_t *h = head - 1;
125 mp_obj_t *t = tail;
126 mp_obj_t v = rt_call_function_1(key_fn, tail[0]); // get pivot using key_fn
127 for (;;) {
128 do ++h; while (rt_compare_op(RT_COMPARE_OP_LESS, rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
129 do --t; while (h < t && rt_compare_op(RT_COMPARE_OP_LESS, v, rt_call_function_1(key_fn, t[0])) == mp_const_true);
130 if (h >= t) break;
131 mp_obj_t x = h[0];
132 h[0] = t[0];
133 t[0] = x;
134 }
135 mp_obj_t x = h[0];
136 h[0] = tail[0];
137 tail[0] = x;
138 mp_quicksort(head, t, key_fn);
139 head = h + 1;
140 }
141}
142
143static mp_obj_t list_sort(mp_obj_t self_in, mp_obj_t key_fn) {
144 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
145 mp_obj_list_t *self = self_in;
146 if (self->len > 1) {
147 mp_quicksort(self->items, self->items + self->len - 1, key_fn);
148 }
149 return mp_const_none; // return None, as per CPython
150}
151
John R. Lenton069ded92014-01-03 23:22:53 +0000152static mp_obj_t list_clear(mp_obj_t self_in) {
153 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
154 mp_obj_list_t *self = self_in;
155 self->len = 0;
156 self->items = m_renew(mp_obj_t, self->items, self->alloc, 4);
157 self->alloc = 4;
158 return mp_const_none;
159}
160
John R. Lenton26c21162014-01-03 23:42:17 +0000161static mp_obj_t list_copy(mp_obj_t self_in) {
162 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
163 mp_obj_list_t *self = self_in;
164 return mp_obj_new_list(self->len, self->items);
165}
166
John R. Lentone241e8c2014-01-03 23:57:28 +0000167static mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
168 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
169 mp_obj_list_t *self = self_in;
170 int count = 0;
171 for (int i = 0; i < self->len; i++) {
172 if (mp_obj_equal(self->items[i], value)) {
173 count++;
174 }
175 }
176
177 return mp_obj_new_int(count);
178}
179
John R. Lenton5d4a8212014-01-04 00:26:30 +0000180static mp_obj_t list_index(int n_args, const mp_obj_t *args) {
181 assert(2 <= n_args && n_args <= 4);
182 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
183 mp_obj_list_t *self = args[0];
184 mp_obj_t *value = args[1];
John R. Lentonc5531622014-01-05 21:57:27 +0000185 uint start = 0;
186 uint stop = self->len;
John R. Lenton5d4a8212014-01-04 00:26:30 +0000187
John R. Lentonc5531622014-01-05 21:57:27 +0000188 if (n_args >= 3) {
189 start = mp_get_index(self->base.type, self->len, args[2]);
190 if (n_args >= 4) {
191 stop = mp_get_index(self->base.type, self->len, args[3]);
192 }
193 }
John R. Lenton5d4a8212014-01-04 00:26:30 +0000194
John R. Lentonc5531622014-01-05 21:57:27 +0000195 for (uint i = start; i < stop; i++) {
John R. Lenton5d4a8212014-01-04 00:26:30 +0000196 if (mp_obj_equal(self->items[i], value)) {
197 return mp_obj_new_int(i);
198 }
199 }
200
Damien Georgef0691f42014-01-05 13:44:06 +0000201 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "object not in list"));
John R. Lenton5d4a8212014-01-04 00:26:30 +0000202}
203
John R. Lenton45a87442014-01-04 01:15:01 +0000204static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
205 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
206 mp_obj_list_t *self = self_in;
207 // insert has its own strange index logic
208 int index = MP_OBJ_SMALL_INT_VALUE(idx);
209 if (index < 0) {
210 index += self->len;
211 }
212 if (index < 0) {
213 index = 0;
214 }
215 if (index > self->len) {
216 index = self->len;
217 }
218
219 mp_obj_list_append(self_in, mp_const_none);
220
221 for (int i = self->len-1; i > index; i--) {
222 self->items[i] = self->items[i-1];
223 }
224 self->items[index] = obj;
225
226 return mp_const_none;
227}
228
John R. Lenton49fb6e52014-01-04 01:56:53 +0000229static mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) {
230 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
231 mp_obj_t args[] = {self_in, value};
232 args[1] = list_index(2, args);
233 list_pop(2, args);
234
235 return mp_const_none;
236}
237
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000238static mp_obj_t list_reverse(mp_obj_t self_in) {
239 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
240 mp_obj_list_t *self = self_in;
241
242 int len = self->len;
243 for (int i = 0; i < len/2; i++) {
244 mp_obj_t *a = self->items[i];
245 self->items[i] = self->items[len-i-1];
246 self->items[len-i-1] = a;
247 }
248
249 return mp_const_none;
250}
251
Damiend99b0522013-12-21 18:17:45 +0000252static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
John R. Lenton069ded92014-01-03 23:22:53 +0000253static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
John R. Lenton26c21162014-01-03 23:42:17 +0000254static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
John R. Lentone241e8c2014-01-03 23:57:28 +0000255static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
John R. Lenton5d4a8212014-01-04 00:26:30 +0000256static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
John R. Lenton45a87442014-01-04 01:15:01 +0000257static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
John R. Lenton25f417c2014-01-03 22:53:18 +0000258static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
John R. Lenton49fb6e52014-01-04 01:56:53 +0000259static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
John R. Lenton6e1e98f2014-01-04 02:10:29 +0000260static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
Damiend99b0522013-12-21 18:17:45 +0000261static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
262
ian-v7a16fad2014-01-06 09:52:29 -0800263const mp_method_t list_type_methods[] = {
264 { "append", &list_append_obj },
265 { "clear", &list_clear_obj },
266 { "copy", &list_copy_obj },
267 { "count", &list_count_obj },
268 { "index", &list_index_obj },
269 { "pop", &list_pop_obj },
270 { "remove", &list_remove_obj },
271 { "reverse", &list_reverse_obj },
272 { "sort", &list_sort_obj },
273 { NULL, NULL }, // end-of-list sentinel
274};
Damiend99b0522013-12-21 18:17:45 +0000275const mp_obj_type_t list_type = {
276 { &mp_const_type },
277 "list",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200278 .print = list_print,
279 .make_new = list_make_new,
280 .unary_op = NULL,
281 .binary_op = list_binary_op,
282 .getiter = list_getiter,
ian-v7a16fad2014-01-06 09:52:29 -0800283 .methods = list_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000284};
285
286static mp_obj_list_t *list_new(uint n) {
287 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
288 o->base.type = &list_type;
289 o->alloc = n < 4 ? 4 : n;
290 o->len = n;
291 o->items = m_new(mp_obj_t, o->alloc);
292 return o;
293}
294
295mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
296 mp_obj_list_t *o = list_new(n);
297 for (int i = 0; i < n; i++) {
298 o->items[i] = items[i];
299 }
300 return o;
301}
302
303mp_obj_t mp_obj_new_list_reverse(uint n, mp_obj_t *items) {
304 mp_obj_list_t *o = list_new(n);
305 for (int i = 0; i < n; i++) {
306 o->items[i] = items[n - i - 1];
307 }
308 return o;
309}
310
311void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
312 mp_obj_list_t *self = self_in;
313 *len = self->len;
314 *items = self->items;
315}
316
317void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
318 mp_obj_list_t *self = self_in;
319 uint i = mp_get_index(self->base.type, self->len, index);
320 self->items[i] = value;
321}
322
323/******************************************************************************/
324/* list iterator */
325
326typedef struct _mp_obj_list_it_t {
327 mp_obj_base_t base;
328 mp_obj_list_t *list;
329 machine_uint_t cur;
330} mp_obj_list_it_t;
331
332mp_obj_t list_it_iternext(mp_obj_t self_in) {
333 mp_obj_list_it_t *self = self_in;
334 if (self->cur < self->list->len) {
335 mp_obj_t o_out = self->list->items[self->cur];
336 self->cur += 1;
337 return o_out;
338 } else {
339 return mp_const_stop_iteration;
340 }
341}
342
343static const mp_obj_type_t list_it_type = {
344 { &mp_const_type },
345 "list_iterator",
Paul Sokolovsky860ffb02014-01-05 22:34:09 +0200346 .iternext = list_it_iternext,
ian-v7a16fad2014-01-06 09:52:29 -0800347 .methods = NULL,
Damiend99b0522013-12-21 18:17:45 +0000348};
349
350mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
351 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
352 o->base.type = &list_it_type;
353 o->list = list;
354 o->cur = cur;
355 return o;
356}