[libgee] Add Iterator.map method
- From: Maciej Marcin Piechotka <mpiechotka src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgee] Add Iterator.map method
- Date: Mon, 15 Aug 2011 18:02:13 +0000 (UTC)
commit 8047802086f6a8033f1c2b7626442a1780ba1828
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date: Sat Apr 30 12:11:34 2011 +0200
Add Iterator.map method
gee/iterator.vala | 30 ++++++++++++++++++++++++++++++
tests/testcollection.vala | 43 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 73 insertions(+), 0 deletions(-)
---
diff --git a/gee/iterator.vala b/gee/iterator.vala
index 91ced75..11d8585 100644
--- a/gee/iterator.vala
+++ b/gee/iterator.vala
@@ -29,6 +29,7 @@ namespace Gee {
public delegate void ForallFunc<G> (owned G g);
public delegate Lazy<A>? UnfoldFunc<A> ();
public delegate Iterator.Stream StreamFunc<A, G> (Iterator.Stream state, owned G? g, out Lazy<A>? lazy);
+ public delegate A MapFunc<A, G> (owned G g);
}
/**
@@ -204,6 +205,35 @@ public interface Gee.Iterator<G> : Object {
}, initial);
}
+ /**
+ * Maps the iterator. It produces iterator that is get by applying
+ * map function to the values of this iterator.
+ *
+ * The value is lazy evaluated but previous value is guaranteed to be
+ * evaluated before { link next} call.
+ *
+ * If the current iterator is { link valid} so is the resulting. Using
+ * the iterator after this called is not allowed.
+ *
+ * @param f Mapping function
+ * @return Iterator listing mapped value
+ */
+ public virtual Iterator<A> map<A> (MapFunc<A, G> f) {
+ return stream<A>((state, item, out val) => {
+ switch (state) {
+ case Stream.YIELD:
+ return Stream.CONTINUE;
+ case Stream.CONTINUE:
+ val = new Lazy<A>(() => {return (f ((owned)item));});
+ return Stream.YIELD;
+ case Stream.END:
+ return Stream.END;
+ default:
+ assert_not_reached ();
+ }
+ });
+ }
+
public enum Stream {
YIELD,
CONTINUE,
diff --git a/tests/testcollection.vala b/tests/testcollection.vala
index eba36aa..3e86984 100644
--- a/tests/testcollection.vala
+++ b/tests/testcollection.vala
@@ -47,6 +47,7 @@ public abstract class CollectionTests : Gee.TestCase {
add_test ("[Collection] GObject properties", test_gobject_properties);
add_test ("[Collection] fold", test_fold);
add_test ("[Collection] foreach", test_foreach);
+ add_test ("[Collection] map", test_map);
}
protected Collection<string> test_collection;
@@ -782,4 +783,46 @@ public abstract class CollectionTests : Gee.TestCase {
iter.foreach ((x) => {count++;});
assert (count == 6);
}
+
+ public void test_map () {
+ assert (test_collection.add ("one"));
+ assert (test_collection.add ("two"));
+ assert (test_collection.add ("three"));
+
+ bool one = false;
+ bool two = false;
+ bool three = false;
+
+ int i = 0;
+ var iter = test_collection.iterator().map<int> ((str) => {
+ if (str == "one") {
+ assert (!one);
+ one = true;
+ } else if (str == "two") {
+ assert (!two);
+ two = true;
+ } else if (str == "three") {
+ assert (!three);
+ three = true;
+ } else {
+ assert_not_reached ();
+ }
+ return i++;
+ });
+ int j = 0;
+ while (iter.next ()) {
+ assert (i == j);
+ assert (j == iter.get ());
+ assert (j == iter.get ());
+ j++;
+ assert (i == j);
+ }
+
+ assert (i == j);
+ assert (i == test_collection.size);
+ assert (one);
+ assert (two);
+ assert (three);
+ }
}
+
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]