blob: 5d4e8c03ab3ad160b8e09993b480e0d66cc25718 [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
79static mp_obj_t list_pop(mp_obj_t self_in, mp_obj_t arg) {
80 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
81 mp_obj_list_t *self = self_in;
82 uint index = mp_get_index(self->base.type, self->len, arg);
83 mp_obj_t ret = self->items[index];
84 self->len -= 1;
85 memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
86 return ret;
87}
88
89// TODO make this conform to CPython's definition of sort
90static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn) {
91 while (head < tail) {
92 mp_obj_t *h = head - 1;
93 mp_obj_t *t = tail;
94 mp_obj_t v = rt_call_function_1(key_fn, tail[0]); // get pivot using key_fn
95 for (;;) {
96 do ++h; while (rt_compare_op(RT_COMPARE_OP_LESS, rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
97 do --t; while (h < t && rt_compare_op(RT_COMPARE_OP_LESS, v, rt_call_function_1(key_fn, t[0])) == mp_const_true);
98 if (h >= t) break;
99 mp_obj_t x = h[0];
100 h[0] = t[0];
101 t[0] = x;
102 }
103 mp_obj_t x = h[0];
104 h[0] = tail[0];
105 tail[0] = x;
106 mp_quicksort(head, t, key_fn);
107 head = h + 1;
108 }
109}
110
111static mp_obj_t list_sort(mp_obj_t self_in, mp_obj_t key_fn) {
112 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
113 mp_obj_list_t *self = self_in;
114 if (self->len > 1) {
115 mp_quicksort(self->items, self->items + self->len - 1, key_fn);
116 }
117 return mp_const_none; // return None, as per CPython
118}
119
120static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
121static MP_DEFINE_CONST_FUN_OBJ_2(list_pop_obj, list_pop);
122static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
123
124const mp_obj_type_t list_type = {
125 { &mp_const_type },
126 "list",
127 list_print, // print
128 NULL, // call_n
129 NULL, // unary_op
130 list_binary_op, // binary_op
131 list_getiter, // getiter
132 NULL, // iternext
133 { // method list
134 { "append", &list_append_obj },
135 { "pop", &list_pop_obj },
136 { "sort", &list_sort_obj },
137 { NULL, NULL }, // end-of-list sentinel
138 },
139};
140
141static mp_obj_list_t *list_new(uint n) {
142 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
143 o->base.type = &list_type;
144 o->alloc = n < 4 ? 4 : n;
145 o->len = n;
146 o->items = m_new(mp_obj_t, o->alloc);
147 return o;
148}
149
150mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
151 mp_obj_list_t *o = list_new(n);
152 for (int i = 0; i < n; i++) {
153 o->items[i] = items[i];
154 }
155 return o;
156}
157
158mp_obj_t mp_obj_new_list_reverse(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[n - i - 1];
162 }
163 return o;
164}
165
166void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
167 mp_obj_list_t *self = self_in;
168 *len = self->len;
169 *items = self->items;
170}
171
172void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
173 mp_obj_list_t *self = self_in;
174 uint i = mp_get_index(self->base.type, self->len, index);
175 self->items[i] = value;
176}
177
178/******************************************************************************/
179/* list iterator */
180
181typedef struct _mp_obj_list_it_t {
182 mp_obj_base_t base;
183 mp_obj_list_t *list;
184 machine_uint_t cur;
185} mp_obj_list_it_t;
186
187mp_obj_t list_it_iternext(mp_obj_t self_in) {
188 mp_obj_list_it_t *self = self_in;
189 if (self->cur < self->list->len) {
190 mp_obj_t o_out = self->list->items[self->cur];
191 self->cur += 1;
192 return o_out;
193 } else {
194 return mp_const_stop_iteration;
195 }
196}
197
198static const mp_obj_type_t list_it_type = {
199 { &mp_const_type },
200 "list_iterator",
201 NULL, // print
202 NULL, // call_n
203 NULL, // unary_op
204 NULL, // binary_op
205 NULL, // getiter
206 list_it_iternext, // iternext
207 { { NULL, NULL }, }, // method list
208};
209
210mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
211 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
212 o->base.type = &list_it_type;
213 o->list = list;
214 o->cur = cur;
215 return o;
216}