From e827e98a6f51dc45c81b0de56aa3f09ffcb7b869 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 8 Feb 2014 22:55:00 +0200 Subject: Implement tuple comparison. --- py/objtuple.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'py/objtuple.c') diff --git a/py/objtuple.c b/py/objtuple.c index 3e5041c9d..8c1320a5f 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -74,6 +74,18 @@ static mp_obj_t tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m } } +// Don't pass RT_BINARY_OP_NOT_EQUAL here +static bool tuple_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) { + assert(MP_OBJ_IS_TYPE(self_in, &tuple_type)); + if (!MP_OBJ_IS_TYPE(another_in, &tuple_type)) { + return false; + } + mp_obj_tuple_t *self = self_in; + mp_obj_tuple_t *another = another_in; + + return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len); +} + static mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) { mp_obj_tuple_t *self = self_in; switch (op) { @@ -102,6 +114,15 @@ static mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { uint index = mp_get_index(o->base.type, o->len, rhs); return o->items[index]; } + case RT_BINARY_OP_EQUAL: + case RT_BINARY_OP_LESS: + case RT_BINARY_OP_LESS_EQUAL: + case RT_BINARY_OP_MORE: + case RT_BINARY_OP_MORE_EQUAL: + return MP_BOOL(tuple_cmp_helper(op, lhs, rhs)); + case RT_BINARY_OP_NOT_EQUAL: + return MP_BOOL(!tuple_cmp_helper(RT_BINARY_OP_EQUAL, lhs, rhs)); + default: // op not supported return NULL; -- cgit v1.2.3 From ee4aaf7cddd044afb3e6f2d4ead0635ee6607630 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 8 Feb 2014 23:17:51 +0200 Subject: Implement tuple addition. --- py/objtuple.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'py/objtuple.c') diff --git a/py/objtuple.c b/py/objtuple.c index 8c1320a5f..b7710e1d5 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -114,6 +114,16 @@ static mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { 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, &tuple_type)) { + return NULL; + } + mp_obj_tuple_t *p = rhs; + mp_obj_tuple_t *s = mp_obj_new_tuple(o->len + p->len, NULL); + m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t); + return s; + } case RT_BINARY_OP_EQUAL: case RT_BINARY_OP_LESS: case RT_BINARY_OP_LESS_EQUAL: -- cgit v1.2.3 From f7c2410e65e3d0d1278c22cbf457fa0ac5c205a2 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 8 Feb 2014 23:19:48 +0200 Subject: Implement tuple multiplication. --- py/objtuple.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'py/objtuple.c') diff --git a/py/objtuple.c b/py/objtuple.c index b7710e1d5..18eb6df4d 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -124,6 +124,16 @@ static mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t); return s; } + case RT_BINARY_OP_MULTIPLY: + { + if (!MP_OBJ_IS_SMALL_INT(rhs)) { + return NULL; + } + int n = MP_OBJ_SMALL_INT_VALUE(rhs); + mp_obj_tuple_t *s = mp_obj_new_tuple(o->len * n, NULL); + mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items); + return s; + } case RT_BINARY_OP_EQUAL: case RT_BINARY_OP_LESS: case RT_BINARY_OP_LESS_EQUAL: -- cgit v1.2.3 From 624eff6a8a948c5ffa7c7d17fab69b3739f2e711 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 10 Feb 2014 06:42:20 +0200 Subject: Implement tuple.index(). --- py/objtuple.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'py/objtuple.c') diff --git a/py/objtuple.c b/py/objtuple.c index 18eb6df4d..581bfb629 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -153,6 +153,18 @@ static mp_obj_t tuple_getiter(mp_obj_t o_in) { return mp_obj_new_tuple_iterator(o_in, 0); } +static mp_obj_t tuple_index(uint n_args, const mp_obj_t *args) { + assert(MP_OBJ_IS_TYPE(args[0], &tuple_type)); + mp_obj_tuple_t *self = args[0]; + return mp_seq_index_obj(self->items, self->len, n_args, args); +} +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tuple_index_obj, 2, 4, tuple_index); + +static const mp_method_t tuple_type_methods[] = { + { "index", &tuple_index_obj }, + { NULL, NULL }, // end-of-list sentinel +}; + const mp_obj_type_t tuple_type = { { &mp_const_type }, "tuple", @@ -161,6 +173,7 @@ const mp_obj_type_t tuple_type = { .unary_op = tuple_unary_op, .binary_op = tuple_binary_op, .getiter = tuple_getiter, + .methods = tuple_type_methods, }; // the zero-length tuple -- cgit v1.2.3 From ac0134d427677438ec07f76f9b28b3f514cebde7 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 10 Feb 2014 07:10:55 +0200 Subject: Factor out mp_seq_count_obj() and implement tuple.count(). --- py/objtuple.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'py/objtuple.c') diff --git a/py/objtuple.c b/py/objtuple.c index 581bfb629..7221db774 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -153,6 +153,13 @@ static mp_obj_t tuple_getiter(mp_obj_t o_in) { return mp_obj_new_tuple_iterator(o_in, 0); } +static mp_obj_t tuple_count(mp_obj_t self_in, mp_obj_t value) { + assert(MP_OBJ_IS_TYPE(self_in, &tuple_type)); + mp_obj_tuple_t *self = self_in; + return mp_seq_count_obj(self->items, self->len, value); +} +static MP_DEFINE_CONST_FUN_OBJ_2(tuple_count_obj, tuple_count); + static mp_obj_t tuple_index(uint n_args, const mp_obj_t *args) { assert(MP_OBJ_IS_TYPE(args[0], &tuple_type)); mp_obj_tuple_t *self = args[0]; @@ -161,6 +168,7 @@ static mp_obj_t tuple_index(uint n_args, const mp_obj_t *args) { static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tuple_index_obj, 2, 4, tuple_index); static const mp_method_t tuple_type_methods[] = { + { "count", &tuple_count_obj }, { "index", &tuple_index_obj }, { NULL, NULL }, // end-of-list sentinel }; -- cgit v1.2.3