author | Claus Gittinger <cg@exept.de> |
Thu, 09 Jun 2016 17:14:41 +0200 | |
changeset 19976 | 64c8185fd0de |
parent 19975 | a2fa3e857c74 |
child 20079 | 8d884971c2ed |
child 20231 | 27fdecf3cd79 |
permissions | -rw-r--r-- |
19975 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
5 | 4 |
COPYRIGHT (c) 1989 by Claus Gittinger |
159 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
15 |
|
17262 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1 | 18 |
SequenceableCollection subclass:#LinkedList |
1110 | 19 |
instanceVariableNames:'firstLink lastLink numberOfNodes' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
1364 | 22 |
category:'Collections-Linked' |
1 | 23 |
! |
24 |
||
88 | 25 |
!LinkedList class methodsFor:'documentation'! |
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
159 | 30 |
All Rights Reserved |
1 | 31 |
|
88 | 32 |
This software is furnished under a license and may be used |
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
1 | 40 |
|
88 | 41 |
documentation |
42 |
" |
|
43 |
this class implements an anchor to a list of Links. |
|
10647 | 44 |
The data itself is held in the link elements. |
243 | 45 |
See (the abstract) Link, ValueLink and (possibly other) classes, |
46 |
which can be used as elements of a linkedList. |
|
47 |
||
360 | 48 |
LinkedList does not care for storage; all it does is handling |
49 |
chained link elements, which must respond to #nextLink/#nextLink:. |
|
17123 | 50 |
(i.e. any object which can do this, can be used as elements of a linked list). |
2146 | 51 |
An abstract superclass for linkElements is Link; a concrete class is |
52 |
ValueLink, which holds a reference to some object. |
|
53 |
||
54 |
[warning:] |
|
13727 | 55 |
Be careful when subclassing Link, since there is a big drawback, |
56 |
which may be overlooked by beginners: |
|
17123 | 57 |
a Link element can ONLY be in one LinkedList at a time |
13727 | 58 |
- adding the same element to another LinkedList |
17123 | 59 |
will remove it from the first as a side effect. |
13727 | 60 |
Therefore, NEVER simply add something to a linkedList (except for |
61 |
valueLinks) unless you know what you do. |
|
62 |
The ST-80 implementors probably wanted this behavior, to move |
|
63 |
processes from the waitingList to runLists and vice versa; |
|
64 |
however, literature seems to not point this out enough. |
|
360 | 65 |
|
243 | 66 |
Although LinkedList is a subclass of SequenceableCollection (and therefore |
67 |
supports indexed access via at:), you should be careful in using it or |
|
10647 | 68 |
other methods based upon at:. |
360 | 69 |
The reason is that #at: walks the linkedlist to find the indexed element |
10647 | 70 |
and is therefore slow. |
71 |
This means that some linear-in-time algorithms inherited from |
|
360 | 72 |
SequenceableCollection become square in runtime. |
243 | 73 |
In general, if you need access via a numeric index, you better use Array, |
74 |
OrderedCollection or similar. |
|
360 | 75 |
|
76 |
For the above reasons, the system does not make heavily use of LinkedLists; |
|
77 |
the only good application is where elements must be repeatedly be removed |
|
78 |
at the front and added at the end. |
|
17123 | 79 |
(the scheduler's process handling code does this to manage process lists.) |
1290 | 80 |
|
4063 | 81 |
[memory requirements:] |
13727 | 82 |
(OBJ-HEADER + (3 * ptr-size)) * size |
83 |
+ any additional instvars due to subclassing |
|
4063 | 84 |
|
1290 | 85 |
[author:] |
13727 | 86 |
Claus Gittinger (July 1993) |
2146 | 87 |
|
88 |
[see also:] |
|
13727 | 89 |
Link ValueLink Process |
360 | 90 |
" |
91 |
! |
|
92 |
||
10647 | 93 |
examples |
360 | 94 |
" |
19970 | 95 |
[exBegin] |
96 |
|l| |
|
97 |
||
98 |
l := LinkedList new. |
|
99 |
l addLast:'one'. |
|
100 |
l addLast:'two'. |
|
101 |
l addLast:'three'. |
|
102 |
l addLast:'four'. |
|
103 |
l inspect |
|
104 |
[exEnd] |
|
105 |
||
106 |
[exBegin] |
|
360 | 107 |
|l| |
108 |
||
109 |
l := LinkedList new. |
|
110 |
l addLast:(ValueLink new value:'one'). |
|
111 |
l addLast:(ValueLink new value:'two'). |
|
112 |
l addLast:(ValueLink new value:'three'). |
|
113 |
l addLast:(ValueLink new value:'four'). |
|
114 |
l inspect |
|
19970 | 115 |
[exEnd] |
360 | 116 |
|
19970 | 117 |
[exBegin] |
360 | 118 |
|l| |
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 |
(l at:3) value inspect. 'slow operation for large lists'. |
|
19970 | 126 |
[exEnd] |
360 | 127 |
|
128 |
||
19970 | 129 |
[exBegin] |
360 | 130 |
|l link| |
131 |
||
132 |
l := LinkedList new. |
|
133 |
l addLast:(ValueLink new value:'one'). |
|
134 |
l addLast:(ValueLink new value:'two'). |
|
135 |
l addLast:(ValueLink new value:'three'). |
|
136 |
l addLast:(ValueLink new value:'four'). |
|
137 |
link := l removeFirst. |
|
138 |
l addLast:link. |
|
10647 | 139 |
l inspect. |
19970 | 140 |
[exEnd] |
88 | 141 |
" |
142 |
! ! |
|
1 | 143 |
|
144 |
!LinkedList class methodsFor:'instance creation'! |
|
145 |
||
146 |
new |
|
147 |
"create and return a new LinkedList" |
|
148 |
||
4300 | 149 |
^ self basicNew initialize |
1 | 150 |
! ! |
151 |
||
152 |
!LinkedList methodsFor:'accessing'! |
|
153 |
||
159 | 154 |
at:index |
19970 | 155 |
"return the n'th value - use of this method should be avoided, |
159 | 156 |
since it is slow to walk through the list - think about using |
9118 | 157 |
another collection if you need indexed access. |
17575 | 158 |
Notice: |
159 |
that many methods in SeqColl are based on at:-access, |
|
19970 | 160 |
so other inherited methods may be very slow (showing O^2 runtime). |
17575 | 161 |
It is a very bad idea to access LinkedList elements by index. |
162 |
many algorithms degenerate to poor performance if you do. |
|
163 |
This method is provided for protocol completeness, |
|
164 |
but please consider using another type of collection if you use it" |
|
159 | 165 |
|
9117 | 166 |
^ self at:index ifAbsent:[ self subscriptBoundsError:index] |
167 |
! |
|
168 |
||
19975 | 169 |
at:index ifAbsent:exceptionValue |
19970 | 170 |
"return the n'th value - use of this method should be avoided, |
9117 | 171 |
since it is slow to walk through the list - think about using |
9118 | 172 |
another collection if you need indexed access. |
17575 | 173 |
Notice: |
174 |
that many methods in SeqColl are based on at:-access, |
|
19970 | 175 |
so other inherited methods may be very slow (showing O^2 runtime). |
17575 | 176 |
It is a very bad idea to access LinkedList elements by index. |
177 |
many algorithms degenerate to poor performance if you do. |
|
178 |
This method is provided for protocol completeness, |
|
179 |
but please consider using another type of collection if you use it" |
|
9117 | 180 |
|
19975 | 181 |
|theLink| |
159 | 182 |
|
19975 | 183 |
theLink := self linkAt:index ifAbsent:[^ exceptionValue value]. |
19970 | 184 |
^ theLink value |
606 | 185 |
! |
186 |
||
187 |
first |
|
19970 | 188 |
"return the first value in the list" |
606 | 189 |
|
19970 | 190 |
^ self firstLink value |
606 | 191 |
! |
192 |
||
10647 | 193 |
firstIfEmpty:exceptionalValue |
19970 | 194 |
"return the first value in the list or exceptionlValue, if empty" |
17262 | 195 |
|
196 |
firstLink isNil ifTrue:[^ exceptionalValue value]. |
|
19970 | 197 |
^ firstLink value |
17262 | 198 |
! |
199 |
||
200 |
firstLink |
|
201 |
"return the first node in the list" |
|
202 |
||
203 |
firstLink isNil ifTrue:[^ self emptyCollectionError]. |
|
204 |
^ firstLink |
|
205 |
! |
|
206 |
||
207 |
firstLinkIfEmpty:exceptionalValue |
|
208 |
"return the first node in the list or exceptionlValue, if empty" |
|
209 |
||
10647 | 210 |
firstLink isNil ifTrue:[^ exceptionalValue value]. |
211 |
^ firstLink |
|
212 |
! |
|
213 |
||
606 | 214 |
last |
19970 | 215 |
"return last value in the list" |
17262 | 216 |
|
17274 | 217 |
lastLink isNil ifTrue:[^ self emptyCollectionError]. |
218 |
^ lastLink value |
|
17262 | 219 |
! |
220 |
||
221 |
lastLink |
|
222 |
"return last node in the list" |
|
223 |
||
606 | 224 |
lastLink isNil ifTrue:[self emptyCollectionError]. |
225 |
^ lastLink |
|
17272 | 226 |
! |
227 |
||
19970 | 228 |
linkAt:index ifAbsent:exceptionBlock |
229 |
"return the n'th link - use of this method should be avoided, |
|
17272 | 230 |
since it is slow to walk through the list - think about using |
231 |
another collection if you need indexed access. |
|
17575 | 232 |
Notice: |
233 |
that many methods in SeqColl are based on at:-access, |
|
19970 | 234 |
so other inherited methods may be very slow (showing O^2 runtime). |
17575 | 235 |
It is a very bad idea to access LinkedList elements by index. |
236 |
many algorithms degenerate to poor performance if you do. |
|
237 |
This method is provided for protocol completeness, |
|
238 |
but please consider using another type of collection if you use it" |
|
17272 | 239 |
|
240 |
|theLink |
|
241 |
runIndex "{Class: SmallInteger}"| |
|
242 |
||
243 |
theLink := firstLink. |
|
244 |
runIndex := 1. |
|
245 |
[runIndex == index] whileFalse:[ |
|
246 |
theLink isNil ifTrue:[^ exceptionBlock value]. |
|
247 |
theLink := theLink nextLink. |
|
248 |
runIndex := runIndex + 1. |
|
249 |
]. |
|
250 |
^ theLink |
|
606 | 251 |
! ! |
252 |
||
253 |
!LinkedList methodsFor:'adding & removing'! |
|
254 |
||
17262 | 255 |
add:aLinkOrAnyOtherObject |
606 | 256 |
"adds aLink to the end of the sequence. Returns aLink" |
257 |
||
17262 | 258 |
|newLink| |
259 |
||
260 |
newLink := aLinkOrAnyOtherObject asLink. |
|
261 |
||
262 |
newLink nextLink:nil. |
|
606 | 263 |
lastLink isNil ifTrue:[ |
17262 | 264 |
firstLink := newLink |
606 | 265 |
] ifFalse: [ |
17262 | 266 |
lastLink nextLink:newLink |
606 | 267 |
]. |
17262 | 268 |
lastLink := newLink. |
606 | 269 |
numberOfNodes := numberOfNodes + 1. |
17262 | 270 |
^ newLink |
606 | 271 |
! |
272 |
||
19970 | 273 |
add:aLinkOrAnyOtherObject after:aLinkOrValue |
274 |
"adds aLinkOrAnyOtherObject after another aLinkOrValue. |
|
275 |
If aLinkOrValue is nil, linkToAdd is inserted at the beginning. |
|
276 |
If aLinkOrValue is not in the list, linkToAdd is added at the end. |
|
277 |
Returns aLinkOrAnyOtherObject." |
|
606 | 278 |
|
19970 | 279 |
|this linkToAdd linkOrValue| |
606 | 280 |
|
19970 | 281 |
aLinkOrValue isNil ifTrue:[^ self addFirst:aLinkOrAnyOtherObject ]. |
17262 | 282 |
|
283 |
linkToAdd := aLinkOrAnyOtherObject asLink. |
|
19970 | 284 |
linkOrValue := aLinkOrValue value. |
285 |
||
606 | 286 |
this := firstLink. |
19970 | 287 |
[this notNil and:[this ~~ linkOrValue]] whileTrue:[ |
17262 | 288 |
this := this nextLink |
606 | 289 |
]. |
290 |
this isNil ifTrue:[^ self add:linkToAdd ]. |
|
291 |
linkToAdd nextLink:(this nextLink). |
|
292 |
this nextLink:linkToAdd. |
|
10647 | 293 |
numberOfNodes := numberOfNodes + 1. |
606 | 294 |
^ linkToAdd |
295 |
! |
|
296 |
||
17262 | 297 |
addFirst:aLinkOrAnyOtherObject |
606 | 298 |
"adds aLink to the beginning of the sequence. Returns aLink" |
299 |
||
17262 | 300 |
|linkToAdd| |
301 |
||
302 |
linkToAdd := aLinkOrAnyOtherObject asLink. |
|
303 |
||
606 | 304 |
firstLink isNil ifTrue:[ |
17262 | 305 |
lastLink := linkToAdd. |
606 | 306 |
]. |
17262 | 307 |
linkToAdd nextLink:firstLink. |
308 |
firstLink := linkToAdd. |
|
606 | 309 |
numberOfNodes := numberOfNodes + 1. |
17262 | 310 |
^ linkToAdd |
606 | 311 |
! |
312 |
||
19970 | 313 |
remove:aLinkOrValue ifAbsent:exceptionBlock |
314 |
"remove the argument, aLinkOrValue from the sequence and return it; |
|
315 |
if absent, evaluate the exceptionBlock." |
|
316 |
||
317 |
|prevNode nextNode thisNode linkOrValue| |
|
606 | 318 |
|
19970 | 319 |
linkOrValue := aLinkOrValue value. |
320 |
thisNode := firstLink. |
|
321 |
[thisNode notNil] whileTrue:[ |
|
322 |
nextNode := thisNode nextLink. |
|
323 |
(thisNode value = linkOrValue) ifTrue:[ |
|
324 |
prevNode isNil ifTrue:[ |
|
325 |
firstLink := nextNode |
|
326 |
] ifFalse:[ |
|
327 |
prevNode nextLink:nextNode |
|
328 |
]. |
|
329 |
nextNode isNil ifTrue:[ |
|
330 |
lastLink := prevNode |
|
331 |
]. |
|
332 |
numberOfNodes := numberOfNodes - 1. |
|
333 |
thisNode nextLink:nil. |
|
334 |
^ thisNode value |
|
335 |
]. |
|
336 |
prevNode := thisNode. |
|
337 |
thisNode := nextNode |
|
338 |
]. |
|
339 |
^ exceptionBlock value |
|
2349 | 340 |
|
19970 | 341 |
"Created: / 30-11-2010 / 13:38:25 / cg" |
606 | 342 |
! |
343 |
||
1110 | 344 |
removeAll |
1164 | 345 |
"remove all elements from the sequence. Returns the receiver." |
1110 | 346 |
|
347 |
firstLink := lastLink := nil. |
|
348 |
numberOfNodes := 0 |
|
349 |
||
350 |
"Created: 21.3.1996 / 15:24:38 / cg" |
|
1164 | 351 |
"Modified: 12.4.1996 / 13:34:53 / cg" |
1110 | 352 |
! |
353 |
||
606 | 354 |
removeFirst |
355 |
"remove and return the first node from the sequence" |
|
356 |
||
357 |
|link| |
|
358 |
||
359 |
firstLink isNil ifTrue:[ |
|
10647 | 360 |
^ self emptyCollectionError |
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
361 |
]. |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
362 |
link := firstLink. |
10647 | 363 |
firstLink := firstLink nextLink. |
364 |
firstLink isNil ifTrue:[ |
|
365 |
lastLink := nil |
|
606 | 366 |
]. |
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
367 |
link nextLink:nil. |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
368 |
numberOfNodes := numberOfNodes - 1. |
606 | 369 |
^ link |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
370 |
! |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
371 |
|
19970 | 372 |
removeIdentical:aLinkOrValue ifAbsent:exceptionBlock |
373 |
"remove the argument, aLinkOrValue from the sequence and return it; |
|
13163
e4845fce0ada
changed comment: #removeIdentical:ifAbsent:
Stefan Vogel <sv@exept.de>
parents:
13153
diff
changeset
|
374 |
if absent, evaluate the exceptionBlock." |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
375 |
|
19970 | 376 |
|prevNode nextNode thisNode linkOrValue| |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
377 |
|
19970 | 378 |
linkOrValue := aLinkOrValue value. |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
379 |
thisNode := firstLink. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
380 |
[thisNode notNil] whileTrue:[ |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
381 |
nextNode := thisNode nextLink. |
19970 | 382 |
(thisNode value == linkOrValue) ifTrue:[ |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
383 |
prevNode isNil ifTrue:[ |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
384 |
firstLink := nextNode |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
385 |
] ifFalse:[ |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
386 |
prevNode nextLink:nextNode |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
387 |
]. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
388 |
nextNode isNil ifTrue:[ |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
389 |
lastLink := prevNode |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
390 |
]. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
391 |
numberOfNodes := numberOfNodes - 1. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
392 |
thisNode nextLink:nil. |
19970 | 393 |
^ thisNode value |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
394 |
]. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
395 |
prevNode := thisNode. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
396 |
thisNode := nextNode |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
397 |
]. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
398 |
^ exceptionBlock value |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
399 |
|
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
400 |
"Created: / 30-11-2010 / 13:38:25 / cg" |
17274 | 401 |
! |
402 |
||
403 |
removeLast |
|
404 |
"remove the last link element and return it; |
|
405 |
if empty, raise an exception." |
|
406 |
||
407 |
|nextNode thisNode| |
|
408 |
||
409 |
thisNode := firstLink. |
|
410 |
thisNode isNil ifTrue:[ |
|
411 |
^ self emptyCollectionError |
|
412 |
]. |
|
413 |
thisNode == lastLink ifTrue:[ |
|
414 |
firstLink := lastLink := nil. |
|
415 |
numberOfNodes := 0. |
|
416 |
^ thisNode |
|
417 |
]. |
|
418 |
||
419 |
[ |
|
420 |
nextNode := thisNode nextLink. |
|
421 |
nextNode == lastLink ifTrue:[ |
|
422 |
firstLink == lastLink ifTrue:[ |
|
423 |
firstLink := thisNode |
|
424 |
]. |
|
425 |
lastLink := thisNode. |
|
426 |
numberOfNodes := numberOfNodes - 1. |
|
427 |
thisNode nextLink:nil. |
|
428 |
^ nextNode. |
|
429 |
]. |
|
430 |
thisNode := nextNode |
|
431 |
] loop. |
|
606 | 432 |
! ! |
433 |
||
434 |
!LinkedList methodsFor:'enumerating'! |
|
435 |
||
436 |
do:aBlock |
|
19976 | 437 |
"evaluate the argument, aBlock with 1 arg for every value element in the list" |
17274 | 438 |
|
439 |
|thisNode| |
|
440 |
||
441 |
thisNode := firstLink. |
|
442 |
[thisNode notNil] whileTrue:[ |
|
19976 | 443 |
aBlock value:thisNode value. |
17274 | 444 |
thisNode := thisNode nextLink |
445 |
] |
|
446 |
! |
|
447 |
||
448 |
linksDo:aBlock |
|
449 |
"evaluate the argument, aBlock with 1 arg for every link element in the list" |
|
606 | 450 |
|
451 |
|thisNode| |
|
452 |
||
453 |
thisNode := firstLink. |
|
454 |
[thisNode notNil] whileTrue:[ |
|
17274 | 455 |
aBlock value:thisNode. |
456 |
thisNode := thisNode nextLink |
|
606 | 457 |
] |
17406 | 458 |
! |
459 |
||
460 |
printElementsDo:aBlock |
|
461 |
"perform aBlock (1 arg) for all elements. |
|
462 |
Used in #printOn:." |
|
463 |
||
464 |
^ self linksDo:aBlock |
|
606 | 465 |
! ! |
466 |
||
5244 | 467 |
!LinkedList methodsFor:'initialization'! |
606 | 468 |
|
469 |
initialize |
|
470 |
numberOfNodes := 0 |
|
93 | 471 |
! ! |
472 |
||
473 |
!LinkedList methodsFor:'queries'! |
|
474 |
||
606 | 475 |
size |
476 |
"return the size of the LinkedList i.e. the number of nodes" |
|
477 |
||
478 |
^ numberOfNodes |
|
1 | 479 |
! ! |
480 |
||
17209 | 481 |
!LinkedList methodsFor:'searching-equality'! |
482 |
||
19970 | 483 |
indexOf:aLinkOrValue startingAt:start |
484 |
"search the collection for aLinkOrValue, starting the search at index start; |
|
485 |
if found, return the index otherwise return 0. |
|
486 |
Here, index is defined as the link-node's position in the list. |
|
17575 | 487 |
The comparison is done using = (i.e. equality test - not identity test). |
488 |
Warning: |
|
489 |
it is a very bad idea to access LinkedList elements by index. |
|
490 |
many algorithms degenerate to poor performance if you do. |
|
491 |
This method is provided for protocol completeness, |
|
492 |
but please consider using another type of collection if you use it. |
|
493 |
" |
|
17209 | 494 |
|
19970 | 495 |
|theNode idx "{ Class: SmallInteger }" |
496 |
linkOrValue| |
|
17209 | 497 |
|
19970 | 498 |
linkOrValue := aLinkOrValue value. |
17209 | 499 |
theNode := firstLink. |
500 |
idx := 1. |
|
501 |
[idx < start] whileTrue:[ |
|
17575 | 502 |
theNode isNil ifTrue:[^ 0]. "reached the end" |
503 |
theNode := theNode nextLink. |
|
504 |
idx := idx + 1. |
|
17209 | 505 |
]. |
506 |
[theNode notNil] whileTrue:[ |
|
19970 | 507 |
(linkOrValue = theNode value) ifTrue:[^ idx]. |
17575 | 508 |
theNode := theNode nextLink. |
509 |
idx := idx + 1. |
|
17209 | 510 |
]. "reached the end" |
511 |
^ 0 |
|
512 |
||
513 |
" |
|
514 |
|l| |
|
515 |
||
516 |
l := LinkedList new. |
|
517 |
l indexOf:'hello' |
|
518 |
" |
|
519 |
||
520 |
" |
|
521 |
|l v| |
|
522 |
||
523 |
l := LinkedList new. |
|
524 |
l add:(ValueLink new value:'one'). |
|
525 |
l add:(ValueLink new value:'two'). |
|
526 |
l add:(v := ValueLink new value:'hello'). |
|
527 |
l indexOf:v |
|
528 |
" |
|
529 |
! ! |
|
530 |
||
531 |
!LinkedList methodsFor:'searching-identity'! |
|
1 | 532 |
|
19970 | 533 |
identityIndexOf:aLinkOrValue startingAt:start |
534 |
"search the collection for aLinkOrValue, starting the search at index start; |
|
535 |
if found, return the index otherwise return 0. |
|
536 |
Here, index is defined as the link-node's position in the list. |
|
537 |
The comparison is done using == (i.e. identity test - not equality test). |
|
538 |
Warning: |
|
539 |
it is a very bad idea to access LinkedList elements by index. |
|
17575 | 540 |
many algorithms degenerate to poor performance if you do. |
541 |
This method is provided for protocol completeness, |
|
19970 | 542 |
but please consider using another type of collection if you use it. |
543 |
" |
|
243 | 544 |
|
19970 | 545 |
|theNode idx "{ Class: SmallInteger }" |
546 |
linkOrValue| |
|
243 | 547 |
|
19970 | 548 |
linkOrValue := aLinkOrValue value. |
243 | 549 |
theNode := firstLink. |
550 |
idx := 1. |
|
551 |
[idx < start] whileTrue:[ |
|
13150
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
552 |
theNode isNil ifTrue:[^ 0]. "reached the end" |
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
553 |
theNode := theNode nextLink. |
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
554 |
idx := idx + 1. |
243 | 555 |
]. |
556 |
[theNode notNil] whileTrue:[ |
|
19970 | 557 |
(linkOrValue == theNode value) ifTrue:[^ idx]. |
13150
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
558 |
theNode := theNode nextLink. |
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
559 |
idx := idx + 1. |
243 | 560 |
]. "reached the end" |
561 |
^ 0 |
|
562 |
||
563 |
" |
|
564 |
|l| |
|
565 |
||
566 |
l := LinkedList new. |
|
10647 | 567 |
l indexOf:'hello' |
243 | 568 |
" |
569 |
||
570 |
" |
|
571 |
|l v| |
|
572 |
||
573 |
l := LinkedList new. |
|
574 |
l add:(ValueLink new value:'one'). |
|
575 |
l add:(ValueLink new value:'two'). |
|
576 |
l add:(v := ValueLink new value:'hello'). |
|
19970 | 577 |
l indexOf:v |
243 | 578 |
" |
1 | 579 |
! ! |
580 |
||
17210 | 581 |
!LinkedList methodsFor:'testing'! |
582 |
||
583 |
isEmpty |
|
584 |
"return true, if the collection is empty" |
|
585 |
||
586 |
^ firstLink isNil |
|
587 |
! |
|
588 |
||
589 |
isFixedSize |
|
590 |
"return true if the receiver cannot grow" |
|
591 |
||
592 |
^ false |
|
593 |
! |
|
594 |
||
595 |
notEmpty |
|
596 |
"return true, if the collection is not empty" |
|
597 |
||
598 |
^ firstLink notNil |
|
599 |
! ! |
|
600 |
||
629 | 601 |
!LinkedList class methodsFor:'documentation'! |
602 |
||
603 |
version |
|
19970 | 604 |
^ '$Header$' |
13150
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
605 |
! |
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
606 |
|
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
607 |
version_CVS |
19970 | 608 |
^ '$Header$' |
629 | 609 |
! ! |
15428 | 610 |