blob: 0a795a2b089f07a91dfe8c7103cb6df64a8f699e [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"
9#include "obj.h"
10#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
20static mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur);
John R. Lenton4cb80582014-01-03 02:27:08 +000021static mp_obj_list_t *list_new(uint n);
Damiend99b0522013-12-21 18:17:45 +000022
23/******************************************************************************/
24/* list */
25
26static void list_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
27 mp_obj_list_t *o = o_in;
28 print(env, "[");
29 for (int i = 0; i < o->len; i++) {
30 if (i > 0) {
31 print(env, ", ");
32 }
33 mp_obj_print_helper(print, env, o->items[i]);
34 }
35 print(env, "]");
36}
37
38static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
39 mp_obj_list_t *o = lhs;
40 switch (op) {
41 case RT_BINARY_OP_SUBSCR:
42 {
43 // list load
44 uint index = mp_get_index(o->base.type, o->len, rhs);
45 return o->items[index];
46 }
John R. Lenton4cb80582014-01-03 02:27:08 +000047 case RT_BINARY_OP_ADD:
48 {
John R. Lenton81ad89c2014-01-03 02:32:40 +000049 if (!MP_OBJ_IS_TYPE(rhs, &list_type)) {
50 return NULL;
51 }
52 mp_obj_list_t *p = rhs;
53 mp_obj_list_t *s = list_new(o->len + p->len);
John R. Lenton9bc56d92014-01-03 10:13:38 +000054 memcpy(s->items, o->items, sizeof(mp_obj_t) * o->len);
55 memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len);
John R. Lenton81ad89c2014-01-03 02:32:40 +000056 return s;
John R. Lenton4cb80582014-01-03 02:27:08 +000057 }
Damiend99b0522013-12-21 18:17:45 +000058 default:
59 // op not supported
60 return NULL;
61 }
62}
63
64static mp_obj_t list_getiter(mp_obj_t o_in) {
65 return mp_obj_new_list_iterator(o_in, 0);
66}
67
68mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
69 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
70 mp_obj_list_t *self = self_in;
71 if (self->len >= self->alloc) {
Damien732407f2013-12-29 19:33:23 +000072 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
Damiend99b0522013-12-21 18:17:45 +000073 self->alloc *= 2;
Damiend99b0522013-12-21 18:17:45 +000074 }
75 self->items[self->len++] = arg;
76 return mp_const_none; // return None, as per CPython
77}
78
John R. Lenton25f417c2014-01-03 22:53:18 +000079static mp_obj_t list_pop(int n_args, const mp_obj_t *args) {
80 assert(1 <= n_args && n_args <= 2);
81 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
82 mp_obj_list_t *self = args[0];
83 if (self->len == 0) {
84 nlr_jump(mp_obj_new_exception_msg(rt_q_IndexError, "pop from empty list"));
85 }
86 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 +000087 mp_obj_t ret = self->items[index];
88 self->len -= 1;
89 memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
John R. Lenton25f417c2014-01-03 22:53:18 +000090 if (self->alloc > 2 * self->len) {
91 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
92 self->alloc /= 2;
93 }
Damiend99b0522013-12-21 18:17:45 +000094 return ret;
95}
96
97// TODO make this conform to CPython's definition of sort
98static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn) {
99 while (head < tail) {
100 mp_obj_t *h = head - 1;
101 mp_obj_t *t = tail;
102 mp_obj_t v = rt_call_function_1(key_fn, tail[0]); // get pivot using key_fn
103 for (;;) {
104 do ++h; while (rt_compare_op(RT_COMPARE_OP_LESS, rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
105 do --t; while (h < t && rt_compare_op(RT_COMPARE_OP_LESS, v, rt_call_function_1(key_fn, t[0])) == mp_const_true);
106 if (h >= t) break;
107 mp_obj_t x = h[0];
108 h[0] = t[0];
109 t[0] = x;
110 }
111 mp_obj_t x = h[0];
112 h[0] = tail[0];
113 tail[0] = x;
114 mp_quicksort(head, t, key_fn);
115 head = h + 1;
116 }
117}
118
119static mp_obj_t list_sort(mp_obj_t self_in, mp_obj_t key_fn) {
120 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
121 mp_obj_list_t *self = self_in;
122 if (self->len > 1) {
123 mp_quicksort(self->items, self->items + self->len - 1, key_fn);
124 }
125 return mp_const_none; // return None, as per CPython
126}
127
John R. Lenton069ded92014-01-03 23:22:53 +0000128static mp_obj_t list_clear(mp_obj_t self_in) {
129 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
130 mp_obj_list_t *self = self_in;
131 self->len = 0;
132 self->items = m_renew(mp_obj_t, self->items, self->alloc, 4);
133 self->alloc = 4;
134 return mp_const_none;
135}
136
John R. Lenton26c21162014-01-03 23:42:17 +0000137static mp_obj_t list_copy(mp_obj_t self_in) {
138 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
139 mp_obj_list_t *self = self_in;
140 return mp_obj_new_list(self->len, self->items);
141}
142
John R. Lentone241e8c2014-01-03 23:57:28 +0000143static mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
144 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
145 mp_obj_list_t *self = self_in;
146 int count = 0;
147 for (int i = 0; i < self->len; i++) {
148 if (mp_obj_equal(self->items[i], value)) {
149 count++;
150 }
151 }
152
153 return mp_obj_new_int(count);
154}
155
John R. Lenton5d4a8212014-01-04 00:26:30 +0000156static mp_obj_t list_index(int n_args, const mp_obj_t *args) {
157 assert(2 <= n_args && n_args <= 4);
158 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
159 mp_obj_list_t *self = args[0];
160 mp_obj_t *value = args[1];
161
162 uint start = mp_get_index(self->base.type, self->len,
163 n_args >= 3 ? args[2] : mp_obj_new_int(0));
164 uint stop = mp_get_index(self->base.type, self->len,
165 n_args >= 4 ? args[3] : mp_obj_new_int(-1));
166
167 for (uint i = start; i <= stop; i++) {
168 if (mp_obj_equal(self->items[i], value)) {
169 return mp_obj_new_int(i);
170 }
171 }
172
173 nlr_jump(mp_obj_new_exception_msg(rt_q_ValueError, "Object not in list."));
174}
175
John R. Lenton45a87442014-01-04 01:15:01 +0000176static mp_obj_t list_insert(mp_obj_t self_in, mp_obj_t idx, mp_obj_t obj) {
177 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
178 mp_obj_list_t *self = self_in;
179 // insert has its own strange index logic
180 int index = MP_OBJ_SMALL_INT_VALUE(idx);
181 if (index < 0) {
182 index += self->len;
183 }
184 if (index < 0) {
185 index = 0;
186 }
187 if (index > self->len) {
188 index = self->len;
189 }
190
191 mp_obj_list_append(self_in, mp_const_none);
192
193 for (int i = self->len-1; i > index; i--) {
194 self->items[i] = self->items[i-1];
195 }
196 self->items[index] = obj;
197
198 return mp_const_none;
199}
200
John R. Lenton49fb6e52014-01-04 01:56:53 +0000201static mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) {
202 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
203 mp_obj_t args[] = {self_in, value};
204 args[1] = list_index(2, args);
205 list_pop(2, args);
206
207 return mp_const_none;
208}
209
Damiend99b0522013-12-21 18:17:45 +0000210static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
John R. Lenton069ded92014-01-03 23:22:53 +0000211static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
John R. Lenton26c21162014-01-03 23:42:17 +0000212static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
John R. Lentone241e8c2014-01-03 23:57:28 +0000213static MP_DEFINE_CONST_FUN_OBJ_2(list_count_obj, list_count);
John R. Lenton5d4a8212014-01-04 00:26:30 +0000214static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_index_obj, 2, 4, list_index);
John R. Lenton45a87442014-01-04 01:15:01 +0000215static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
John R. Lenton25f417c2014-01-03 22:53:18 +0000216static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
John R. Lenton49fb6e52014-01-04 01:56:53 +0000217static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
Damiend99b0522013-12-21 18:17:45 +0000218static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
219
220const mp_obj_type_t list_type = {
221 { &mp_const_type },
222 "list",
223 list_print, // print
224 NULL, // call_n
225 NULL, // unary_op
226 list_binary_op, // binary_op
227 list_getiter, // getiter
228 NULL, // iternext
229 { // method list
230 { "append", &list_append_obj },
John R. Lenton069ded92014-01-03 23:22:53 +0000231 { "clear", &list_clear_obj },
John R. Lenton26c21162014-01-03 23:42:17 +0000232 { "copy", &list_copy_obj },
John R. Lentone241e8c2014-01-03 23:57:28 +0000233 { "count", &list_count_obj },
John R. Lenton5d4a8212014-01-04 00:26:30 +0000234 { "index", &list_index_obj },
John R. Lenton45a87442014-01-04 01:15:01 +0000235 { "insert", &list_insert_obj },
Damiend99b0522013-12-21 18:17:45 +0000236 { "pop", &list_pop_obj },
John R. Lenton49fb6e52014-01-04 01:56:53 +0000237 { "remove", &list_remove_obj },
Damiend99b0522013-12-21 18:17:45 +0000238 { "sort", &list_sort_obj },
239 { NULL, NULL }, // end-of-list sentinel
240 },
241};
242
243static mp_obj_list_t *list_new(uint n) {
244 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
245 o->base.type = &list_type;
246 o->alloc = n < 4 ? 4 : n;
247 o->len = n;
248 o->items = m_new(mp_obj_t, o->alloc);
249 return o;
250}
251
252mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
253 mp_obj_list_t *o = list_new(n);
254 for (int i = 0; i < n; i++) {
255 o->items[i] = items[i];
256 }
257 return o;
258}
259
260mp_obj_t mp_obj_new_list_reverse(uint n, mp_obj_t *items) {
261 mp_obj_list_t *o = list_new(n);
262 for (int i = 0; i < n; i++) {
263 o->items[i] = items[n - i - 1];
264 }
265 return o;
266}
267
268void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
269 mp_obj_list_t *self = self_in;
270 *len = self->len;
271 *items = self->items;
272}
273
274void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
275 mp_obj_list_t *self = self_in;
276 uint i = mp_get_index(self->base.type, self->len, index);
277 self->items[i] = value;
278}
279
280/******************************************************************************/
281/* list iterator */
282
283typedef struct _mp_obj_list_it_t {
284 mp_obj_base_t base;
285 mp_obj_list_t *list;
286 machine_uint_t cur;
287} mp_obj_list_it_t;
288
289mp_obj_t list_it_iternext(mp_obj_t self_in) {
290 mp_obj_list_it_t *self = self_in;
291 if (self->cur < self->list->len) {
292 mp_obj_t o_out = self->list->items[self->cur];
293 self->cur += 1;
294 return o_out;
295 } else {
296 return mp_const_stop_iteration;
297 }
298}
299
300static const mp_obj_type_t list_it_type = {
301 { &mp_const_type },
302 "list_iterator",
303 NULL, // print
304 NULL, // call_n
305 NULL, // unary_op
306 NULL, // binary_op
307 NULL, // getiter
308 list_it_iternext, // iternext
309 { { NULL, NULL }, }, // method list
310};
311
312mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
313 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
314 o->base.type = &list_it_type;
315 o->list = list;
316 o->cur = cur;
317 return o;
318}