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.
24 redef fun [](index
) do return get_node
(index
).item
26 redef fun []=(index
, item
) do get_node
(index
).item
= item
29 redef fun first
do return _head
.item
32 redef fun first
=(e
) do _head
.item
= e
35 redef fun last
do return _tail
.item
38 redef fun last
=(e
) do _tail
.item
= e
43 redef fun is_empty
do return _head
== null
58 redef fun has
(e
) do return search_node_after
(e
, _head
) != null
64 if node
.item
!= e
then return false
75 if node
.item
!= e
then nb
+= 1
81 # Return a list of elements between 'from' and 'to'.
82 fun slice
(from
: Int, to
: Int): List[E
] do
83 assert from
>= 0 and from
< length
84 assert to
>= 0 and to
< length
and from
<= to
85 var list
= new List[E
]
98 var node
= new ListNode[E
](e
)
111 var node
= new ListNode[E
](e
)
112 if _head
== null then
122 redef fun insert
(e
, i
)
124 var node
= get_node
(i
)
129 var nnode
= new ListNode[E
](e
)
141 # Append `l` to `self` but clear `l`.
146 if _tail
== null then
148 else if l
._head
!= null then
150 _tail
.next
.prev
= _tail
164 if _tail
== null then
178 if _head
== null then
188 var node
= search_node_after
(e
, _head
)
189 if node
!= null then remove_node
(node
)
192 redef fun remove_at
(i
)
194 var node
= get_node
(i
)
195 if node
!= null then remove_node
(node
)
205 redef fun iterator
: ListIterator[E
] do return new ListIterator[E
](self)
206 redef fun reverse_iterator
: ListIterator[E
] do return new ListReverseIterator[E
](self)
208 # Build an empty list.
211 # Build a list filled with the items of `coll`.
212 init from
(coll
: Collection[E
]) do append
(coll
)
214 # The first node of the list
215 private var head
: nullable ListNode[E
] = null
217 # The last node of the list
218 private var tail
: nullable ListNode[E
] = null
220 # Get the `i`th node. get `null` otherwise.
221 private fun get_node
(i
: Int): nullable ListNode[E
]
227 while n
!= null and i
> 0 do
234 # get the first node that contains `e` after 'after', null otherwise
235 private fun search_node_after
(e
: E
, after
: nullable ListNode[E
]): nullable ListNode[E
]
238 while n
!= null and n
.item
!= e
do n
= n
.next
242 # Remove the node (ie. atach prev and next)
243 private fun remove_node
(node
: ListNode[E
])
245 if node
.prev
== null then
247 if node
.next
== null then
250 node
.next
.prev
= null
252 else if node
.next
== null then
254 node
.prev
.next
= null
256 node
.prev
.next
= node
.next
257 node
.next
.prev
= node
.prev
261 private fun insert_before
(element
: E
, node
: ListNode[E
])
263 var nnode
= new ListNode[E
](element
)
276 # This is the iterator class of List
277 class ListIterator[E
]
278 super IndexedIterator[E
]
279 redef fun item
do return _node
.item
281 # Set item `e` at self `index`.
282 fun item
=(e
: E
) do _node
.item
= e
284 redef fun is_ok
do return not _node
== null
292 # Build a new iterator for `list`.
299 private var list
: List[E
]
301 # The current node of the list
302 private var node
: nullable ListNode[E
] = null
304 # The index of the current node
307 # Remove the current item
310 _list
.remove_node
(_node
.as(not null))
313 # Insert before the current item
314 fun insert_before
(element
: E
)
316 _list
.insert_before
(element
, _node
.as(not null))
320 private class ListReverseIterator[E
]
321 super ListIterator[E
]
337 # Linked nodes that constitute a linked list.
338 private class ListNode[E
]
342 var next
: nullable ListNode[E
] = null
345 var prev
: nullable ListNode[E
] = null