blob: 4d70c37eb3ebab66003dc1c9dc94269635e79d98 [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);
21
22/******************************************************************************/
23/* list */
24
25static void list_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) {
26 mp_obj_list_t *o = o_in;
27 print(env, "[");
28 for (int i = 0; i < o->len; i++) {
29 if (i > 0) {
30 print(env, ", ");
31 }
32 mp_obj_print_helper(print, env, o->items[i]);
33 }
34 print(env, "]");
35}
36
37static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
38 mp_obj_list_t *o = lhs;
39 switch (op) {
40 case RT_BINARY_OP_SUBSCR:
41 {
42 // list load
43 uint index = mp_get_index(o->base.type, o->len, rhs);
44 return o->items[index];
45 }
46 default:
47 // op not supported
48 return NULL;
49 }
50}
51
52static mp_obj_t list_getiter(mp_obj_t o_in) {
53 return mp_obj_new_list_iterator(o_in, 0);
54}
55
56mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) {
57 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
58 mp_obj_list_t *self = self_in;
59 if (self->len >= self->alloc) {
60 self->alloc *= 2;
61 self->items = m_renew(mp_obj_t, self->items, self->alloc);
62 }
63 self->items[self->len++] = arg;
64 return mp_const_none; // return None, as per CPython
65}
66
67static mp_obj_t list_pop(mp_obj_t self_in, mp_obj_t arg) {
68 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
69 mp_obj_list_t *self = self_in;
70 uint index = mp_get_index(self->base.type, self->len, arg);
71 mp_obj_t ret = self->items[index];
72 self->len -= 1;
73 memcpy(self->items + index, self->items + index + 1, (self->len - index) * sizeof(mp_obj_t));
74 return ret;
75}
76
77// TODO make this conform to CPython's definition of sort
78static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn) {
79 while (head < tail) {
80 mp_obj_t *h = head - 1;
81 mp_obj_t *t = tail;
82 mp_obj_t v = rt_call_function_1(key_fn, tail[0]); // get pivot using key_fn
83 for (;;) {
84 do ++h; while (rt_compare_op(RT_COMPARE_OP_LESS, rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
85 do --t; while (h < t && rt_compare_op(RT_COMPARE_OP_LESS, v, rt_call_function_1(key_fn, t[0])) == mp_const_true);
86 if (h >= t) break;
87 mp_obj_t x = h[0];
88 h[0] = t[0];
89 t[0] = x;
90 }
91 mp_obj_t x = h[0];
92 h[0] = tail[0];
93 tail[0] = x;
94 mp_quicksort(head, t, key_fn);
95 head = h + 1;
96 }
97}
98
99static mp_obj_t list_sort(mp_obj_t self_in, mp_obj_t key_fn) {
100 assert(MP_OBJ_IS_TYPE(self_in, &list_type));
101 mp_obj_list_t *self = self_in;
102 if (self->len > 1) {
103 mp_quicksort(self->items, self->items + self->len - 1, key_fn);
104 }
105 return mp_const_none; // return None, as per CPython
106}
107
108static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
109static MP_DEFINE_CONST_FUN_OBJ_2(list_pop_obj, list_pop);
110static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
111
112const mp_obj_type_t list_type = {
113 { &mp_const_type },
114 "list",
115 list_print, // print
116 NULL, // call_n
117 NULL, // unary_op
118 list_binary_op, // binary_op
119 list_getiter, // getiter
120 NULL, // iternext
121 { // method list
122 { "append", &list_append_obj },
123 { "pop", &list_pop_obj },
124 { "sort", &list_sort_obj },
125 { NULL, NULL }, // end-of-list sentinel
126 },
127};
128
129static mp_obj_list_t *list_new(uint n) {
130 mp_obj_list_t *o = m_new_obj(mp_obj_list_t);
131 o->base.type = &list_type;
132 o->alloc = n < 4 ? 4 : n;
133 o->len = n;
134 o->items = m_new(mp_obj_t, o->alloc);
135 return o;
136}
137
138mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) {
139 mp_obj_list_t *o = list_new(n);
140 for (int i = 0; i < n; i++) {
141 o->items[i] = items[i];
142 }
143 return o;
144}
145
146mp_obj_t mp_obj_new_list_reverse(uint n, mp_obj_t *items) {
147 mp_obj_list_t *o = list_new(n);
148 for (int i = 0; i < n; i++) {
149 o->items[i] = items[n - i - 1];
150 }
151 return o;
152}
153
154void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) {
155 mp_obj_list_t *self = self_in;
156 *len = self->len;
157 *items = self->items;
158}
159
160void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
161 mp_obj_list_t *self = self_in;
162 uint i = mp_get_index(self->base.type, self->len, index);
163 self->items[i] = value;
164}
165
166/******************************************************************************/
167/* list iterator */
168
169typedef struct _mp_obj_list_it_t {
170 mp_obj_base_t base;
171 mp_obj_list_t *list;
172 machine_uint_t cur;
173} mp_obj_list_it_t;
174
175mp_obj_t list_it_iternext(mp_obj_t self_in) {
176 mp_obj_list_it_t *self = self_in;
177 if (self->cur < self->list->len) {
178 mp_obj_t o_out = self->list->items[self->cur];
179 self->cur += 1;
180 return o_out;
181 } else {
182 return mp_const_stop_iteration;
183 }
184}
185
186static const mp_obj_type_t list_it_type = {
187 { &mp_const_type },
188 "list_iterator",
189 NULL, // print
190 NULL, // call_n
191 NULL, // unary_op
192 NULL, // binary_op
193 NULL, // getiter
194 list_it_iternext, // iternext
195 { { NULL, NULL }, }, // method list
196};
197
198mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) {
199 mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t);
200 o->base.type = &list_it_type;
201 o->list = list;
202 o->cur = cur;
203 return o;
204}