blob: 47bd6d985a1a30c850c3d9c69925712d5cfa0360 [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
128static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
John R. Lenton25f417c2014-01-03 22:53:18 +0000129static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
Damiend99b0522013-12-21 18:17:45 +0000130static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
131
132const mp_obj_type_t list_type = {
133 { &mp_const_type },
134 "list",
135 list_print, // print
136 NULL, // call_n
137 NULL, // unary_op
138 list_binary_op, // binary_op
139 list_getiter, // getiter
140 NULL, // iternext
141 { // method list
142 { "append", &list_append_obj },
143 { "pop", &list_pop_obj },
144 { "sort", &list_sort_obj },
145 { NULL, NULL }, // end-of-list sentinel
146 },
147};
148
149static mp_obj_list_t *list_new(uint n) {
150 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
151 o->base.type = &list_type;
152 o->alloc = n < 4 ? 4 : n;
153 o->len = n;
154 o->items = m_new(mp_obj_t, o->alloc);
155 return o;
156}
157
158mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
159 mp_obj_list_t *o = list_new(n);
160 for (int i = 0; i < n; i++) {
161 o->items[i] = items[i];
162 }
163 return o;
164}
165
166mp_obj_t mp_obj_new_list_reverse(uint n, mp_obj_t *items) {
167 mp_obj_list_t *o = list_new(n);
168 for (int i = 0; i < n; i++) {
169 o->items[i] = items[n - i - 1];
170 }
171 return o;
172}
173
174void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
175 mp_obj_list_t *self = self_in;
176 *len = self->len;
177 *items = self->items;
178}
179
180void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
181 mp_obj_list_t *self = self_in;
182 uint i = mp_get_index(self->base.type, self->len, index);
183 self->items[i] = value;
184}
185
186/******************************************************************************/
187/* list iterator */
188
189typedef struct _mp_obj_list_it_t {
190 mp_obj_base_t base;
191 mp_obj_list_t *list;
192 machine_uint_t cur;
193} mp_obj_list_it_t;
194
195mp_obj_t list_it_iternext(mp_obj_t self_in) {
196 mp_obj_list_it_t *self = self_in;
197 if (self->cur < self->list->len) {
198 mp_obj_t o_out = self->list->items[self->cur];
199 self->cur += 1;
200 return o_out;
201 } else {
202 return mp_const_stop_iteration;
203 }
204}
205
206static const mp_obj_type_t list_it_type = {
207 { &mp_const_type },
208 "list_iterator",
209 NULL, // print
210 NULL, // call_n
211 NULL, // unary_op
212 NULL, // binary_op
213 NULL, // getiter
214 list_it_iternext, // iternext
215 { { NULL, NULL }, }, // method list
216};
217
218mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
219 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
220 o->base.type = &list_it_type;
221 o->list = list;
222 o->cur = cur;
223 return o;
224}