blob: 1a9d30f8368fd8e32e2f6eecb4a7538bc1aeb5c7 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien George66eaf842014-03-26 19:27:58 +000027#include <stdlib.h>
John R. Lenton9daa7892014-01-14 23:55:01 +000028#include <assert.h>
29
Damien George51dfcb42015-01-01 20:27:54 +000030#include "py/runtime.h"
John R. Lenton9daa7892014-01-14 23:55:01 +000031
Paul Sokolovskye2d44e32015-04-06 23:50:37 +030032#if MICROPY_PY_BUILTINS_ENUMERATE
33
John R. Lenton9daa7892014-01-14 23:55:01 +000034typedef struct _mp_obj_enumerate_t {
35 mp_obj_base_t base;
36 mp_obj_t iter;
Damien George40f3c022014-07-03 13:25:24 +010037 mp_int_t cur;
John R. Lenton9daa7892014-01-14 23:55:01 +000038} mp_obj_enumerate_t;
39
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020040STATIC mp_obj_t enumerate_iternext(mp_obj_t self_in);
John R. Lenton9daa7892014-01-14 23:55:01 +000041
Damien George5b3f0b72016-01-03 15:55:55 +000042STATIC mp_obj_t enumerate_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Paul Sokolovsky47d3bd32014-05-06 19:25:25 +030043#if MICROPY_CPYTHON_COMPAT
Damien George22d85ec2016-01-13 15:47:56 +000044 static const mp_arg_t allowed_args[] = {
45 { MP_QSTR_iterable, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
46 { MP_QSTR_start, MP_ARG_INT, {.u_int = 0} },
47 };
48
Damien George491cbd62014-05-06 16:38:54 +000049 // parse args
Damien George22d85ec2016-01-13 15:47:56 +000050 struct {
51 mp_arg_val_t iterable, start;
52 } arg_vals;
53 mp_arg_parse_all_kw_array(n_args, n_kw, args,
54 MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&arg_vals);
Damien George491cbd62014-05-06 16:38:54 +000055
56 // create enumerate object
John R. Lenton9daa7892014-01-14 23:55:01 +000057 mp_obj_enumerate_t *o = m_new_obj(mp_obj_enumerate_t);
Damien George5b3f0b72016-01-03 15:55:55 +000058 o->base.type = type;
Damien Georgeae8d8672016-01-09 23:14:54 +000059 o->iter = mp_getiter(arg_vals.iterable.u_obj, NULL);
Damien George22d85ec2016-01-13 15:47:56 +000060 o->cur = arg_vals.start.u_int;
Damien Georgec53b4082014-05-06 16:52:35 +000061#else
Damien George3a2171e2015-09-04 16:53:46 +010062 (void)n_kw;
John R. Lenton9daa7892014-01-14 23:55:01 +000063 mp_obj_enumerate_t *o = m_new_obj(mp_obj_enumerate_t);
Damien George5b3f0b72016-01-03 15:55:55 +000064 o->base.type = type;
Damien Georgeae8d8672016-01-09 23:14:54 +000065 o->iter = mp_getiter(args[0], NULL);
Damien George20006db2014-01-18 14:10:48 +000066 o->cur = n_args > 1 ? mp_obj_get_int(args[1]) : 0;
Damien Georgec53b4082014-05-06 16:52:35 +000067#endif
Damien George491cbd62014-05-06 16:38:54 +000068
Damien George999cedb2015-11-27 17:01:44 +000069 return MP_OBJ_FROM_PTR(o);
John R. Lenton9daa7892014-01-14 23:55:01 +000070}
71
Damien George3e1a5c12014-03-29 13:43:38 +000072const mp_obj_type_t mp_type_enumerate = {
Damien Georgec5966122014-02-15 16:10:44 +000073 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000074 .name = MP_QSTR_enumerate,
John R. Lenton9daa7892014-01-14 23:55:01 +000075 .make_new = enumerate_make_new,
76 .iternext = enumerate_iternext,
Damien Georgeae8d8672016-01-09 23:14:54 +000077 .getiter = mp_identity_getiter,
John R. Lenton9daa7892014-01-14 23:55:01 +000078};
79
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020080STATIC mp_obj_t enumerate_iternext(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +000081 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_enumerate));
Damien George999cedb2015-11-27 17:01:44 +000082 mp_obj_enumerate_t *self = MP_OBJ_TO_PTR(self_in);
Damien Georged17926d2014-03-30 13:35:08 +010083 mp_obj_t next = mp_iternext(self->iter);
Damien Georgeea8d06c2014-04-17 23:19:36 +010084 if (next == MP_OBJ_STOP_ITERATION) {
85 return MP_OBJ_STOP_ITERATION;
John R. Lenton9daa7892014-01-14 23:55:01 +000086 } else {
87 mp_obj_t items[] = {MP_OBJ_NEW_SMALL_INT(self->cur++), next};
88 return mp_obj_new_tuple(2, items);
89 }
90}
Paul Sokolovskye2d44e32015-04-06 23:50:37 +030091
92#endif // MICROPY_PY_BUILTINS_ENUMERATE