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" |
| 9 | #include "obj.h" |
| 10 | #include "runtime0.h" |
| 11 | #include "runtime.h" |
| 12 | |
| 13 | typedef 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 | |
| 20 | 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] | 21 | static mp_obj_list_t *list_new(uint n); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 22 | |
| 23 | /******************************************************************************/ |
| 24 | /* list */ |
| 25 | |
| 26 | static 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 | |
| 38 | static 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. Lenton | 4cb8058 | 2014-01-03 02:27:08 +0000 | [diff] [blame] | 47 | case RT_BINARY_OP_ADD: |
| 48 | { |
John R. Lenton | 81ad89c | 2014-01-03 02:32:40 +0000 | [diff] [blame] | 49 | 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. Lenton | 9bc56d9 | 2014-01-03 10:13:38 +0000 | [diff] [blame] | 54 | 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. Lenton | 81ad89c | 2014-01-03 02:32:40 +0000 | [diff] [blame] | 56 | return s; |
John R. Lenton | 4cb8058 | 2014-01-03 02:27:08 +0000 | [diff] [blame] | 57 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 58 | default: |
| 59 | // op not supported |
| 60 | return NULL; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static mp_obj_t list_getiter(mp_obj_t o_in) { |
| 65 | return mp_obj_new_list_iterator(o_in, 0); |
| 66 | } |
| 67 | |
| 68 | mp_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) { |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 72 | 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] | 73 | self->alloc *= 2; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 74 | } |
| 75 | self->items[self->len++] = arg; |
| 76 | return mp_const_none; // return None, as per CPython |
| 77 | } |
| 78 | |
John R. Lenton | 25f417c | 2014-01-03 22:53:18 +0000 | [diff] [blame^] | 79 | static 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]); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 87 | 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. Lenton | 25f417c | 2014-01-03 22:53:18 +0000 | [diff] [blame^] | 90 | 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 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | // TODO make this conform to CPython's definition of sort |
| 98 | static 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 | |
| 119 | static 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 | |
| 128 | static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append); |
John R. Lenton | 25f417c | 2014-01-03 22:53:18 +0000 | [diff] [blame^] | 129 | 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] | 130 | static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort); |
| 131 | |
| 132 | const 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 | |
| 149 | static 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 | |
| 158 | mp_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 | |
| 166 | mp_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 | |
| 174 | void 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 | |
| 180 | void 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 | |
| 189 | typedef 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 | |
| 195 | mp_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 | |
| 206 | static 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 | |
| 218 | mp_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 | } |