[libgee] Add custom foreach function
- From: Maciej Marcin Piechotka <mpiechotka src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgee] Add custom foreach function
- Date: Wed, 27 Apr 2011 21:27:58 +0000 (UTC)
commit 1194e8de73e7c056ba8188ec1a1070fd1506c11f
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date: Wed Apr 20 18:12:56 2011 +0200
Add custom foreach function
gee/arraylist.vala | 11 +++++++++++
gee/hashset.vala | 16 ++++++++++++++++
gee/linkedlist.vala | 17 ++++++++++++++++-
gee/priorityqueue.vala | 15 +++++++++++++++
gee/readonlycollection.vala | 5 +++++
gee/treeset.vala | 15 +++++++++++++++
6 files changed, 78 insertions(+), 1 deletions(-)
---
diff --git a/gee/arraylist.vala b/gee/arraylist.vala
index 44e4fe4..2cfe751 100644
--- a/gee/arraylist.vala
+++ b/gee/arraylist.vala
@@ -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;
+ }
}
}
diff --git a/gee/hashset.vala b/gee/hashset.vala
index 94bea79..98ae682 100644
--- a/gee/hashset.vala
+++ b/gee/hashset.vala
@@ -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];
+ }
+ }
+ }
}
}
diff --git a/gee/linkedlist.vala b/gee/linkedlist.vala
index 9cb2d0b..8ba00ba 100644
--- a/gee/linkedlist.vala
+++ b/gee/linkedlist.vala
@@ -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) {
diff --git a/gee/priorityqueue.vala b/gee/priorityqueue.vala
index 0b6a93e..93c41d5 100644
--- a/gee/priorityqueue.vala
+++ b/gee/priorityqueue.vala
@@ -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 ();
+ }
+ }*/
}
}
diff --git a/gee/readonlycollection.vala b/gee/readonlycollection.vala
index 2e7123e..82a41b6 100644
--- a/gee/readonlycollection.vala
+++ b/gee/readonlycollection.vala
@@ -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 {
diff --git a/gee/treeset.vala b/gee/treeset.vala
index 58669c5..bcae419 100644
--- a/gee/treeset.vala
+++ b/gee/treeset.vala
@@ -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;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]