X-Git-Url: http://nitlanguage.org diff --git a/examples/circular_list.nit b/examples/circular_list.nit index 8b99d48..41d2e05 100644 --- a/examples/circular_list.nit +++ b/examples/circular_list.nit @@ -12,19 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Implementation of circular lists +# Example of an implementation of circular lists # This example shows the usage of generics and somewhat a specialisation of collections. -module circular_list +module circular_list is example # Sequences of elements implemented with a double-linked circular list class CircularList[E] # Like standard Array or LinkedList, CircularList is a Sequence. super Sequence[E] - # NaiveCollection contains working (but inefficient) implementation of - # the methods of Collection. - super NaiveCollection[E] - # The first node of the list if any # The special case of an empty list is handled by a null node private var node: nullable CLNode[E] = null @@ -124,11 +120,11 @@ end private class CircularListIterator[E] super IndexedIterator[E] - redef var key: Int + redef var index: Int = 0 # The current node pointed. # Is null if the list is empty. - var node: nullable CLNode[E] + var node: nullable CLNode[E] is noinit # The list iterated. var list: CircularList[E] @@ -137,22 +133,20 @@ private class CircularListIterator[E] do # Empty lists are not OK. # Pointing again the first node is not OK. - return self.node != null and (self.key == 0 or self.node != self.list.node) + return self.node != null and (self.index == 0 or self.node != self.list.node) end redef fun next do self.node = self.node.next - self.key += 1 + self.index += 1 end redef fun item do return self.node.item - init(list: CircularList[E]) + init do self.node = list.node - self.list = list - self.key = 0 end end