objmap.c 2.6 KB
Newer Older
1
/*
2
 * This file is part of the MicroPython project, http://micropython.org/
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

27
#include <stdlib.h>
J
John R. Lenton 已提交
28 29
#include <assert.h>

30
#include "py/runtime.h"
J
John R. Lenton 已提交
31 32 33

typedef struct _mp_obj_map_t {
    mp_obj_base_t base;
34
    size_t n_iters;
J
John R. Lenton 已提交
35 36 37 38
    mp_obj_t fun;
    mp_obj_t iters[];
} mp_obj_map_t;

39
STATIC mp_obj_t map_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
40
    mp_arg_check_num(n_args, n_kw, 2, MP_OBJ_FUN_ARGS_MAX, false);
41
    mp_obj_map_t *o = mp_obj_malloc_var(mp_obj_map_t, mp_obj_t, n_args - 1, type);
J
John R. Lenton 已提交
42
    o->n_iters = n_args - 1;
43
    o->fun = args[0];
44
    for (size_t i = 0; i < n_args - 1; i++) {
45
        o->iters[i] = mp_getiter(args[i + 1], NULL);
J
John R. Lenton 已提交
46
    }
47
    return MP_OBJ_FROM_PTR(o);
J
John R. Lenton 已提交
48 49
}

50
STATIC mp_obj_t map_iternext(mp_obj_t self_in) {
51
    mp_check_self(mp_obj_is_type(self_in, &mp_type_map));
52
    mp_obj_map_t *self = MP_OBJ_TO_PTR(self_in);
J
John R. Lenton 已提交
53 54
    mp_obj_t *nextses = m_new(mp_obj_t, self->n_iters);

55
    for (size_t i = 0; i < self->n_iters; i++) {
D
Damien George 已提交
56
        mp_obj_t next = mp_iternext(self->iters[i]);
57
        if (next == MP_OBJ_STOP_ITERATION) {
J
John R. Lenton 已提交
58
            m_del(mp_obj_t, nextses, self->n_iters);
59
            return MP_OBJ_STOP_ITERATION;
J
John R. Lenton 已提交
60 61 62
        }
        nextses[i] = next;
    }
D
Damien George 已提交
63
    return mp_call_function_n_kw(self->fun, self->n_iters, 0, nextses);
J
John R. Lenton 已提交
64 65
}

66
const mp_obj_type_t mp_type_map = {
67
    { &mp_type_type },
68
    .name = MP_QSTR_map,
J
John R. Lenton 已提交
69
    .make_new = map_make_new,
70
    .getiter = mp_identity_getiter,
J
John R. Lenton 已提交
71 72
    .iternext = map_iternext,
};