lib: improve list iterator
authorJean Privat <jean@pryen.org>
Fri, 10 Jul 2009 19:48:07 +0000 (15:48 -0400)
committerJean Privat <jean@pryen.org>
Wed, 15 Jul 2009 16:19:40 +0000 (12:19 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

lib/standard/list.nit
tests/sav/test_list2.sav [new file with mode: 0644]
tests/test_list2.nit [new file with mode: 0644]

index 13a8e29..ec803ba 100644 (file)
@@ -170,7 +170,7 @@ special IndexedCollection[E]
        end
 
 
-       redef fun iterator: ListIterator[E] do return new ListIterator[E](_head)
+       redef fun iterator: ListIterator[E] do return new ListIterator[E](self)
 
        # Build an empty list.
        init do end
@@ -245,7 +245,7 @@ class ListIterator[E]
 special IndexedIterator[E]
        redef fun item do return _node.item
 
-       # redef fun item=(e) do _node.item = e
+       fun item=(e: E) do _node.item = e
 
        redef fun is_ok do return not _node == null
 
@@ -256,17 +256,33 @@ special IndexedIterator[E]
        end
 
        # Build a new iterator from `node'.
-       private init(node: nullable ListNode[E])
+       private init(list: List[E])
        do
-               _node = node
+               _list = list
+               _node = list._head
                _index = 0
        end
 
+       # The current list
+       var _list: List[E]
+
        # The current node of the list
        var _node: nullable ListNode[E]
 
        # The index of the current node
        redef readable var _index: Int
+
+       # Remove the current item
+       fun delete
+       do
+               _list.remove_node(_node.as(not null))
+       end
+
+       # Insert before the current item
+       fun insert_before(element: E)
+       do
+               _list.insert_before(element, _node.as(not null))
+       end
 end
 
 # Linked nodes that constitute a linked list.
diff --git a/tests/sav/test_list2.sav b/tests/sav/test_list2.sav
new file mode 100644 (file)
index 0000000..8fc8aa8
--- /dev/null
@@ -0,0 +1,3 @@
+1, 2, 3, 4, 5, 6
+11, 12, 13, 14, 15, 16
+1, 21, 2, 22, 3, 23, 4, 24, 5, 25, 6, 26
diff --git a/tests/test_list2.nit b/tests/test_list2.nit
new file mode 100644 (file)
index 0000000..ab0290b
--- /dev/null
@@ -0,0 +1,33 @@
+# 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.
+
+var l = new List[Int]
+l.add_all([1, 2, 3, 4, 5, 6])
+print l.join(", ")
+var it = l.iterator
+while it.is_ok do
+       it.item += 10
+       it.next
+end
+print l.join(", ")
+var it = l.iterator
+while it.is_ok do
+       it.insert_before(it.item-10)
+       it.insert_before(it.item+10)
+       it.delete
+       it.next
+end
+print l.join(", ")