Skip to content

Commit 9daa789

Browse files
committed
added enumerate()
1 parent 6c2401e commit 9daa789

6 files changed

Lines changed: 65 additions & 0 deletions

File tree

py/mpqstrraw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Q(chr)
4141
Q(complex)
4242
Q(dict)
4343
Q(divmod)
44+
Q(enumerate)
4445
Q(float)
4546
Q(hash)
4647
Q(int)

py/obj.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
294294
void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
295295
mp_obj_t list_sort(mp_obj_t args, struct _mp_map_t *kwargs);
296296

297+
// enumerate
298+
extern const mp_obj_type_t enumerate_type;
299+
297300
// dict
298301
extern const mp_obj_type_t dict_type;
299302
uint mp_obj_dict_len(mp_obj_t self_in);

py/objenumerate.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <stdlib.h>
2+
#include <assert.h>
3+
4+
#include "misc.h"
5+
#include "mpconfig.h"
6+
#include "obj.h"
7+
#include "runtime.h"
8+
9+
typedef struct _mp_obj_enumerate_t {
10+
mp_obj_base_t base;
11+
mp_obj_t iter;
12+
machine_int_t cur;
13+
} mp_obj_enumerate_t;
14+
15+
static mp_obj_t enumerate_getiter(mp_obj_t self_in) {
16+
return self_in;
17+
}
18+
19+
static mp_obj_t enumerate_iternext(mp_obj_t self_in);
20+
21+
/* TODO: enumerate is one of the ones that can take args or kwargs.
22+
Sticking to args for now */
23+
static mp_obj_t enumerate_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
24+
/* NOTE: args are backwards */
25+
assert(n_args > 0);
26+
args += n_args - 1;
27+
mp_obj_enumerate_t *o = m_new_obj(mp_obj_enumerate_t);
28+
o->base.type = &enumerate_type;
29+
o->iter = rt_getiter(args[0]);
30+
o->cur = n_args > 1 ? mp_obj_get_int(args[-1]) : 0;
31+
32+
return o;
33+
}
34+
35+
const mp_obj_type_t enumerate_type = {
36+
{ &mp_const_type },
37+
"enumerate",
38+
.make_new = enumerate_make_new,
39+
.iternext = enumerate_iternext,
40+
.getiter = enumerate_getiter,
41+
};
42+
43+
static mp_obj_t enumerate_iternext(mp_obj_t self_in) {
44+
assert(MP_OBJ_IS_TYPE(self_in, &enumerate_type));
45+
mp_obj_enumerate_t *self = self_in;
46+
mp_obj_t next = rt_iternext(self->iter);
47+
if (next == mp_const_stop_iteration) {
48+
return mp_const_stop_iteration;
49+
} else {
50+
mp_obj_t items[] = {MP_OBJ_NEW_SMALL_INT(self->cur++), next};
51+
return mp_obj_new_tuple(2, items);
52+
}
53+
}

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ PY_O_BASENAME = \
7777
objclosure.o \
7878
objcomplex.o \
7979
objdict.o \
80+
objenumerate.o \
8081
objexcept.o \
8182
objfloat.o \
8283
objfun.o \

py/runtime.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ void rt_init(void) {
105105
mp_map_add_qstr(&map_builtins, MP_QSTR_complex, (mp_obj_t)&complex_type);
106106
#endif
107107
mp_map_add_qstr(&map_builtins, MP_QSTR_dict, (mp_obj_t)&dict_type);
108+
mp_map_add_qstr(&map_builtins, MP_QSTR_enumerate, (mp_obj_t)&enumerate_type);
108109
#if MICROPY_ENABLE_FLOAT
109110
mp_map_add_qstr(&map_builtins, MP_QSTR_float, (mp_obj_t)&float_type);
110111
#endif

tests/basics/tests/enumerate.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
print(list(enumerate([])))
2+
print(list(enumerate([1, 2, 3])))
3+
print(list(enumerate([1, 2, 3], 5)))
4+
print(list(enumerate([1, 2, 3], -5)))
5+
print(list(enumerate(range(10000))))
6+

0 commit comments

Comments
 (0)