[libgee] Move first from Iterator to BidirIterator and remove from MapIterator



commit 2ec5206bfb60e62bfad3736435a8dc31f685b030
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Tue Aug 24 12:47:24 2010 +0200

    Move first from Iterator to BidirIterator and remove from MapIterator

 gee/abstractmultiset.vala         |   11 -----------
 gee/bidiriterator.vala            |    7 +++++++
 gee/hashmap.vala                  |   10 ----------
 gee/hashset.vala                  |   10 ----------
 gee/iterator.vala                 |   15 ++++-----------
 gee/mapiterator.vala              |   15 ++++-----------
 gee/priorityqueue.vala            |    9 ---------
 gee/readonlycollection.vala       |    4 ----
 gee/readonlylist.vala             |    4 ++++
 gee/readonlymap.vala              |    4 ----
 tests/testcollection.vala         |   10 ++++++----
 tests/testreadonlycollection.vala |    4 +++-
 tests/testreadonlymap.vala        |    4 +++-
 tests/testsortedset.vala          |   23 +++++++++++++++++++++++
 14 files changed, 54 insertions(+), 76 deletions(-)
---
diff --git a/gee/abstractmultiset.vala b/gee/abstractmultiset.vala
index 6d2a5e5..ef88988 100644
--- a/gee/abstractmultiset.vala
+++ b/gee/abstractmultiset.vala
@@ -118,17 +118,6 @@ public abstract class Gee.AbstractMultiSet<G> : AbstractCollection<G>, MultiSet<
 			return _pending > 0 || _iter.has_next ();
 		}
 
-		public bool first () {
-			if (_set._nitems == 0) {
-				return false;
-			}
-			_pending = 0;
-			if (_iter.first ()) {
-				_pending = _iter.get_value () - 1;
-			}
-			return true;
-		}
-
 		public new G get () {
 			assert (! _removed);
 			return _iter.get_key ();
diff --git a/gee/bidiriterator.vala b/gee/bidiriterator.vala
index e4cf8d6..9dd28af 100644
--- a/gee/bidiriterator.vala
+++ b/gee/bidiriterator.vala
@@ -39,6 +39,13 @@ public interface Gee.BidirIterator<G> : Gee.Iterator<G> {
 	public abstract bool has_previous ();
 
 	/**
+	 * Rewinds to the first element in the iteration.
+	 *
+	 * @return `true` if the iterator has a first element
+	 */
+	public abstract bool first ();
+
+	/**
 	 * Advances to the last element in the iteration.
 	 *
 	 * @return `true` if the iterator has a last element
diff --git a/gee/hashmap.vala b/gee/hashmap.vala
index 496d7b1..78c03f1 100644
--- a/gee/hashmap.vala
+++ b/gee/hashmap.vala
@@ -491,16 +491,6 @@ public class Gee.HashMap<K,V> : Gee.AbstractMap<K,V> {
 			}
 			return (_next != null);
 		}
-
-		public bool first () {
-			assert (_stamp == _map._stamp);
-			if (_map.size == 0) {
-				return false;
-			}
-			_index = -1;
-			_next = null;
-			return next ();
-		}
 	}
 
 	private class KeyIterator<K,V> : NodeIterator<K,V>, Iterator<K> {
diff --git a/gee/hashset.vala b/gee/hashset.vala
index be86a10..46d3d20 100644
--- a/gee/hashset.vala
+++ b/gee/hashset.vala
@@ -245,16 +245,6 @@ public class Gee.HashSet<G> : AbstractSet<G> {
 			return (_next != null);
 		}
 
-		public bool first () {
-			assert (_stamp == _set._stamp);
-			if (_set.size == 0) {
-				return false;
-			}
-			_index = -1;
-			_next = null;
-			return next ();
-		}
-
 		public new G get () {
 			assert (_stamp == _set._stamp);
 			assert (_node != null);
diff --git a/gee/iterator.vala b/gee/iterator.vala
index 56111c4..5979c60 100644
--- a/gee/iterator.vala
+++ b/gee/iterator.vala
@@ -27,12 +27,12 @@
  * An iterator over a collection.
  *
  * Gee's iterators are "on-track" iterators. They always point to an item
- * except before the first call to { link next} or { link first}, or, when an
- * item has been removed, until the next call to { link next} or { link first}.
+ * except before the first call to { link next}, or, when an
+ * item has been removed, until the next call to { link next}.
  *
  * Please note that when the iterator is out of track, neither { link get} nor
  * { link remove} are defined and both will fail. After the next call to
- * { link next} or { link first}, they will be defined again.
+ * { link next}, they will be defined again.
  */
 public interface Gee.Iterator<G> : Object {
 	/**
@@ -50,13 +50,6 @@ public interface Gee.Iterator<G> : Object {
 	public abstract bool has_next ();
 
 	/**
-	 * Rewinds to the first element in the iteration.
-	 *
-	 * @return `true` if the iterator has a first element
-	 */
-	public abstract bool first ();
-
-	/**
 	 * Returns the current element in the iteration.
 	 *
 	 * @return the current element in the iteration
@@ -66,7 +59,7 @@ public interface Gee.Iterator<G> : Object {
 	/**
 	 * Removes the current element in the iteration. The cursor is set in an
 	 * in-between state. Both { link get} and { link remove} will fail until
-	 * the next move of the cursor (calling { link next} or { link first}).
+	 * the next move of the cursor (calling { link next}).
 	 */
 	public abstract void remove ();
 }
diff --git a/gee/mapiterator.vala b/gee/mapiterator.vala
index 485168a..8e191e8 100644
--- a/gee/mapiterator.vala
+++ b/gee/mapiterator.vala
@@ -24,12 +24,12 @@
  * An iterator over a map.
  *
  * Gee's iterators are "on-track" iterators. They always point to an item
- * except before the first call to { link next} or { link first}, or, when an
- * item has been removed, until the next call to { link next} or { link first}.
+ * except before the first call to { link next}, or, when an
+ * item has been removed, until the next call to { link next}.
  *
  * Please note that when the iterator is out of track, neither { link get_key},
  * { link get_value}, { link set_value} nor { link unset} are defined and all
- * will fail. After the next call to { link next} or { link first}, they will
+ * will fail. After the next call to { link next}, they will
  * be defined again.
  */
 public interface Gee.MapIterator<K,V> : Object {
@@ -48,13 +48,6 @@ public interface Gee.MapIterator<K,V> : Object {
 	public abstract bool has_next ();
 
 	/**
-	 * Rewinds to the first entry in the iteration.
-	 *
-	 * @return `true` if the iterator has a first entry
-	 */
-	public abstract bool first ();
-
-	/**
 	 * Returns the current key in the iteration.
 	 *
 	 * @return the current key in the iteration
@@ -79,7 +72,7 @@ public interface Gee.MapIterator<K,V> : Object {
 	 * Unsets the current entry in the iteration. The cursor is set in an
 	 * in-between state. { link get_key}, { link get_value}, { link set_value}
 	 * and { link unset} will fail until the next move of the cursor (calling
-	 * { link next} or { link first}).
+	 * { link next}).
 	 */
 	public abstract void unset ();
 }
diff --git a/gee/priorityqueue.vala b/gee/priorityqueue.vala
index 3b10f91..a348970 100644
--- a/gee/priorityqueue.vala
+++ b/gee/priorityqueue.vala
@@ -981,15 +981,6 @@ public class Gee.PriorityQueue<G> : Gee.AbstractQueue<G> {
 			return false;
 		}
 
-		public bool first () {
-			assert (stamp == queue._stamp);
-			position = queue._r;
-			started = false;
-			from_type1_children = false;
-			from_type2_child = false;
-			return next ();
-		}
-
 		public new G get () {
 			assert (stamp == queue._stamp);
 			assert (position != null);
diff --git a/gee/readonlycollection.vala b/gee/readonlycollection.vala
index 54ff850..c9fb287 100644
--- a/gee/readonlycollection.vala
+++ b/gee/readonlycollection.vala
@@ -151,10 +151,6 @@ internal class Gee.ReadOnlyCollection<G> : Object, Iterable<G>, Collection<G> {
 			return _iter.has_next ();
 		}
 
-		public bool first () {
-			return _iter.first ();
-		}
-
 		public new G get () {
 			return _iter.get ();
 		}
diff --git a/gee/readonlylist.vala b/gee/readonlylist.vala
index ec140c4..811f729 100644
--- a/gee/readonlylist.vala
+++ b/gee/readonlylist.vala
@@ -141,6 +141,10 @@ internal class Gee.ReadOnlyList<G> : Gee.ReadOnlyCollection<G>, List<G> {
 			return ((ListIterator<G>) _iter).has_previous ();
 		}
 
+		public bool first () {
+			return ((ListIterator<G>) _iter).first ();
+		}
+
 		public bool last () {
 			return ((ListIterator<G>) _iter).last ();
 		}
diff --git a/gee/readonlymap.vala b/gee/readonlymap.vala
index 87b0ffc..ddff5b6 100644
--- a/gee/readonlymap.vala
+++ b/gee/readonlymap.vala
@@ -230,10 +230,6 @@ internal class Gee.ReadOnlyMap<K,V> : Object, Iterable<Map.Entry<K,V>>, Map<K,V>
 			return _iter.has_next ();
 		}
 
-		public bool first () {
-			return _iter.first ();
-		}
-
 		public K get_key () {
 			return _iter.get_key ();
 		}
diff --git a/tests/testcollection.vala b/tests/testcollection.vala
index 6155f9e..43c2ada 100644
--- a/tests/testcollection.vala
+++ b/tests/testcollection.vala
@@ -64,7 +64,6 @@ public abstract class CollectionTests : Gee.TestCase {
 		Iterator<string> iterator = test_collection.iterator ();
 		assert (! iterator.has_next ());
 		assert (! iterator.next ());
-		assert (! iterator.first ());
 
 		// Check for some elements in the collection
 		assert (test_collection.add ("one"));
@@ -113,8 +112,9 @@ public abstract class CollectionTests : Gee.TestCase {
 		assert (three_found);
 		assert (three_found_once);
 
-		// Do it twice to check first ()
-		assert (iterator.first ());
+		iterator = test_collection.iterator ();
+		assert (iterator.has_next ());
+		assert (iterator.next ());
 
 		one_found = false;
 		two_found = false;
@@ -218,7 +218,9 @@ public abstract class CollectionTests : Gee.TestCase {
 		assert (three_found_once);
 
 		// Check after removal
-		assert (iterator.first ());
+		iterator = test_collection.iterator ();
+		assert (iterator.has_next ());
+		assert (iterator.next ());
 
 		one_found = false;
 		two_found = false;
diff --git a/tests/testreadonlycollection.vala b/tests/testreadonlycollection.vala
index 510a925..30874ee 100644
--- a/tests/testreadonlycollection.vala
+++ b/tests/testreadonlycollection.vala
@@ -94,7 +94,9 @@ public class ReadOnlyCollectionTests : Gee.TestCase {
 		assert (! iterator.has_next ());
 		assert (! iterator.next ());
 
-		assert (iterator.first ());
+		iterator = ro_collection.iterator ();
+		assert (iterator.has_next ());
+		assert (iterator.next ());
 		assert (iterator.get () == "one");
 
 		if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
diff --git a/tests/testreadonlymap.vala b/tests/testreadonlymap.vala
index c32f763..0cb6d2e 100644
--- a/tests/testreadonlymap.vala
+++ b/tests/testreadonlymap.vala
@@ -87,7 +87,9 @@ public class ReadOnlyMapTests : Gee.TestCase {
 		assert (! iterator.has_next ());
 		assert (! iterator.next ());
 
-		assert (iterator.first ());
+		iterator = ro_map.keys.iterator ();
+		assert (iterator.has_next ());
+		assert (iterator.next ());
 		assert (iterator.get () == "one");
 
 		if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
diff --git a/tests/testsortedset.vala b/tests/testsortedset.vala
index a5172b4..5371d7c 100644
--- a/tests/testsortedset.vala
+++ b/tests/testsortedset.vala
@@ -39,6 +39,8 @@ public abstract class SortedSetTests : SetTests {
 		          test_bidir_iterator_can_go_backward);
 		add_test ("[SortedSet] bi-directional iterators are mutable",
 		          test_mutable_bidir_iterator);
+		add_test ("[SortedSet] bi-directional iterators can to beginning",
+		          test_bidir_iterator_first);
 		add_test ("[SortedSet] bi-directional iterators can to end",
 		          test_bidir_iterator_last);
 		get_suite ().add_suite (new SubSet (this, SubSet.Type.HEAD).get_suite ());
@@ -300,6 +302,27 @@ public abstract class SortedSetTests : SetTests {
 		assert (iterator.get () == "five");
 	}
 
+	public void test_bidir_iterator_first () {
+		var test_set = test_collection as SortedSet<string>;
+
+		var iterator = test_set.bidir_iterator ();
+
+		assert (!iterator.first ());
+
+		assert (test_set.add ("one"));
+		assert (test_set.add ("two"));
+		assert (test_set.add ("three"));
+		assert (test_set.add ("four"));
+		assert (test_set.add ("five"));
+		assert (test_set.add ("six"));
+
+		iterator = test_set.bidir_iterator ();
+		assert (iterator.last ());
+		assert (iterator.get () == "two");
+		assert (iterator.first ());
+		assert (iterator.get () == "five");
+	}
+
 	public void test_bidir_iterator_last () {
 		var test_set = test_collection as SortedSet<string>;
 



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