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
84 var node
= new ListNode[E
](e
)
97 var node
= new ListNode[E
](e
)
107 # Append `l' to `self' but clear `l'.
112 if _tail
== null then
114 else if l
._head
!= null then
116 _tail
.next
.prev
= _tail
130 if _tail
== null then
144 if _head
== null then
154 var node
= search_node_after
(e
, _head
)
155 if node
!= null then remove_node
(node
)
158 redef fun remove_at
(i
)
160 var node
= get_node
(i
)
161 if node
!= null then remove_node
(node
)
171 redef fun iterator
: ListIterator[E
] do return new ListIterator[E
](self)
173 # Build an empty list.
176 # Build a list filled with the items of `coll'.
177 init from
(coll
: Collection[E
]) do append
(coll
)
179 # The first node of the list
180 var _head
: nullable ListNode[E
]
182 # The last node of the list
183 var _tail
: nullable ListNode[E
]
185 # Get the `i'th node. get `null' otherwise.
186 private fun get_node
(i
: Int): nullable ListNode[E
]
192 while n
!= null and i
> 0 do
199 # get the first node that contains e after 'after', null otherwise
200 private fun search_node_after
(e
: E
, after
: nullable ListNode[E
]): nullable ListNode[E
]
203 while n
!= null and n
.item
!= e
do n
= n
.next
207 # Remove the node (ie. atach prev and next)
208 private fun remove_node
(node
: ListNode[E
])
210 if node
.prev
== null then
212 if node
.next
== null then
215 node
.next
.prev
= null
217 else if node
.next
== null then
219 node
.prev
.next
= null
221 node
.prev
.next
= node
.next
222 node
.next
.prev
= node
.prev
226 private fun insert_before
(element
: E
, node
: ListNode[E
])
228 var nnode
= new ListNode[E
](element
)
241 # This is the iterator class of List
242 class ListIterator[E
]
243 super IndexedIterator[E
]
244 redef fun item
do return _node
.item
246 fun item
=(e
: E
) do _node
.item
= e
248 redef fun is_ok
do return not _node
== null
256 # Build a new iterator from `node'.
257 private init(list
: List[E
])
267 # The current node of the list
268 var _node
: nullable ListNode[E
]
270 # The index of the current node
271 redef readable var _index
: Int
273 # Remove the current item
276 _list
.remove_node
(_node
.as(not null))
279 # Insert before the current item
280 fun insert_before
(element
: E
)
282 _list
.insert_before
(element
, _node
.as(not null))
286 # Linked nodes that constitute a linked list.
287 private class ListNode[E
]
295 readable writable var _next
: nullable ListNode[E
]
298 readable writable var _prev
: nullable ListNode[E
]