From cc26a2e20ea2609c5521f1336d31480b17dd013f Mon Sep 17 00:00:00 2001 From: Matt Craighead Date: Sun, 30 Oct 2011 18:32:28 -0700 Subject: [PATCH] zip now returns an iterator --- backend.cpp | 51 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/backend.cpp b/backend.cpp index 68053a9..d46925f 100644 --- a/backend.cpp +++ b/backend.cpp @@ -1425,26 +1425,49 @@ public: }; class zip_class_def_singleton: public builtin_class_def_singleton { -public: - zip_class_def_singleton(): builtin_class_def_singleton("zip") {} +private: + class zip_obj: public node { + private: + node *iter1; + node *iter2; - virtual node *__call__(context *globals, context *ctx, tuple *args, dict *kwargs) { - NO_KWARGS_N_ARGS("zip", 2); - node *list1 = args->__getitem__(0); - node *list2 = args->__getitem__(1); + public: + zip_obj(node *iter1, node *iter2) { + this->iter1 = iter1; + this->iter2 = iter2; + } + const char *node_type() { return "zip_obj"; } - if (!list1->is_list() || !list2->is_list() || list1->len() != list2->len()) - error("bad arguments to zip()"); + virtual void mark_live() { + if (!allocator->mark_live(this, sizeof(*this))) { + this->iter1->mark_live(); + this->iter2->mark_live(); + } + } - list *plist = new(allocator) list(); - for (int_t i = 0; i < list1->len(); i++) { + virtual node *__iter__() { return this; } + virtual node *next() { + node *item1 = this->iter1->next(); + node *item2 = this->iter2->next(); + if (!item1 || !item2) + return NULL; node *pair[2]; - pair[0] = list1->__getitem__(i); - pair[1] = list2->__getitem__(i); - plist->append(new(allocator) tuple(2, pair)); + pair[0] = item1; + pair[1] = item2; + return new(allocator) tuple(2, pair); } - return plist; + virtual std::string repr() { return ""; } + }; + +public: + zip_class_def_singleton(): builtin_class_def_singleton("zip") {} + + virtual node *__call__(context *globals, context *ctx, tuple *args, dict *kwargs) { + NO_KWARGS_N_ARGS("zip", 2); + node *iter1 = args->__getitem__(0)->__iter__(); + node *iter2 = args->__getitem__(1)->__iter__(); + return new(allocator) zip_obj(iter1, iter2); } }; -- 2.11.4.GIT