From 3beebc10becc63a2a0f10ccb787afe47382e141e Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Fri, 10 Jul 2009 15:48:07 -0400 Subject: [PATCH] lib: improve list iterator Signed-off-by: Jean Privat --- lib/standard/list.nit | 24 ++++++++++++++++++++---- tests/sav/test_list2.sav | 3 +++ tests/test_list2.nit | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 tests/sav/test_list2.sav create mode 100644 tests/test_list2.nit diff --git a/lib/standard/list.nit b/lib/standard/list.nit index 13a8e29..ec803ba 100644 --- a/lib/standard/list.nit +++ b/lib/standard/list.nit @@ -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 index 0000000..8fc8aa8 --- /dev/null +++ b/tests/sav/test_list2.sav @@ -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 index 0000000..ab0290b --- /dev/null +++ b/tests/test_list2.nit @@ -0,0 +1,33 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2009 Jean Privat +# +# 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(", ") -- 1.7.9.5