From: Florian Brosch Date: Tue, 29 Nov 2011 00:56:46 +0000 (+0100) Subject: gee: Add MapIterator X-Git-Tag: 0.15.1~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d02f685bb8f602c4f63f150f27ad8672b712df1;p=thirdparty%2Fvala.git gee: Add MapIterator --- diff --git a/gee/Makefile.am b/gee/Makefile.am index ab315657d..f9ac2fcdd 100644 --- a/gee/Makefile.am +++ b/gee/Makefile.am @@ -17,6 +17,7 @@ libgee_la_VALASOURCES = \ hashmap.vala \ hashset.vala \ iterable.vala \ + mapiterator.vala \ iterator.vala \ list.vala \ map.vala \ diff --git a/gee/hashmap.vala b/gee/hashmap.vala index 5159fe3ff..1fb2ed310 100644 --- a/gee/hashmap.vala +++ b/gee/hashmap.vala @@ -74,6 +74,10 @@ public class Vala.HashMap : Map { return new ValueCollection (this); } + public override Vala.MapIterator map_iterator () { + return new MapIterator (this); + } + private Node** lookup_node (K key) { uint hash_value = _key_hash_func (key); Node** node = &_nodes[hash_value % _array_size]; @@ -224,6 +228,49 @@ public class Vala.HashMap : Map { } } + private class MapIterator : Vala.MapIterator { + public HashMap map { + set { + _map = value; + _stamp = _map._stamp; + } + } + + private HashMap _map; + private int _index = -1; + private weak Node _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 : Iterator { public HashMap map { set { diff --git a/gee/map.vala b/gee/map.vala index e78f794d4..2c96a3d04 100644 --- a/gee/map.vala +++ b/gee/map.vala @@ -84,5 +84,14 @@ public abstract class Vala.Map { * 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 map_iterator (); } diff --git a/gee/mapiterator.vala b/gee/mapiterator.vala new file mode 100644 index 000000000..78243c2d6 --- /dev/null +++ b/gee/mapiterator.vala @@ -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 + */ + + + +/** + * An iterator over a map. + */ +public abstract class Vala.MapIterator { + /** + * 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 (); +} + + +