summaryrefslogtreecommitdiff
path: root/gee/hashmap.vala
diff options
context:
space:
mode:
authorFlorian Brosch <flo.brosch@gmail.com>2011-11-29 01:56:46 +0100
committerJürg Billeter <j@bitron.ch>2012-01-26 20:31:14 +0100
commit3d02f685bb8f602c4f63f150f27ad8672b712df1 (patch)
tree9d6800965cbfbe88a3025161683dbb7cbf5033a2 /gee/hashmap.vala
parent1c75deb3e89aad84820dcc801a6388003eb1dd23 (diff)
downloadvala-3d02f685bb8f602c4f63f150f27ad8672b712df1.tar.gz
gee: Add MapIterator
Diffstat (limited to 'gee/hashmap.vala')
-rw-r--r--gee/hashmap.vala47
1 files changed, 47 insertions, 0 deletions
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<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 {