[libgee/functional-iterators] Add MappingIterator



commit 162c9761b0159a038589182bbfe3374768ae19ba
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Sat Feb 12 01:20:06 2011 +0000

    Add MappingIterator

 gee/Makefile.am          |    1 +
 gee/iterator.vala        |    5 ++++
 gee/mappingiterator.vala |   59 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 65 insertions(+), 0 deletions(-)
---
diff --git a/gee/Makefile.am b/gee/Makefile.am
index dd69136..336bf1f 100644
--- a/gee/Makefile.am
+++ b/gee/Makefile.am
@@ -30,6 +30,7 @@ libgee_la_SOURCES = \
 	listiterator.vala \
 	map.vala \
 	mapiterator.vala \
+	mappingiterator.vala \
 	multimap.vala \
 	multiset.vala \
 	priorityqueue.vala \
diff --git a/gee/iterator.vala b/gee/iterator.vala
index 48d50b8..ce88f3a 100644
--- a/gee/iterator.vala
+++ b/gee/iterator.vala
@@ -27,6 +27,7 @@
 namespace Gee {
 	public delegate A FoldFunc<A, G> (G g, owned A a);
 	public delegate void ForallFunc<G> (G g);
+	public delegate B MapFunc<G, B> (G g);
 }
 
 /**
@@ -112,5 +113,9 @@ public interface Gee.Iterator<G> : Object {
 		while (next ())
 			f (get ());
 	}
+
+	public virtual Iterator<B> map<B> (owned MapFunc<G, B> map) {
+		return new MappingIterator<G, B> (this, (owned) map);
+	}
 }
 
diff --git a/gee/mappingiterator.vala b/gee/mappingiterator.vala
new file mode 100644
index 0000000..e6f99ea
--- /dev/null
+++ b/gee/mappingiterator.vala
@@ -0,0 +1,59 @@
+/* iterator.vala
+ *
+ * Copyright (C) 2011  Maciej Piechotka
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ * 	Maciej Piechotka <uzytkownik2 gmail com>
+ */
+
+internal class Gee.MappingIterator<A, B> : GLib.Object, Iterator<B> {
+	public MappingIterator (Iterator<B> inner, owned MapFunc<A, B> func) {
+		if (inner.valid) {
+			current = func (inner.get ());
+		}
+		this.inner = inner;
+		this.func = (owned) func;
+	}
+
+	public bool next () {
+		if (!inner.next ())
+			return false;
+		current = func (inner.get ());
+		return true;
+	}
+
+	public bool has_next () {
+		return inner.has_next ();
+	}
+
+	public new B get () {
+		assert (inner.valid);
+		return current;
+	}
+
+	public void remove () {
+		assert_not_reached ();
+	}
+
+	public bool valid {get {return inner.valid;}}
+
+	public bool read_only {get {return true;}}
+
+	private Iterator<A> inner;
+	private MapFunc<A, B> func;
+	private B? current;
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]