Skip to content

Commit a888fe4

Browse files
authored
Merge pull request #1888 from tannewt/iterable_group
Improvements to Group
2 parents bd69212 + 00c3980 commit a888fe4

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

py/obj.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,36 @@ mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) {
531531
return self;
532532
}
533533

534+
typedef struct {
535+
mp_obj_base_t base;
536+
mp_fun_1_t iternext;
537+
mp_obj_t obj;
538+
mp_int_t cur;
539+
} mp_obj_generic_it_t;
540+
541+
STATIC mp_obj_t generic_it_iternext(mp_obj_t self_in) {
542+
mp_obj_generic_it_t *self = MP_OBJ_TO_PTR(self_in);
543+
mp_obj_type_t *type = mp_obj_get_type(self->obj);
544+
mp_obj_t current_length = type->unary_op(MP_UNARY_OP_LEN, self->obj);
545+
if (self->cur < MP_OBJ_SMALL_INT_VALUE(current_length)) {
546+
mp_obj_t o_out = type->subscr(self->obj, MP_OBJ_NEW_SMALL_INT(self->cur), MP_OBJ_SENTINEL);
547+
self->cur += 1;
548+
return o_out;
549+
} else {
550+
return MP_OBJ_STOP_ITERATION;
551+
}
552+
}
553+
554+
mp_obj_t mp_obj_new_generic_iterator(mp_obj_t obj, mp_obj_iter_buf_t *iter_buf) {
555+
assert(sizeof(mp_obj_generic_it_t) <= sizeof(mp_obj_iter_buf_t));
556+
mp_obj_generic_it_t *o = (mp_obj_generic_it_t*)iter_buf;
557+
o->base.type = &mp_type_polymorph_iter;
558+
o->iternext = generic_it_iternext;
559+
o->obj = obj;
560+
o->cur = 0;
561+
return MP_OBJ_FROM_PTR(o);
562+
}
563+
534564
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
535565
mp_obj_type_t *type = mp_obj_get_type(obj);
536566
if (type->buffer_p.get_buffer == NULL) {

py/obj.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,10 @@ mp_obj_t mp_identity(mp_obj_t self);
819819
MP_DECLARE_CONST_FUN_OBJ_1(mp_identity_obj);
820820
mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf);
821821

822+
// Generic iterator that uses unary op and subscr to iterate over a native type. It will be slower
823+
// than a custom iterator but applies broadly.
824+
mp_obj_t mp_obj_new_generic_iterator(mp_obj_t self, mp_obj_iter_buf_t *iter_buf);
825+
822826
// module
823827
typedef struct _mp_obj_module_t {
824828
mp_obj_base_t base;

shared-bindings/displayio/Group.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,21 @@ STATIC mp_obj_t displayio_group_obj_insert(mp_obj_t self_in, mp_obj_t index_obj,
195195
}
196196
MP_DEFINE_CONST_FUN_OBJ_3(displayio_group_insert_obj, displayio_group_obj_insert);
197197

198+
199+
//| .. method:: index(layer)
200+
//|
201+
//| Returns the index of the first copy of layer. Raises ValueError if not found.
202+
//|
203+
STATIC mp_obj_t displayio_group_obj_index(mp_obj_t self_in, mp_obj_t layer) {
204+
displayio_group_t *self = native_group(self_in);
205+
mp_int_t index = common_hal_displayio_group_index(self, layer);
206+
if (index < 0) {
207+
mp_raise_ValueError(translate("object not in sequence"));
208+
}
209+
return MP_OBJ_NEW_SMALL_INT(index);
210+
}
211+
MP_DEFINE_CONST_FUN_OBJ_2(displayio_group_index_obj, displayio_group_obj_index);
212+
198213
//| .. method:: pop(i=-1)
199214
//|
200215
//| Remove the ith item and return it.
@@ -217,6 +232,20 @@ STATIC mp_obj_t displayio_group_obj_pop(size_t n_args, const mp_obj_t *pos_args,
217232
}
218233
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_group_pop_obj, 1, displayio_group_obj_pop);
219234

235+
236+
//| .. method:: remove(layer)
237+
//|
238+
//| Remove the first copy of layer. Raises ValueError if it is not present.
239+
//|
240+
STATIC mp_obj_t displayio_group_obj_remove(mp_obj_t self_in, mp_obj_t layer) {
241+
mp_obj_t index = displayio_group_obj_index(self_in, layer);
242+
displayio_group_t *self = native_group(self_in);
243+
244+
common_hal_displayio_group_pop(self, MP_OBJ_SMALL_INT_VALUE(index));
245+
return mp_const_none;
246+
}
247+
MP_DEFINE_CONST_FUN_OBJ_2(displayio_group_remove_obj, displayio_group_obj_remove);
248+
220249
//| .. method:: __len__()
221250
//|
222251
//| Returns the number of layers in a Group
@@ -281,7 +310,9 @@ STATIC const mp_rom_map_elem_t displayio_group_locals_dict_table[] = {
281310
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&displayio_group_y_obj) },
282311
{ MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&displayio_group_append_obj) },
283312
{ MP_ROM_QSTR(MP_QSTR_insert), MP_ROM_PTR(&displayio_group_insert_obj) },
313+
{ MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&displayio_group_index_obj) },
284314
{ MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&displayio_group_pop_obj) },
315+
{ MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&displayio_group_remove_obj) },
285316
};
286317
STATIC MP_DEFINE_CONST_DICT(displayio_group_locals_dict, displayio_group_locals_dict_table);
287318

@@ -291,5 +322,6 @@ const mp_obj_type_t displayio_group_type = {
291322
.make_new = displayio_group_make_new,
292323
.subscr = group_subscr,
293324
.unary_op = group_unary_op,
325+
.getiter = mp_obj_new_generic_iterator,
294326
.locals_dict = (mp_obj_dict_t*)&displayio_group_locals_dict,
295327
};

shared-bindings/displayio/Group.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void common_hal_displayio_group_append(displayio_group_t* self, mp_obj_t layer);
4444
void common_hal_displayio_group_insert(displayio_group_t* self, size_t index, mp_obj_t layer);
4545
size_t common_hal_displayio_group_get_len(displayio_group_t* self);
4646
mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self, size_t index);
47+
mp_int_t common_hal_displayio_group_index(displayio_group_t* self, mp_obj_t layer);
4748
mp_obj_t common_hal_displayio_group_get(displayio_group_t* self, size_t index);
4849
void common_hal_displayio_group_set(displayio_group_t* self, size_t index, mp_obj_t layer);
4950

shared-module/displayio/Group.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self, size_t index) {
9595
return item;
9696
}
9797

98+
mp_int_t common_hal_displayio_group_index(displayio_group_t* self, mp_obj_t layer) {
99+
for (size_t i = 0; i < self->size; i++) {
100+
if (self->children[i].original == layer) {
101+
return i;
102+
}
103+
}
104+
return -1;
105+
}
106+
98107
size_t common_hal_displayio_group_get_len(displayio_group_t* self) {
99108
return self->size;
100109
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy