author | Claus Gittinger <cg@exept.de> |
Thu, 05 Aug 2004 14:31:05 +0200 | |
changeset 8464 | e9f4d3766157 |
parent 5556 | 1056cc5d6ce0 |
child 9117 | 62e21d3b40e2 |
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. |
|
360 | 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 |
|
2146 | 48 |
list). |
49 |
An abstract superclass for linkElements is Link; a concrete class is |
|
50 |
ValueLink, which holds a reference to some object. |
|
51 |
||
52 |
[warning:] |
|
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 |
|
360 | 66 |
other methods based upon at:. |
67 |
The reason is that #at: walks the linkedlist to find the indexed element |
|
68 |
and is therefore slow. |
|
69 |
This means that some linear-in-time algorithms inherited from |
|
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:] |
80 |
(OBJ-HEADER + (3 * ptr-size)) * size |
|
81 |
+ any additional instvars due to subclassing |
|
82 |
||
1290 | 83 |
[author:] |
84 |
Claus Gittinger |
|
2146 | 85 |
|
86 |
[see also:] |
|
87 |
Link ValueLink Process |
|
360 | 88 |
" |
89 |
! |
|
90 |
||
91 |
examples |
|
92 |
" |
|
1313 | 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 |
|
1313 | 102 |
[exEnd] |
360 | 103 |
|
104 |
||
1313 | 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'. |
|
1313 | 114 |
[exEnd] |
360 | 115 |
|
116 |
||
1313 | 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. |
|
127 |
l inspect. |
|
1313 | 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 |
|
145 |
another collection if you need index 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 |
|
149 |
|theLink |
|
150 |
runIndex "{Class: SmallInteger}"| |
|
151 |
||
152 |
theLink := firstLink. |
|
153 |
runIndex := 1. |
|
243 | 154 |
[runIndex == index] whileFalse:[ |
155 |
theLink isNil ifTrue:[^ self subscriptBoundsError:index]. |
|
159 | 156 |
theLink := theLink nextLink. |
157 |
runIndex := runIndex + 1. |
|
158 |
]. |
|
159 |
^ theLink |
|
606 | 160 |
! |
161 |
||
162 |
first |
|
163 |
"return the first node in the list" |
|
164 |
||
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
165 |
firstLink isNil ifTrue:[^ self emptyCollectionError]. |
606 | 166 |
^ firstLink |
167 |
! |
|
168 |
||
169 |
last |
|
170 |
"return last node in the list" |
|
171 |
||
172 |
lastLink isNil ifTrue:[self emptyCollectionError]. |
|
173 |
^ lastLink |
|
174 |
! ! |
|
175 |
||
176 |
!LinkedList methodsFor:'adding & removing'! |
|
177 |
||
178 |
add:aLink |
|
179 |
"adds aLink to the end of the sequence. Returns aLink" |
|
180 |
||
181 |
aLink nextLink:nil. |
|
182 |
lastLink isNil ifTrue:[ |
|
183 |
firstLink := aLink |
|
184 |
] ifFalse: [ |
|
185 |
lastLink nextLink:aLink |
|
186 |
]. |
|
187 |
lastLink := aLink. |
|
188 |
numberOfNodes := numberOfNodes + 1. |
|
189 |
^ aLink |
|
190 |
! |
|
191 |
||
192 |
add:linkToAdd after:aLink |
|
193 |
"adds linkToAdd after another link, aLink. If aLink is nil, |
|
194 |
linkToAdd is inserted at the beginning. Returns linkToAdd." |
|
195 |
||
196 |
|this| |
|
197 |
||
198 |
aLink isNil ifTrue:[^ self addFirst:linkToAdd ]. |
|
199 |
||
200 |
this := firstLink. |
|
201 |
[this notNil and:[this ~~ aLink]] whileTrue:[ |
|
202 |
this := this nextLink |
|
203 |
]. |
|
204 |
this isNil ifTrue:[^ self add:linkToAdd ]. |
|
205 |
linkToAdd nextLink:(this nextLink). |
|
206 |
this nextLink:linkToAdd. |
|
207 |
^ linkToAdd |
|
208 |
! |
|
209 |
||
210 |
addFirst:aLink |
|
211 |
"adds aLink to the beginning of the sequence. Returns aLink" |
|
212 |
||
213 |
firstLink isNil ifTrue:[ |
|
214 |
firstLink := aLink. |
|
215 |
lastLink := aLink |
|
216 |
] ifFalse: [ |
|
217 |
aLink nextLink:firstLink. |
|
218 |
firstLink := aLink |
|
219 |
]. |
|
220 |
numberOfNodes := numberOfNodes + 1. |
|
221 |
^ aLink |
|
222 |
! |
|
223 |
||
224 |
remove:aLink ifAbsent:exceptionBlock |
|
2350 | 225 |
"remove the argument, aLink from the sequence and return it; |
226 |
if absent, evaluate the exceptionBlock. |
|
227 |
Actually this is really a #removeIdentical (but for compatibility ...)" |
|
606 | 228 |
|
229 |
|prevNode nextNode thisNode| |
|
230 |
||
231 |
thisNode := firstLink. |
|
232 |
[thisNode notNil] whileTrue:[ |
|
2349 | 233 |
nextNode := thisNode nextLink. |
234 |
(thisNode == aLink) ifTrue:[ |
|
235 |
prevNode isNil ifTrue:[ |
|
236 |
firstLink := thisNode nextLink |
|
237 |
] ifFalse:[ |
|
238 |
prevNode nextLink:(thisNode nextLink) |
|
239 |
]. |
|
240 |
(lastLink == thisNode) ifTrue:[ |
|
241 |
thisNode nextLink isNil ifTrue:[ |
|
242 |
lastLink := prevNode |
|
243 |
] ifFalse:[ |
|
244 |
lastLink := thisNode nextLink |
|
245 |
] |
|
246 |
]. |
|
247 |
numberOfNodes := numberOfNodes - 1. |
|
248 |
thisNode nextLink:nil. |
|
249 |
^ aLink |
|
250 |
]. |
|
251 |
prevNode := thisNode. |
|
252 |
thisNode := nextNode |
|
606 | 253 |
]. |
254 |
^ exceptionBlock value |
|
2349 | 255 |
|
2350 | 256 |
"Modified: 1.2.1997 / 12:01:59 / cg" |
606 | 257 |
! |
258 |
||
1110 | 259 |
removeAll |
1164 | 260 |
"remove all elements from the sequence. Returns the receiver." |
1110 | 261 |
|
262 |
firstLink := lastLink := nil. |
|
263 |
numberOfNodes := 0 |
|
264 |
||
265 |
"Created: 21.3.1996 / 15:24:38 / cg" |
|
1164 | 266 |
"Modified: 12.4.1996 / 13:34:53 / cg" |
1110 | 267 |
! |
268 |
||
606 | 269 |
removeFirst |
270 |
"remove and return the first node from the sequence" |
|
271 |
||
272 |
|link| |
|
273 |
||
274 |
firstLink isNil ifTrue:[ |
|
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
275 |
^ self emptyCollectionError |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
276 |
]. |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
277 |
link := firstLink. |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
278 |
(firstLink == lastLink) ifTrue:[ |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
279 |
firstLink := nil. |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
280 |
lastLink := nil |
606 | 281 |
] ifFalse:[ |
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
282 |
firstLink := firstLink nextLink |
606 | 283 |
]. |
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
284 |
link nextLink:nil. |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
285 |
numberOfNodes := numberOfNodes - 1. |
606 | 286 |
^ link |
287 |
! ! |
|
288 |
||
289 |
!LinkedList methodsFor:'enumerating'! |
|
290 |
||
291 |
do:aBlock |
|
292 |
"evaluate the argument, aBlock with 1 arg for every element in the list" |
|
293 |
||
294 |
|thisNode| |
|
295 |
||
296 |
thisNode := firstLink. |
|
297 |
[thisNode notNil] whileTrue:[ |
|
298 |
aBlock value:thisNode. |
|
299 |
thisNode := thisNode nextLink |
|
300 |
] |
|
301 |
! ! |
|
302 |
||
5244 | 303 |
!LinkedList methodsFor:'initialization'! |
606 | 304 |
|
305 |
initialize |
|
306 |
numberOfNodes := 0 |
|
93 | 307 |
! ! |
308 |
||
309 |
!LinkedList methodsFor:'queries'! |
|
310 |
||
606 | 311 |
size |
312 |
"return the size of the LinkedList i.e. the number of nodes" |
|
313 |
||
314 |
^ numberOfNodes |
|
1 | 315 |
! ! |
316 |
||
317 |
!LinkedList methodsFor:'testing'! |
|
318 |
||
243 | 319 |
identityIndexOf:aLink startingAt:start |
320 |
"search the collection for aLink, starting the search at index start; |
|
321 |
if found, return the index otherwise return 0. Here, index is defined |
|
322 |
as the link-nodes position in the list. |
|
323 |
The comparison is done using == |
|
324 |
(i.e. equality test - not identity test)." |
|
325 |
||
326 |
|theNode idx "{ Class: SmallInteger }"| |
|
327 |
||
328 |
theNode := firstLink. |
|
329 |
idx := 1. |
|
330 |
[idx < start] whileTrue:[ |
|
331 |
theNode isNil ifTrue:[^ 0]. "reached the end" |
|
332 |
theNode := theNode nextLink. |
|
333 |
idx := idx + 1. |
|
334 |
]. |
|
335 |
[theNode notNil] whileTrue:[ |
|
336 |
(aLink == theNode) ifTrue:[^ idx]. |
|
337 |
theNode := theNode nextLink. |
|
338 |
idx := idx + 1. |
|
339 |
]. "reached the end" |
|
340 |
^ 0 |
|
341 |
||
342 |
" |
|
343 |
|l| |
|
344 |
||
345 |
l := LinkedList new. |
|
346 |
l indexOf:'hello' |
|
347 |
" |
|
348 |
||
349 |
" |
|
350 |
|l v| |
|
351 |
||
352 |
l := LinkedList new. |
|
353 |
l add:(ValueLink new value:'one'). |
|
354 |
l add:(ValueLink new value:'two'). |
|
355 |
l add:(v := ValueLink new value:'hello'). |
|
356 |
l identityIndexOf:v |
|
357 |
" |
|
1 | 358 |
! |
359 |
||
606 | 360 |
indexOf:aLink startingAt:start |
361 |
"search the collection for aLink, starting the search at index start; |
|
362 |
if found, return the index otherwise return 0. Here, index is defined |
|
363 |
as the link-nodes position in the list. |
|
364 |
The comparison is done using = (i.e. equality test - not identity test)." |
|
1 | 365 |
|
606 | 366 |
|theNode idx "{ Class: SmallInteger }"| |
1 | 367 |
|
606 | 368 |
theNode := firstLink. |
369 |
idx := 1. |
|
370 |
[idx < start] whileTrue:[ |
|
371 |
theNode isNil ifTrue:[^ 0]. "reached the end" |
|
372 |
theNode := theNode nextLink. |
|
373 |
idx := idx + 1. |
|
374 |
]. |
|
375 |
[theNode notNil] whileTrue:[ |
|
376 |
(aLink = theNode) ifTrue:[^ idx]. |
|
377 |
theNode := theNode nextLink. |
|
378 |
idx := idx + 1. |
|
379 |
]. "reached the end" |
|
380 |
^ 0 |
|
1 | 381 |
|
606 | 382 |
" |
383 |
|l| |
|
384 |
||
385 |
l := LinkedList new. |
|
386 |
l indexOf:'hello' |
|
387 |
" |
|
388 |
||
389 |
" |
|
390 |
|l v| |
|
391 |
||
392 |
l := LinkedList new. |
|
393 |
l add:(ValueLink new value:'one'). |
|
394 |
l add:(ValueLink new value:'two'). |
|
395 |
l add:(v := ValueLink new value:'hello'). |
|
396 |
l indexOf:v |
|
397 |
" |
|
5556 | 398 |
! |
399 |
||
400 |
isEmpty |
|
401 |
"return true, if the collection is empty" |
|
402 |
||
403 |
^ firstLink isNil |
|
404 |
! |
|
405 |
||
406 |
notEmpty |
|
407 |
"return true, if the collection is not empty" |
|
408 |
||
409 |
^ firstLink notNil |
|
1 | 410 |
! ! |
411 |
||
629 | 412 |
!LinkedList class methodsFor:'documentation'! |
413 |
||
414 |
version |
|
5556 | 415 |
^ '$Header: /cvs/stx/stx/libbasic/LinkedList.st,v 1.34 2000-08-22 13:48:28 cg Exp $' |
629 | 416 |
! ! |