blob: 97a14eb870d7b9aa39f4a77aacf0bd2394b16e7d [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
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
John R. Lenton07205ec2014-01-13 02:31:00 +000027#include <stdlib.h>
28#include <assert.h>
29
John R. Lenton07205ec2014-01-13 02:31:00 +000030#include "mpconfig.h"
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030031#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000032#include "qstr.h"
John R. Lenton07205ec2014-01-13 02:31:00 +000033#include "obj.h"
34#include "runtime.h"
35
36typedef struct _mp_obj_zip_t {
37 mp_obj_base_t base;
38 int n_iters;
39 mp_obj_t iters[];
40} mp_obj_zip_t;
41
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020042STATIC mp_obj_t zip_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien Georgeee7a8802014-05-11 18:37:21 +010043 mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, false);
Damien George20006db2014-01-18 14:10:48 +000044
John R. Lenton07205ec2014-01-13 02:31:00 +000045 mp_obj_zip_t *o = m_new_obj_var(mp_obj_zip_t, mp_obj_t, n_args);
Damien George3e1a5c12014-03-29 13:43:38 +000046 o->base.type = &mp_type_zip;
John R. Lenton07205ec2014-01-13 02:31:00 +000047 o->n_iters = n_args;
48 for (int i = 0; i < n_args; i++) {
Damien Georged17926d2014-03-30 13:35:08 +010049 o->iters[i] = mp_getiter(args[i]);
John R. Lenton07205ec2014-01-13 02:31:00 +000050 }
51 return o;
52}
53
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020054STATIC mp_obj_t zip_iternext(mp_obj_t self_in) {
Damien George3e1a5c12014-03-29 13:43:38 +000055 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_zip));
John R. Lenton07205ec2014-01-13 02:31:00 +000056 mp_obj_zip_t *self = self_in;
57 mp_obj_t *items;
58 if (self->n_iters == 0) {
Damien Georgeea8d06c2014-04-17 23:19:36 +010059 return MP_OBJ_STOP_ITERATION;
John R. Lenton07205ec2014-01-13 02:31:00 +000060 }
61 mp_obj_t o = mp_obj_new_tuple(self->n_iters, NULL);
62 mp_obj_tuple_get(o, NULL, &items);
63
64 for (int i = 0; i < self->n_iters; i++) {
Damien Georged17926d2014-03-30 13:35:08 +010065 mp_obj_t next = mp_iternext(self->iters[i]);
Damien Georgeea8d06c2014-04-17 23:19:36 +010066 if (next == MP_OBJ_STOP_ITERATION) {
John R. Lenton07205ec2014-01-13 02:31:00 +000067 mp_obj_tuple_del(o);
Damien Georgeea8d06c2014-04-17 23:19:36 +010068 return MP_OBJ_STOP_ITERATION;
John R. Lenton07205ec2014-01-13 02:31:00 +000069 }
70 items[i] = next;
71 }
72 return o;
73}
Damien George0f592032014-01-14 23:18:35 +000074
Damien George3e1a5c12014-03-29 13:43:38 +000075const mp_obj_type_t mp_type_zip = {
Damien Georgec5966122014-02-15 16:10:44 +000076 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000077 .name = MP_QSTR_zip,
Damien George0f592032014-01-14 23:18:35 +000078 .make_new = zip_make_new,
Paul Sokolovskya30cf9f2014-03-30 21:55:12 +030079 .getiter = mp_identity,
Damien George0f592032014-01-14 23:18:35 +000080 .iternext = zip_iternext,
81};