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); |
| 54 | for (int i = 0; i < o->len; i++) { |
| 55 | s->items[i] = o->items[i]; |
| 56 | } |
| 57 | for (int i = 0; i < p->len; i++) { |
| 58 | s->items[i+o->len] = p->items[i]; |
| 59 | } |
| 60 | return s; |
John R. Lenton | 4cb8058 | 2014-01-03 02:27:08 +0000 | [diff] [blame] | 61 | } |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 62 | default: |
| 63 | // op not supported |
| 64 | return NULL; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static mp_obj_t list_getiter(mp_obj_t o_in) { |
| 69 | return mp_obj_new_list_iterator(o_in, 0); |
| 70 | } |
| 71 | |
| 72 | mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) { |
| 73 | assert(MP_OBJ_IS_TYPE(self_in, &list_type)); |
| 74 | mp_obj_list_t *self = self_in; |
| 75 | if (self->len >= self->alloc) { |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 76 | 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] | 77 | self->alloc *= 2; |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 78 | } |
| 79 | self->items[self->len++] = arg; |
| 80 | return mp_const_none; // return None, as per CPython |
| 81 | } |
| 82 | |
| 83 | static mp_obj_t list_pop(mp_obj_t self_in, mp_obj_t arg) { |
| 84 | assert(MP_OBJ_IS_TYPE(self_in, &list_type)); |
| 85 | mp_obj_list_t *self = self_in; |
| 86 | uint index = mp_get_index(self->base.type, self->len, arg); |
| 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)); |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | // TODO make this conform to CPython's definition of sort |
| 94 | static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn) { |
| 95 | while (head < tail) { |
| 96 | mp_obj_t *h = head - 1; |
| 97 | mp_obj_t *t = tail; |
| 98 | mp_obj_t v = rt_call_function_1(key_fn, tail[0]); // get pivot using key_fn |
| 99 | for (;;) { |
| 100 | do ++h; while (rt_compare_op(RT_COMPARE_OP_LESS, rt_call_function_1(key_fn, h[0]), v) == mp_const_true); |
| 101 | do --t; while (h < t && rt_compare_op(RT_COMPARE_OP_LESS, v, rt_call_function_1(key_fn, t[0])) == mp_const_true); |
| 102 | if (h >= t) break; |
| 103 | mp_obj_t x = h[0]; |
| 104 | h[0] = t[0]; |
| 105 | t[0] = x; |
| 106 | } |
| 107 | mp_obj_t x = h[0]; |
| 108 | h[0] = tail[0]; |
| 109 | tail[0] = x; |
| 110 | mp_quicksort(head, t, key_fn); |
| 111 | head = h + 1; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static mp_obj_t list_sort(mp_obj_t self_in, mp_obj_t key_fn) { |
| 116 | assert(MP_OBJ_IS_TYPE(self_in, &list_type)); |
| 117 | mp_obj_list_t *self = self_in; |
| 118 | if (self->len > 1) { |
| 119 | mp_quicksort(self->items, self->items + self->len - 1, key_fn); |
| 120 | } |
| 121 | return mp_const_none; // return None, as per CPython |
| 122 | } |
| 123 | |
| 124 | static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append); |
| 125 | static MP_DEFINE_CONST_FUN_OBJ_2(list_pop_obj, list_pop); |
| 126 | static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort); |
| 127 | |
| 128 | const mp_obj_type_t list_type = { |
| 129 | { &mp_const_type }, |
| 130 | "list", |
| 131 | list_print, // print |
| 132 | NULL, // call_n |
| 133 | NULL, // unary_op |
| 134 | list_binary_op, // binary_op |
| 135 | list_getiter, // getiter |
| 136 | NULL, // iternext |
| 137 | { // method list |
| 138 | { "append", &list_append_obj }, |
| 139 | { "pop", &list_pop_obj }, |
| 140 | { "sort", &list_sort_obj }, |
| 141 | { NULL, NULL }, // end-of-list sentinel |
| 142 | }, |
| 143 | }; |
| 144 | |
| 145 | static mp_obj_list_t *list_new(uint n) { |
| 146 | mp_obj_list_t *o = m_new_obj(mp_obj_list_t); |
| 147 | o->base.type = &list_type; |
| 148 | o->alloc = n < 4 ? 4 : n; |
| 149 | o->len = n; |
| 150 | o->items = m_new(mp_obj_t, o->alloc); |
| 151 | return o; |
| 152 | } |
| 153 | |
| 154 | mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items) { |
| 155 | mp_obj_list_t *o = list_new(n); |
| 156 | for (int i = 0; i < n; i++) { |
| 157 | o->items[i] = items[i]; |
| 158 | } |
| 159 | return o; |
| 160 | } |
| 161 | |
| 162 | mp_obj_t mp_obj_new_list_reverse(uint n, mp_obj_t *items) { |
| 163 | mp_obj_list_t *o = list_new(n); |
| 164 | for (int i = 0; i < n; i++) { |
| 165 | o->items[i] = items[n - i - 1]; |
| 166 | } |
| 167 | return o; |
| 168 | } |
| 169 | |
| 170 | void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items) { |
| 171 | mp_obj_list_t *self = self_in; |
| 172 | *len = self->len; |
| 173 | *items = self->items; |
| 174 | } |
| 175 | |
| 176 | void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { |
| 177 | mp_obj_list_t *self = self_in; |
| 178 | uint i = mp_get_index(self->base.type, self->len, index); |
| 179 | self->items[i] = value; |
| 180 | } |
| 181 | |
| 182 | /******************************************************************************/ |
| 183 | /* list iterator */ |
| 184 | |
| 185 | typedef struct _mp_obj_list_it_t { |
| 186 | mp_obj_base_t base; |
| 187 | mp_obj_list_t *list; |
| 188 | machine_uint_t cur; |
| 189 | } mp_obj_list_it_t; |
| 190 | |
| 191 | mp_obj_t list_it_iternext(mp_obj_t self_in) { |
| 192 | mp_obj_list_it_t *self = self_in; |
| 193 | if (self->cur < self->list->len) { |
| 194 | mp_obj_t o_out = self->list->items[self->cur]; |
| 195 | self->cur += 1; |
| 196 | return o_out; |
| 197 | } else { |
| 198 | return mp_const_stop_iteration; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | static const mp_obj_type_t list_it_type = { |
| 203 | { &mp_const_type }, |
| 204 | "list_iterator", |
| 205 | NULL, // print |
| 206 | NULL, // call_n |
| 207 | NULL, // unary_op |
| 208 | NULL, // binary_op |
| 209 | NULL, // getiter |
| 210 | list_it_iternext, // iternext |
| 211 | { { NULL, NULL }, }, // method list |
| 212 | }; |
| 213 | |
| 214 | mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) { |
| 215 | mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t); |
| 216 | o->base.type = &list_it_type; |
| 217 | o->list = list; |
| 218 | o->cur = cur; |
| 219 | return o; |
| 220 | } |