Add custom foreach function
authorMaciej Piechotka <uzytkownik2@gmail.com>
Wed, 20 Apr 2011 16:12:56 +0000 (18:12 +0200)
committerMaciej Piechotka <uzytkownik2@gmail.com>
Wed, 20 Apr 2011 16:12:56 +0000 (18:12 +0200)
gee/arraylist.vala
gee/hashset.vala
gee/linkedlist.vala
gee/priorityqueue.vala
gee/readonlycollection.vala
gee/treeset.vala

index 44e4fe4..2cfe751 100644 (file)
@@ -380,6 +380,17 @@ public class Gee.ArrayList<G> : AbstractList<G> {
                                return _index >= 0 && _index < _list._size && ! _removed;
                        }
                }
+
+               public void foreach (ForallFunc<G> f) {
+                       assert (_stamp == _list._stamp);
+                       if (_index < 0 || _removed)
+                               _index++;
+                       while (_index < _list._size) {
+                               f (_list._items[_index]);
+                               _index++;
+                       }
+                       _index = _list._size;
+               }
        }
 }
 
index 94bea79..98ae682 100644 (file)
@@ -278,6 +278,22 @@ public class Gee.HashSet<G> : AbstractSet<G> {
                                return _node != null;
                        }
                }
+
+               public void foreach (ForallFunc<G> f) {
+                       assert (_stamp == _set._stamp);
+                       if (_node != null)
+                               f (_node.key);
+                       while (_index + 1 < _set._array_size || _next != null) {
+                               if (_next != null) {
+                                       _node = _next;
+                                       f (_node.key);
+                                       _next = _node.next;
+                               } else {
+                                       _index++;
+                                       _next = _set._nodes[_index];
+                               }
+                       }
+               }
        }
 }
 
index 9cb2d0b..8ba00ba 100644 (file)
@@ -175,7 +175,7 @@ public class Gee.LinkedList<G> : AbstractList<G>, Queue<G>, Deque<G> {
        public override int index_of (G item) {
                int result = -1;
                int idx = 0;
-               foreach (G node_item in this) {
+               foreach (G node_item in (Collection)this) {
                        if (this.equal_func (item, node_item)) {
                                result = idx;
                                break;
@@ -597,6 +597,21 @@ public class Gee.LinkedList<G> : AbstractList<G>, Queue<G>, Deque<G> {
                                return !this.removed && this.position != null;
                        }
                }
+
+               public void foreach (ForallFunc<G> f) {
+                       assert (_stamp == _list._stamp);
+                       if (!started) {
+                               position = _list._head;
+                               if (position != null)
+                                       started = true;
+                       }
+                       removed = false;
+                       while (position != null) {
+                               f (position.data);
+                               position = position.next;
+                       }
+                       position = _list._tail;
+               }
        }
 
        private unowned Node<G>? _get_node_at (int index) {
index 0b6a93e..93c41d5 100644 (file)
@@ -1026,5 +1026,20 @@ public class Gee.PriorityQueue<G> : Gee.AbstractQueue<G> {
                                return started &&  ! removed && position != null;
                        }
                }
+
+               /*public void foreach (ForallFunc<G> f) {
+                       assert (stamp == queue._stamp);
+                       if (position != null)
+                               f (position.data);
+                       else if (_next != null)
+                               removed = false;
+                       if (_next == null)
+                               _has_next ();
+                       while (_next != null) {
+                               position = _next;
+                               f (position.data);
+                               _has_next ();
+                       }
+               }*/
        }
 }
index 2e7123e..82a41b6 100644 (file)
@@ -177,6 +177,11 @@ internal class Gee.ReadOnlyCollection<G> : Object, Iterable<G>, Collection<G> {
                                return true;
                        }
                }
+
+               public void foreach (ForallFunc<G> f) {
+                       _iter.foreach (f);
+               }
+
        }
 
        public virtual Collection<G> read_only_view {
index 58669c5..bcae419 100644 (file)
@@ -718,6 +718,21 @@ public class Gee.TreeSet<G> : AbstractSet<G>, SortedSet<G> {
                        }
                }
 
+               public void foreach (ForallFunc<G> f) {
+                       assert (stamp == _set.stamp);
+                       if (current != null) {
+                               f (current.key);
+                               _next = current.next;
+                       } else if (!started) {
+                               _next = _set._first;
+                       }
+                       while (_next != null) {
+                               current = _next;
+                               f (current.key);
+                               _next = current.next;
+                       }
+               }
+
                private weak Node<G>? current = null;
                private weak Node<G>? _next = null;
                private weak Node<G>? _prev = null;