1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
13 # This module handle double linked lists
16 import abstract_collection
18 # Double linked lists.
22 redef fun [](index
) do return get_node
(index
).item
24 redef fun []=(index
, item
) do get_node
(index
).item
= item
27 redef fun first
do return _head
.item
30 redef fun first
=(e
) do _head
.item
= e
33 redef fun last
do return _tail
.item
36 redef fun last
=(e
) do _tail
.item
= e
41 redef fun is_empty
do return _head
== null
56 redef fun has
(e
) do return search_node_after
(e
, _head
) != null
62 if node
.item
!= e
then return false
73 if node
.item
!= e
then nb
+= 1
79 # Return a list of elements between 'from' and 'to'.
80 fun slice
(from
: Int, to
: Int): List[E
] do
81 assert from
>= 0 and from
< length
82 assert to
>= 0 and to
< length
and from
<= to
83 var list
= new List[E
]
96 var node
= new ListNode[E
](e
)
109 var node
= new ListNode[E
](e
)
110 if _head
== null then
120 redef fun insert
(e
, i
)
122 var node
= get_node
(i
)
127 var nnode
= new ListNode[E
](e
)
139 # Append `l` to `self` but clear `l`.
144 if _tail
== null then
146 else if l
._head
!= null then
148 _tail
.next
.prev
= _tail
162 if _tail
== null then
176 if _head
== null then
186 var node
= search_node_after
(e
, _head
)
187 if node
!= null then remove_node
(node
)
190 redef fun remove_at
(i
)
192 var node
= get_node
(i
)
193 if node
!= null then remove_node
(node
)
203 redef fun iterator
: ListIterator[E
] do return new ListIterator[E
](self)
204 redef fun reverse_iterator
: ListIterator[E
] do return new ListReverseIterator[E
](self)
206 # Build an empty list.
209 # Build a list filled with the items of `coll`.
210 init from
(coll
: Collection[E
]) do append
(coll
)
212 # The first node of the list
213 private var head
: nullable ListNode[E
]
215 # The last node of the list
216 private var tail
: nullable ListNode[E
]
218 # Get the `i`th node. get `null` otherwise.
219 private fun get_node
(i
: Int): nullable ListNode[E
]
225 while n
!= null and i
> 0 do
232 # get the first node that contains `e` after 'after', null otherwise
233 private fun search_node_after
(e
: E
, after
: nullable ListNode[E
]): nullable ListNode[E
]
236 while n
!= null and n
.item
!= e
do n
= n
.next
240 # Remove the node (ie. atach prev and next)
241 private fun remove_node
(node
: ListNode[E
])
243 if node
.prev
== null then
245 if node
.next
== null then
248 node
.next
.prev
= null
250 else if node
.next
== null then
252 node
.prev
.next
= null
254 node
.prev
.next
= node
.next
255 node
.next
.prev
= node
.prev
259 private fun insert_before
(element
: E
, node
: ListNode[E
])
261 var nnode
= new ListNode[E
](element
)
274 # This is the iterator class of List
275 class ListIterator[E
]
276 super IndexedIterator[E
]
277 redef fun item
do return _node
.item
279 fun item
=(e
: E
) do _node
.item
= e
281 redef fun is_ok
do return not _node
== null
289 # Build a new iterator for `list`.
290 private init(list
: List[E
])
298 private var list
: List[E
]
300 # The current node of the list
301 private var node
: nullable ListNode[E
]
303 # The index of the current node
306 # Remove the current item
309 _list
.remove_node
(_node
.as(not null))
312 # Insert before the current item
313 fun insert_before
(element
: E
)
315 _list
.insert_before
(element
, _node
.as(not null))
319 private class ListReverseIterator[E
]
320 super ListIterator[E
]
328 private init(list
: List[E
])
336 # Linked nodes that constitute a linked list.
337 private class ListNode[E
]
345 var next
: nullable ListNode[E
]
348 var prev
: nullable ListNode[E
]