blob: 30d3a41e4a31fcc3371fbbfa9271c0ea13b59518 [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
39static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
40 mp_obj_list_t *o = lhs;
41 switch (op) {
42 case RT_BINARY_OP_SUBSCR:
43 {
44 // list load
45 uint index = mp_get_index(o->base.type, o->len, rhs);
46 return o->items[index];
47 }
John R. Lenton4cb80582014-01-03 02:27:08 +000048 case RT_BINARY_OP_ADD:
49 {
John R. Lenton81ad89c2014-01-03 02:32:40 +000050 if (!MP_OBJ_IS_TYPE(rhs, &list_type)) {
51 return NULL;
52 }
53 mp_obj_list_t *p = rhs;
54 mp_obj_list_t *s = list_new(o->len + p->len);
John R. Lenton9bc56d92014-01-03 10:13:38 +000055 memcpy(s->items, o->items, sizeof(mp_obj_t) * o->len);
56 memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len);
John R. Lenton81ad89c2014-01-03 02:32:40 +000057 return s;
John R. Lenton4cb80582014-01-03 02:27:08 +000058 }
Damiend99b0522013-12-21 18:17:45 +000059 default:
60 // op not supported
61 return NULL;
62 }
63}
64
65static mp_obj_t list_getiter(mp_obj_t o_in) {
66 return mp_obj_new_list_iterator(o_in, 0);
67}
68
69mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
70 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
71 mp_obj_list_t *self = self_in;
72 if (self->len >= self->alloc) {
Damien732407f2013-12-29 19:33:23 +000073 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2);
Damiend99b0522013-12-21 18:17:45 +000074 self->alloc *= 2;
Damiend99b0522013-12-21 18:17:45 +000075 }
76 self->items[self->len++] = arg;
77 return mp_const_none; // return None, as per CPython
78}
79
John R. Lenton25f417c2014-01-03 22:53:18 +000080static mp_obj_t list_pop(int n_args, const mp_obj_t *args) {
81 assert(1 <= n_args && n_args <= 2);
82 assert(MP_OBJ_IS_TYPE(args[0], &list_type));
83 mp_obj_list_t *self = args[0];
84 if (self->len == 0) {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000085 nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "pop from empty list"));
John R. Lenton25f417c2014-01-03 22:53:18 +000086 }
87 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 +000088 mp_obj_t ret = self->items[index];
89 self->len -= 1;
90 memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
John R. Lenton25f417c2014-01-03 22:53:18 +000091 if (self->alloc > 2 * self->len) {
92 self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc/2);
93 self->alloc /= 2;
94 }
Damiend99b0522013-12-21 18:17:45 +000095 return ret;
96}
97
98// TODO make this conform to CPython's definition of sort
99static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn) {
100 while (head < tail) {
101 mp_obj_t *h = head - 1;
102 mp_obj_t *t = tail;
103 mp_obj_t v = rt_call_function_1(key_fn, tail[0]); // get pivot using key_fn
104 for (;;) {
105 do ++h; while (rt_compare_op(RT_COMPARE_OP_LESS, rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
106 do --t; while (h < t && rt_compare_op(RT_COMPARE_OP_LESS, v, rt_call_function_1(key_fn, t[0])) == mp_const_true);
107 if (h >= t) break;
108 mp_obj_t x = h[0];
109 h[0] = t[0];
110 t[0] = x;
111 }
112 mp_obj_t x = h[0];
113 h[0] = tail[0];
114 tail[0] = x;
115 mp_quicksort(head, t, key_fn);
116 head = h + 1;
117 }
118}
119
120static mp_obj_t list_sort(mp_obj_t self_in, mp_obj_t key_fn) {
121 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
122 mp_obj_list_t *self = self_in;
123 if (self->len > 1) {
124 mp_quicksort(self->items, self->items + self->len - 1, key_fn);
125 }
126 return mp_const_none; // return None, as per CPython
127}
128
John R. Lenton069ded92014-01-03 23:22:53 +0000129static mp_obj_t list_clear(mp_obj_t self_in) {
130 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
131 mp_obj_list_t *self = self_in;
132 self->len = 0;
133 self->items = m_renew(mp_obj_t, self->items, self->alloc, 4);
134 self->alloc = 4;
135 return mp_const_none;
136}
137
John R. Lenton26c21162014-01-03 23:42:17 +0000138static mp_obj_t list_copy(mp_obj_t self_in) {
139 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
140 mp_obj_list_t *self = self_in;
141 return mp_obj_new_list(self->len, self->items);
142}
143
Damiend99b0522013-12-21 18:17:45 +0000144static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
John R. Lenton069ded92014-01-03 23:22:53 +0000145static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
John R. Lenton26c21162014-01-03 23:42:17 +0000146static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
John R. Lenton25f417c2014-01-03 22:53:18 +0000147static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
Damiend99b0522013-12-21 18:17:45 +0000148static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
149
150const mp_obj_type_t list_type = {
151 { &mp_const_type },
152 "list",
153 list_print, // print
154 NULL, // call_n
155 NULL, // unary_op
156 list_binary_op, // binary_op
157 list_getiter, // getiter
158 NULL, // iternext
159 { // method list
160 { "append", &list_append_obj },
John R. Lenton069ded92014-01-03 23:22:53 +0000161 { "clear", &list_clear_obj },
John R. Lenton26c21162014-01-03 23:42:17 +0000162 { "copy", &list_copy_obj },
Damiend99b0522013-12-21 18:17:45 +0000163 { "pop", &list_pop_obj },
164 { "sort", &list_sort_obj },
165 { NULL, NULL }, // end-of-list sentinel
166 },
167};
168
169static mp_obj_list_t *list_new(uint n) {
170 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
171 o->base.type = &list_type;
172 o->alloc = n < 4 ? 4 : n;
173 o->len = n;
174 o->items = m_new(mp_obj_t, o->alloc);
175 return o;
176}
177
178mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
179 mp_obj_list_t *o = list_new(n);
180 for (int i = 0; i < n; i++) {
181 o->items[i] = items[i];
182 }
183 return o;
184}
185
186mp_obj_t mp_obj_new_list_reverse(uint n, mp_obj_t *items) {
187 mp_obj_list_t *o = list_new(n);
188 for (int i = 0; i < n; i++) {
189 o->items[i] = items[n - i - 1];
190 }
191 return o;
192}
193
194void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
195 mp_obj_list_t *self = self_in;
196 *len = self->len;
197 *items = self->items;
198}
199
200void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
201 mp_obj_list_t *self = self_in;
202 uint i = mp_get_index(self->base.type, self->len, index);
203 self->items[i] = value;
204}
205
206/******************************************************************************/
207/* list iterator */
208
209typedef struct _mp_obj_list_it_t {
210 mp_obj_base_t base;
211 mp_obj_list_t *list;
212 machine_uint_t cur;
213} mp_obj_list_it_t;
214
215mp_obj_t list_it_iternext(mp_obj_t self_in) {
216 mp_obj_list_it_t *self = self_in;
217 if (self->cur < self->list->len) {
218 mp_obj_t o_out = self->list->items[self->cur];
219 self->cur += 1;
220 return o_out;
221 } else {
222 return mp_const_stop_iteration;
223 }
224}
225
226static const mp_obj_type_t list_it_type = {
227 { &mp_const_type },
228 "list_iterator",
229 NULL, // print
230 NULL, // call_n
231 NULL, // unary_op
232 NULL, // binary_op
233 NULL, // getiter
234 list_it_iternext, // iternext
235 { { NULL, NULL }, }, // method list
236};
237
238mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
239 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
240 o->base.type = &list_it_type;
241 o->list = list;
242 o->cur = cur;
243 return o;
244}