blob: 30188261131f15297eab2c93457d161c58fb6d50 [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
Damiend99b0522013-12-21 18:17:45 +0000180static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
John R. Lenton069ded92014-01-03 23:22:53 +0000181static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
John R. Lenton26c21162014-01-03 23:42:17 +0000182static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
John R. Lentone241e8c2014-01-03 23:57:28 +0000183static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
John R. Lenton25f417c2014-01-03 22:53:18 +0000184static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
Damiend99b0522013-12-21 18:17:45 +0000185static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
186
187const mp_obj_type_t list_type = {
188 { &mp_const_type },
189 "list",
190 list_print, // print
Damien George71c51812014-01-04 20:21:15 +0000191 list_make_new, // make_new
Damiend99b0522013-12-21 18:17:45 +0000192 NULL, // call_n
193 NULL, // unary_op
194 list_binary_op, // binary_op
195 list_getiter, // getiter
196 NULL, // iternext
197 { // method list
198 { "append", &list_append_obj },
John R. Lenton069ded92014-01-03 23:22:53 +0000199 { "clear", &list_clear_obj },
John R. Lenton26c21162014-01-03 23:42:17 +0000200 { "copy", &list_copy_obj },
John R. Lentone241e8c2014-01-03 23:57:28 +0000201 { "count", &list_count_obj },
Damiend99b0522013-12-21 18:17:45 +0000202 { "pop", &list_pop_obj },
203 { "sort", &list_sort_obj },
204 { NULL, NULL }, // end-of-list sentinel
205 },
206};
207
208static mp_obj_list_t *list_new(uint n) {
209 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
210 o->base.type = &list_type;
211 o->alloc = n < 4 ? 4 : n;
212 o->len = n;
213 o->items = m_new(mp_obj_t, o->alloc);
214 return o;
215}
216
217mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
218 mp_obj_list_t *o = list_new(n);
219 for (int i = 0; i < n; i++) {
220 o->items[i] = items[i];
221 }
222 return o;
223}
224
225mp_obj_t mp_obj_new_list_reverse(uint n, mp_obj_t *items) {
226 mp_obj_list_t *o = list_new(n);
227 for (int i = 0; i < n; i++) {
228 o->items[i] = items[n - i - 1];
229 }
230 return o;
231}
232
233void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
234 mp_obj_list_t *self = self_in;
235 *len = self->len;
236 *items = self->items;
237}
238
239void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
240 mp_obj_list_t *self = self_in;
241 uint i = mp_get_index(self->base.type, self->len, index);
242 self->items[i] = value;
243}
244
245/******************************************************************************/
246/* list iterator */
247
248typedef struct _mp_obj_list_it_t {
249 mp_obj_base_t base;
250 mp_obj_list_t *list;
251 machine_uint_t cur;
252} mp_obj_list_it_t;
253
254mp_obj_t list_it_iternext(mp_obj_t self_in) {
255 mp_obj_list_it_t *self = self_in;
256 if (self->cur < self->list->len) {
257 mp_obj_t o_out = self->list->items[self->cur];
258 self->cur += 1;
259 return o_out;
260 } else {
261 return mp_const_stop_iteration;
262 }
263}
264
265static const mp_obj_type_t list_it_type = {
266 { &mp_const_type },
267 "list_iterator",
268 NULL, // print
Damien George71c51812014-01-04 20:21:15 +0000269 NULL, // make_new
Damiend99b0522013-12-21 18:17:45 +0000270 NULL, // call_n
271 NULL, // unary_op
272 NULL, // binary_op
273 NULL, // getiter
274 list_it_iternext, // iternext
275 { { NULL, NULL }, }, // method list
276};
277
278mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
279 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
280 o->base.type = &list_it_type;
281 o->list = list;
282 o->cur = cur;
283 return o;
284}