author | Claus Gittinger <cg@exept.de> |
Thu, 05 Jul 2007 13:23:33 +0200 | |
changeset 10647 | 9345efb77a76 |
parent 9118 | 192331be0626 |
child 13150 | e20625aa25a7 |
child 17711 | 39faaaf888b4 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
159 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
13 |
"{ Package: 'stx:libbasic' }" |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
14 |
|
1 | 15 |
SequenceableCollection subclass:#LinkedList |
1110 | 16 |
instanceVariableNames:'firstLink lastLink numberOfNodes' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
1364 | 19 |
category:'Collections-Linked' |
1 | 20 |
! |
21 |
||
88 | 22 |
!LinkedList class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
159 | 27 |
All Rights Reserved |
1 | 28 |
|
88 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
1 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
40 |
this class implements an anchor to a list of Links. |
|
10647 | 41 |
The data itself is held in the link elements. |
243 | 42 |
See (the abstract) Link, ValueLink and (possibly other) classes, |
43 |
which can be used as elements of a linkedList. |
|
44 |
||
360 | 45 |
LinkedList does not care for storage; all it does is handling |
46 |
chained link elements, which must respond to #nextLink/#nextLink:. |
|
47 |
(i.e. any object which can do this, can be used as elements of a linked |
|
10647 | 48 |
list). |
2146 | 49 |
An abstract superclass for linkElements is Link; a concrete class is |
50 |
ValueLink, which holds a reference to some object. |
|
51 |
||
52 |
[warning:] |
|
10647 | 53 |
Be careful when subclassing Link, since there is a big drawback, |
54 |
which may be overlooked by beginners: |
|
55 |
a Link element can only be in one LinkedList |
|
56 |
- adding the same element to another LinkedList |
|
57 |
will remove it from the first as a side effect. |
|
58 |
Therefore, NEVER simply add something to a linkedList (except for |
|
59 |
valueLinks) unless you know what you do. |
|
60 |
The ST-80 implementors probably wanted this behavior, to move |
|
61 |
processes from the waitingList to runLists and vice versa; |
|
62 |
however, literature seems to not point this out enough. |
|
360 | 63 |
|
243 | 64 |
Although LinkedList is a subclass of SequenceableCollection (and therefore |
65 |
supports indexed access via at:), you should be careful in using it or |
|
10647 | 66 |
other methods based upon at:. |
360 | 67 |
The reason is that #at: walks the linkedlist to find the indexed element |
10647 | 68 |
and is therefore slow. |
69 |
This means that some linear-in-time algorithms inherited from |
|
360 | 70 |
SequenceableCollection become square in runtime. |
243 | 71 |
In general, if you need access via a numeric index, you better use Array, |
72 |
OrderedCollection or similar. |
|
360 | 73 |
|
74 |
For the above reasons, the system does not make heavily use of LinkedLists; |
|
75 |
the only good application is where elements must be repeatedly be removed |
|
76 |
at the front and added at the end. |
|
77 |
(the schedulers process handling code does this to manage process lists.) |
|
1290 | 78 |
|
4063 | 79 |
[memory requirements:] |
10647 | 80 |
(OBJ-HEADER + (3 * ptr-size)) * size |
81 |
+ any additional instvars due to subclassing |
|
4063 | 82 |
|
1290 | 83 |
[author:] |
10647 | 84 |
Claus Gittinger |
2146 | 85 |
|
86 |
[see also:] |
|
10647 | 87 |
Link ValueLink Process |
360 | 88 |
" |
89 |
! |
|
90 |
||
10647 | 91 |
examples |
360 | 92 |
" |
10647 | 93 |
[exBegin] |
360 | 94 |
|l| |
95 |
||
96 |
l := LinkedList new. |
|
97 |
l addLast:(ValueLink new value:'one'). |
|
98 |
l addLast:(ValueLink new value:'two'). |
|
99 |
l addLast:(ValueLink new value:'three'). |
|
100 |
l addLast:(ValueLink new value:'four'). |
|
101 |
l inspect |
|
10647 | 102 |
[exEnd] |
360 | 103 |
|
104 |
||
10647 | 105 |
[exBegin] |
360 | 106 |
|l| |
107 |
||
108 |
l := LinkedList new. |
|
109 |
l addLast:(ValueLink new value:'one'). |
|
110 |
l addLast:(ValueLink new value:'two'). |
|
111 |
l addLast:(ValueLink new value:'three'). |
|
112 |
l addLast:(ValueLink new value:'four'). |
|
113 |
(l at:3) value inspect. 'slow operation for large lists'. |
|
10647 | 114 |
[exEnd] |
360 | 115 |
|
116 |
||
10647 | 117 |
[exBegin] |
360 | 118 |
|l link| |
119 |
||
120 |
l := LinkedList new. |
|
121 |
l addLast:(ValueLink new value:'one'). |
|
122 |
l addLast:(ValueLink new value:'two'). |
|
123 |
l addLast:(ValueLink new value:'three'). |
|
124 |
l addLast:(ValueLink new value:'four'). |
|
125 |
link := l removeFirst. |
|
126 |
l addLast:link. |
|
10647 | 127 |
l inspect. |
128 |
[exEnd] |
|
88 | 129 |
" |
130 |
! ! |
|
1 | 131 |
|
132 |
!LinkedList class methodsFor:'instance creation'! |
|
133 |
||
134 |
new |
|
135 |
"create and return a new LinkedList" |
|
136 |
||
4300 | 137 |
^ self basicNew initialize |
1 | 138 |
! ! |
139 |
||
140 |
!LinkedList methodsFor:'accessing'! |
|
141 |
||
159 | 142 |
at:index |
143 |
"return the n'th element - use of this method should be avoided, |
|
144 |
since it is slow to walk through the list - think about using |
|
9118 | 145 |
another collection if you need indexed access. |
241 | 146 |
Notice, that many methods in SeqColl are based on at:-access, |
147 |
so other inherited methods may be very slow (showing square runtime)." |
|
159 | 148 |
|
9117 | 149 |
^ self at:index ifAbsent:[ self subscriptBoundsError:index] |
150 |
! |
|
151 |
||
152 |
at:index ifAbsent:exceptionBlock |
|
153 |
"return the n'th element - use of this method should be avoided, |
|
154 |
since it is slow to walk through the list - think about using |
|
9118 | 155 |
another collection if you need indexed access. |
9117 | 156 |
Notice, that many methods in SeqColl are based on at:-access, |
157 |
so other inherited methods may be very slow (showing square runtime)." |
|
158 |
||
159 | 159 |
|theLink |
160 |
runIndex "{Class: SmallInteger}"| |
|
161 |
||
162 |
theLink := firstLink. |
|
163 |
runIndex := 1. |
|
243 | 164 |
[runIndex == index] whileFalse:[ |
10647 | 165 |
theLink isNil ifTrue:[^ exceptionBlock value]. |
166 |
theLink := theLink nextLink. |
|
167 |
runIndex := runIndex + 1. |
|
159 | 168 |
]. |
169 |
^ theLink |
|
606 | 170 |
! |
171 |
||
172 |
first |
|
173 |
"return the first node in the list" |
|
174 |
||
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
175 |
firstLink isNil ifTrue:[^ self emptyCollectionError]. |
606 | 176 |
^ firstLink |
177 |
! |
|
178 |
||
10647 | 179 |
firstIfEmpty:exceptionalValue |
180 |
"return the first node in the list or exceptionlValue, if empty" |
|
181 |
||
182 |
firstLink isNil ifTrue:[^ exceptionalValue value]. |
|
183 |
^ firstLink |
|
184 |
! |
|
185 |
||
606 | 186 |
last |
187 |
"return last node in the list" |
|
188 |
||
189 |
lastLink isNil ifTrue:[self emptyCollectionError]. |
|
190 |
^ lastLink |
|
191 |
! ! |
|
192 |
||
193 |
!LinkedList methodsFor:'adding & removing'! |
|
194 |
||
195 |
add:aLink |
|
196 |
"adds aLink to the end of the sequence. Returns aLink" |
|
197 |
||
198 |
aLink nextLink:nil. |
|
199 |
lastLink isNil ifTrue:[ |
|
200 |
firstLink := aLink |
|
201 |
] ifFalse: [ |
|
202 |
lastLink nextLink:aLink |
|
203 |
]. |
|
204 |
lastLink := aLink. |
|
205 |
numberOfNodes := numberOfNodes + 1. |
|
206 |
^ aLink |
|
207 |
! |
|
208 |
||
209 |
add:linkToAdd after:aLink |
|
210 |
"adds linkToAdd after another link, aLink. If aLink is nil, |
|
211 |
linkToAdd is inserted at the beginning. Returns linkToAdd." |
|
212 |
||
213 |
|this| |
|
214 |
||
215 |
aLink isNil ifTrue:[^ self addFirst:linkToAdd ]. |
|
216 |
||
217 |
this := firstLink. |
|
218 |
[this notNil and:[this ~~ aLink]] whileTrue:[ |
|
219 |
this := this nextLink |
|
220 |
]. |
|
221 |
this isNil ifTrue:[^ self add:linkToAdd ]. |
|
222 |
linkToAdd nextLink:(this nextLink). |
|
223 |
this nextLink:linkToAdd. |
|
10647 | 224 |
numberOfNodes := numberOfNodes + 1. |
606 | 225 |
^ linkToAdd |
226 |
! |
|
227 |
||
228 |
addFirst:aLink |
|
229 |
"adds aLink to the beginning of the sequence. Returns aLink" |
|
230 |
||
231 |
firstLink isNil ifTrue:[ |
|
10647 | 232 |
lastLink := aLink. |
606 | 233 |
]. |
10647 | 234 |
aLink nextLink:firstLink. |
235 |
firstLink := aLink. |
|
606 | 236 |
numberOfNodes := numberOfNodes + 1. |
237 |
^ aLink |
|
238 |
! |
|
239 |
||
240 |
remove:aLink ifAbsent:exceptionBlock |
|
10647 | 241 |
"remove the argument, aLink from the sequence and return it; |
2350 | 242 |
if absent, evaluate the exceptionBlock. |
243 |
Actually this is really a #removeIdentical (but for compatibility ...)" |
|
606 | 244 |
|
245 |
|prevNode nextNode thisNode| |
|
246 |
||
247 |
thisNode := firstLink. |
|
248 |
[thisNode notNil] whileTrue:[ |
|
10647 | 249 |
nextNode := thisNode nextLink. |
250 |
(thisNode == aLink) ifTrue:[ |
|
251 |
prevNode isNil ifTrue:[ |
|
252 |
firstLink := nextNode |
|
253 |
] ifFalse:[ |
|
254 |
prevNode nextLink:nextNode |
|
255 |
]. |
|
256 |
nextNode isNil ifTrue:[ |
|
257 |
lastLink := prevNode |
|
258 |
]. |
|
259 |
numberOfNodes := numberOfNodes - 1. |
|
260 |
thisNode nextLink:nil. |
|
261 |
^ aLink |
|
262 |
]. |
|
263 |
prevNode := thisNode. |
|
264 |
thisNode := nextNode |
|
606 | 265 |
]. |
266 |
^ exceptionBlock value |
|
2349 | 267 |
|
2350 | 268 |
"Modified: 1.2.1997 / 12:01:59 / cg" |
606 | 269 |
! |
270 |
||
1110 | 271 |
removeAll |
1164 | 272 |
"remove all elements from the sequence. Returns the receiver." |
1110 | 273 |
|
274 |
firstLink := lastLink := nil. |
|
275 |
numberOfNodes := 0 |
|
276 |
||
277 |
"Created: 21.3.1996 / 15:24:38 / cg" |
|
1164 | 278 |
"Modified: 12.4.1996 / 13:34:53 / cg" |
1110 | 279 |
! |
280 |
||
606 | 281 |
removeFirst |
282 |
"remove and return the first node from the sequence" |
|
283 |
||
284 |
|link| |
|
285 |
||
286 |
firstLink isNil ifTrue:[ |
|
10647 | 287 |
^ self emptyCollectionError |
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
288 |
]. |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
289 |
link := firstLink. |
10647 | 290 |
firstLink := firstLink nextLink. |
291 |
firstLink isNil ifTrue:[ |
|
292 |
lastLink := nil |
|
606 | 293 |
]. |
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
294 |
link nextLink:nil. |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
295 |
numberOfNodes := numberOfNodes - 1. |
606 | 296 |
^ link |
297 |
! ! |
|
298 |
||
299 |
!LinkedList methodsFor:'enumerating'! |
|
300 |
||
301 |
do:aBlock |
|
302 |
"evaluate the argument, aBlock with 1 arg for every element in the list" |
|
303 |
||
304 |
|thisNode| |
|
305 |
||
306 |
thisNode := firstLink. |
|
307 |
[thisNode notNil] whileTrue:[ |
|
308 |
aBlock value:thisNode. |
|
309 |
thisNode := thisNode nextLink |
|
310 |
] |
|
311 |
! ! |
|
312 |
||
5244 | 313 |
!LinkedList methodsFor:'initialization'! |
606 | 314 |
|
315 |
initialize |
|
316 |
numberOfNodes := 0 |
|
93 | 317 |
! ! |
318 |
||
319 |
!LinkedList methodsFor:'queries'! |
|
320 |
||
606 | 321 |
size |
322 |
"return the size of the LinkedList i.e. the number of nodes" |
|
323 |
||
324 |
^ numberOfNodes |
|
1 | 325 |
! ! |
326 |
||
327 |
!LinkedList methodsFor:'testing'! |
|
328 |
||
10647 | 329 |
identityIndexOf:aLink startingAt:start |
243 | 330 |
"search the collection for aLink, starting the search at index start; |
331 |
if found, return the index otherwise return 0. Here, index is defined |
|
332 |
as the link-nodes position in the list. |
|
10647 | 333 |
The comparison is done using == |
243 | 334 |
(i.e. equality test - not identity test)." |
335 |
||
336 |
|theNode idx "{ Class: SmallInteger }"| |
|
337 |
||
338 |
theNode := firstLink. |
|
339 |
idx := 1. |
|
340 |
[idx < start] whileTrue:[ |
|
341 |
theNode isNil ifTrue:[^ 0]. "reached the end" |
|
342 |
theNode := theNode nextLink. |
|
343 |
idx := idx + 1. |
|
344 |
]. |
|
345 |
[theNode notNil] whileTrue:[ |
|
346 |
(aLink == theNode) ifTrue:[^ idx]. |
|
347 |
theNode := theNode nextLink. |
|
348 |
idx := idx + 1. |
|
349 |
]. "reached the end" |
|
350 |
^ 0 |
|
351 |
||
352 |
" |
|
353 |
|l| |
|
354 |
||
355 |
l := LinkedList new. |
|
10647 | 356 |
l indexOf:'hello' |
243 | 357 |
" |
358 |
||
359 |
" |
|
360 |
|l v| |
|
361 |
||
362 |
l := LinkedList new. |
|
363 |
l add:(ValueLink new value:'one'). |
|
364 |
l add:(ValueLink new value:'two'). |
|
365 |
l add:(v := ValueLink new value:'hello'). |
|
10647 | 366 |
l identityIndexOf:v |
243 | 367 |
" |
1 | 368 |
! |
369 |
||
10647 | 370 |
indexOf:aLink startingAt:start |
606 | 371 |
"search the collection for aLink, starting the search at index start; |
372 |
if found, return the index otherwise return 0. Here, index is defined |
|
373 |
as the link-nodes position in the list. |
|
374 |
The comparison is done using = (i.e. equality test - not identity test)." |
|
1 | 375 |
|
606 | 376 |
|theNode idx "{ Class: SmallInteger }"| |
1 | 377 |
|
606 | 378 |
theNode := firstLink. |
379 |
idx := 1. |
|
380 |
[idx < start] whileTrue:[ |
|
381 |
theNode isNil ifTrue:[^ 0]. "reached the end" |
|
382 |
theNode := theNode nextLink. |
|
383 |
idx := idx + 1. |
|
384 |
]. |
|
385 |
[theNode notNil] whileTrue:[ |
|
386 |
(aLink = theNode) ifTrue:[^ idx]. |
|
387 |
theNode := theNode nextLink. |
|
388 |
idx := idx + 1. |
|
389 |
]. "reached the end" |
|
390 |
^ 0 |
|
1 | 391 |
|
606 | 392 |
" |
393 |
|l| |
|
394 |
||
395 |
l := LinkedList new. |
|
10647 | 396 |
l indexOf:'hello' |
606 | 397 |
" |
398 |
||
399 |
" |
|
400 |
|l v| |
|
401 |
||
402 |
l := LinkedList new. |
|
403 |
l add:(ValueLink new value:'one'). |
|
404 |
l add:(ValueLink new value:'two'). |
|
405 |
l add:(v := ValueLink new value:'hello'). |
|
10647 | 406 |
l indexOf:v |
606 | 407 |
" |
5556 | 408 |
! |
409 |
||
410 |
isEmpty |
|
411 |
"return true, if the collection is empty" |
|
412 |
||
413 |
^ firstLink isNil |
|
414 |
! |
|
415 |
||
416 |
notEmpty |
|
417 |
"return true, if the collection is not empty" |
|
418 |
||
419 |
^ firstLink notNil |
|
1 | 420 |
! ! |
421 |
||
629 | 422 |
!LinkedList class methodsFor:'documentation'! |
423 |
||
424 |
version |
|
10647 | 425 |
^ '$Header: /cvs/stx/stx/libbasic/LinkedList.st,v 1.37 2007-07-05 11:23:33 cg Exp $' |
629 | 426 |
! ! |