author | Claus Gittinger <cg@exept.de> |
Mon, 16 Dec 2019 11:42:04 +0100 | |
changeset 25127 | 22ff3c08a076 |
parent 24614 | e64574a14076 |
permissions | -rw-r--r-- |
24614 | 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 |
20231 | 185 |
|
186 |
" |
|
187 |
|l| |
|
188 |
||
189 |
l := LinkedList new. |
|
190 |
l add:'one'. |
|
191 |
l add:'two'. |
|
192 |
l add:'hello'. |
|
193 |
l at:3 ifAbsent:'missing'. |
|
194 |
l at:4 ifAbsent:'missing'. |
|
195 |
" |
|
606 | 196 |
! |
197 |
||
198 |
first |
|
19970 | 199 |
"return the first value in the list" |
606 | 200 |
|
19970 | 201 |
^ self firstLink value |
606 | 202 |
! |
203 |
||
10647 | 204 |
firstIfEmpty:exceptionalValue |
19970 | 205 |
"return the first value in the list or exceptionlValue, if empty" |
17262 | 206 |
|
207 |
firstLink isNil ifTrue:[^ exceptionalValue value]. |
|
19970 | 208 |
^ firstLink value |
17262 | 209 |
! |
210 |
||
211 |
firstLink |
|
212 |
"return the first node in the list" |
|
213 |
||
214 |
firstLink isNil ifTrue:[^ self emptyCollectionError]. |
|
215 |
^ firstLink |
|
216 |
! |
|
217 |
||
218 |
firstLinkIfEmpty:exceptionalValue |
|
219 |
"return the first node in the list or exceptionlValue, if empty" |
|
220 |
||
10647 | 221 |
firstLink isNil ifTrue:[^ exceptionalValue value]. |
222 |
^ firstLink |
|
223 |
! |
|
224 |
||
606 | 225 |
last |
19970 | 226 |
"return last value in the list" |
17262 | 227 |
|
17274 | 228 |
lastLink isNil ifTrue:[^ self emptyCollectionError]. |
229 |
^ lastLink value |
|
17262 | 230 |
! |
231 |
||
232 |
lastLink |
|
233 |
"return last node in the list" |
|
234 |
||
606 | 235 |
lastLink isNil ifTrue:[self emptyCollectionError]. |
236 |
^ lastLink |
|
17272 | 237 |
! |
238 |
||
19970 | 239 |
linkAt:index ifAbsent:exceptionBlock |
240 |
"return the n'th link - use of this method should be avoided, |
|
17272 | 241 |
since it is slow to walk through the list - think about using |
242 |
another collection if you need indexed access. |
|
17575 | 243 |
Notice: |
20231 | 244 |
that many methods in the superclass, SequenceableCollection are based on at:-access, |
19970 | 245 |
so other inherited methods may be very slow (showing O^2 runtime). |
17575 | 246 |
It is a very bad idea to access LinkedList elements by index. |
247 |
many algorithms degenerate to poor performance if you do. |
|
248 |
This method is provided for protocol completeness, |
|
249 |
but please consider using another type of collection if you use it" |
|
17272 | 250 |
|
251 |
|theLink |
|
20231 | 252 |
count "{Class: SmallInteger}"| |
253 |
||
254 |
count := index. |
|
255 |
(count < 1 or:[count > numberOfNodes]) ifTrue:[ |
|
256 |
^ exceptionBlock value. |
|
257 |
]. |
|
17272 | 258 |
|
259 |
theLink := firstLink. |
|
20231 | 260 |
count-1 timesRepeat:[ |
17272 | 261 |
theLink isNil ifTrue:[^ exceptionBlock value]. |
262 |
theLink := theLink nextLink. |
|
263 |
]. |
|
264 |
^ theLink |
|
606 | 265 |
! ! |
266 |
||
267 |
!LinkedList methodsFor:'adding & removing'! |
|
268 |
||
17262 | 269 |
add:aLinkOrAnyOtherObject |
606 | 270 |
"adds aLink to the end of the sequence. Returns aLink" |
271 |
||
17262 | 272 |
|newLink| |
273 |
||
274 |
newLink := aLinkOrAnyOtherObject asLink. |
|
275 |
||
276 |
newLink nextLink:nil. |
|
606 | 277 |
lastLink isNil ifTrue:[ |
17262 | 278 |
firstLink := newLink |
606 | 279 |
] ifFalse: [ |
17262 | 280 |
lastLink nextLink:newLink |
606 | 281 |
]. |
17262 | 282 |
lastLink := newLink. |
606 | 283 |
numberOfNodes := numberOfNodes + 1. |
17262 | 284 |
^ newLink |
606 | 285 |
! |
286 |
||
19970 | 287 |
add:aLinkOrAnyOtherObject after:aLinkOrValue |
288 |
"adds aLinkOrAnyOtherObject after another aLinkOrValue. |
|
289 |
If aLinkOrValue is nil, linkToAdd is inserted at the beginning. |
|
290 |
If aLinkOrValue is not in the list, linkToAdd is added at the end. |
|
291 |
Returns aLinkOrAnyOtherObject." |
|
606 | 292 |
|
19970 | 293 |
|this linkToAdd linkOrValue| |
606 | 294 |
|
19970 | 295 |
aLinkOrValue isNil ifTrue:[^ self addFirst:aLinkOrAnyOtherObject ]. |
17262 | 296 |
|
297 |
linkToAdd := aLinkOrAnyOtherObject asLink. |
|
19970 | 298 |
linkOrValue := aLinkOrValue value. |
299 |
||
606 | 300 |
this := firstLink. |
19970 | 301 |
[this notNil and:[this ~~ linkOrValue]] whileTrue:[ |
17262 | 302 |
this := this nextLink |
606 | 303 |
]. |
304 |
this isNil ifTrue:[^ self add:linkToAdd ]. |
|
305 |
linkToAdd nextLink:(this nextLink). |
|
306 |
this nextLink:linkToAdd. |
|
10647 | 307 |
numberOfNodes := numberOfNodes + 1. |
606 | 308 |
^ linkToAdd |
309 |
! |
|
310 |
||
17262 | 311 |
addFirst:aLinkOrAnyOtherObject |
606 | 312 |
"adds aLink to the beginning of the sequence. Returns aLink" |
313 |
||
17262 | 314 |
|linkToAdd| |
315 |
||
316 |
linkToAdd := aLinkOrAnyOtherObject asLink. |
|
317 |
||
606 | 318 |
firstLink isNil ifTrue:[ |
17262 | 319 |
lastLink := linkToAdd. |
606 | 320 |
]. |
17262 | 321 |
linkToAdd nextLink:firstLink. |
322 |
firstLink := linkToAdd. |
|
606 | 323 |
numberOfNodes := numberOfNodes + 1. |
17262 | 324 |
^ linkToAdd |
606 | 325 |
! |
326 |
||
19970 | 327 |
remove:aLinkOrValue ifAbsent:exceptionBlock |
328 |
"remove the argument, aLinkOrValue from the sequence and return it; |
|
329 |
if absent, evaluate the exceptionBlock." |
|
330 |
||
331 |
|prevNode nextNode thisNode linkOrValue| |
|
606 | 332 |
|
19970 | 333 |
linkOrValue := aLinkOrValue value. |
334 |
thisNode := firstLink. |
|
335 |
[thisNode notNil] whileTrue:[ |
|
336 |
nextNode := thisNode nextLink. |
|
337 |
(thisNode value = linkOrValue) ifTrue:[ |
|
338 |
prevNode isNil ifTrue:[ |
|
339 |
firstLink := nextNode |
|
340 |
] ifFalse:[ |
|
341 |
prevNode nextLink:nextNode |
|
342 |
]. |
|
343 |
nextNode isNil ifTrue:[ |
|
344 |
lastLink := prevNode |
|
345 |
]. |
|
346 |
numberOfNodes := numberOfNodes - 1. |
|
347 |
thisNode nextLink:nil. |
|
348 |
^ thisNode value |
|
349 |
]. |
|
350 |
prevNode := thisNode. |
|
351 |
thisNode := nextNode |
|
352 |
]. |
|
353 |
^ exceptionBlock value |
|
2349 | 354 |
|
19970 | 355 |
"Created: / 30-11-2010 / 13:38:25 / cg" |
606 | 356 |
! |
357 |
||
1110 | 358 |
removeAll |
1164 | 359 |
"remove all elements from the sequence. Returns the receiver." |
1110 | 360 |
|
361 |
firstLink := lastLink := nil. |
|
362 |
numberOfNodes := 0 |
|
363 |
||
364 |
"Created: 21.3.1996 / 15:24:38 / cg" |
|
1164 | 365 |
"Modified: 12.4.1996 / 13:34:53 / cg" |
1110 | 366 |
! |
367 |
||
606 | 368 |
removeFirst |
369 |
"remove and return the first node from the sequence" |
|
370 |
||
371 |
|link| |
|
372 |
||
373 |
firstLink isNil ifTrue:[ |
|
10647 | 374 |
^ self emptyCollectionError |
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
375 |
]. |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
376 |
link := firstLink. |
10647 | 377 |
firstLink := firstLink nextLink. |
378 |
firstLink isNil ifTrue:[ |
|
379 |
lastLink := nil |
|
606 | 380 |
]. |
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
381 |
link nextLink:nil. |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
382 |
numberOfNodes := numberOfNodes - 1. |
606 | 383 |
^ link |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
384 |
! |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
385 |
|
19970 | 386 |
removeIdentical:aLinkOrValue ifAbsent:exceptionBlock |
387 |
"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
|
388 |
if absent, evaluate the exceptionBlock." |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
389 |
|
19970 | 390 |
|prevNode nextNode thisNode linkOrValue| |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
391 |
|
19970 | 392 |
linkOrValue := aLinkOrValue value. |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
393 |
thisNode := firstLink. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
394 |
[thisNode notNil] whileTrue:[ |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
395 |
nextNode := thisNode nextLink. |
19970 | 396 |
(thisNode value == linkOrValue) ifTrue:[ |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
397 |
prevNode isNil ifTrue:[ |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
398 |
firstLink := nextNode |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
399 |
] ifFalse:[ |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
400 |
prevNode nextLink:nextNode |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
401 |
]. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
402 |
nextNode isNil ifTrue:[ |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
403 |
lastLink := prevNode |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
404 |
]. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
405 |
numberOfNodes := numberOfNodes - 1. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
406 |
thisNode nextLink:nil. |
19970 | 407 |
^ thisNode value |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
408 |
]. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
409 |
prevNode := thisNode. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
410 |
thisNode := nextNode |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
411 |
]. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
412 |
^ exceptionBlock value |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
413 |
|
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
414 |
"Created: / 30-11-2010 / 13:38:25 / cg" |
17274 | 415 |
! |
416 |
||
417 |
removeLast |
|
418 |
"remove the last link element and return it; |
|
419 |
if empty, raise an exception." |
|
420 |
||
421 |
|nextNode thisNode| |
|
422 |
||
423 |
thisNode := firstLink. |
|
424 |
thisNode isNil ifTrue:[ |
|
425 |
^ self emptyCollectionError |
|
426 |
]. |
|
427 |
thisNode == lastLink ifTrue:[ |
|
428 |
firstLink := lastLink := nil. |
|
429 |
numberOfNodes := 0. |
|
430 |
^ thisNode |
|
431 |
]. |
|
432 |
||
433 |
[ |
|
434 |
nextNode := thisNode nextLink. |
|
435 |
nextNode == lastLink ifTrue:[ |
|
436 |
firstLink == lastLink ifTrue:[ |
|
437 |
firstLink := thisNode |
|
438 |
]. |
|
439 |
lastLink := thisNode. |
|
440 |
numberOfNodes := numberOfNodes - 1. |
|
441 |
thisNode nextLink:nil. |
|
442 |
^ nextNode. |
|
443 |
]. |
|
444 |
thisNode := nextNode |
|
445 |
] loop. |
|
606 | 446 |
! ! |
447 |
||
448 |
!LinkedList methodsFor:'enumerating'! |
|
449 |
||
450 |
do:aBlock |
|
19976 | 451 |
"evaluate the argument, aBlock with 1 arg for every value element in the list" |
17274 | 452 |
|
453 |
|thisNode| |
|
454 |
||
20231 | 455 |
"aBlock may add elements, so do not use 'numberOfNodes-1 timesRepeat:[]'" |
17274 | 456 |
thisNode := firstLink. |
457 |
[thisNode notNil] whileTrue:[ |
|
19976 | 458 |
aBlock value:thisNode value. |
17274 | 459 |
thisNode := thisNode nextLink |
460 |
] |
|
461 |
! |
|
462 |
||
463 |
linksDo:aBlock |
|
464 |
"evaluate the argument, aBlock with 1 arg for every link element in the list" |
|
606 | 465 |
|
466 |
|thisNode| |
|
467 |
||
20231 | 468 |
"aBlock may add elements, so do not use 'numberOfNodes-1 timesRepeat:[]'" |
606 | 469 |
thisNode := firstLink. |
470 |
[thisNode notNil] whileTrue:[ |
|
17274 | 471 |
aBlock value:thisNode. |
472 |
thisNode := thisNode nextLink |
|
606 | 473 |
] |
17406 | 474 |
! |
475 |
||
476 |
printElementsDo:aBlock |
|
477 |
"perform aBlock (1 arg) for all elements. |
|
478 |
Used in #printOn:." |
|
479 |
||
24614 | 480 |
self linksDo:aBlock |
606 | 481 |
! ! |
482 |
||
5244 | 483 |
!LinkedList methodsFor:'initialization'! |
606 | 484 |
|
485 |
initialize |
|
486 |
numberOfNodes := 0 |
|
93 | 487 |
! ! |
488 |
||
489 |
!LinkedList methodsFor:'queries'! |
|
490 |
||
23711 | 491 |
isEmpty |
492 |
"return true, if the collection is empty" |
|
493 |
||
494 |
^ firstLink isNil |
|
495 |
! |
|
496 |
||
497 |
notEmpty |
|
498 |
"return true, if the collection is not empty" |
|
499 |
||
500 |
^ firstLink notNil |
|
501 |
! |
|
502 |
||
606 | 503 |
size |
504 |
"return the size of the LinkedList i.e. the number of nodes" |
|
505 |
||
506 |
^ numberOfNodes |
|
1 | 507 |
! ! |
508 |
||
17209 | 509 |
!LinkedList methodsFor:'searching-equality'! |
510 |
||
19970 | 511 |
indexOf:aLinkOrValue startingAt:start |
512 |
"search the collection for aLinkOrValue, starting the search at index start; |
|
513 |
if found, return the index otherwise return 0. |
|
514 |
Here, index is defined as the link-node's position in the list. |
|
17575 | 515 |
The comparison is done using = (i.e. equality test - not identity test). |
516 |
Warning: |
|
517 |
it is a very bad idea to access LinkedList elements by index. |
|
518 |
many algorithms degenerate to poor performance if you do. |
|
519 |
This method is provided for protocol completeness, |
|
520 |
but please consider using another type of collection if you use it. |
|
521 |
" |
|
17209 | 522 |
|
20231 | 523 |
|theNode count "{ Class: SmallInteger }" |
19970 | 524 |
linkOrValue| |
17209 | 525 |
|
20231 | 526 |
count := start. |
527 |
(count < 1 or:[count > numberOfNodes]) ifTrue:[ |
|
528 |
^ 0. |
|
529 |
]. |
|
530 |
||
531 |
theNode := firstLink. |
|
532 |
count-1 timesRepeat:[ |
|
533 |
theNode isNil ifTrue:[^ 0]. "reached the end" |
|
534 |
theNode := theNode nextLink. |
|
535 |
]. |
|
536 |
||
19970 | 537 |
linkOrValue := aLinkOrValue value. |
20231 | 538 |
|
539 |
[theNode notNil] whileTrue:[ |
|
540 |
(linkOrValue = theNode value) ifTrue:[^ count]. |
|
17575 | 541 |
theNode := theNode nextLink. |
20231 | 542 |
count := count + 1. |
17209 | 543 |
]. "reached the end" |
544 |
^ 0 |
|
545 |
||
546 |
" |
|
547 |
|l| |
|
548 |
||
549 |
l := LinkedList new. |
|
20231 | 550 |
l indexOf:'hello' startingAt:1 |
17209 | 551 |
" |
552 |
||
553 |
" |
|
554 |
|l v| |
|
555 |
||
556 |
l := LinkedList new. |
|
557 |
l add:(ValueLink new value:'one'). |
|
558 |
l add:(ValueLink new value:'two'). |
|
559 |
l add:(v := ValueLink new value:'hello'). |
|
20231 | 560 |
l indexOf:v startingAt:2. |
17209 | 561 |
" |
562 |
! ! |
|
563 |
||
564 |
!LinkedList methodsFor:'searching-identity'! |
|
1 | 565 |
|
19970 | 566 |
identityIndexOf:aLinkOrValue startingAt:start |
567 |
"search the collection for aLinkOrValue, starting the search at index start; |
|
568 |
if found, return the index otherwise return 0. |
|
569 |
Here, index is defined as the link-node's position in the list. |
|
570 |
The comparison is done using == (i.e. identity test - not equality test). |
|
571 |
Warning: |
|
572 |
it is a very bad idea to access LinkedList elements by index. |
|
17575 | 573 |
many algorithms degenerate to poor performance if you do. |
574 |
This method is provided for protocol completeness, |
|
19970 | 575 |
but please consider using another type of collection if you use it. |
576 |
" |
|
243 | 577 |
|
20231 | 578 |
|theNode count "{ Class: SmallInteger }" |
19970 | 579 |
linkOrValue| |
243 | 580 |
|
20231 | 581 |
count := start. |
582 |
(count < 1 or:[count > numberOfNodes]) ifTrue:[ |
|
583 |
^ 0. |
|
584 |
]. |
|
585 |
||
586 |
theNode := firstLink. |
|
587 |
count-1 timesRepeat:[ |
|
588 |
theNode isNil ifTrue:[^ 0]. "reached the end" |
|
589 |
theNode := theNode nextLink. |
|
590 |
]. |
|
591 |
||
19970 | 592 |
linkOrValue := aLinkOrValue value. |
20231 | 593 |
|
594 |
[theNode notNil] whileTrue:[ |
|
595 |
(linkOrValue == theNode value) ifTrue:[^ count]. |
|
13150
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
596 |
theNode := theNode nextLink. |
20231 | 597 |
count := count + 1. |
243 | 598 |
]. "reached the end" |
599 |
^ 0 |
|
600 |
||
601 |
" |
|
602 |
|l| |
|
603 |
||
604 |
l := LinkedList new. |
|
20231 | 605 |
l identityIndexOf:'hello' startingAt:1 |
243 | 606 |
" |
607 |
||
608 |
" |
|
609 |
|l v| |
|
610 |
||
611 |
l := LinkedList new. |
|
612 |
l add:(ValueLink new value:'one'). |
|
613 |
l add:(ValueLink new value:'two'). |
|
614 |
l add:(v := ValueLink new value:'hello'). |
|
20231 | 615 |
l identityIndexOf:v startingAt:2. |
243 | 616 |
" |
1 | 617 |
! ! |
618 |
||
17210 | 619 |
!LinkedList methodsFor:'testing'! |
620 |
||
621 |
isFixedSize |
|
622 |
"return true if the receiver cannot grow" |
|
623 |
||
624 |
^ false |
|
625 |
! ! |
|
626 |
||
629 | 627 |
!LinkedList class methodsFor:'documentation'! |
628 |
||
629 |
version |
|
19970 | 630 |
^ '$Header$' |
13150
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
631 |
! |
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
632 |
|
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
633 |
version_CVS |
19970 | 634 |
^ '$Header$' |
629 | 635 |
! ! |
15428 | 636 |