[libgee] Add Traversable<G>.filter method



commit 36e766ada6d7094868fbbb3ed404b2f8c62984f7
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Mon Aug 1 03:51:01 2011 +0100

    Add Traversable<G>.filter method

 gee/abstractcollection.vala |    4 ++++
 gee/abstractmap.vala        |    4 ++++
 gee/abstractmultiset.vala   |    4 ++++
 gee/arraylist.vala          |    4 ++++
 gee/hashmap.vala            |   12 ++++++++++++
 gee/hashset.vala            |    4 ++++
 gee/linkedlist.vala         |    4 ++++
 gee/priorityqueue.vala      |    4 ++++
 gee/readonlycollection.vala |    8 ++++++++
 gee/readonlymap.vala        |    4 ++++
 gee/traversable.vala        |   24 ++++++++++++++++++++++++
 gee/treemap.vala            |   24 ++++++++++++++++++++++++
 gee/treeset.vala            |    8 ++++++++
 gee/unfolditerator.vala     |    4 ++++
 14 files changed, 112 insertions(+), 0 deletions(-)
---
diff --git a/gee/abstractcollection.vala b/gee/abstractcollection.vala
index ec8f568..f335c1b 100644
--- a/gee/abstractcollection.vala
+++ b/gee/abstractcollection.vala
@@ -282,6 +282,10 @@ public abstract class Gee.AbstractCollection<G> : Object, Traversable<G>, Iterab
 		return iterator ().stream<A> ((owned) f);
 	}
 
+	public virtual Iterator<G> filter (owned Predicate<G> f) {
+		return Traversable.filter_impl<G> (this, (owned) f);
+	}
+
 	private weak Collection<G> _read_only_view;
 
 	/**
diff --git a/gee/abstractmap.vala b/gee/abstractmap.vala
index 63f5bbd..e8dee90 100644
--- a/gee/abstractmap.vala
+++ b/gee/abstractmap.vala
@@ -221,4 +221,8 @@ public abstract class Gee.AbstractMap<K,V> : Object, Traversable<Map.Entry<K,V>>
 	public virtual Iterator<A> stream<A> (owned StreamFunc<Map.Entry<K,V>, A> f) {
 		return iterator ().stream<A> ((owned) f);
 	}
+
+	public Gee.Iterator<Map.Entry<K,V>> filter (owned Predicate<Map.Entry<K,V>> f) {
+		return Traversable.filter_impl<Map.Entry<K,V>> (this, (owned)f);
+	}
 }
diff --git a/gee/abstractmultiset.vala b/gee/abstractmultiset.vala
index 99d1540..3beec43 100644
--- a/gee/abstractmultiset.vala
+++ b/gee/abstractmultiset.vala
@@ -165,5 +165,9 @@ public abstract class Gee.AbstractMultiSet<G> : AbstractCollection<G>, MultiSet<
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<A, G> f) {
 			return Gee.Iterator.stream_impl<G, A>(this, (owned)f);
 		}
+
+		public Gee.Iterator<G> filter (owned Predicate<G> f) {
+			return Traversable.filter_impl<G> (this, (owned)f);
+		}
 	}
 }
diff --git a/gee/arraylist.vala b/gee/arraylist.vala
index 33cf575..0b38068 100644
--- a/gee/arraylist.vala
+++ b/gee/arraylist.vala
@@ -395,6 +395,10 @@ public class Gee.ArrayList<G> : AbstractList<G> {
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<A, G> f) {
 			return Gee.Iterator.stream_impl<G, A>(this, (owned)f);
 		}
+
+		public Gee.Iterator<G> filter (owned Predicate<G> f) {
+			return Traversable.filter_impl<G> (this, (owned)f);
+		}
 	}
 }
 
diff --git a/gee/hashmap.vala b/gee/hashmap.vala
index fd1f6f5..fa47b17 100644
--- a/gee/hashmap.vala
+++ b/gee/hashmap.vala
@@ -561,6 +561,10 @@ public class Gee.HashMap<K,V> : Gee.AbstractMap<K,V> {
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<A, K> f) {
 			return Gee.Iterator.stream_impl<K, A>(this, (owned)f);
 		}
+
+		public Gee.Iterator<K> filter (owned Predicate<K> f) {
+			return Traversable.filter_impl<K> (this, (owned)f);
+		}
 	}
 
 	private class MapIterator<K,V> : NodeIterator<K,V>, Gee.MapIterator<K,V> {
@@ -646,6 +650,10 @@ public class Gee.HashMap<K,V> : Gee.AbstractMap<K,V> {
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<A, V> f) {
 			return Gee.Iterator.stream_impl<V, A>(this, (owned)f);
 		}
+
+		public Gee.Iterator<V> filter (owned Predicate<V> f) {
+			return Traversable.filter_impl<V> (this, (owned)f);
+		}
 	}
 
 	private class EntryIterator<K,V> : NodeIterator<K,V>, Traversable<Map.Entry<K,V>>, Iterator<Map.Entry<K,V>> {
@@ -685,6 +693,10 @@ public class Gee.HashMap<K,V> : Gee.AbstractMap<K,V> {
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<Map.Entry<K,V>, A> f) {
 			return Gee.Iterator.stream_impl<Map.Entry<K,V>, A>(this, (owned)f);
 		}
+
+		public Gee.Iterator<Map.Entry<K,V>> filter (owned Predicate<Map.Entry<K,V>> f) {
+			return Traversable.filter_impl<Map.Entry<K,V>> (this, (owned)f);
+		}
 	}
 }
 
diff --git a/gee/hashset.vala b/gee/hashset.vala
index debbe17..fa11f0f 100644
--- a/gee/hashset.vala
+++ b/gee/hashset.vala
@@ -298,6 +298,10 @@ public class Gee.HashSet<G> : AbstractSet<G> {
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<A, G> f) {
 			return Gee.Iterator.stream_impl<G, A>(this, (owned)f);
 		}
+
+		public Gee.Iterator<G> filter (owned Predicate<G> f) {
+			return Traversable.filter_impl<G> (this, (owned)f);
+		}
 	}
 }
 
diff --git a/gee/linkedlist.vala b/gee/linkedlist.vala
index 4c6f87f..c8901b0 100644
--- a/gee/linkedlist.vala
+++ b/gee/linkedlist.vala
@@ -616,6 +616,10 @@ public class Gee.LinkedList<G> : AbstractList<G>, Queue<G>, Deque<G> {
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<A, G> f) {
 			return Gee.Iterator.stream_impl<G, A>(this, (owned)f);
 		}
+
+		public Gee.Iterator<G> filter (owned Predicate<G> f) {
+			return Traversable.filter_impl<G> (this, (owned)f);
+		}
 	}
 
 	private unowned Node<G>? _get_node_at (int index) {
diff --git a/gee/priorityqueue.vala b/gee/priorityqueue.vala
index f65df83..1da8dcc 100644
--- a/gee/priorityqueue.vala
+++ b/gee/priorityqueue.vala
@@ -1037,5 +1037,9 @@ public class Gee.PriorityQueue<G> : Gee.AbstractQueue<G> {
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<A, G> f) {
 			return Gee.Iterator.stream_impl<G, A>(this, (owned)f);
 		}
+
+		public Gee.Iterator<G> filter (owned Predicate<G> f) {
+			return Traversable.filter_impl<G> (this, (owned)f);
+		}
 	}
 }
diff --git a/gee/readonlycollection.vala b/gee/readonlycollection.vala
index a51b5f3..5712dea 100644
--- a/gee/readonlycollection.vala
+++ b/gee/readonlycollection.vala
@@ -80,6 +80,10 @@ internal class Gee.ReadOnlyCollection<G> : Object, Iterable<G>, Traversable<G>,
 		return _collection.stream<A> ((owned)f);
 	}
 
+	public Gee.Iterator<G> filter (owned Predicate<G> f) {
+		return _collection.filter ((owned)f);
+	}
+
 	/**
 	 * { inheritDoc}
 	 */
@@ -199,6 +203,10 @@ internal class Gee.ReadOnlyCollection<G> : Object, Iterable<G>, Traversable<G>,
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<A, G> f) {
 			return _iter.stream<A> ((owned)f);
 		}
+
+		public Gee.Iterator<G> filter (owned Predicate<G> f) {
+			return _iter.filter ((owned)f);
+		}
 	}
 
 	public virtual Collection<G> read_only_view {
diff --git a/gee/readonlymap.vala b/gee/readonlymap.vala
index 5be7ff0..8cfe0f6 100644
--- a/gee/readonlymap.vala
+++ b/gee/readonlymap.vala
@@ -236,6 +236,10 @@ internal class Gee.ReadOnlyMap<K,V> : Object, Traversable<Map.Entry<K,V>>, Itera
 		return _map.stream<A> ((owned) f);
 	}
 
+	public Iterator<Map.Entry<K, V>> filter (owned Predicate<Map.Entry<K, V>> f) {
+		return Traversable.filter_impl<Map.Entry<K, V>> (this, (owned)f);
+	}
+
 	protected class MapIterator<K,V> : Object, Gee.MapIterator<K,V> {
 		protected Gee.MapIterator<K,V> _iter;
 
diff --git a/gee/traversable.vala b/gee/traversable.vala
index d90dd9e..deae175 100644
--- a/gee/traversable.vala
+++ b/gee/traversable.vala
@@ -28,6 +28,7 @@ namespace Gee {
 	public delegate Lazy<A>? UnfoldFunc<A> ();
 	public delegate Traversable.Stream StreamFunc<G, A> (Traversable.Stream state, owned Lazy<G>? g, out Lazy<A>? lazy);
 	public delegate A MapFunc<A, G> (owned G g);
+	public delegate bool Predicate<G> (G g);
 }
 
 public interface Gee.Traversable<G> : Object
@@ -164,6 +165,29 @@ public interface Gee.Traversable<G> : Object
 		});
 	}
 
+	public abstract Iterator<G> filter (owned Predicate<G> f);
+
+	public static Iterator<G> filter_impl<G> (Traversable<G> input, owned Predicate<G> pred) {
+		return input.stream<G> ((state, item, out val) => {
+			switch (state) {
+			case Stream.YIELD:
+				return Stream.CONTINUE;
+			case Stream.CONTINUE:
+				G g = item.get ();
+				if (pred (g)) {
+					val = item;
+					return Stream.YIELD;
+				} else {
+					return Stream.CONTINUE;
+				}
+			case Stream.END:
+				return Stream.END;
+			default:
+				assert_not_reached ();
+			};
+		});
+	}
+
 	public enum Stream {
 		YIELD,
 		CONTINUE,
diff --git a/gee/treemap.vala b/gee/treemap.vala
index 8b60c34..355c5b0 100644
--- a/gee/treemap.vala
+++ b/gee/treemap.vala
@@ -1670,6 +1670,10 @@ public class Gee.TreeMap<K,V> : Gee.AbstractSortedMap<K,V> {
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<A, K> f) {
 			return Gee.Iterator.stream_impl<K, A>(this, (owned)f);
 		}
+
+		public Gee.Iterator<K> filter (owned Predicate<K> f) {
+			return Traversable.filter_impl<K> (this, (owned)f);
+		}
 	}
 
 	private class SubKeyIterator<K,V> : SubNodeIterator<K,V>, Traversable<K>, Gee.Iterator<K>, BidirIterator<K> {
@@ -1696,6 +1700,10 @@ public class Gee.TreeMap<K,V> : Gee.AbstractSortedMap<K,V> {
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<A, K> f) {
 			return Gee.Iterator.stream_impl<K, A>(this, (owned)f);
 		}
+
+		public Gee.Iterator<K> filter (owned Predicate<K> f) {
+			return Traversable.filter_impl<K> (this, (owned)f);
+		}
 	}
 
 	private class ValueIterator<K,V> : NodeIterator<K,V>, Traversable<V>, Gee.Iterator<V>, Gee.BidirIterator<V> {
@@ -1734,6 +1742,10 @@ public class Gee.TreeMap<K,V> : Gee.AbstractSortedMap<K,V> {
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<A, V> f) {
 			return Gee.Iterator.stream_impl<V, A>(this, (owned)f);
 		}
+
+		public Gee.Iterator<V> filter (owned Predicate<V> f) {
+			return Traversable.filter_impl<V> (this, (owned)f);
+		}
 	}
 
 	private class SubValueIterator<K,V> : SubNodeIterator<K,V>, Traversable<V>, Gee.Iterator<V>, BidirIterator<V> {
@@ -1760,6 +1772,10 @@ public class Gee.TreeMap<K,V> : Gee.AbstractSortedMap<K,V> {
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<A, V> f) {
 			return Gee.Iterator.stream_impl<V, A>(this, (owned)f);
 		}
+
+		public Gee.Iterator<V> filter (owned Predicate<V> f) {
+			return Traversable.filter_impl<V> (this, (owned)f);
+		}
 	}
 
 	private class EntryIterator<K,V> : NodeIterator<K,V>, Traversable<Map.Entry<K, V>>, Gee.Iterator<Map.Entry<K,V>>, Gee.BidirIterator<Map.Entry<K,V>> {
@@ -1802,6 +1818,10 @@ public class Gee.TreeMap<K,V> : Gee.AbstractSortedMap<K,V> {
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<Map.Entry<K, V>, A> f) {
 			return Gee.Iterator.stream_impl<Map.Entry<K, V>, A>(this, (owned)f);
 		}
+
+		public Gee.Iterator<Map.Entry<K, V>> filter (owned Predicate<Map.Entry<K, V>> f) {
+			return Traversable.filter_impl<Map.Entry<K, V>> (this, (owned)f);
+		}
 	}
 
 	private class SubEntryIterator<K,V> : SubNodeIterator<K,V>, Traversable<Map.Entry<K, V>>, Gee.Iterator<Map.Entry<K,V>>, Gee.BidirIterator<Map.Entry<K,V>> {
@@ -1832,6 +1852,10 @@ public class Gee.TreeMap<K,V> : Gee.AbstractSortedMap<K,V> {
 		public Gee.Iterator<A> stream<A> (owned StreamFunc<Map.Entry<K, V>, A> f) {
 			return Gee.Iterator.stream_impl<Map.Entry<K, V>, A>(this, (owned)f);
 		}
+
+		public Gee.Iterator<Map.Entry<K, V>> filter (owned Predicate<Map.Entry<K, V>> f) {
+			return Traversable.filter_impl<Map.Entry<K, V>> (this, (owned)f);
+		}
 	}
 	
 	private class MapIterator<K,V> : NodeIterator<K,V>, Gee.MapIterator<K,V>, BidirMapIterator<K,V> {
diff --git a/gee/treeset.vala b/gee/treeset.vala
index 112fbe5..0060345 100644
--- a/gee/treeset.vala
+++ b/gee/treeset.vala
@@ -737,6 +737,10 @@ public class Gee.TreeSet<G> : AbstractSortedSet<G> {
 			return Gee.Iterator.stream_impl<G, A>(this, (owned)f);
 		}
 
+		public Gee.Iterator<G> filter (owned Predicate<G> f) {
+			return Traversable.filter_impl<G> (this, (owned)f);
+		}
+
 		private weak Node<G>? current = null;
 		private weak Node<G>? _next = null;
 		private weak Node<G>? _prev = null;
@@ -1132,6 +1136,10 @@ public class Gee.TreeSet<G> : AbstractSortedSet<G> {
 			return Gee.Iterator.stream_impl<G, A>(this, (owned)f);
 		}
 
+		public Gee.Iterator<G> filter (owned Predicate<G> f) {
+			return Traversable.filter_impl<G> (this, (owned)f);
+		}
+
 		private new TreeSet<G> set;
 		private Range<G> range;
 		private Iterator<G>? iterator = null;
diff --git a/gee/unfolditerator.vala b/gee/unfolditerator.vala
index b017544..442451c 100644
--- a/gee/unfolditerator.vala
+++ b/gee/unfolditerator.vala
@@ -90,6 +90,10 @@ internal class Gee.UnfoldIterator<G> : Object, Traversable<G>, Iterator<G> {
 		return Iterator.stream_impl<G, A>(this, (owned)f);
 	}
 
+	public Iterator<G> filter (owned Predicate<G> f) {
+		return Traversable.filter_impl<G> (this, (owned)f);
+	}
+
 	private UnfoldFunc<G> _func;
 	private Lazy<G>? _current;
 	private Lazy<G>? _next;



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