]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
gee: Add MapIterator
authorFlorian Brosch <flo.brosch@gmail.com>
Tue, 29 Nov 2011 00:56:46 +0000 (01:56 +0100)
committerJürg Billeter <j@bitron.ch>
Thu, 26 Jan 2012 19:31:14 +0000 (20:31 +0100)
gee/Makefile.am
gee/hashmap.vala
gee/map.vala
gee/mapiterator.vala [new file with mode: 0644]

index ab315657d6fb4a4ee0b9be603437c91c3dc9d54d..f9ac2fcdd9667ce8c805c3322b098e75717a5e04 100644 (file)
@@ -17,6 +17,7 @@ libgee_la_VALASOURCES = \
        hashmap.vala \
        hashset.vala \
        iterable.vala \
+       mapiterator.vala \
        iterator.vala \
        list.vala \
        map.vala \
index 5159fe3ff6528f71a94d5995b58f0d6075c2b826..1fb2ed310ec388b749405b6dd5e86b350143a1b8 100644 (file)
@@ -74,6 +74,10 @@ public class Vala.HashMap<K,V> : Map<K,V> {
                return new ValueCollection<K,V> (this);
        }
 
+       public override Vala.MapIterator<K,V> map_iterator () {
+               return new MapIterator<K,V> (this);
+       }
+
        private Node<K,V>** lookup_node (K key) {
                uint hash_value = _key_hash_func (key);
                Node<K,V>** node = &_nodes[hash_value % _array_size];
@@ -224,6 +228,49 @@ public class Vala.HashMap<K,V> : Map<K,V> {
                }
        }
 
+       private class MapIterator<K,V> : Vala.MapIterator<K, V> {
+               public HashMap<K,V> map {
+                       set {
+                               _map = value;
+                               _stamp = _map._stamp;
+                       }
+               }
+
+               private HashMap<K,V> _map;
+               private int _index = -1;
+               private weak Node<K,V> _node;
+
+               // concurrent modification protection
+               private int _stamp;
+
+               public MapIterator (HashMap map) {
+                       this.map = map;
+               }
+
+               public override bool next () {
+                       if (_node != null) {
+                               _node = _node.next;
+                       }
+                       while (_node == null && _index + 1 < _map._array_size) {
+                               _index++;
+                               _node = _map._nodes[_index];
+                       }
+                       return (_node != null);
+               }
+
+               public override K? get_key () {
+                       assert (_stamp == _map._stamp);
+                       assert (_node != null);
+                       return _node.key;
+               }
+
+               public override V? get_value () {
+                       assert (_stamp == _map._stamp);
+                       assert (_node != null);
+                       return _node.value;
+               }
+       }
+
        private class KeyIterator<K,V> : Iterator<K> {
                public HashMap<K,V> map {
                        set {
index e78f794d4b69df6f3d5a11eb5722c9cc78cef310..2c96a3d0452d836d95c055a8af14a5e62ca82c12 100644 (file)
@@ -84,5 +84,14 @@ public abstract class Vala.Map<K,V> {
         * read-only collections.
         */
        public abstract void clear ();
+
+       /**
+        * Returns a Iterator that can be used for simple iteration over a
+        * map.
+        *
+        * @return a Iterator that can be used for simple iteration over a
+        *         map
+        */
+       public abstract MapIterator<K,V> map_iterator ();
 }
 
diff --git a/gee/mapiterator.vala b/gee/mapiterator.vala
new file mode 100644 (file)
index 0000000..78243c2
--- /dev/null
@@ -0,0 +1,52 @@
+/* mapiterator.vala
+ *
+ * Copyright (C) 2011  Florian Brosch
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Florian Brosch <flo.brosch@gmail.com>
+ */
+
+
+
+/**
+ * An iterator over a map.
+ */
+public abstract class Vala.MapIterator<K,V> {
+       /**
+        * Advances to the next element in the iteration.
+        *
+        * @return true if the iterator has a next element
+        */
+       public abstract bool next ();
+
+       /**
+        * Returns the current key in the iteration.
+        *
+        * @return the current key in the iteration
+        */
+       public abstract K get_key ();
+
+       /**
+        * Returns the current value in the iteration.
+        *
+        * @return the current value in the iteration
+        */
+       public abstract V get_value ();
+}
+
+
+