STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { mp_obj_list_t *o = lhs; switch (op) { case MP_BINARY_OP_SUBSCR: { #if MICROPY_ENABLE_SLICE if (MP_OBJ_IS_TYPE(rhs, &mp_type_slice)) { machine_uint_t start, stop; if (!m_seq_get_fast_slice_indexes(o->len, rhs, &start, &stop)) { assert(0); } mp_obj_list_t *res = list_new(stop - start); m_seq_copy(res->items, o->items + start, res->len, mp_obj_t); return res; } #endif uint index = mp_get_index(o->base.type, o->len, rhs, false); return o->items[index]; } case MP_BINARY_OP_ADD: { if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) { return NULL; } mp_obj_list_t *p = rhs; mp_obj_list_t *s = list_new(o->len + p->len); m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t); return s; } case MP_BINARY_OP_INPLACE_ADD: { if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) { return NULL; } list_extend(lhs, rhs); return o; } case MP_BINARY_OP_MULTIPLY: { machine_int_t n; if (!mp_obj_get_int_maybe(rhs, &n)) { return NULL; } mp_obj_list_t *s = list_new(o->len * n); mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items); return s; } case MP_BINARY_OP_EQUAL: case MP_BINARY_OP_LESS: case MP_BINARY_OP_LESS_EQUAL: case MP_BINARY_OP_MORE: case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(list_cmp_helper(op, lhs, rhs)); case MP_BINARY_OP_NOT_EQUAL: return MP_BOOL(!list_cmp_helper(MP_BINARY_OP_EQUAL, lhs, rhs)); default: // op not supported return NULL; } }
static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { mp_obj_list_t *o = lhs; switch (op) { case RT_BINARY_OP_SUBSCR: { // list load uint index = mp_get_index(o->base.type, o->len, rhs); return o->items[index]; } case RT_BINARY_OP_ADD: { if (!MP_OBJ_IS_TYPE(rhs, &list_type)) { return NULL; } mp_obj_list_t *p = rhs; mp_obj_list_t *s = list_new(o->len + p->len); memcpy(s->items, o->items, sizeof(mp_obj_t) * o->len); memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len); return s; } case RT_BINARY_OP_INPLACE_ADD: { if (!MP_OBJ_IS_TYPE(rhs, &list_type)) { return NULL; } list_extend(lhs, rhs); return o; } case RT_BINARY_OP_MULTIPLY: { if (!MP_OBJ_IS_SMALL_INT(rhs)) { return NULL; } int n = MP_OBJ_SMALL_INT_VALUE(rhs); mp_obj_list_t *s = list_new(o->len * n); mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items); return s; } case RT_COMPARE_OP_EQUAL: case RT_COMPARE_OP_LESS: case RT_COMPARE_OP_LESS_EQUAL: case RT_COMPARE_OP_MORE: case RT_COMPARE_OP_MORE_EQUAL: return MP_BOOL(list_cmp_helper(op, lhs, rhs)); case RT_COMPARE_OP_NOT_EQUAL: return MP_BOOL(!list_cmp_helper(RT_COMPARE_OP_EQUAL, lhs, rhs)); default: // op not supported return NULL; } }
STATIC mp_obj_t list_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) { mp_obj_list_t *o = MP_OBJ_TO_PTR(lhs); switch (op) { case MP_BINARY_OP_ADD: { if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) { return MP_OBJ_NULL; // op not supported } mp_obj_list_t *p = MP_OBJ_TO_PTR(rhs); mp_obj_list_t *s = list_new(o->len + p->len); mp_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t); return MP_OBJ_FROM_PTR(s); } case MP_BINARY_OP_INPLACE_ADD: { list_extend(lhs, rhs); return lhs; } case MP_BINARY_OP_MULTIPLY: { mp_int_t n; if (!mp_obj_get_int_maybe(rhs, &n)) { return MP_OBJ_NULL; // op not supported } if (n < 0) { n = 0; } mp_obj_list_t *s = list_new(o->len * n); mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items); return MP_OBJ_FROM_PTR(s); } case MP_BINARY_OP_EQUAL: case MP_BINARY_OP_LESS: case MP_BINARY_OP_LESS_EQUAL: case MP_BINARY_OP_MORE: case MP_BINARY_OP_MORE_EQUAL: { if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) { if (op == MP_BINARY_OP_EQUAL) { return mp_const_false; } return MP_OBJ_NULL; // op not supported } mp_obj_list_t *another = MP_OBJ_TO_PTR(rhs); bool res = mp_seq_cmp_objs(op, o->items, o->len, another->items, another->len); return mp_obj_new_bool(res); } default: return MP_OBJ_NULL; // op not supported } }
int main() { char* first = malloc(sizeof(char)); *first = 'b'; char* second = malloc(sizeof(char)); *second = 'o'; char* third = malloc(sizeof(char)); *third = 'n'; char* fourth = malloc(sizeof(char)); *fourth = 'v'; char* fifth = malloc(sizeof(char)); *fifth = 'h'; char* sixth = malloc(sizeof(char)); *sixth = 'a'; char* seventh = malloc(sizeof(char)); *seventh = 'm'; char* eighth = malloc(sizeof(char)); *eighth = 'X'; list l1 = NULL; list_init(&l1, first); list_append(&l1, second); list_append(&l1, third); list_append(&l1, fourth); list l2 = list_new(); list_append(&l2, fifth); list_append(&l2, sixth); list_append(&l2, seventh); list_append(&l2, eighth); list_extend(&l1, &l2); list_erase(&l1, list_find(l1, 'X')); list_remove(&l1, 'v'); list_print(l1); return list_delete(l1); }
/* * Concatenate lists "l1" and "l2" into a new list, stored in "tv". * Return FAIL when out of memory. */ int list_concat(list_T *l1, list_T *l2, typval_T *tv) { list_T *l; if (l1 == NULL || l2 == NULL) return FAIL; /* make a copy of the first list. */ l = list_copy(l1, FALSE, 0); if (l == NULL) return FAIL; tv->v_type = VAR_LIST; tv->vval.v_list = l; /* append all items from the second list */ return list_extend(l, l2, NULL); }
STATIC mp_obj_t list_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) { mp_obj_list_t *o = lhs; switch (op) { case MP_BINARY_OP_ADD: { if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) { return MP_OBJ_NULL; // op not supported } mp_obj_list_t *p = rhs; mp_obj_list_t *s = list_new(o->len + p->len); mp_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t); return s; } case MP_BINARY_OP_INPLACE_ADD: { if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) { return MP_OBJ_NULL; // op not supported } list_extend(lhs, rhs); return o; } case MP_BINARY_OP_MULTIPLY: { mp_int_t n; if (!mp_obj_get_int_maybe(rhs, &n)) { return MP_OBJ_NULL; // op not supported } if (n < 0) { n = 0; } mp_obj_list_t *s = list_new(o->len * n); mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items); return s; } case MP_BINARY_OP_EQUAL: case MP_BINARY_OP_LESS: case MP_BINARY_OP_LESS_EQUAL: case MP_BINARY_OP_MORE: case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(list_cmp_helper(op, lhs, rhs)); default: return MP_OBJ_NULL; // op not supported } }
list concat_list(const list p1, const list p2) { list ls = list_copy(p1); list_extend(&ls, &p2); return ls; }