[libgee] Add Iterator.at_element property to check when other calls are legal



commit 839d4c5c33f5c9fffca80feac876feebb5c61cb9
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Sun Oct 17 16:28:04 2010 +0100

    Add Iterator.at_element property to check when other calls are legal

 gee/abstractmultiset.vala   |    6 ++++++
 gee/arraylist.vala          |    7 +++++++
 gee/hashmap.vala            |    6 ++++++
 gee/hashset.vala            |    6 ++++++
 gee/iterator.vala           |    6 ++++++
 gee/linkedlist.vala         |    6 ++++++
 gee/mapiterator.vala        |    7 +++++++
 gee/priorityqueue.vala      |    9 ++++++++-
 gee/readonlycollection.vala |    6 ++++++
 gee/readonlymap.vala        |    6 ++++++
 gee/treemap.vala            |    6 ++++++
 gee/treeset.vala            |   13 +++++++++++++
 tests/testcollection.vala   |   10 ++++++++++
 13 files changed, 93 insertions(+), 1 deletions(-)
---
diff --git a/gee/abstractmultiset.vala b/gee/abstractmultiset.vala
index ef88988..14a9eff 100644
--- a/gee/abstractmultiset.vala
+++ b/gee/abstractmultiset.vala
@@ -132,5 +132,11 @@ public abstract class Gee.AbstractMultiSet<G> : AbstractCollection<G>, MultiSet<
 			_set._nitems--;
 			_removed = true;
 		}
+		
+		public bool at_element {
+			get {
+				return ! _removed && _iter.at_element;
+			}
+		}
 	}
 }
diff --git a/gee/arraylist.vala b/gee/arraylist.vala
index 1aa32a6..4bf0f5a 100644
--- a/gee/arraylist.vala
+++ b/gee/arraylist.vala
@@ -361,6 +361,13 @@ public class Gee.ArrayList<G> : AbstractList<G> {
 			assert (_index < _list._size);
 			return _index;
 		}
+		
+		public bool at_element {
+			get {
+				stderr.printf ("%d %s\n", _index, _removed ? "true" : "false");
+				return _index >= 0 && _index < _list._size && ! _removed;
+			}
+		}
 	}
 }
 
diff --git a/gee/hashmap.vala b/gee/hashmap.vala
index 78c03f1..9aec84c 100644
--- a/gee/hashmap.vala
+++ b/gee/hashmap.vala
@@ -491,6 +491,12 @@ public class Gee.HashMap<K,V> : Gee.AbstractMap<K,V> {
 			}
 			return (_next != null);
 		}
+		
+		public bool at_element {
+			get {
+				return _node != null;
+			}
+		}
 	}
 
 	private class KeyIterator<K,V> : NodeIterator<K,V>, Iterator<K> {
diff --git a/gee/hashset.vala b/gee/hashset.vala
index 46d3d20..052e62e 100644
--- a/gee/hashset.vala
+++ b/gee/hashset.vala
@@ -259,6 +259,12 @@ public class Gee.HashSet<G> : AbstractSet<G> {
 			_node = null;
 			_stamp = _set._stamp;
 		}
+		
+		public bool at_element {
+			get {
+				return _node != null;
+			}
+		}
 	}
 }
 
diff --git a/gee/iterator.vala b/gee/iterator.vala
index 5979c60..3b09d63 100644
--- a/gee/iterator.vala
+++ b/gee/iterator.vala
@@ -62,5 +62,11 @@ public interface Gee.Iterator<G> : Object {
 	 * the next move of the cursor (calling { link next}).
 	 */
 	public abstract void remove ();
+	
+	/**
+	 * Determines wheather the call to { link get} is legal. It is false at the
+	 * beginning and after { link remove} call and true otherwise.
+	 */
+	public abstract bool at_element { get; }
 }
 
diff --git a/gee/linkedlist.vala b/gee/linkedlist.vala
index b70569d..a991e6c 100644
--- a/gee/linkedlist.vala
+++ b/gee/linkedlist.vala
@@ -570,6 +570,12 @@ public class Gee.LinkedList<G> : AbstractList<G>, Queue<G>, Deque<G> {
 
 			return this._index;
 		}
+		
+		public bool at_element {
+			get {
+				return !this.removed && this.position != null;
+			}
+		}
 	}
 
 	private unowned Node<G>? _get_node_at (int index) {
diff --git a/gee/mapiterator.vala b/gee/mapiterator.vala
index 8e191e8..3f5e9c2 100644
--- a/gee/mapiterator.vala
+++ b/gee/mapiterator.vala
@@ -75,5 +75,12 @@ public interface Gee.MapIterator<K,V> : Object {
 	 * { link next}).
 	 */
 	public abstract void unset ();
+	
+	/**
+	 * Determines wheather the call to { link get_key}, { link get_value} and 
+	 * { link set_value} is legal. It is false at the beginning and after
+	 * { link unset} call and true otherwise.
+	 */
+	public abstract bool at_element { get; }
 }
 
diff --git a/gee/priorityqueue.vala b/gee/priorityqueue.vala
index a348970..75ae27b 100644
--- a/gee/priorityqueue.vala
+++ b/gee/priorityqueue.vala
@@ -925,6 +925,7 @@ public class Gee.PriorityQueue<G> : Gee.AbstractQueue<G> {
 				return false;
 			}
 			removed = false;
+			started = true;
 			position = _next;
 			_next = null;
 			return (position != null);
@@ -943,7 +944,6 @@ public class Gee.PriorityQueue<G> : Gee.AbstractQueue<G> {
 
 		private bool _has_next() {
 			if (!started) {
-				started = true;
 				return _next != null;
 			} else if (_next is Type1Node) {
 				var node = _next as Type1Node<G>;
@@ -1005,5 +1005,12 @@ public class Gee.PriorityQueue<G> : Gee.AbstractQueue<G> {
 			assert (position != null);
 			return position;
 		}
+		
+		
+		public bool at_element {
+			get {
+				return started &&  ! removed && position != null;
+			}
+		}
 	}
 }
diff --git a/gee/readonlycollection.vala b/gee/readonlycollection.vala
index c9fb287..fb563bb 100644
--- a/gee/readonlycollection.vala
+++ b/gee/readonlycollection.vala
@@ -158,6 +158,12 @@ internal class Gee.ReadOnlyCollection<G> : Object, Iterable<G>, Collection<G> {
 		public void remove () {
 			assert_not_reached ();
 		}
+		
+		public bool at_element {
+			get {
+				return _iter.at_element;
+			}
+		}
 	}
 
 	public virtual Collection<G> read_only_view {
diff --git a/gee/readonlymap.vala b/gee/readonlymap.vala
index ddff5b6..f0f7150 100644
--- a/gee/readonlymap.vala
+++ b/gee/readonlymap.vala
@@ -245,6 +245,12 @@ internal class Gee.ReadOnlyMap<K,V> : Object, Iterable<Map.Entry<K,V>>, Map<K,V>
 		public void unset () {
 			assert_not_reached ();
 		}
+		
+		public bool at_element {
+			get {
+				return _iter.at_element;
+			}
+		}
 	}
 }
 
diff --git a/gee/treemap.vala b/gee/treemap.vala
index d1bbd43..a8a9b7f 100644
--- a/gee/treemap.vala
+++ b/gee/treemap.vala
@@ -706,6 +706,12 @@ public class Gee.TreeMap<K,V> : Gee.AbstractMap<K,V> {
 			_map.stamp++;
 			assert (stamp == _map.stamp);
 		}
+		
+		public bool at_element {
+			get {
+				return current != null;
+			}
+		}
 	}
 
 	private class KeyIterator<K,V> : NodeIterator<K,V>, Gee.Iterator<K>, BidirIterator<K> {
diff --git a/gee/treeset.vala b/gee/treeset.vala
index 38a2fd9..412f988 100644
--- a/gee/treeset.vala
+++ b/gee/treeset.vala
@@ -697,6 +697,13 @@ public class Gee.TreeSet<G> : AbstractSet<G>, SortedSet<G> {
 				return _next != null;
 			}
 		}
+		
+		public bool at_element {
+			get {
+				assert (stamp == _set.stamp);
+				return current != null;
+			}
+		}
 
 		private weak Node<G>? current = null;
 		private weak Node<G>? _next = null;
@@ -1065,6 +1072,12 @@ public class Gee.TreeSet<G> : AbstractSet<G>, SortedSet<G> {
 			assert (iterator != null);
 			iterator.remove ();
 		}
+		
+		public bool at_element {
+			get {
+				return iterator.at_element;
+			}
+		}
 
 		private new TreeSet<G> set;
 		private Range<G> range;
diff --git a/tests/testcollection.vala b/tests/testcollection.vala
index 43c2ada..06d5d94 100644
--- a/tests/testcollection.vala
+++ b/tests/testcollection.vala
@@ -77,14 +77,19 @@ public abstract class CollectionTests : Gee.TestCase {
 		bool two_found_once = true;
 		bool three_found_once = true;
 		iterator = test_collection.iterator ();
+		bool at_element = iterator.at_element;
+		assert (! at_element);
 		while (true) {
 			has_next = iterator.has_next ();
+			assert (at_element == iterator.at_element);
 			assert (has_next == iterator.next ());
+			assert (at_element = iterator.at_element);
 			if (! has_next) {
 				break;
 			}
 
 			string element = iterator.get ();
+			assert (iterator.at_element);
 			if (element == "one") {
 				if (one_found) {
 					one_found_once = false;
@@ -104,7 +109,9 @@ public abstract class CollectionTests : Gee.TestCase {
 		}
 		has_next = iterator.has_next ();
 		assert (! has_next);
+		assert (iterator.at_element);
 		assert (has_next == iterator.next ());
+		assert (iterator.at_element);
 		assert (one_found);
 		assert (one_found_once);
 		assert (two_found);
@@ -114,6 +121,7 @@ public abstract class CollectionTests : Gee.TestCase {
 
 		iterator = test_collection.iterator ();
 		assert (iterator.has_next ());
+		assert (! iterator.at_element);
 		assert (iterator.next ());
 
 		one_found = false;
@@ -185,6 +193,7 @@ public abstract class CollectionTests : Gee.TestCase {
 			if (! has_next) {
 				break;
 			}
+			assert (iterator.at_element);
 
 			string element = iterator.get ();
 			if (element == "one") {
@@ -200,6 +209,7 @@ public abstract class CollectionTests : Gee.TestCase {
 
 				// Remove this element
 				iterator.remove ();
+				assert (! iterator.at_element);
 			} else if (element == "three") {
 				if (three_found) {
 					three_found_once = false;



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