Skip to content

Commit ac0134d

Browse files
committed
Factor out mp_seq_count_obj() and implement tuple.count().
1 parent 624eff6 commit ac0134d

5 files changed

Lines changed: 27 additions & 8 deletions

File tree

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,4 @@ bool m_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_ui
403403
bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, uint len2);
404404
bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *items2, uint len2);
405405
mp_obj_t mp_seq_index_obj(const mp_obj_t *items, uint len, uint n_args, const mp_obj_t *args);
406+
mp_obj_t mp_seq_count_obj(const mp_obj_t *items, uint len, mp_obj_t value);

py/objlist.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,7 @@ static mp_obj_t list_copy(mp_obj_t self_in) {
260260
static mp_obj_t list_count(mp_obj_t self_in, mp_obj_t value) {
261261
assert(MP_OBJ_IS_TYPE(self_in, &list_type));
262262
mp_obj_list_t *self = self_in;
263-
int count = 0;
264-
for (int i = 0; i < self->len; i++) {
265-
if (mp_obj_equal(self->items[i], value)) {
266-
count++;
267-
}
268-
}
269-
270-
return mp_obj_new_int(count);
263+
return mp_seq_count_obj(self->items, self->len, value);
271264
}
272265

273266
static mp_obj_t list_index(uint n_args, const mp_obj_t *args) {

py/objtuple.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ static mp_obj_t tuple_getiter(mp_obj_t o_in) {
153153
return mp_obj_new_tuple_iterator(o_in, 0);
154154
}
155155

156+
static mp_obj_t tuple_count(mp_obj_t self_in, mp_obj_t value) {
157+
assert(MP_OBJ_IS_TYPE(self_in, &tuple_type));
158+
mp_obj_tuple_t *self = self_in;
159+
return mp_seq_count_obj(self->items, self->len, value);
160+
}
161+
static MP_DEFINE_CONST_FUN_OBJ_2(tuple_count_obj, tuple_count);
162+
156163
static mp_obj_t tuple_index(uint n_args, const mp_obj_t *args) {
157164
assert(MP_OBJ_IS_TYPE(args[0], &tuple_type));
158165
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) {
161168
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tuple_index_obj, 2, 4, tuple_index);
162169

163170
static const mp_method_t tuple_type_methods[] = {
171+
{ "count", &tuple_count_obj },
164172
{ "index", &tuple_index_obj },
165173
{ NULL, NULL }, // end-of-list sentinel
166174
};

py/sequence.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,15 @@ mp_obj_t mp_seq_index_obj(const mp_obj_t *items, uint len, uint n_args, const mp
164164

165165
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "object not in sequence"));
166166
}
167+
168+
mp_obj_t mp_seq_count_obj(const mp_obj_t *items, uint len, mp_obj_t value) {
169+
uint count = 0;
170+
for (uint i = 0; i < len; i++) {
171+
if (mp_obj_equal(items[i], value)) {
172+
count++;
173+
}
174+
}
175+
176+
// Common sense says this cannot overflow small int
177+
return MP_OBJ_NEW_SMALL_INT(count);
178+
}

tests/basics/tuple_count.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a = (1, 2, 3)
2+
a = a + a + a
3+
b = (0, 0, a, 0, a, 0)
4+
print(a.count(2))
5+
print(b.count(a))

0 commit comments

Comments
 (0)