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