Add Iterator.at_element property to check when other calls are legal
authorMaciej Piechotka <uzytkownik2@gmail.com>
Sun, 17 Oct 2010 15:28:04 +0000 (16:28 +0100)
committerMaciej Piechotka <uzytkownik2@gmail.com>
Sun, 17 Oct 2010 15:28:04 +0000 (16:28 +0100)
13 files changed:
gee/abstractmultiset.vala
gee/arraylist.vala
gee/hashmap.vala
gee/hashset.vala
gee/iterator.vala
gee/linkedlist.vala
gee/mapiterator.vala
gee/priorityqueue.vala
gee/readonlycollection.vala
gee/readonlymap.vala
gee/treemap.vala
gee/treeset.vala
tests/testcollection.vala

index ef88988..14a9eff 100644 (file)
@@ -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;
+                       }
+               }
        }
 }
index 1aa32a6..4bf0f5a 100644 (file)
@@ -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;
+                       }
+               }
        }
 }
 
index 78c03f1..9aec84c 100644 (file)
@@ -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> {
index 46d3d20..052e62e 100644 (file)
@@ -259,6 +259,12 @@ public class Gee.HashSet<G> : AbstractSet<G> {
                        _node = null;
                        _stamp = _set._stamp;
                }
+               
+               public bool at_element {
+                       get {
+                               return _node != null;
+                       }
+               }
        }
 }
 
index 5979c60..3b09d63 100644 (file)
@@ -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; }
 }
 
index b70569d..a991e6c 100644 (file)
@@ -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) {
index 8e191e8..3f5e9c2 100644 (file)
@@ -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; }
 }
 
index a348970..75ae27b 100644 (file)
@@ -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;
+                       }
+               }
        }
 }
index c9fb287..fb563bb 100644 (file)
@@ -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 {
index ddff5b6..f0f7150 100644 (file)
@@ -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;
+                       }
+               }
        }
 }
 
index d1bbd43..a8a9b7f 100644 (file)
@@ -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> {
index 38a2fd9..412f988 100644 (file)
@@ -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;
index 43c2ada..06d5d94 100644 (file)
@@ -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;