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