lib: add iterate methods on Collection
authorJean Privat <jean@pryen.org>
Tue, 25 Aug 2009 16:07:00 +0000 (12:07 -0400)
committerJean Privat <jean@pryen.org>
Thu, 27 Aug 2009 17:45:35 +0000 (13:45 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

lib/standard/collection/abstract_collection.nit
lib/standard/collection/array.nit
lib/standard/collection/hash_collection.nit
lib/standard/collection/range.nit
tests/sav/test_iterate.sav [new file with mode: 0644]
tests/test_iterate.nit [new file with mode: 0644]

index c0c895c..926585c 100644 (file)
@@ -42,6 +42,17 @@ interface Collection[E]
        # Get a new iterator on the collection.
        fun iterator: Iterator[E] is abstract
 
+       # Iterate over each element of the collection
+       fun iterate
+               !each(e: E)
+       do
+               var i = iterator
+               while i.is_ok do
+                       each(i.item)
+                       i.next
+               end
+       end
+
        # Is there no item in the collection ?
        fun is_empty: Bool is abstract 
 
index 2e3b30b..88d76c1 100644 (file)
@@ -228,6 +228,18 @@ end
 class Array[E]
 special AbstractArray[E]
 special ArrayCapable[E]
+       redef fun iterate
+               !each(e: E)
+       do
+               var i = 0
+               var l = _length
+               var items = _items
+               while i < length do
+                       each(items[i])
+                       i += 1
+               end
+       end
+
        redef fun [](index)
        do
                assert index: index >= 0 and index < _length
index 7fb541d..fde2fff 100644 (file)
@@ -185,6 +185,16 @@ special HashCollection[K, HashMapNode[K, V], V]
 
        redef fun iterator: HashMapIterator[K, V] do return new HashMapIterator[K,V](self)
 
+       redef fun iterate
+               !each(e: V)
+       do
+               var c = _first_item
+               while c != null do
+                       each(c.second)
+                       c = c._next_item
+               end
+       end
+
        redef fun first
        do
                assert _length > 0
index 0641c2b..aa96cf0 100644 (file)
@@ -42,6 +42,17 @@ special Collection[E]
 
        redef fun iterator do return new IteratorRange[E](self)
 
+       redef fun iterate
+               !each(e: E)
+       do
+               var c = _first
+               var l = _last
+               while c <= l do
+                       each(c)
+                       c = c.succ
+               end
+       end
+
        redef fun length
        do
                var nb = _first.distance(_after)
diff --git a/tests/sav/test_iterate.sav b/tests/sav/test_iterate.sav
new file mode 100644 (file)
index 0000000..2df32ac
--- /dev/null
@@ -0,0 +1,152 @@
+Array: iterator
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+Array: iterate
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+Array: for
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+
+List: iterator
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+List: iterate
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+List: for
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+
+ArraySet: iterator
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+ArraySet: iterate
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+ArraySet: for
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+
+HashSet: iterator
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+HashSet: iterate
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+HashSet: for
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+
+ORange: iterator
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+ORange: iterate
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+ORange: for
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+
+CRange: iterator
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+CRange: iterate
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+CRange: for
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+
+ArrayMap: iterator
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+ArrayMap: iterate
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+ArrayMap: for
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+
+HashMap: iterator
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+HashMap: iterate
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+HashMap: for
+  0->0
+  1->1
+  2->2
+  3->3
+  4->4
+
diff --git a/tests/test_iterate.nit b/tests/test_iterate.nit
new file mode 100644 (file)
index 0000000..b2d95c5
--- /dev/null
@@ -0,0 +1,82 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 Jean Privat <jean@pryen.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+fun test_iterator(c: Collection[Int])
+do
+       var idx = 0
+       var it = c.iterator
+       while it.is_ok do
+               var i = it.item
+               print "  {idx}->{i}"
+               it.next
+               idx += 1
+       end
+end
+
+fun test_iterate(c: Collection[Int])
+do
+       var idx = 0
+       c.iterate !each i do
+               print "  {idx}->{i}"
+               idx += 1
+       end
+end
+
+fun test_for(c: Collection[Int])
+do
+       var idx = 0
+       for i in c do
+               print "  {idx}->{i}"
+               idx += 1
+       end
+end
+
+fun test_coll(c: Collection[Int], s: String)
+do
+       print("{s}: iterator")
+       test_iterator(c)
+       print("{s}: iterate")
+       test_iterate(c)
+       print("{s}: for")
+       test_for(c)
+       print("")
+end
+
+fun init_seq(c: SimpleCollection[Int]): SimpleCollection[Int]
+do
+       for i in [0..5[ do
+               c.add(i)
+       end
+       return c
+end
+
+fun init_map(c: Map[Int, Int]): Map[Int, Int]
+do
+       for i in [0..5[ do
+               c[i*10] = i
+       end
+       return c
+end
+
+test_coll(init_seq(new Array[Int]), "Array")
+test_coll(init_seq(new List[Int]), "List")
+test_coll(init_seq(new ArraySet[Int]), "ArraySet")
+test_coll(init_seq(new HashSet[Int]), "HashSet")
+test_coll([0..5[, "ORange")
+test_coll([0..4], "CRange")
+test_coll(init_map(new ArrayMap[Int, Int]), "ArrayMap")
+test_coll(init_map(new HashMap[Int, Int]), "HashMap")
+