author | Stefan Vogel <sv@exept.de> |
Wed, 04 Feb 2015 17:54:02 +0100 | |
changeset 17406 | 87bc620b6ee0 |
parent 17274 | e28efb6e95e8 |
child 17575 | 11ce80e0ee6b |
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 |
" |
|
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
13 |
|
17262 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1 | 16 |
SequenceableCollection subclass:#LinkedList |
1110 | 17 |
instanceVariableNames:'firstLink lastLink numberOfNodes' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
1364 | 20 |
category:'Collections-Linked' |
1 | 21 |
! |
22 |
||
88 | 23 |
!LinkedList class methodsFor:'documentation'! |
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
159 | 28 |
All Rights Reserved |
1 | 29 |
|
88 | 30 |
This software is furnished under a license and may be used |
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
1 | 38 |
|
88 | 39 |
documentation |
40 |
" |
|
41 |
this class implements an anchor to a list of Links. |
|
10647 | 42 |
The data itself is held in the link elements. |
243 | 43 |
See (the abstract) Link, ValueLink and (possibly other) classes, |
44 |
which can be used as elements of a linkedList. |
|
45 |
||
360 | 46 |
LinkedList does not care for storage; all it does is handling |
47 |
chained link elements, which must respond to #nextLink/#nextLink:. |
|
17123 | 48 |
(i.e. any object which can do this, can be used as elements of a linked 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:] |
|
13727 | 53 |
Be careful when subclassing Link, since there is a big drawback, |
54 |
which may be overlooked by beginners: |
|
17123 | 55 |
a Link element can ONLY be in one LinkedList at a time |
13727 | 56 |
- adding the same element to another LinkedList |
17123 | 57 |
will remove it from the first as a side effect. |
13727 | 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. |
|
17123 | 77 |
(the scheduler's process handling code does this to manage process lists.) |
1290 | 78 |
|
4063 | 79 |
[memory requirements:] |
13727 | 80 |
(OBJ-HEADER + (3 * ptr-size)) * size |
81 |
+ any additional instvars due to subclassing |
|
4063 | 82 |
|
1290 | 83 |
[author:] |
13727 | 84 |
Claus Gittinger (July 1993) |
2146 | 85 |
|
86 |
[see also:] |
|
13727 | 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 |
|
17274 | 149 |
self obsoleteFeatureWarning:'this will soon change to return the link''s value instead of the link itself'. |
17273 | 150 |
|
9117 | 151 |
^ self at:index ifAbsent:[ self subscriptBoundsError:index] |
152 |
! |
|
153 |
||
154 |
at:index ifAbsent:exceptionBlock |
|
155 |
"return the n'th element - use of this method should be avoided, |
|
156 |
since it is slow to walk through the list - think about using |
|
9118 | 157 |
another collection if you need indexed access. |
9117 | 158 |
Notice, that many methods in SeqColl are based on at:-access, |
159 |
so other inherited methods may be very slow (showing square runtime)." |
|
160 |
||
159 | 161 |
|theLink |
162 |
runIndex "{Class: SmallInteger}"| |
|
163 |
||
17274 | 164 |
self obsoleteFeatureWarning:'this will soon change to return the link''s value instead of the link itself'. |
17273 | 165 |
|
159 | 166 |
theLink := firstLink. |
167 |
runIndex := 1. |
|
243 | 168 |
[runIndex == index] whileFalse:[ |
17273 | 169 |
theLink isNil ifTrue:[^ exceptionBlock value]. |
170 |
theLink := theLink nextLink. |
|
171 |
runIndex := runIndex + 1. |
|
159 | 172 |
]. |
173 |
^ theLink |
|
606 | 174 |
! |
175 |
||
176 |
first |
|
177 |
"return the first node in the list" |
|
178 |
||
17274 | 179 |
self obsoleteFeatureWarning:'this will soon change to return the link''s value instead of the link itself'. |
17262 | 180 |
|
17274 | 181 |
^ self firstLink "value" |
606 | 182 |
! |
183 |
||
10647 | 184 |
firstIfEmpty:exceptionalValue |
185 |
"return the first node in the list or exceptionlValue, if empty" |
|
186 |
||
17274 | 187 |
self obsoleteFeatureWarning:'this will soon change to return the link''s value instead of the link itself'. |
17262 | 188 |
|
189 |
firstLink isNil ifTrue:[^ exceptionalValue value]. |
|
17274 | 190 |
^ firstLink "value" |
17262 | 191 |
! |
192 |
||
193 |
firstLink |
|
194 |
"return the first node in the list" |
|
195 |
||
196 |
firstLink isNil ifTrue:[^ self emptyCollectionError]. |
|
197 |
^ firstLink |
|
198 |
! |
|
199 |
||
200 |
firstLinkIfEmpty:exceptionalValue |
|
201 |
"return the first node in the list or exceptionlValue, if empty" |
|
202 |
||
10647 | 203 |
firstLink isNil ifTrue:[^ exceptionalValue value]. |
204 |
^ firstLink |
|
205 |
! |
|
206 |
||
606 | 207 |
last |
208 |
"return last node in the list" |
|
209 |
||
17274 | 210 |
self obsoleteFeatureWarning:'this will soon change to return the link''s value instead of the link itself'. |
17262 | 211 |
|
17274 | 212 |
lastLink isNil ifTrue:[^ self emptyCollectionError]. |
213 |
^ lastLink value |
|
17262 | 214 |
! |
215 |
||
216 |
lastLink |
|
217 |
"return last node in the list" |
|
218 |
||
606 | 219 |
lastLink isNil ifTrue:[self emptyCollectionError]. |
220 |
^ lastLink |
|
17272 | 221 |
! |
222 |
||
223 |
linkAt:index |
|
224 |
"return the n'th element - use of this method should be avoided, |
|
225 |
since it is slow to walk through the list - think about using |
|
226 |
another collection if you need indexed access. |
|
227 |
Notice, that many methods in SeqColl are based on at:-access, |
|
228 |
so other inherited methods may be very slow (showing square runtime)." |
|
229 |
||
230 |
^ self linkAt:index ifAbsent:[ self subscriptBoundsError:index] |
|
231 |
! |
|
232 |
||
233 |
linkAt:index ifAbsent:exceptionBlock |
|
234 |
"return the n'th element - use of this method should be avoided, |
|
235 |
since it is slow to walk through the list - think about using |
|
236 |
another collection if you need indexed access. |
|
237 |
Notice, that many methods in SeqColl are based on at:-access, |
|
238 |
so other inherited methods may be very slow (showing square runtime)." |
|
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 |
||
17262 | 273 |
add:aLinkOrAnyOtherObject after:aLink |
606 | 274 |
"adds linkToAdd after another link, aLink. If aLink is nil, |
275 |
linkToAdd is inserted at the beginning. Returns linkToAdd." |
|
276 |
||
17262 | 277 |
|this linkToAdd| |
606 | 278 |
|
17262 | 279 |
aLink isNil ifTrue:[^ self addFirst:aLinkOrAnyOtherObject ]. |
280 |
||
281 |
linkToAdd := aLinkOrAnyOtherObject asLink. |
|
606 | 282 |
|
283 |
this := firstLink. |
|
284 |
[this notNil and:[this ~~ aLink]] whileTrue:[ |
|
17262 | 285 |
this := this nextLink |
606 | 286 |
]. |
287 |
this isNil ifTrue:[^ self add:linkToAdd ]. |
|
288 |
linkToAdd nextLink:(this nextLink). |
|
289 |
this nextLink:linkToAdd. |
|
10647 | 290 |
numberOfNodes := numberOfNodes + 1. |
606 | 291 |
^ linkToAdd |
292 |
! |
|
293 |
||
17262 | 294 |
addFirst:aLinkOrAnyOtherObject |
606 | 295 |
"adds aLink to the beginning of the sequence. Returns aLink" |
296 |
||
17262 | 297 |
|linkToAdd| |
298 |
||
299 |
linkToAdd := aLinkOrAnyOtherObject asLink. |
|
300 |
||
606 | 301 |
firstLink isNil ifTrue:[ |
17262 | 302 |
lastLink := linkToAdd. |
606 | 303 |
]. |
17262 | 304 |
linkToAdd nextLink:firstLink. |
305 |
firstLink := linkToAdd. |
|
606 | 306 |
numberOfNodes := numberOfNodes + 1. |
17262 | 307 |
^ linkToAdd |
606 | 308 |
! |
309 |
||
310 |
remove:aLink ifAbsent:exceptionBlock |
|
10647 | 311 |
"remove the argument, aLink from the sequence and return it; |
2350 | 312 |
if absent, evaluate the exceptionBlock. |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
313 |
Actually this is really a #removeIdentical (but for compatibility...)" |
606 | 314 |
|
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
315 |
^ self removeIdentical:aLink ifAbsent:exceptionBlock |
2349 | 316 |
|
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
317 |
"Modified: / 30-11-2010 / 13:38:45 / cg" |
606 | 318 |
! |
319 |
||
1110 | 320 |
removeAll |
1164 | 321 |
"remove all elements from the sequence. Returns the receiver." |
1110 | 322 |
|
323 |
firstLink := lastLink := nil. |
|
324 |
numberOfNodes := 0 |
|
325 |
||
326 |
"Created: 21.3.1996 / 15:24:38 / cg" |
|
1164 | 327 |
"Modified: 12.4.1996 / 13:34:53 / cg" |
1110 | 328 |
! |
329 |
||
606 | 330 |
removeFirst |
331 |
"remove and return the first node from the sequence" |
|
332 |
||
333 |
|link| |
|
334 |
||
335 |
firstLink isNil ifTrue:[ |
|
10647 | 336 |
^ self emptyCollectionError |
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
337 |
]. |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
338 |
link := firstLink. |
10647 | 339 |
firstLink := firstLink nextLink. |
340 |
firstLink isNil ifTrue:[ |
|
341 |
lastLink := nil |
|
606 | 342 |
]. |
5519
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
343 |
link nextLink:nil. |
925d9a1eda2c
fixed emptyCollection error signaling
Claus Gittinger <cg@exept.de>
parents:
5244
diff
changeset
|
344 |
numberOfNodes := numberOfNodes - 1. |
606 | 345 |
^ link |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
346 |
! |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
347 |
|
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
348 |
removeIdentical:aLink ifAbsent:exceptionBlock |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
349 |
"remove the argument, aLink from the sequence and return it; |
13163
e4845fce0ada
changed comment: #removeIdentical:ifAbsent:
Stefan Vogel <sv@exept.de>
parents:
13153
diff
changeset
|
350 |
if absent, evaluate the exceptionBlock." |
13153
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
351 |
|
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
352 |
|prevNode nextNode thisNode| |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
353 |
|
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
354 |
thisNode := firstLink. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
355 |
[thisNode notNil] whileTrue:[ |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
356 |
nextNode := thisNode nextLink. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
357 |
(thisNode == aLink) ifTrue:[ |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
358 |
prevNode isNil ifTrue:[ |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
359 |
firstLink := nextNode |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
360 |
] ifFalse:[ |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
361 |
prevNode nextLink:nextNode |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
362 |
]. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
363 |
nextNode isNil ifTrue:[ |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
364 |
lastLink := prevNode |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
365 |
]. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
366 |
numberOfNodes := numberOfNodes - 1. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
367 |
thisNode nextLink:nil. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
368 |
^ aLink |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
369 |
]. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
370 |
prevNode := thisNode. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
371 |
thisNode := nextNode |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
372 |
]. |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
373 |
^ exceptionBlock value |
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
374 |
|
572dc11e2e96
added: #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
13150
diff
changeset
|
375 |
"Created: / 30-11-2010 / 13:38:25 / cg" |
17274 | 376 |
! |
377 |
||
378 |
removeLast |
|
379 |
"remove the last link element and return it; |
|
380 |
if empty, raise an exception." |
|
381 |
||
382 |
|nextNode thisNode| |
|
383 |
||
384 |
thisNode := firstLink. |
|
385 |
thisNode isNil ifTrue:[ |
|
386 |
^ self emptyCollectionError |
|
387 |
]. |
|
388 |
thisNode == lastLink ifTrue:[ |
|
389 |
firstLink := lastLink := nil. |
|
390 |
numberOfNodes := 0. |
|
391 |
^ thisNode |
|
392 |
]. |
|
393 |
||
394 |
[ |
|
395 |
nextNode := thisNode nextLink. |
|
396 |
nextNode == lastLink ifTrue:[ |
|
397 |
firstLink == lastLink ifTrue:[ |
|
398 |
firstLink := thisNode |
|
399 |
]. |
|
400 |
lastLink := thisNode. |
|
401 |
numberOfNodes := numberOfNodes - 1. |
|
402 |
thisNode nextLink:nil. |
|
403 |
^ nextNode. |
|
404 |
]. |
|
405 |
thisNode := nextNode |
|
406 |
] loop. |
|
606 | 407 |
! ! |
408 |
||
409 |
!LinkedList methodsFor:'enumerating'! |
|
410 |
||
411 |
do:aBlock |
|
17274 | 412 |
"evaluate the argument, aBlock with 1 arg for every link element in the list" |
413 |
||
414 |
|thisNode| |
|
415 |
||
416 |
self obsoleteFeatureWarning:'this will soon change to enumerate the link values instead of the link itself'. |
|
417 |
||
418 |
thisNode := firstLink. |
|
419 |
[thisNode notNil] whileTrue:[ |
|
420 |
aBlock value:thisNode "value". |
|
421 |
thisNode := thisNode nextLink |
|
422 |
] |
|
423 |
! |
|
424 |
||
425 |
linksDo:aBlock |
|
426 |
"evaluate the argument, aBlock with 1 arg for every link element in the list" |
|
606 | 427 |
|
428 |
|thisNode| |
|
429 |
||
430 |
thisNode := firstLink. |
|
431 |
[thisNode notNil] whileTrue:[ |
|
17274 | 432 |
aBlock value:thisNode. |
433 |
thisNode := thisNode nextLink |
|
606 | 434 |
] |
17406 | 435 |
! |
436 |
||
437 |
printElementsDo:aBlock |
|
438 |
"perform aBlock (1 arg) for all elements. |
|
439 |
Used in #printOn:." |
|
440 |
||
441 |
^ self linksDo:aBlock |
|
606 | 442 |
! ! |
443 |
||
5244 | 444 |
!LinkedList methodsFor:'initialization'! |
606 | 445 |
|
446 |
initialize |
|
447 |
numberOfNodes := 0 |
|
93 | 448 |
! ! |
449 |
||
450 |
!LinkedList methodsFor:'queries'! |
|
451 |
||
606 | 452 |
size |
453 |
"return the size of the LinkedList i.e. the number of nodes" |
|
454 |
||
455 |
^ numberOfNodes |
|
1 | 456 |
! ! |
457 |
||
17209 | 458 |
!LinkedList methodsFor:'searching-equality'! |
459 |
||
460 |
indexOf:aLink startingAt:start |
|
461 |
"search the collection for aLink, starting the search at index start; |
|
462 |
if found, return the index otherwise return 0. Here, index is defined |
|
463 |
as the link-nodes position in the list. |
|
464 |
The comparison is done using = (i.e. equality test - not identity test)." |
|
465 |
||
466 |
|theNode idx "{ Class: SmallInteger }"| |
|
467 |
||
468 |
theNode := firstLink. |
|
469 |
idx := 1. |
|
470 |
[idx < start] whileTrue:[ |
|
471 |
theNode isNil ifTrue:[^ 0]. "reached the end" |
|
472 |
theNode := theNode nextLink. |
|
473 |
idx := idx + 1. |
|
474 |
]. |
|
475 |
[theNode notNil] whileTrue:[ |
|
476 |
(aLink = theNode) ifTrue:[^ idx]. |
|
477 |
theNode := theNode nextLink. |
|
478 |
idx := idx + 1. |
|
479 |
]. "reached the end" |
|
480 |
^ 0 |
|
481 |
||
482 |
" |
|
483 |
|l| |
|
484 |
||
485 |
l := LinkedList new. |
|
486 |
l indexOf:'hello' |
|
487 |
" |
|
488 |
||
489 |
" |
|
490 |
|l v| |
|
491 |
||
492 |
l := LinkedList new. |
|
493 |
l add:(ValueLink new value:'one'). |
|
494 |
l add:(ValueLink new value:'two'). |
|
495 |
l add:(v := ValueLink new value:'hello'). |
|
496 |
l indexOf:v |
|
497 |
" |
|
498 |
! ! |
|
499 |
||
500 |
!LinkedList methodsFor:'searching-identity'! |
|
1 | 501 |
|
10647 | 502 |
identityIndexOf:aLink startingAt:start |
243 | 503 |
"search the collection for aLink, starting the search at index start; |
504 |
if found, return the index otherwise return 0. Here, index is defined |
|
505 |
as the link-nodes position in the list. |
|
10647 | 506 |
The comparison is done using == |
13150
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
507 |
(i.e. identity test - not equality test)." |
243 | 508 |
|
509 |
|theNode idx "{ Class: SmallInteger }"| |
|
510 |
||
511 |
theNode := firstLink. |
|
512 |
idx := 1. |
|
513 |
[idx < start] whileTrue:[ |
|
13150
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
514 |
theNode isNil ifTrue:[^ 0]. "reached the end" |
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
515 |
theNode := theNode nextLink. |
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
516 |
idx := idx + 1. |
243 | 517 |
]. |
518 |
[theNode notNil] whileTrue:[ |
|
13150
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
519 |
(aLink == theNode) ifTrue:[^ idx]. |
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
520 |
theNode := theNode nextLink. |
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
521 |
idx := idx + 1. |
243 | 522 |
]. "reached the end" |
523 |
^ 0 |
|
524 |
||
525 |
" |
|
526 |
|l| |
|
527 |
||
528 |
l := LinkedList new. |
|
10647 | 529 |
l indexOf:'hello' |
243 | 530 |
" |
531 |
||
532 |
" |
|
533 |
|l v| |
|
534 |
||
535 |
l := LinkedList new. |
|
536 |
l add:(ValueLink new value:'one'). |
|
537 |
l add:(ValueLink new value:'two'). |
|
538 |
l add:(v := ValueLink new value:'hello'). |
|
10647 | 539 |
l identityIndexOf:v |
243 | 540 |
" |
1 | 541 |
! ! |
542 |
||
17210 | 543 |
!LinkedList methodsFor:'testing'! |
544 |
||
545 |
isEmpty |
|
546 |
"return true, if the collection is empty" |
|
547 |
||
548 |
^ firstLink isNil |
|
549 |
! |
|
550 |
||
551 |
isFixedSize |
|
552 |
"return true if the receiver cannot grow" |
|
553 |
||
554 |
^ false |
|
555 |
! |
|
556 |
||
557 |
notEmpty |
|
558 |
"return true, if the collection is not empty" |
|
559 |
||
560 |
^ firstLink notNil |
|
561 |
! ! |
|
562 |
||
629 | 563 |
!LinkedList class methodsFor:'documentation'! |
564 |
||
565 |
version |
|
17406 | 566 |
^ '$Header: /cvs/stx/stx/libbasic/LinkedList.st,v 1.50 2015-02-04 16:54:02 stefan Exp $' |
13150
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
567 |
! |
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
568 |
|
e20625aa25a7
comment changed: #identityIndexOf:startingAt:
Stefan Vogel <sv@exept.de>
parents:
10647
diff
changeset
|
569 |
version_CVS |
17406 | 570 |
^ '$Header: /cvs/stx/stx/libbasic/LinkedList.st,v 1.50 2015-02-04 16:54:02 stefan Exp $' |
629 | 571 |
! ! |
15428 | 572 |