Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1 | #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 George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame^] | 9 | #include "mpqstr.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 10 | #include "obj.h" |
| 11 | #include "runtime0.h" |
| 12 | #include "runtime.h" |
| 13 | |
| 14 | typedef 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 | |
| 21 | static mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur); |
John R. Lenton | 4cb8058 | 2014-01-03 02:27:08 +0000 | [diff] [blame] | 22 | static mp_obj_list_t *list_new(uint n); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 23 | |
| 24 | /******************************************************************************/ |
| 25 | /* list */ |
| 26 | |
| 27 | static 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 | |
| 39 | static 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. Lenton | 4cb8058 | 2014-01-03 02:27:08 +0000 | [diff] [blame] | 48 | case RT_BINARY_OP_ADD: |
| 49 | { |
John R. Lenton | 81ad89c | 2014-01-03 02:32:40 +0000 | [diff] [blame] | 50 | 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. Lenton | 9bc56d9 | 2014-01-03 10:13:38 +0000 | [diff] [blame] | 55 | 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. Lenton | 81ad89c | 2014-01-03 02:32:40 +0000 | [diff] [blame] | 57 | return s; |
John R. Lenton | 4cb8058 | 2014-01-03 02:27:08 +0000 | [diff] [blame] | 58 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 59 | default: |
| 60 | // op not supported |
| 61 | return NULL; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static mp_obj_t list_getiter(mp_obj_t o_in) { |
| 66 | return mp_obj_new_list_iterator(o_in, 0); |
| 67 | } |
| 68 | |
| 69 | mp_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) { |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 73 | self->items = m_renew(mp_obj_t, self->items, self->alloc, self->alloc * 2); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 74 | self->alloc *= 2; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 75 | } |
| 76 | self->items[self->len++] = arg; |
| 77 | return mp_const_none; // return None, as per CPython |
| 78 | } |
| 79 | |
John R. Lenton | 25f417c | 2014-01-03 22:53:18 +0000 | [diff] [blame] | 80 | static 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 George | eb7bfcb | 2014-01-04 15:57:35 +0000 | [diff] [blame^] | 85 | nlr_jump(mp_obj_new_exception_msg(MP_QSTR_IndexError, "pop from empty list")); |
John R. Lenton | 25f417c | 2014-01-03 22:53:18 +0000 | [diff] [blame] | 86 | } |
| 87 | uint index = mp_get_index(self->base.type, self->len, n_args == 1 ? mp_obj_new_int(-1) : args[1]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 88 | 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. Lenton | 25f417c | 2014-01-03 22:53:18 +0000 | [diff] [blame] | 91 | 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 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | // TODO make this conform to CPython's definition of sort |
| 99 | static 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 | |
| 120 | static 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. Lenton | 069ded9 | 2014-01-03 23:22:53 +0000 | [diff] [blame] | 129 | static 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. Lenton | 26c2116 | 2014-01-03 23:42:17 +0000 | [diff] [blame] | 138 | static 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 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 144 | static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append); |
John R. Lenton | 069ded9 | 2014-01-03 23:22:53 +0000 | [diff] [blame] | 145 | static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear); |
John R. Lenton | 26c2116 | 2014-01-03 23:42:17 +0000 | [diff] [blame] | 146 | static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy); |
John R. Lenton | 25f417c | 2014-01-03 22:53:18 +0000 | [diff] [blame] | 147 | static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 148 | static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort); |
| 149 | |
| 150 | const 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. Lenton | 069ded9 | 2014-01-03 23:22:53 +0000 | [diff] [blame] | 161 | { "clear", &list_clear_obj }, |
John R. Lenton | 26c2116 | 2014-01-03 23:42:17 +0000 | [diff] [blame] | 162 | { "copy", &list_copy_obj }, |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 163 | { "pop", &list_pop_obj }, |
| 164 | { "sort", &list_sort_obj }, |
| 165 | { NULL, NULL }, // end-of-list sentinel |
| 166 | }, |
| 167 | }; |
| 168 | |
| 169 | static 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 | |
| 178 | mp_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 | |
| 186 | mp_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 | |
| 194 | void 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 | |
| 200 | void 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 | |
| 209 | typedef 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 | |
| 215 | mp_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 | |
| 226 | static 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 | |
| 238 | mp_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 | } |