author | Merge Script |
Thu, 19 May 2016 07:07:52 +0200 | |
branch | jv |
changeset 19882 | 8a3f4071dfec |
parent 19862 | 6c010853ea32 |
parent 19865 | 6b114e3b40b9 |
child 20079 | 8d884971c2ed |
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 |
" |
|
5401 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
17705 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
1 | 16 |
SequenceableCollection subclass:#OrderedCollection |
1071 | 17 |
instanceVariableNames:'contentsArray firstIndex lastIndex' |
18879 | 18 |
classVariableNames:'MinContentsArraySize' |
1071 | 19 |
poolDictionaries:'' |
20 |
category:'Collections-Sequenceable' |
|
1 | 21 |
! |
22 |
||
33 | 23 |
!OrderedCollection class methodsFor:'documentation'! |
24 |
||
88 | 25 |
copyright |
26 |
" |
|
27 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
159 | 28 |
All Rights Reserved |
88 | 29 |
|
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 |
! |
|
38 |
||
33 | 39 |
documentation |
40 |
" |
|
18182 | 41 |
OrderedCollections (OCs) have their elements ordered as they were added. |
42 |
In addition, they provide all indexing access protocol |
|
1387 | 43 |
and bulk copying (much like Arrays). |
44 |
||
19096 | 45 |
Insertion and removal at both ends is possible and also usually fast |
18182 | 46 |
- therefore they can be used for queues and stacks. |
19096 | 47 |
|
1387 | 48 |
[Instance variables:] |
49 |
contentsArray <Array> the actual contents |
|
50 |
||
51 |
firstIndex <SmallInteger> index of first valid element |
|
52 |
||
53 |
lastIndex <SmallInteger> index of last valid element |
|
54 |
||
55 |
[performance hint:] |
|
19096 | 56 |
They overallocate a few slots to make insertion at either end often O(1), |
57 |
but sometimes O(n), where n is the current size of the collection |
|
58 |
(i.e. they have reallocate the underlying element buffer and therefore copy the |
|
59 |
elements into a new one. However, this reallocation is not done on every insert, |
|
60 |
and if elements are deleted and others reinserted, the buffer is usually already able |
|
61 |
to hold the new element) |
|
62 |
||
63 |
Insertion in the middle is O(n), and therefore slower, because elements have to be |
|
64 |
shuffled towards either end, in order to make space for the new element. |
|
65 |
Therefore, it is often cheaper, to instantiate a new object, and copy over the |
|
66 |
elements. |
|
67 |
see SegmentedOrderedCollection for a collection, which is specialized for this |
|
68 |
kind of usage with many elements. |
|
1 | 69 |
|
1387 | 70 |
[beginners bug hint:] |
71 |
notice that: |
|
72 |
Array new:n |
|
73 |
is quite different from: |
|
74 |
OrderedCollection new:n |
|
1290 | 75 |
|
1387 | 76 |
The later creates an OC which is prepared to hold <n> elements, |
77 |
but has a logical size of 0 (zero). To get an OC containing <n> nils, |
|
78 |
use: |
|
79 |
(OrderedCollection new) grow:n |
|
16713 | 80 |
or the syntactic sugar for that: |
81 |
OrderedCollection newWithSize:n |
|
1387 | 82 |
|
16713 | 83 |
I know, this is confusing for beginners and was a bad semantic decision. |
84 |
However, that's the way the standard was defined and how all Smalltalk's behave, |
|
85 |
so we can't change that here. |
|
1 | 86 |
|
4064 | 87 |
[memory requirements:] |
88 |
OBJ-HEADER + (3 * ptr-size) |
|
89 |
+ (size-roundedUpToNextPowerOf2 * ptr-size) |
|
90 |
||
19096 | 91 |
[complexity:] |
92 |
access by index: O(1) |
|
93 |
insertion at either end: mostly O(1) |
|
94 |
removal at either end: O(1) |
|
95 |
insertion in the middle: O(n) |
|
19097 | 96 |
searching: O(n) |
19098 | 97 |
min/max: O(n) |
19096 | 98 |
|
1290 | 99 |
[see also:] |
100 |
Array |
|
101 |
||
102 |
[author:] |
|
103 |
Claus Gittinger |
|
33 | 104 |
" |
1321 | 105 |
! |
106 |
||
107 |
examples |
|
108 |
" |
|
1387 | 109 |
using OC as a stack: |
1321 | 110 |
[exBegin] |
1387 | 111 |
|stack top| |
1321 | 112 |
|
1387 | 113 |
stack := OrderedCollection new. |
1321 | 114 |
|
1387 | 115 |
1 to:10 do:[:i | |
116 |
stack add:i |
|
117 |
]. |
|
1321 | 118 |
|
1387 | 119 |
10 timesRepeat:[ |
120 |
top := stack removeLast. |
|
1422 | 121 |
Transcript showCR:top |
1387 | 122 |
] |
1321 | 123 |
[exEnd] |
1387 | 124 |
using OC as a queue (you should use Queue right away ..): |
1321 | 125 |
[exBegin] |
1387 | 126 |
|queue dequeued| |
1321 | 127 |
|
1387 | 128 |
queue := OrderedCollection new. |
1321 | 129 |
|
1387 | 130 |
1 to:10 do:[:i | |
131 |
queue addLast:i |
|
132 |
]. |
|
1321 | 133 |
|
1387 | 134 |
10 timesRepeat:[ |
135 |
dequeued := queue removeFirst. |
|
1422 | 136 |
Transcript showCR:dequeued |
1387 | 137 |
] |
1321 | 138 |
[exEnd] |
1324 | 139 |
|
140 |
||
1387 | 141 |
examples to support the performance hint (see documentation) |
1324 | 142 |
|
143 |
timing removal of all odd elements in a collection of 10000: |
|
144 |
(940 ms on P5/133) |
|
145 |
[exBegin] |
|
146 |
|coll time| |
|
147 |
||
148 |
coll := (1 to:10000) asOrderedCollection. |
|
149 |
time := Time millisecondsToRun:[ |
|
150 |
coll removeAllSuchThat:[:el | el even] |
|
151 |
]. |
|
1422 | 152 |
Transcript show:'time is '; show:time; showCR:' ms'. |
1324 | 153 |
[exEnd] |
154 |
||
1387 | 155 |
tuning the removal by doing it reverse |
156 |
(less copying in #removeAtIndex:) speeds it up by a factor of 2: |
|
1324 | 157 |
[exBegin] |
158 |
|coll time| |
|
159 |
||
160 |
coll := (1 to:10000) asOrderedCollection. |
|
161 |
time := Time millisecondsToRun:[ |
|
162 |
coll size to:1 by:-1 do:[:index | |
|
163 |
(coll at:index) even ifTrue:[ |
|
164 |
coll removeAtIndex:index |
|
165 |
] |
|
166 |
] |
|
167 |
]. |
|
1422 | 168 |
Transcript show:'time is '; show:time; showCR:' ms'. |
1324 | 169 |
[exEnd] |
170 |
||
1387 | 171 |
rebuilding a new collection: |
1324 | 172 |
(64 ms on P5/133) |
173 |
[exBegin] |
|
174 |
|coll time| |
|
175 |
||
176 |
coll := (1 to:10000) asOrderedCollection. |
|
177 |
time := Time millisecondsToRun:[ |
|
178 |
coll := coll select:[:el | el odd] |
|
179 |
]. |
|
1422 | 180 |
Transcript show:'time is '; show:time; showCR:' ms'. |
1324 | 181 |
[exEnd] |
11271 | 182 |
|
183 |
adding at the end (fast): |
|
184 |
[exBegin] |
|
185 |
|coll time| |
|
186 |
||
187 |
coll := OrderedCollection new. |
|
188 |
time := TimeDuration toRun:[ |
|
189 |
(1 to:100000) do:[:el | coll add:el]. |
|
190 |
]. |
|
191 |
Transcript show:'time is '; showCR:time. |
|
192 |
self assert:(coll = (1 to:100000)). |
|
193 |
self assert:(coll asBag = (1 to:100000) asBag). |
|
194 |
[exEnd] |
|
195 |
adding at front (fast): |
|
196 |
[exBegin] |
|
197 |
|coll time| |
|
198 |
||
199 |
coll := OrderedCollection new. |
|
200 |
time := TimeDuration toRun:[ |
|
201 |
(1 to:100000) reverseDo:[:el | coll addFirst:el]. |
|
202 |
]. |
|
203 |
Transcript show:'time is '; showCR:time. |
|
204 |
self assert:(coll = (1 to:100000)). |
|
205 |
self assert:(coll asBag = (1 to:100000) asBag). |
|
206 |
[exEnd] |
|
207 |
inserting in the middle (slow): |
|
208 |
[exBegin] |
|
209 |
|coll time| |
|
210 |
||
211 |
coll := OrderedCollection new. |
|
212 |
time := TimeDuration toRun:[ |
|
213 |
(1 to:100000) do:[:el | coll add:el beforeIndex:(coll size // 2)+1 ]. |
|
214 |
]. |
|
215 |
Transcript show:'time is '; showCR:time. |
|
216 |
self assert:(coll asBag = (1 to:100000) asBag). |
|
217 |
[exEnd] |
|
1321 | 218 |
" |
33 | 219 |
! ! |
1 | 220 |
|
18879 | 221 |
!OrderedCollection class methodsFor:'initialization'! |
222 |
||
223 |
initialize |
|
224 |
MinContentsArraySize := 3. "the minimum size of a non-empty contentsArray" |
|
225 |
! ! |
|
226 |
||
1 | 227 |
!OrderedCollection class methodsFor:'instance creation'! |
228 |
||
229 |
new |
|
1106 | 230 |
"create a new, empty OrderedCollection" |
1 | 231 |
|
18879 | 232 |
MinContentsArraySize isNil ifTrue:[ |
233 |
self initialize. |
|
234 |
]. |
|
235 |
^ self basicNew initContents:0 |
|
236 |
||
237 |
" |
|
238 |
self new |
|
239 |
||
240 |
|nEmpty| |
|
241 |
nEmpty := 0. |
|
242 |
self allInstancesDo:[:e| e size == 0 ifTrue:[nEmpty := nEmpty + 1]]. |
|
243 |
nEmpty |
|
244 |
||
245 |
|nEmpty| |
|
246 |
nEmpty := OrderedCollection new. |
|
247 |
self allInstancesDo:[:e| (e size == 0 and:[e contentsArray size ~~ 0]) ifTrue:[nEmpty add:e]]. |
|
248 |
nEmpty |
|
249 |
" |
|
1106 | 250 |
|
251 |
"Modified: 19.3.1996 / 17:53:12 / cg" |
|
293 | 252 |
! |
253 |
||
606 | 254 |
new:size |
1106 | 255 |
"create a new, empty OrderedCollection with a preallocated physical |
256 |
size. |
|
257 |
NOTICE: |
|
258 |
the logical size of the returned collection is 0 (i.e. it is empty). |
|
259 |
This may be confusing, in that it is different from what Array>>new: |
|
16713 | 260 |
returns. However, that's the way OrderedCollections work in every other |
261 |
Smalltalk, so here it should as well. |
|
262 |
See also newWithSize:, which might do what you really want. |
|
1106 | 263 |
" |
606 | 264 |
|
18879 | 265 |
|sz| |
266 |
||
267 |
MinContentsArraySize isNil ifTrue:[ |
|
268 |
self initialize. |
|
269 |
]. |
|
270 |
sz := size. |
|
271 |
^ self basicNew initContents:sz |
|
1106 | 272 |
|
273 |
"Modified: 19.3.1996 / 17:53:47 / cg" |
|
4427 | 274 |
! |
275 |
||
6700
e4254778c239
new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents:
6088
diff
changeset
|
276 |
new:size withAll:element |
e4254778c239
new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents:
6088
diff
changeset
|
277 |
"return a new collection of size, where all elements are |
e4254778c239
new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents:
6088
diff
changeset
|
278 |
initialized to element." |
e4254778c239
new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents:
6088
diff
changeset
|
279 |
|
e4254778c239
new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents:
6088
diff
changeset
|
280 |
|newCollection| |
e4254778c239
new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents:
6088
diff
changeset
|
281 |
|
16707 | 282 |
newCollection := self newWithSize:size. |
6700
e4254778c239
new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents:
6088
diff
changeset
|
283 |
newCollection atAllPut:element. |
e4254778c239
new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents:
6088
diff
changeset
|
284 |
^ newCollection |
e4254778c239
new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents:
6088
diff
changeset
|
285 |
|
e4254778c239
new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents:
6088
diff
changeset
|
286 |
" |
e4254778c239
new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents:
6088
diff
changeset
|
287 |
OrderedCollection new:10 withAll:1234 |
e4254778c239
new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents:
6088
diff
changeset
|
288 |
" |
e4254778c239
new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents:
6088
diff
changeset
|
289 |
! |
e4254778c239
new:withAll: redefined to work around the wierd new: behavior.
Claus Gittinger <cg@exept.de>
parents:
6088
diff
changeset
|
290 |
|
4427 | 291 |
newFrom:aCollection |
292 |
"return a new OrderedCollection filled with all elements from the argument, |
|
293 |
aCollection" |
|
294 |
||
295 |
|newColl| |
|
296 |
||
297 |
newColl := self new:(aCollection size). |
|
298 |
newColl addAll:aCollection. |
|
299 |
^ newColl |
|
300 |
||
301 |
" |
|
302 |
OrderedCollection newFrom:#(1 2 3 4) |
|
303 |
OrderedCollection newFrom:(Set with:1 with:2 with:3) |
|
304 |
" |
|
17167 | 305 |
! |
306 |
||
307 |
newLikelyToRemainEmpty |
|
308 |
"create a new, empty OrderedCollection, for which we already know |
|
309 |
that it is very likely to remain empty. |
|
310 |
Use this for collections which may sometimes get elements added, but usually not. |
|
311 |
This creates an initial contents array of size 0 in contrast to the default new:0, |
|
312 |
which preserves space for 3 elements." |
|
313 |
||
18879 | 314 |
MinContentsArraySize isNil ifTrue:[ |
315 |
self initialize. |
|
316 |
]. |
|
317 |
^ self basicNew initContents:0 |
|
17167 | 318 |
|
319 |
" |
|
320 |
self newLikelyToRemainEmpty size |
|
321 |
self newLikelyToRemainEmpty add:1; size |
|
322 |
self newLikelyToRemainEmpty addAll:#(1 2); size |
|
323 |
" |
|
293 | 324 |
! ! |
325 |
||
19791 | 326 |
|
19865 | 327 |
|
606 | 328 |
!OrderedCollection methodsFor:'accessing'! |
1 | 329 |
|
606 | 330 |
at:anInteger |
331 |
"return the element at index, anInteger" |
|
1 | 332 |
|
3107 | 333 |
|idx "{ Class: SmallInteger }"| |
387 | 334 |
|
606 | 335 |
idx := anInteger + firstIndex - 1. |
336 |
((anInteger < 1) or:[idx > lastIndex]) ifTrue:[ |
|
3107 | 337 |
^ self subscriptBoundsError:anInteger |
338 |
]. |
|
339 |
^ contentsArray basicAt:idx |
|
340 |
||
341 |
"Modified: / 12.11.1997 / 17:56:05 / cg" |
|
360 | 342 |
! |
343 |
||
18811 | 344 |
at:anInteger ifAbsent:exceptionValue |
345 |
"return the element at index, anInteger. |
|
346 |
If the index is invalid, return the value from exceptionValue" |
|
347 |
||
348 |
|idx "{ Class: SmallInteger }"| |
|
349 |
||
350 |
idx := anInteger + firstIndex - 1. |
|
351 |
((anInteger < 1) or:[idx > lastIndex]) ifTrue:[ |
|
352 |
^ exceptionValue value |
|
353 |
]. |
|
354 |
^ contentsArray basicAt:idx |
|
355 |
! |
|
356 |
||
606 | 357 |
at:anInteger put:anObject |
1221 | 358 |
"set the element at index, to be anInteger. |
359 |
Return anObject (sigh)." |
|
1 | 360 |
|
3107 | 361 |
|idx "{ Class: SmallInteger }"| |
1 | 362 |
|
606 | 363 |
idx := anInteger + firstIndex - 1. |
364 |
((anInteger < 1) or:[idx > lastIndex]) ifTrue:[ |
|
1221 | 365 |
^ self subscriptBoundsError:anInteger |
3107 | 366 |
]. |
367 |
^ contentsArray basicAt:idx put:anObject |
|
1221 | 368 |
|
3107 | 369 |
"Modified: / 12.11.1997 / 17:56:32 / cg" |
202 | 370 |
! |
371 |
||
606 | 372 |
first |
373 |
"return the first element" |
|
1 | 374 |
|
606 | 375 |
firstIndex <= lastIndex ifTrue:[ |
376 |
^ contentsArray at:firstIndex |
|
1 | 377 |
]. |
606 | 378 |
"error if collection is empty" |
379 |
^ self emptyCollectionError |
|
2 | 380 |
|
93 | 381 |
" |
606 | 382 |
(OrderedCollection withAll:#(1 2 3 4 5)) first |
383 |
(SortedCollection withAll:#(5 4 3 2 1)) first |
|
93 | 384 |
" |
13 | 385 |
! |
386 |
||
606 | 387 |
last |
388 |
"return the last element" |
|
68 | 389 |
|
606 | 390 |
firstIndex <= lastIndex ifTrue:[ |
391 |
^ contentsArray at:lastIndex |
|
392 |
]. |
|
393 |
"error if collection is empty" |
|
394 |
^ self emptyCollectionError |
|
68 | 395 |
|
93 | 396 |
" |
606 | 397 |
(OrderedCollection withAll:#(1 2 3 4 5)) last |
398 |
(SortedCollection withAll:#(5 4 3 2 1)) last |
|
93 | 399 |
" |
1 | 400 |
! ! |
401 |
||
402 |
!OrderedCollection methodsFor:'adding & removing'! |
|
403 |
||
606 | 404 |
add:anObject |
405 |
"add the argument, anObject to the end of the collection |
|
406 |
Return the argument, anObject." |
|
407 |
||
408 |
|idx "{ Class:SmallInteger }"| |
|
1 | 409 |
|
606 | 410 |
idx := lastIndex. |
1 | 411 |
|
606 | 412 |
(idx == contentsArray size) ifTrue:[ |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
413 |
self makeRoomAtLast. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
414 |
idx := lastIndex. |
42 | 415 |
]. |
3107 | 416 |
lastIndex := idx := idx + 1. |
417 |
contentsArray basicAt:idx put:anObject. |
|
1 | 418 |
^ anObject |
42 | 419 |
|
348 | 420 |
" |
606 | 421 |
|c| |
422 |
c := #(4 3 2 1) asOrderedCollection. |
|
423 |
c add:'here' |
|
348 | 424 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
425 |
|
3107 | 426 |
"Modified: / 12.11.1997 / 17:49:47 / cg" |
1 | 427 |
! |
428 |
||
606 | 429 |
add:newObject after:oldObject |
430 |
"insert the argument, newObject after oldObject. |
|
431 |
If oldObject is not in the receiver, report an error, |
|
432 |
otherwise return the argument, anObject." |
|
1 | 433 |
|
606 | 434 |
|idx| |
1 | 435 |
|
606 | 436 |
idx := self indexOf:oldObject. |
437 |
idx ~~ 0 ifTrue:[ |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
438 |
self add:newObject beforeIndex:(idx + 1). |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
439 |
^ newObject |
42 | 440 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
441 |
^ self errorValueNotFound:oldObject |
42 | 442 |
|
348 | 443 |
" |
606 | 444 |
|c| |
445 |
c := #(4 3 2 1) asOrderedCollection. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
446 |
c add:'here' after:3; yourself. |
606 | 447 |
" |
448 |
" |
|
449 |
|c| |
|
450 |
c := #(4 3 2 1) asOrderedCollection. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
451 |
c add:'here' after:1; yourself. |
606 | 452 |
" |
453 |
" |
|
454 |
|c| |
|
455 |
c := #(4 3 2 1) asOrderedCollection. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
456 |
c add:'here' after:5; yourself |
606 | 457 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
458 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
459 |
"Modified: 12.4.1996 / 13:51:56 / cg" |
606 | 460 |
! |
461 |
||
462 |
add:anObject afterIndex:index |
|
463 |
"insert the argument, anObject to become located at index. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
464 |
Return the receiver (sigh - ST-80 compatibility)." |
606 | 465 |
|
466 |
^ self add:anObject beforeIndex:(index + 1) |
|
467 |
||
468 |
" |
|
469 |
|c| |
|
470 |
c := #(4 3 2 1) asOrderedCollection. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
471 |
c add:'here' afterIndex:2 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
472 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
473 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
474 |
|c| |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
475 |
c := #(4 3 2 1) asOrderedCollection. |
606 | 476 |
c add:'here' afterIndex:4 |
348 | 477 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
478 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
479 |
|c| |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
480 |
c := #(4 3 2 1) asOrderedCollection. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
481 |
c add:'here' afterIndex:0 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
482 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
483 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
484 |
|c| |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
485 |
c := #(4 3 2 1) asOrderedCollection. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
486 |
c add:'here' afterIndex:5 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
487 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
488 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
489 |
"Modified: 12.4.1996 / 13:52:41 / cg" |
348 | 490 |
! |
491 |
||
606 | 492 |
add:newObject before:oldObject |
493 |
"insert the argument, newObject before oldObject. |
|
494 |
If oldObject is not in the receiver, report an error, |
|
495 |
otherwise return the argument, anObject." |
|
348 | 496 |
|
606 | 497 |
|idx| |
348 | 498 |
|
606 | 499 |
idx := self indexOf:oldObject. |
500 |
idx ~~ 0 ifTrue:[ |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
501 |
self add:newObject beforeIndex:idx. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
502 |
^ newObject |
348 | 503 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
504 |
^ self errorValueNotFound:oldObject |
348 | 505 |
|
506 |
" |
|
606 | 507 |
|c| |
508 |
c := #(4 3 2 1) asOrderedCollection. |
|
509 |
c add:'here' before:3. |
|
510 |
c add:'here' before:5 |
|
348 | 511 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
512 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
513 |
"Modified: 12.4.1996 / 13:25:47 / cg" |
348 | 514 |
! |
515 |
||
606 | 516 |
add:anObject beforeIndex:index |
517 |
"insert the argument, anObject to become located at index. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
518 |
Return the receiver (sigh - ST-80 compatibility)." |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
519 |
|
3236
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
520 |
|idx physicalIndex| |
348 | 521 |
|
3236
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
522 |
physicalIndex := index + firstIndex - 1. |
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
523 |
physicalIndex > lastIndex ifTrue:[ |
10839
285fa5534f61
#add:beforeIndex: take care for redefinitions of #add: in subclasses sending #add:beforeIndex:
Stefan Vogel <sv@exept.de>
parents:
10799
diff
changeset
|
524 |
"can only add to the end of the collection" |
3236
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
525 |
physicalIndex == (lastIndex+1) ifTrue:[ |
10839
285fa5534f61
#add:beforeIndex: take care for redefinitions of #add: in subclasses sending #add:beforeIndex:
Stefan Vogel <sv@exept.de>
parents:
10799
diff
changeset
|
526 |
idx := lastIndex. |
285fa5534f61
#add:beforeIndex: take care for redefinitions of #add: in subclasses sending #add:beforeIndex:
Stefan Vogel <sv@exept.de>
parents:
10799
diff
changeset
|
527 |
(idx == contentsArray size) ifTrue:[ |
285fa5534f61
#add:beforeIndex: take care for redefinitions of #add: in subclasses sending #add:beforeIndex:
Stefan Vogel <sv@exept.de>
parents:
10799
diff
changeset
|
528 |
self makeRoomAtLast. |
285fa5534f61
#add:beforeIndex: take care for redefinitions of #add: in subclasses sending #add:beforeIndex:
Stefan Vogel <sv@exept.de>
parents:
10799
diff
changeset
|
529 |
idx := lastIndex. |
285fa5534f61
#add:beforeIndex: take care for redefinitions of #add: in subclasses sending #add:beforeIndex:
Stefan Vogel <sv@exept.de>
parents:
10799
diff
changeset
|
530 |
]. |
285fa5534f61
#add:beforeIndex: take care for redefinitions of #add: in subclasses sending #add:beforeIndex:
Stefan Vogel <sv@exept.de>
parents:
10799
diff
changeset
|
531 |
lastIndex := idx := idx + 1. |
285fa5534f61
#add:beforeIndex: take care for redefinitions of #add: in subclasses sending #add:beforeIndex:
Stefan Vogel <sv@exept.de>
parents:
10799
diff
changeset
|
532 |
] ifFalse:[ |
285fa5534f61
#add:beforeIndex: take care for redefinitions of #add: in subclasses sending #add:beforeIndex:
Stefan Vogel <sv@exept.de>
parents:
10799
diff
changeset
|
533 |
^ self subscriptBoundsError:index. |
285fa5534f61
#add:beforeIndex: take care for redefinitions of #add: in subclasses sending #add:beforeIndex:
Stefan Vogel <sv@exept.de>
parents:
10799
diff
changeset
|
534 |
] |
285fa5534f61
#add:beforeIndex: take care for redefinitions of #add: in subclasses sending #add:beforeIndex:
Stefan Vogel <sv@exept.de>
parents:
10799
diff
changeset
|
535 |
] ifFalse:[ |
285fa5534f61
#add:beforeIndex: take care for redefinitions of #add: in subclasses sending #add:beforeIndex:
Stefan Vogel <sv@exept.de>
parents:
10799
diff
changeset
|
536 |
"notice: this may change firstIndex" |
285fa5534f61
#add:beforeIndex: take care for redefinitions of #add: in subclasses sending #add:beforeIndex:
Stefan Vogel <sv@exept.de>
parents:
10799
diff
changeset
|
537 |
idx := self makeRoomAtIndex:physicalIndex. |
3236
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
538 |
]. |
3107 | 539 |
contentsArray basicAt:idx put:anObject. |
348 | 540 |
|
541 |
" |
|
606 | 542 |
|c| |
543 |
c := #(4 3 2 1) asOrderedCollection. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
544 |
c add:'here' beforeIndex:3 |
606 | 545 |
" |
546 |
" |
|
547 |
|c| |
|
548 |
c := #(4 3 2 1) asOrderedCollection. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
549 |
c add:'here' beforeIndex:1 |
606 | 550 |
" |
551 |
" |
|
552 |
|c| |
|
553 |
c := #(4 3 2 1) asOrderedCollection. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
554 |
c add:'here' beforeIndex:5 |
348 | 555 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
556 |
|
3236
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
557 |
"Modified: / 28.1.1998 / 17:17:48 / cg" |
1 | 558 |
! |
559 |
||
3109 | 560 |
addAll:aCollection |
7752 | 561 |
"add all elements of the argument, aCollection to the receiver. |
562 |
Returns the argument, aCollection (sigh)." |
|
3109 | 563 |
|
564 |
self addAll:aCollection beforeIndex:(1 + self size). |
|
565 |
^ aCollection |
|
566 |
! |
|
567 |
||
15442 | 568 |
addAll:aCollection afterIndex:index |
569 |
"insert the argument, anObject to become located after index. |
|
570 |
Return the receiver (sigh - ST-80 compatibility)." |
|
571 |
||
572 |
^ self addAll:aCollection beforeIndex:(index + 1) |
|
573 |
||
574 |
" |
|
575 |
|c| |
|
576 |
c := #(4 3 2 1) asOrderedCollection. |
|
577 |
c addAll:#(10 20 30) afterIndex:2 |
|
578 |
" |
|
579 |
" |
|
580 |
|c| |
|
581 |
c := #(4 3 2 1) asOrderedCollection. |
|
582 |
c addAll:#(10 20 30) afterIndex:4 |
|
583 |
" |
|
584 |
" |
|
585 |
|c| |
|
586 |
c := #(4 3 2 1) asOrderedCollection. |
|
587 |
c addAll:#(10 20 30) afterIndex:0 |
|
588 |
" |
|
589 |
||
590 |
"Modified: 12.4.1996 / 13:52:41 / cg" |
|
591 |
! |
|
592 |
||
2222 | 593 |
addAll:aCollection beforeIndex:index |
594 |
"insert all elements of the argument, anObject to become located at index. |
|
595 |
The collection may be unordered, but then order of the sliced-in elements |
|
596 |
is undefined. |
|
597 |
Return the receiver." |
|
598 |
||
599 |
|idx count| |
|
600 |
||
601 |
aCollection isSequenceable ifTrue:[ |
|
602 |
"/ we are lucky - that thing can count & do bulk copies |
|
603 |
||
604 |
count := aCollection size. |
|
2549 | 605 |
idx := self makeRoomAtIndex:(index + firstIndex - 1) for:count. |
2222 | 606 |
"/ notice: the above may change firstIndex |
607 |
contentsArray replaceFrom:idx to:(idx + count - 1) with:aCollection startingAt:1. |
|
608 |
^ self |
|
609 |
]. |
|
610 |
||
611 |
idx := index. |
|
612 |
aCollection do:[:element | |
|
613 |
self add:element beforeIndex:idx. |
|
614 |
idx := idx + 1. |
|
615 |
]. |
|
616 |
||
617 |
" |
|
618 |
|c| |
|
619 |
c := #(1 2 3 4) asOrderedCollection. |
|
620 |
c addAll:'here' beforeIndex:3 |
|
621 |
" |
|
622 |
" |
|
623 |
|c| |
|
624 |
c := #(1 2 3 4) asOrderedCollection. |
|
2549 | 625 |
c removeFirst. |
626 |
c addAll:'here' beforeIndex:3 |
|
627 |
" |
|
628 |
" |
|
629 |
|c| |
|
630 |
c := #(1 2 3 4) asOrderedCollection. |
|
2222 | 631 |
c addAll:'here' beforeIndex:1 |
632 |
" |
|
633 |
" |
|
634 |
|c| |
|
635 |
c := #(1 2 3 4) asOrderedCollection. |
|
636 |
c addAll:'here' beforeIndex:5 |
|
637 |
" |
|
638 |
||
639 |
" |
|
640 |
|c| |
|
641 |
c := #(1 2 3 4) asOrderedCollection. |
|
642 |
c addAll:('hello' asSet) beforeIndex:3 |
|
643 |
" |
|
644 |
||
2549 | 645 |
"Modified: 15.4.1997 / 12:43:59 / cg" |
2222 | 646 |
! |
647 |
||
606 | 648 |
addFirst:anObject |
649 |
"add the argument, anObject to the beginning of the collection. |
|
650 |
Return the argument, anObject." |
|
56 | 651 |
|
3107 | 652 |
|idx "{ Class:SmallInteger }"| |
653 |
||
654 |
(idx := firstIndex) == 1 ifTrue:[ |
|
655 |
self makeRoomAtFront. |
|
656 |
idx := firstIndex. |
|
348 | 657 |
]. |
3107 | 658 |
firstIndex := idx := idx - 1. |
659 |
contentsArray basicAt:idx put:anObject. |
|
606 | 660 |
^ anObject |
56 | 661 |
|
662 |
" |
|
606 | 663 |
|c| |
664 |
c := #(1 2 3 4) asOrderedCollection. |
|
665 |
c addFirst:'here'. |
|
666 |
c |
|
667 |
" |
|
668 |
||
669 |
" |
|
670 |
|c| |
|
671 |
c := #() asOrderedCollection. |
|
672 |
c addFirst:'here'. |
|
673 |
c |
|
56 | 674 |
" |
3107 | 675 |
|
676 |
"Modified: / 12.11.1997 / 17:58:05 / cg" |
|
677 |
! |
|
678 |
||
3368 | 679 |
clearContents |
680 |
"remove all elements from the collection but keep the contentsArray. |
|
681 |
Useful for huge lists, if the contents will be rebuild soon (using #add:) |
|
682 |
to a size which is similar to the lists current size. |
|
16304 | 683 |
Destructive: modifies the receiver. |
3368 | 684 |
Returns the receiver." |
685 |
||
686 |
"/ clear those references, to give the garbage collector |
|
687 |
"/ a chance ... |
|
688 |
||
689 |
firstIndex >= lastIndex ifTrue:[ |
|
690 |
contentsArray from:firstIndex to:lastIndex put:nil. |
|
691 |
]. |
|
692 |
firstIndex := 1. |
|
693 |
lastIndex := 0. |
|
694 |
||
695 |
"/ this is the same as: |
|
696 |
"/ self initContents:(self size) |
|
697 |
||
698 |
"Modified: 12.4.1996 / 13:34:19 / cg" |
|
699 |
! |
|
700 |
||
1 | 701 |
remove:anObject ifAbsent:exceptionBlock |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
702 |
"remove the first element which is equal to anObject; |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
703 |
if found, remove and return it; |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
704 |
if not, return the value from evaluating exceptionBlock. |
16304 | 705 |
Destructive: modifies the receiver. |
2362 | 706 |
Uses equality compare (=) to search for the element." |
1 | 707 |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
708 |
|index retVal| |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
709 |
|
12035 | 710 |
index := self indexOf:anObject. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
711 |
index ~~ 0 ifTrue:[ |
12035 | 712 |
retVal := contentsArray at:index+firstIndex-1. |
2361
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
713 |
self removeFromIndex:index toIndex:index. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
714 |
^ retVal |
1 | 715 |
]. |
716 |
^ exceptionBlock value |
|
348 | 717 |
|
718 |
" |
|
12035 | 719 |
#(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:[self halt]; yourself |
720 |
#(1 2 3 4 5) asOrderedCollection remove:3 ifAbsent:[self halt]; yourself |
|
1496 | 721 |
|
12035 | 722 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection remove:3 ifAbsent:'oops' |
348 | 723 |
#(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:'oops' |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
724 |
#(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops' |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
725 |
#(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' |
348 | 726 |
" |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
727 |
|
2362 | 728 |
"Modified: 8.2.1997 / 19:17:19 / cg" |
1 | 729 |
! |
730 |
||
114 | 731 |
removeAll |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
732 |
"remove all elements from the collection. |
16304 | 733 |
Returns the receiver. |
734 |
Destructive: modifies the receiver." |
|
114 | 735 |
|
18879 | 736 |
self initContents:0 |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
737 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
738 |
"Modified: 12.4.1996 / 13:34:19 / cg" |
114 | 739 |
! |
740 |
||
348 | 741 |
removeAllSuchThat:aBlock |
742 |
"remove all elements that meet a test criteria as specified in aBlock. |
|
743 |
The argument, aBlock is evaluated for successive elements and all those, |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
744 |
for which it returns true, are removed. |
16304 | 745 |
Destructive: modifies the receiver. |
1496 | 746 |
Return a collection containing the removed elements. |
747 |
||
16304 | 748 |
Performance: |
19831 | 749 |
this is an O(N) algorithm (the receiver's elements are scanned once)." |
348 | 750 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
751 |
"/ this is a q&d implementation (possibly slow). |
348 | 752 |
|
19831 | 753 |
|removed element |
754 |
srcIndex "{ Class: SmallInteger }" |
|
19865 | 755 |
dstIndex "{ Class: SmallInteger }" |
756 |
lastIdx "{ Class: SmallInteger }"| |
|
348 | 757 |
|
19831 | 758 |
"/ first search forward to the first element which has |
759 |
"/ to be removed (meets the criteria) |
|
760 |
srcIndex := firstIndex. |
|
19865 | 761 |
lastIdx := lastIndex. |
762 |
||
19831 | 763 |
[ |
19865 | 764 |
srcIndex > lastIdx |
19831 | 765 |
or:[ aBlock value:(contentsArray at:srcIndex) ] |
766 |
] whileFalse:[ |
|
767 |
srcIndex := srcIndex + 1. |
|
768 |
]. |
|
19865 | 769 |
srcIndex > lastIdx ifTrue:[ |
19831 | 770 |
"/ nothing removed |
771 |
^ #() |
|
772 |
]. |
|
773 |
||
774 |
"/ now srcIndex is the index of element, which is the first to be removed |
|
775 |
removed := OrderedCollection new. |
|
776 |
removed add:(contentsArray at:srcIndex). |
|
777 |
||
778 |
dstIndex := srcIndex. |
|
779 |
srcIndex := srcIndex + 1. |
|
780 |
||
19865 | 781 |
srcIndex to:lastIdx do:[:idx| |
782 |
element := contentsArray at:idx. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
783 |
(aBlock value:element) ifTrue:[ |
16304 | 784 |
removed add:element |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
785 |
] ifFalse:[ |
16304 | 786 |
contentsArray at:dstIndex put:element. |
787 |
dstIndex := dstIndex + 1. |
|
788 |
]. |
|
348 | 789 |
]. |
19865 | 790 |
contentsArray from:dstIndex to:lastIdx put:nil. |
16304 | 791 |
lastIndex := dstIndex - 1. |
348 | 792 |
^ removed |
793 |
||
794 |
" |
|
795 |
|coll| |
|
796 |
||
797 |
coll := OrderedCollection withAll:(1 to:10). |
|
19831 | 798 |
Transcript show:'removed: '; showCR:(coll removeAllSuchThat:[:el | el even]). |
799 |
Transcript show:'coll: '; showCR:coll |
|
348 | 800 |
" |
19831 | 801 |
|
16304 | 802 |
" |
803 |
|coll1 coll2| |
|
804 |
||
19831 | 805 |
Transcript showCR:'no element removed:'. |
806 |
||
16304 | 807 |
coll1 := OrderedCollection withAll:(1 to:1000). |
19831 | 808 |
Transcript show:' (1000) - '; showCR:( |
809 |
Time millisecondsToRun:[ |
|
810 |
100 timesRepeat:[ coll1 copy removeAllSuchThat:[:el | el == 500] ] |
|
811 |
]). |
|
812 |
||
813 |
coll2 := OrderedCollection withAll:(1 to:10000). |
|
814 |
Transcript show:' (10000) - '; showCR:( |
|
815 |
Time millisecondsToRun:[ |
|
816 |
100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el == 5000] ] |
|
817 |
]). |
|
818 |
||
819 |
coll2 := OrderedCollection withAll:(1 to:50000). |
|
820 |
Transcript show:' (50000) - '; showCR:( |
|
821 |
Time millisecondsToRun:[ |
|
822 |
100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el == 25000] ] |
|
823 |
]). |
|
824 |
||
825 |
Transcript showCR:'small number of elements removed:'. |
|
826 |
||
827 |
coll1 := OrderedCollection withAll:(1 to:1000). |
|
828 |
Transcript show:' (1000) - '; showCR:( |
|
829 |
Time millisecondsToRun:[ |
|
830 |
100 timesRepeat:[ coll1 copy removeAllSuchThat:[:el | el between:500 and:550] ] |
|
831 |
]). |
|
832 |
||
833 |
coll2 := OrderedCollection withAll:(1 to:10000). |
|
834 |
Transcript show:' (10000) - '; showCR:( |
|
835 |
Time millisecondsToRun:[ |
|
836 |
100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el between:5000 and:5050] ] |
|
837 |
]). |
|
838 |
||
839 |
coll2 := OrderedCollection withAll:(1 to:50000). |
|
840 |
Transcript show:' (50000) - '; showCR:( |
|
841 |
Time millisecondsToRun:[ |
|
842 |
100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el between:25000 and:25050] ] |
|
843 |
]). |
|
844 |
||
845 |
Transcript showCR:'many elements removed:'. |
|
846 |
||
847 |
coll1 := OrderedCollection withAll:(1 to:1000). |
|
848 |
Transcript show:' (1000) - '; showCR:( |
|
16304 | 849 |
Time millisecondsToRun:[ |
850 |
100 timesRepeat:[ coll1 copy removeAllSuchThat:[:el | el even] ] |
|
851 |
]). |
|
852 |
||
853 |
coll2 := OrderedCollection withAll:(1 to:10000). |
|
19831 | 854 |
Transcript show:' (10000) - '; showCR:( |
16304 | 855 |
Time millisecondsToRun:[ |
856 |
100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el even] ] |
|
857 |
]). |
|
858 |
||
859 |
coll2 := OrderedCollection withAll:(1 to:50000). |
|
19831 | 860 |
Transcript show:' (50000) - '; showCR:( |
16304 | 861 |
Time millisecondsToRun:[ |
862 |
100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el even] ] |
|
863 |
]). |
|
864 |
" |
|
19831 | 865 |
" |
866 |
compiled bytecode bytecode/new (no jitter) |
|
867 |
no element removed: |
|
868 |
(1000) - 10 20 20 |
|
869 |
(10000) - 70 240 200 |
|
870 |
(50000) - 390 1190 940 |
|
871 |
small number of elements removed: |
|
872 |
(1000) - 10 30 20 |
|
873 |
(10000) - 160 300 260 |
|
874 |
(50000) - 700 1540 1180 |
|
875 |
many elements removed: |
|
876 |
(1000) - 10 20 30 |
|
877 |
(10000) - 130 290 260 |
|
878 |
(50000) - 720 1470 1300 |
|
879 |
" |
|
2362 | 880 |
"Modified: 8.2.1997 / 19:19:00 / cg" |
348 | 881 |
! |
882 |
||
606 | 883 |
removeFirst |
3695 | 884 |
"remove the first element from the collection; return the element. |
16304 | 885 |
If there is no element in the receiver collection, raise an error. |
886 |
Destructive: modifies the receiver" |
|
202 | 887 |
|
3968 | 888 |
|anObject |
889 |
fI "{ Class: SmallInteger }" | |
|
13 | 890 |
|
2362 | 891 |
fI := firstIndex. |
892 |
||
893 |
fI > lastIndex ifTrue:[ |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
894 |
"error if collection is empty" |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
895 |
^ self emptyCollectionError. |
606 | 896 |
]. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
897 |
|
3107 | 898 |
anObject := contentsArray basicAt:fI. |
1496 | 899 |
|
900 |
"/ nil it out, to allow GC to reclaim it. |
|
3107 | 901 |
contentsArray basicAt:fI put:nil. |
1496 | 902 |
|
2362 | 903 |
fI := fI + 1. |
1 | 904 |
|
2362 | 905 |
fI > lastIndex ifTrue:[ |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
906 |
"reset to avoid ever growing" |
2362 | 907 |
fI := 1. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
908 |
lastIndex := 0 |
1 | 909 |
]. |
2362 | 910 |
firstIndex := fI. |
1 | 911 |
^ anObject |
13 | 912 |
|
913 |
" |
|
606 | 914 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself |
915 |
OrderedCollection new removeFirst |
|
916 |
(SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself |
|
93 | 917 |
" |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
918 |
|
3968 | 919 |
"Modified: / 5.2.1999 / 23:22:58 / cg" |
606 | 920 |
! |
921 |
||
922 |
removeFirst:n |
|
923 |
"remove the first n elements from the collection; |
|
16304 | 924 |
Return a collection containing the removed elements. |
925 |
Destructive: modifies the receiver" |
|
606 | 926 |
|
2362 | 927 |
|mySize ret newFirstIndex| |
606 | 928 |
|
929 |
mySize := self size. |
|
930 |
mySize < n ifTrue:[ |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
931 |
"error if collection has not enough elements" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
932 |
^ self notEnoughElementsError. |
606 | 933 |
]. |
934 |
||
935 |
ret := Array new:n. |
|
936 |
ret replaceFrom:1 to:n with:contentsArray startingAt:firstIndex. |
|
1496 | 937 |
|
606 | 938 |
"/ |
939 |
"/ nil-out contents array, to not keep elements from being GC'd |
|
940 |
"/ |
|
2362 | 941 |
newFirstIndex := firstIndex + n. |
942 |
contentsArray from:firstIndex to:newFirstIndex - 1 put:nil. |
|
606 | 943 |
|
2362 | 944 |
newFirstIndex > lastIndex ifTrue:[ |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
945 |
"reset to avoid ever growing" |
2362 | 946 |
newFirstIndex := 1. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
947 |
lastIndex := 0 |
606 | 948 |
]. |
2362 | 949 |
firstIndex := newFirstIndex. |
606 | 950 |
^ ret |
93 | 951 |
|
952 |
" |
|
606 | 953 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:2; yourself |
954 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:0; yourself |
|
955 |
OrderedCollection new removeFirst:2 |
|
956 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:6 |
|
957 |
(SortedCollection withAll:#(5 4 3 2 1)) removeFirst:2; yourself |
|
13 | 958 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
959 |
|
2362 | 960 |
"Modified: 8.2.1997 / 19:20:18 / cg" |
1 | 961 |
! |
962 |
||
3695 | 963 |
removeFirstIfAbsent:exceptionBlock |
964 |
"remove the first element from the collection; return the element. |
|
965 |
If there is no element in the receiver collection, return the value from |
|
16304 | 966 |
exceptionBlock. |
967 |
Destructive: modifies the receiver" |
|
3695 | 968 |
|
969 |
|anObject fI "{ Class: SmallInteger }" | |
|
970 |
||
971 |
fI := firstIndex. |
|
972 |
||
973 |
fI > lastIndex ifTrue:[ |
|
974 |
"error if collection is empty" |
|
975 |
^ exceptionBlock value. |
|
976 |
]. |
|
977 |
||
978 |
anObject := contentsArray basicAt:fI. |
|
979 |
||
980 |
"/ nil it out, to allow GC to reclaim it. |
|
981 |
contentsArray basicAt:fI put:nil. |
|
982 |
||
983 |
fI := fI + 1. |
|
984 |
||
985 |
fI > lastIndex ifTrue:[ |
|
986 |
"reset to avoid ever growing" |
|
987 |
fI := 1. |
|
988 |
lastIndex := 0 |
|
989 |
]. |
|
990 |
firstIndex := fI. |
|
991 |
^ anObject |
|
992 |
||
993 |
" |
|
994 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself |
|
995 |
OrderedCollection new removeFirst |
|
996 |
(SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself |
|
997 |
" |
|
998 |
||
999 |
"Modified: / 12.11.1997 / 17:58:43 / cg" |
|
1000 |
"Created: / 30.7.1998 / 13:19:42 / cg" |
|
1001 |
! |
|
1002 |
||
2549 | 1003 |
removeFrom:startIndex to:stopIndex |
1004 |
"added for ST-80 compatibility. |
|
1005 |
Same as removeFromIndex:toIndex:." |
|
1006 |
||
1007 |
^ self removeFromIndex:startIndex toIndex:stopIndex |
|
1008 |
||
1009 |
"Created: 15.4.1997 / 12:39:00 / cg" |
|
1010 |
! |
|
1011 |
||
606 | 1012 |
removeFromIndex:startIndex toIndex:stopIndex |
1013 |
"remove the elements stored under startIndex up to and including |
|
1014 |
the elements under stopIndex. |
|
16304 | 1015 |
Destructive: modifies the receiver |
606 | 1016 |
Return the receiver. |
1017 |
Returning the receiver here is a historic leftover - it may change. |
|
14917 | 1018 |
Please use yourself in a cascade, if you need the receiver's value |
606 | 1019 |
when using this method." |
1020 |
||
3968 | 1021 |
|nDeleted "{ Class: SmallInteger }" |
1022 |
fI "{ Class: SmallInteger }" |
|
1023 |
lI "{ Class: SmallInteger }" |
|
2364 | 1024 |
newLastIndex sz| |
1 | 1025 |
|
2364 | 1026 |
sz := self size. |
1027 |
||
1028 |
(startIndex < 1 or:[stopIndex > sz]) ifTrue:[ |
|
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1029 |
^ self notEnoughElementsError |
606 | 1030 |
]. |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1031 |
|
2362 | 1032 |
nDeleted := stopIndex - startIndex + 1. |
1033 |
nDeleted < 0 ifTrue:[ |
|
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1034 |
"/ mhmh - what should be done here ? |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1035 |
^ self error:'bad index range' |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1036 |
]. |
2362 | 1037 |
nDeleted == 0 ifTrue:[^ self]. |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1038 |
|
3968 | 1039 |
fI := firstIndex. |
1040 |
lI := lastIndex. |
|
1041 |
||
2362 | 1042 |
"/ |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1043 |
"/ can be done faster, when removing the first elements |
2362 | 1044 |
"/ |
2364 | 1045 |
startIndex == 1 ifTrue:[ |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1046 |
"/ nil out (helps GC) |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1047 |
contentsArray |
3968 | 1048 |
from:fI |
1049 |
to:fI + nDeleted - 1 |
|
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1050 |
put:nil. |
3968 | 1051 |
firstIndex := fI := fI + nDeleted |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1052 |
] ifFalse:[ |
2362 | 1053 |
"/ |
1054 |
"/ can be done faster, when removing the last elements |
|
1055 |
"/ |
|
2364 | 1056 |
stopIndex == sz ifTrue:[ |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1057 |
"/ nil out (helps GC) |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1058 |
contentsArray |
3968 | 1059 |
from:lI - nDeleted + 1 |
1060 |
to:lI |
|
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1061 |
put:nil. |
3968 | 1062 |
lastIndex := lI := lI - nDeleted |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1063 |
] ifFalse:[ |
2362 | 1064 |
"/ |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1065 |
"/ must shuffle |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1066 |
"/ TODO: |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1067 |
"/ for big collections, try to copy the smallest |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1068 |
"/ possible number of elements |
606 | 1069 |
|
3968 | 1070 |
newLastIndex := lI - nDeleted. |
2362 | 1071 |
|
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1072 |
contentsArray |
3968 | 1073 |
replaceFrom:(fI + startIndex - 1) |
2362 | 1074 |
to:newLastIndex |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1075 |
with:contentsArray |
3968 | 1076 |
startingAt:(fI + stopIndex). |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1077 |
|
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1078 |
"/ nil out rest (helps GC) |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1079 |
contentsArray |
2362 | 1080 |
from:(newLastIndex + 1) |
3968 | 1081 |
to:lI |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1082 |
put:nil. |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1083 |
|
3968 | 1084 |
lastIndex := lI := newLastIndex. |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1085 |
] |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1086 |
]. |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1087 |
|
3968 | 1088 |
fI > lI ifTrue:[ |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1089 |
"reset to avoid ever growing" |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1090 |
firstIndex := 1. |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1091 |
lastIndex := 0 |
606 | 1092 |
] |
13 | 1093 |
|
1094 |
" |
|
606 | 1095 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:3 toIndex:6 |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1096 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:6 toIndex:8 |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1097 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:1 toIndex:3 |
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1098 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:6 toIndex:9 |
606 | 1099 |
#(1 2 3 4 5) asOrderedCollection removeFromIndex:3 toIndex:6 |
217 | 1100 |
" |
2355
9476ba4d810d
faster #removeFromIndex:toIndex.
Claus Gittinger <cg@exept.de>
parents:
2354
diff
changeset
|
1101 |
|
3968 | 1102 |
"Modified: / 5.2.1999 / 23:22:07 / cg" |
217 | 1103 |
! |
1104 |
||
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
1105 |
removeIdentical:anObject ifAbsent:exceptionBlock |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
1106 |
"remove the first element which is identical to anObject; |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
1107 |
if found, remove and return it; |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
1108 |
if not, return the value from evaluating exceptionBlock. |
16304 | 1109 |
Destructive: modifies the receiver. |
2361
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
1110 |
Uses identity compare (==) to search for the element." |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
1111 |
|
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
1112 |
|index| |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
1113 |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
1114 |
index := contentsArray identityIndexOf:anObject startingAt:firstIndex endingAt:lastIndex. |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
1115 |
index ~~ 0 ifTrue:[ |
6755
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1116 |
index == firstIndex ifTrue:[ |
6756 | 1117 |
contentsArray basicAt:firstIndex put:nil. |
6755
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1118 |
firstIndex := firstIndex + 1. |
6756 | 1119 |
firstIndex > lastIndex ifTrue:[ |
1120 |
"reset to avoid ever growing" |
|
1121 |
firstIndex := 1. |
|
1122 |
lastIndex := 0 |
|
1123 |
]. |
|
6755
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1124 |
] ifFalse:[ |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1125 |
index == lastIndex ifTrue:[ |
6756 | 1126 |
contentsArray basicAt:lastIndex put:nil. |
6755
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1127 |
lastIndex := lastIndex - 1. |
6756 | 1128 |
firstIndex > lastIndex ifTrue:[ |
1129 |
"reset to avoid ever growing" |
|
1130 |
firstIndex := 1. |
|
1131 |
lastIndex := 0 |
|
1132 |
]. |
|
6755
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1133 |
] ifFalse:[ |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1134 |
index := index - firstIndex + 1. |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1135 |
self removeFromIndex:index toIndex:index. |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1136 |
] |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1137 |
]. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
1138 |
^ anObject |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
1139 |
]. |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
1140 |
^ exceptionBlock value |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
1141 |
|
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
1142 |
" |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
1143 |
#(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops' |
2361
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
1144 |
#(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops'; yourself |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
1145 |
#(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' |
6755
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1146 |
#(fee foo bar baz) asOrderedCollection removeIdentical:#fee; yourself |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1147 |
#(fee foo bar baz) asOrderedCollection removeIdentical:#foo; yourself |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1148 |
#(fee foo bar baz) asOrderedCollection removeIdentical:#baz; yourself |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1149 |
#(fee) asOrderedCollection removeIdentical:#fee; yourself |
a5704d23e08f
slightly faster removeIdentical
Claus Gittinger <cg@exept.de>
parents:
6700
diff
changeset
|
1150 |
#(fee) asOrderedCollection removeIdentical:#foo; yourself |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
1151 |
" |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
1152 |
|
2361
dd95b53674e9
oops - removeIdentical was wrong
Claus Gittinger <cg@exept.de>
parents:
2355
diff
changeset
|
1153 |
"Modified: 8.2.1997 / 18:57:43 / cg" |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
1154 |
! |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
1155 |
|
16304 | 1156 |
removeIndices:aSortedCollectionOfIndices |
1157 |
"remove all elements stored in any of aSortedCollectionOfIndices, |
|
1158 |
which must be sorted and sequenceable. |
|
1159 |
Destructive: modifies the receiver. |
|
1160 |
Returns a collection of removed elements. |
|
1161 |
||
1162 |
Performance: |
|
18184 | 1163 |
this is an O(N) algorithm (N being the size of the receiver). |
1164 |
||
1165 |
This could be done much better, especially if the removed indices are |
|
1166 |
at either end of the receiver. However, as it is currently not heavily used, |
|
1167 |
I leave that as an exercise to the brave reader..." |
|
16304 | 1168 |
|
1169 |
|removed element dstIndex indexIndex nextIndex numIndices| |
|
1170 |
||
1171 |
removed := OrderedCollection new. |
|
1172 |
||
1173 |
dstIndex := firstIndex. |
|
1174 |
numIndices := aSortedCollectionOfIndices size. |
|
1175 |
indexIndex := 1. |
|
1176 |
nextIndex := aSortedCollectionOfIndices at:1. |
|
1177 |
firstIndex to: lastIndex do:[:srcIndex | |
|
1178 |
element := contentsArray at:srcIndex. |
|
1179 |
srcIndex == nextIndex ifTrue:[ |
|
1180 |
removed add:element. |
|
1181 |
indexIndex := indexIndex + 1. |
|
1182 |
indexIndex > numIndices ifTrue:[ |
|
1183 |
nextIndex := nil. |
|
1184 |
] ifFalse:[ |
|
1185 |
nextIndex := aSortedCollectionOfIndices at:indexIndex. |
|
1186 |
]. |
|
1187 |
] ifFalse:[ |
|
1188 |
contentsArray at:dstIndex put:element. |
|
1189 |
dstIndex := dstIndex + 1. |
|
1190 |
]. |
|
1191 |
]. |
|
1192 |
contentsArray from:dstIndex to:lastIndex put:nil. |
|
1193 |
lastIndex := dstIndex - 1. |
|
1194 |
^ removed |
|
1195 |
||
1196 |
" |
|
1197 |
|coll| |
|
1198 |
||
1199 |
coll := OrderedCollection withAll:(1 to:10). |
|
1200 |
Transcript showCR:(coll removeIndices:#(1 5 7)). |
|
1201 |
Transcript showCR:coll |
|
1202 |
" |
|
1203 |
! |
|
1204 |
||
606 | 1205 |
removeLast |
16304 | 1206 |
"remove the last element from the collection. |
1207 |
Return the removed element. |
|
1208 |
Destructive: modifies the receiver" |
|
606 | 1209 |
|
3107 | 1210 |
|anObject |
1211 |
idx "{ Class: SmallInteger }" | |
|
217 | 1212 |
|
3107 | 1213 |
idx := lastIndex. |
1214 |
firstIndex > idx ifTrue:[ |
|
1215 |
"error if collection is empty" |
|
1216 |
^ self emptyCollectionError. |
|
606 | 1217 |
]. |
3107 | 1218 |
anObject := contentsArray basicAt:idx. |
1496 | 1219 |
|
1220 |
"/ nil it (helps GC) |
|
3107 | 1221 |
contentsArray basicAt:idx put:nil. |
1222 |
lastIndex := idx := idx - 1. |
|
606 | 1223 |
|
3107 | 1224 |
firstIndex > idx ifTrue:[ |
1225 |
"reset to avoid ever growing" |
|
1226 |
firstIndex := 1. |
|
1227 |
lastIndex := 0 |
|
606 | 1228 |
]. |
1229 |
^ anObject |
|
217 | 1230 |
|
1231 |
" |
|
606 | 1232 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast; yourself |
1233 |
OrderedCollection new removeLast |
|
1234 |
(SortedCollection withAll:#(5 4 3 2 1)) removeLast; yourself |
|
217 | 1235 |
" |
3107 | 1236 |
|
1237 |
"Modified: / 12.11.1997 / 17:58:57 / cg" |
|
13 | 1238 |
! |
1239 |
||
606 | 1240 |
removeLast:n |
11330 | 1241 |
"remove the last n elements from the receiver collection. |
16304 | 1242 |
Destructive: modifies the receiver. |
11330 | 1243 |
Return a collection of removed elements." |
606 | 1244 |
|
1245 |
|mySize ret| |
|
1246 |
||
1247 |
mySize := self size. |
|
1248 |
mySize < n ifTrue:[ |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1249 |
"error if collection has not enough elements" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1250 |
^ self notEnoughElementsError. |
606 | 1251 |
]. |
13 | 1252 |
|
606 | 1253 |
ret := Array new:n. |
1254 |
ret replaceFrom:1 to:n with:contentsArray startingAt:lastIndex - n + 1. |
|
1496 | 1255 |
|
606 | 1256 |
"/ |
1257 |
"/ nil-out contents array, to not keep elements from being GC'd |
|
1258 |
"/ |
|
1259 |
contentsArray from:lastIndex - n + 1 to:lastIndex put:nil. |
|
1260 |
lastIndex := lastIndex - n. |
|
13 | 1261 |
|
606 | 1262 |
firstIndex > lastIndex ifTrue:[ |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1263 |
"reset to avoid ever growing" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1264 |
firstIndex := 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1265 |
lastIndex := 0 |
13 | 1266 |
]. |
606 | 1267 |
^ ret |
13 | 1268 |
|
1269 |
" |
|
606 | 1270 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:2; yourself |
1271 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:0; yourself |
|
1272 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:6; yourself |
|
1273 |
(SortedCollection withAll:#(5 4 3 2 1)) removeLast:2; yourself |
|
11330 | 1274 |
(SortedCollection withAll:#(5 4 3 2 1)) removeLast:0; yourself |
1275 |
(SortedCollection withAll:#(5 4 3 2 1)) removeLast:0 |
|
13 | 1276 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1277 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1278 |
"Modified: 12.4.1996 / 13:39:12 / cg" |
4862 | 1279 |
! |
1280 |
||
1281 |
reset |
|
1282 |
"logically remove all elements from the collection. |
|
16305 | 1283 |
That's almost the same as #removeAll, but keeps the contentsArray. |
4862 | 1284 |
Returns the receiver." |
1285 |
||
1286 |
firstIndex := contentsArray size // 3 max: 1. |
|
1287 |
lastIndex := firstIndex - 1 |
|
1 | 1288 |
! ! |
1289 |
||
606 | 1290 |
!OrderedCollection methodsFor:'converting'! |
1 | 1291 |
|
1170 | 1292 |
asArray |
1293 |
"return the receiver as an array." |
|
1294 |
||
4153 | 1295 |
^ contentsArray copyFrom:firstIndex to:lastIndex |
1170 | 1296 |
|
4153 | 1297 |
"/ |newArray sz| |
1298 |
"/ |
|
1299 |
"/ sz := self size. |
|
1300 |
"/ newArray := Array new:sz. |
|
1301 |
"/ newArray replaceFrom:1 to:sz with:contentsArray startingAt:firstIndex. |
|
1302 |
"/ ^ newArray |
|
1303 |
||
1304 |
" |
|
1305 |
OrderedCollection new asArray |
|
1306 |
OrderedCollection new add:1;add:2;asArray |
|
1307 |
OrderedCollection new add:1;removeFirst;asArray |
|
1308 |
" |
|
1170 | 1309 |
|
1310 |
" |
|
1311 |
|o rnd| |
|
1312 |
||
1313 |
o := OrderedCollection new. |
|
1314 |
rnd := Random new. |
|
1315 |
10000 timesRepeat:[ |
|
1316 |
o add:rnd next. |
|
1317 |
]. |
|
1318 |
Time millisecondsToRun:[o asArray] |
|
1319 |
" |
|
1320 |
||
1321 |
"Modified: 13.4.1996 / 12:10:56 / cg" |
|
1322 |
! |
|
1323 |
||
14999 | 1324 |
asNewOrderedCollection |
1325 |
"return the receiver as an ordered collection. |
|
1326 |
Make sure to return a unique new OrderedCollection" |
|
1327 |
||
1328 |
"could be an instance of a subclass..." |
|
1329 |
self class == OrderedCollection ifTrue:[ |
|
1330 |
^ self copy |
|
1331 |
]. |
|
1332 |
^ super asOrderedCollection |
|
1333 |
||
1334 |
" |
|
1335 |
|s| |
|
1336 |
s := #(1 2 3 4) asOrderedCollection. |
|
1337 |
self assert:(s ~~ s asNewOrderedCollection). |
|
1338 |
self assert:(s = s asNewOrderedCollection). |
|
1339 |
" |
|
1340 |
! |
|
1341 |
||
606 | 1342 |
asOrderedCollection |
15062 | 1343 |
"return the receiver as an ordered collection. |
1344 |
Notice: this returns the receiver. Use asNewOrderedCollection, if you intent to |
|
1345 |
modify the returned collection." |
|
1 | 1346 |
|
606 | 1347 |
"could be an instance of a subclass..." |
1348 |
self class == OrderedCollection ifTrue:[ |
|
15062 | 1349 |
^ self |
606 | 1350 |
]. |
1351 |
^ super asOrderedCollection |
|
1 | 1352 |
! ! |
1353 |
||
606 | 1354 |
!OrderedCollection methodsFor:'copying'! |
13 | 1355 |
|
606 | 1356 |
, aCollection |
14071 | 1357 |
"return a new collection formed from concatenating the receiver with the argument" |
1 | 1358 |
|
606 | 1359 |
|newCollection| |
1 | 1360 |
|
606 | 1361 |
newCollection := self copyEmpty:(self size + aCollection size). |
1362 |
self do:[:element | |
|
14071 | 1363 |
newCollection add:element |
42 | 1364 |
]. |
606 | 1365 |
aCollection do:[:element | |
14071 | 1366 |
newCollection add:element |
606 | 1367 |
]. |
1368 |
^ newCollection |
|
42 | 1369 |
|
253 | 1370 |
" |
606 | 1371 |
#(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection |
1372 |
#(1 2 3) asSortedCollection , #(99 101 100) asSortedCollection |
|
253 | 1373 |
" |
14071 | 1374 |
|
1375 |
"Modified (comment): / 01-04-2012 / 13:17:30 / cg" |
|
42 | 1376 |
! |
1377 |
||
606 | 1378 |
copy |
1379 |
"return a new OrderedCollection containing the elements of the receiver." |
|
1380 |
||
1381 |
"redefinition is a consequence of the implementation with a |
|
1382 |
separate array - otherwise we get a shallow copy of the |
|
1383 |
contents array, which is not what we want here" |
|
1384 |
||
1385 |
^ self copyFrom:1 to:self size |
|
1386 |
! |
|
1387 |
||
1388 |
copyEmpty |
|
1389 |
"return a copy of the receiver without any elements." |
|
1390 |
||
18879 | 1391 |
^ self copyEmpty:0 |
42 | 1392 |
|
18879 | 1393 |
" |
1394 |
self new copyEmpty |
|
1395 |
" |
|
606 | 1396 |
! |
1397 |
||
1398 |
postCopy |
|
1399 |
"have to copy the contentsArray too" |
|
1400 |
||
1401 |
contentsArray := contentsArray shallowCopy |
|
1402 |
! ! |
|
1403 |
||
1404 |
!OrderedCollection methodsFor:'enumerating'! |
|
1405 |
||
1406 |
collect:aBlock |
|
1407 |
"evaluate the argument, aBlock for every element in the collection |
|
1408 |
and return a collection of the results" |
|
1409 |
||
1410 |
|newCollection |
|
1411 |
start "{ Class:SmallInteger }" |
|
1412 |
stop "{ Class:SmallInteger }" | |
|
1413 |
||
1414 |
newCollection := self copyEmpty:(self size). |
|
1415 |
stop := lastIndex. |
|
1416 |
start := firstIndex. |
|
1417 |
start to:stop do:[:index | |
|
1418 |
newCollection add:(aBlock value:(contentsArray at:index)). |
|
1419 |
]. |
|
1420 |
^ newCollection |
|
1421 |
||
253 | 1422 |
" |
606 | 1423 |
#(1 2 3 4) asOrderedCollection collect:[:i | i * i] |
1424 |
#(1 2 3 4) asOrderedCollection collect:[:i | i even] |
|
1425 |
" |
|
1426 |
! |
|
1427 |
||
5508 | 1428 |
collect:collectBlock thenSelect:selectBlock |
5874 | 1429 |
"combination of collect followed by select; |
7164 | 1430 |
redefined to avoid the creation of an intermediate (garbage) collection." |
5508 | 1431 |
|
1432 |
|newCollection newElement |
|
1433 |
start "{ Class:SmallInteger }" |
|
1434 |
stop "{ Class:SmallInteger }" | |
|
1435 |
||
7166 | 1436 |
newCollection := self copyEmpty. |
5508 | 1437 |
stop := lastIndex. |
1438 |
start := firstIndex. |
|
1439 |
start to:stop do:[:index | |
|
1440 |
newElement := collectBlock value:(contentsArray at:index). |
|
1441 |
(selectBlock value:newElement) ifTrue:[ |
|
1442 |
newCollection add:newElement. |
|
1443 |
] |
|
1444 |
]. |
|
1445 |
^ newCollection |
|
1446 |
||
1447 |
" |
|
1448 |
#(1 2 3 4) asOrderedCollection collect:[:i | i * i] thenSelect:[:each | each > 5] |
|
1449 |
( #(1 2 3 4) asOrderedCollection collect:[:i | i * i]) select:[:each | each > 5] |
|
1450 |
||
7165 | 1451 |
|coll| |
1452 |
coll := #(1 2 3 4) asOrderedCollection. |
|
5508 | 1453 |
Time millisecondsToRun:[ |
1454 |
100000 timesRepeat:[ |
|
7165 | 1455 |
coll collect:[:i | i * i] thenSelect:[:each | each > 5] |
5508 | 1456 |
] |
7165 | 1457 |
] |
5508 | 1458 |
|
7165 | 1459 |
|coll| |
1460 |
coll := #(1 2 3 4) asOrderedCollection. |
|
5508 | 1461 |
Time millisecondsToRun:[ |
1462 |
100000 timesRepeat:[ |
|
7165 | 1463 |
( coll collect:[:i | i * i]) select:[:each | each > 5] |
5508 | 1464 |
] |
7165 | 1465 |
] |
5508 | 1466 |
" |
1467 |
! |
|
1468 |
||
606 | 1469 |
do:aBlock |
1470 |
"evaluate the argument, aBlock for every element in the collection." |
|
1471 |
||
1472 |
contentsArray from:firstIndex to:lastIndex do:aBlock |
|
1473 |
! |
|
1474 |
||
1475 |
keysAndValuesDo:aTwoArgBlock |
|
1476 |
"evaluate the argument, aBlock for every element in the collection, |
|
1477 |
passing both index and element as arguments." |
|
1478 |
||
1479 |
|start "{ Class:SmallInteger }" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1480 |
stop "{ Class:SmallInteger }" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1481 |
idx "{ Class:SmallInteger }" | |
606 | 1482 |
|
1483 |
stop := lastIndex. |
|
1484 |
start := firstIndex. |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1485 |
idx := 1. |
606 | 1486 |
start to:stop do:[:index | |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1487 |
aTwoArgBlock value:idx value:(contentsArray at:index). |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1488 |
idx := idx + 1. |
606 | 1489 |
] |
1490 |
||
1491 |
" |
|
1492 |
#(10 20 30 40) asOrderedCollection keysAndValuesDo:[:index :value | |
|
1422 | 1493 |
Transcript show:index; show:' '; showCR:value |
606 | 1494 |
] |
1495 |
" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1496 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1497 |
|oc| |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1498 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1499 |
oc := #(10 20 30 40 50 60 70 80) asOrderedCollection. |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1500 |
oc removeFirst; removeFirst. |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1501 |
oc keysAndValuesDo:[:index :value | |
1422 | 1502 |
Transcript show:index; show:' '; showCR:value |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1503 |
] |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1504 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1505 |
|
606 | 1506 |
! |
1507 |
||
679 | 1508 |
keysAndValuesReverseDo:aTwoArgBlock |
1509 |
"evaluate the argument, aBlock for every element in the collection, |
|
1510 |
passing both index and element as arguments." |
|
1511 |
||
1512 |
|start "{ Class:SmallInteger }" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1513 |
stop "{ Class:SmallInteger }" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1514 |
idx "{ Class:SmallInteger }"| |
679 | 1515 |
|
1516 |
stop := lastIndex. |
|
1517 |
start := firstIndex. |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1518 |
idx := (stop - start + 1). |
679 | 1519 |
stop to:start by: -1 do:[:index | |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1520 |
aTwoArgBlock value:idx value:(contentsArray at:index). |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1521 |
idx := idx - 1. |
679 | 1522 |
] |
1523 |
||
1524 |
" |
|
1525 |
#(10 20 30 40) asOrderedCollection keysAndValuesReverseDo:[:index :value | |
|
1422 | 1526 |
Transcript show:index; show:' '; showCR:value |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1527 |
] |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1528 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1529 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1530 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1531 |
|oc| |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1532 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1533 |
oc := #(10 20 30 40 50 60 70 80) asOrderedCollection. |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1534 |
oc removeFirst; removeFirst. |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1535 |
oc keysAndValuesReverseDo:[:index :value | |
1422 | 1536 |
Transcript show:index; show:' '; showCR:value |
679 | 1537 |
] |
1538 |
" |
|
1071 | 1539 |
! |
1540 |
||
1541 |
reverseDo:aBlock |
|
1542 |
"evaluate the argument, aBlock for every element in the collection |
|
1543 |
procesing elements in reverse direction (i.e. starting with the last)" |
|
1544 |
||
1545 |
contentsArray from:firstIndex to:lastIndex reverseDo:aBlock |
|
7165 | 1546 |
! |
1547 |
||
1548 |
select:selectBlock thenCollect:collectBlock |
|
1549 |
"combination of select followed by collect; |
|
1550 |
redefined to avoid the creation of an intermediate (garbage) collection." |
|
1551 |
||
1552 |
|newCollection element |
|
1553 |
start "{ Class:SmallInteger }" |
|
1554 |
stop "{ Class:SmallInteger }" | |
|
1555 |
||
7166 | 1556 |
newCollection := self copyEmpty. |
7165 | 1557 |
stop := lastIndex. |
1558 |
start := firstIndex. |
|
1559 |
start to:stop do:[:index | |
|
1560 |
element := contentsArray at:index. |
|
1561 |
(selectBlock value:element) ifTrue:[ |
|
1562 |
newCollection add:(collectBlock value:element). |
|
1563 |
] |
|
1564 |
]. |
|
1565 |
^ newCollection |
|
1566 |
||
1567 |
" |
|
1568 |
#(1 2 3 4 5 6 7 8) asOrderedCollection |
|
1569 |
select:[:each | each > 5] thenCollect:[:i | i * i] |
|
1570 |
( #(1 2 3 4 5 6 7 8) asOrderedCollection |
|
1571 |
select:[:each | each > 5]) collect:[:i | i * i] |
|
1572 |
||
1573 |
|coll| |
|
1574 |
coll := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
1575 |
Time millisecondsToRun:[ |
|
1576 |
100000 timesRepeat:[ |
|
1577 |
coll select:[:each | each > 5] thenCollect:[:i | i * i] |
|
1578 |
] |
|
7166 | 1579 |
] |
7165 | 1580 |
|
1581 |
|coll| |
|
1582 |
coll := #(1 2 3 4 5 6 7 8) asOrderedCollection. |
|
1583 |
Time millisecondsToRun:[ |
|
1584 |
100000 timesRepeat:[ |
|
1585 |
( coll select:[:each | each > 5]) collect:[:i | i * i] |
|
1586 |
] |
|
7166 | 1587 |
] |
7165 | 1588 |
" |
1 | 1589 |
! ! |
1590 |
||
56 | 1591 |
!OrderedCollection methodsFor:'filling & replacing'! |
13 | 1592 |
|
1593 |
replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
1170 | 1594 |
"replace elements in the receiver between index start and stop, |
1595 |
with elements taken from replacementCollection starting at repStart. |
|
3236
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
1596 |
Return the receiver. |
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
1597 |
Redefined here; can be done faster as the inherited operation." |
13 | 1598 |
|
1599 |
|end| |
|
1600 |
||
1601 |
end := stop + firstIndex - 1. |
|
1602 |
((start >= 1) and:[end <= lastIndex]) ifTrue:[ |
|
1170 | 1603 |
aCollection class == self class ifTrue:[ |
1604 |
contentsArray |
|
1605 |
replaceFrom:(start + firstIndex - 1) |
|
1606 |
to:end |
|
1607 |
with:aCollection contentsArray |
|
1608 |
startingAt:(repStart + aCollection firstIndex - 1). |
|
1609 |
] ifFalse:[ |
|
1610 |
contentsArray |
|
1611 |
replaceFrom:(start + firstIndex - 1) |
|
1612 |
to:end |
|
1613 |
with:aCollection |
|
1614 |
startingAt:repStart. |
|
1615 |
]. |
|
1616 |
^ self |
|
13 | 1617 |
]. |
1618 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
293 | 1619 |
|
1620 |
" |
|
15757 | 1621 |
|c1 c2| |
1622 |
||
1623 |
c1 := #(1 2 3 4 5 6) asOrderedCollection. |
|
1624 |
c2 := #(a b c d e f) asOrderedCollection. |
|
1625 |
c2 replaceFrom:3 to:6 with:c1. |
|
1626 |
c2 |
|
1627 |
" |
|
1628 |
" |
|
1629 |
|c1 c2| |
|
1630 |
||
1631 |
c1 := #(1 2 3 4 5 6) asOrderedCollection. |
|
1632 |
c2 := #(a b c d e f) asOrderedCollection. |
|
1633 |
c2 replaceFrom:3 to:6 with:c1 startingAt:2. |
|
1634 |
c2 |
|
1635 |
" |
|
1636 |
" |
|
293 | 1637 |
|c| |
1638 |
||
1639 |
c := #(1 2 3 4 5 6) asOrderedCollection. |
|
1640 |
c replaceFrom:3 to:6 with:c startingAt:2. |
|
15757 | 1641 |
c |
293 | 1642 |
" |
1643 |
" |
|
1644 |
|c| |
|
1645 |
||
1646 |
c := #(1 2 3 4 5 6) asOrderedCollection. |
|
1647 |
c replaceFrom:3 to:5 with:c startingAt:4. |
|
1648 |
c |
|
1649 |
" |
|
1170 | 1650 |
|
3236
b5f80b416cff
check for valid subscript in #add:beforeIndex:
Claus Gittinger <cg@exept.de>
parents:
3109
diff
changeset
|
1651 |
"Modified: / 28.1.1998 / 16:49:31 / cg" |
13 | 1652 |
! ! |
1653 |
||
606 | 1654 |
!OrderedCollection methodsFor:'grow & shrink'! |
77 | 1655 |
|
18379 | 1656 |
ensureSizeAtLeast:minSize |
1657 |
"ensure that the size is at least minSize. |
|
1658 |
If the receiver's size is smaller, grow the receiver to minSize, |
|
1659 |
filling new slots with nil. |
|
1660 |
Otherwise, if the size is already >= minSize, leave the receiver unchanged." |
|
1661 |
||
1662 |
(self size < minSize) ifTrue:[ |
|
1663 |
self grow:minSize |
|
1664 |
]. |
|
1665 |
||
1666 |
" |
|
1667 |
|oc| |
|
1668 |
||
1669 |
oc := OrderedCollection new. |
|
1670 |
oc ensureSizeAtLeast:10. |
|
1671 |
oc at:10 put:10. |
|
1672 |
oc add:11. |
|
1673 |
oc at:11. |
|
1674 |
oc ensureSizeAtLeast:20. |
|
1675 |
oc at:20 put:20. |
|
1676 |
oc. |
|
1677 |
" |
|
1678 |
! |
|
1679 |
||
606 | 1680 |
grow:newSize |
14917 | 1681 |
"grow the receiver to newSize. |
1682 |
This only logically changes the receiver's size; |
|
19836 | 1683 |
the underlying contentsArray is kept |
1684 |
(except if growing to a zero size, or too small for newSize)" |
|
77 | 1685 |
|
606 | 1686 |
|oldSize newContents oldLast newLast| |
77 | 1687 |
|
606 | 1688 |
oldSize := lastIndex - firstIndex + 1. |
1689 |
newSize ~~ oldSize ifTrue:[ |
|
14917 | 1690 |
newLast := firstIndex + newSize - 1. |
1691 |
newSize < oldSize ifTrue:[ |
|
1692 |
newSize == 0 ifTrue:[ |
|
18879 | 1693 |
self initContents:0. |
14917 | 1694 |
^ self. |
1695 |
]. |
|
1696 |
oldLast := lastIndex. |
|
1697 |
lastIndex := newLast. |
|
1698 |
" |
|
1699 |
nil out rest, to give GC a chance to reclaim things |
|
1700 |
" |
|
1701 |
contentsArray from:lastIndex + 1 to:oldLast put:nil. |
|
1702 |
] ifFalse:[ |
|
1703 |
newLast <= contentsArray size ifTrue:[ |
|
1704 |
lastIndex := newLast. |
|
1705 |
^ self |
|
1706 |
]. |
|
77 | 1707 |
|
14917 | 1708 |
newContents := Array basicNew:newSize. |
1709 |
newContents replaceFrom:1 to:oldSize with:contentsArray startingAt:firstIndex. |
|
1710 |
contentsArray := newContents. |
|
1711 |
firstIndex := 1. |
|
1712 |
lastIndex := newSize |
|
1713 |
] |
|
606 | 1714 |
] |
1715 |
! ! |
|
77 | 1716 |
|
1717 |
||
19791 | 1718 |
|
1 | 1719 |
!OrderedCollection methodsFor:'private'! |
1720 |
||
606 | 1721 |
initContents:size |
1722 |
"setup the receiver-collection to hold size entries" |
|
33 | 1723 |
|
18879 | 1724 |
size == 0 ifTrue:[ |
1725 |
contentsArray := #(). "save garbage" |
|
1726 |
] ifFalse:[ |
|
1727 |
contentsArray := Array basicNew:size. |
|
1728 |
]. |
|
606 | 1729 |
firstIndex := 1. |
1730 |
lastIndex := 0 |
|
1 | 1731 |
! |
1732 |
||
1733 |
makeRoomAtFront |
|
282 | 1734 |
"grow/shift the contents for more room at the beginning. |
1735 |
Does not change the logical size. |
|
18183 | 1736 |
i.e. the contents array is changed from: |
1737 |
#(1 2 3 4 5 6) -> #(nil 1 2 3 4 5 6) |
|
1738 |
and the start/stopIndices are adjusted as required" |
|
1 | 1739 |
|
1740 |
|newContents |
|
33 | 1741 |
oldSize "{ Class:SmallInteger }" |
1742 |
newSize "{ Class:SmallInteger }" |
|
1743 |
startIndex "{ Class:SmallInteger }" |
|
1744 |
sz "{ Class:SmallInteger }"| |
|
1 | 1745 |
|
1746 |
oldSize := contentsArray size. |
|
506
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1747 |
sz := lastIndex - firstIndex + 1. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1748 |
|
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1749 |
((oldSize == 0) or:[sz == 0]) ifTrue:[ |
18879 | 1750 |
contentsArray := Array basicNew:MinContentsArraySize. |
11275
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1751 |
firstIndex := 2. lastIndex := 1. |
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1752 |
^ self |
93 | 1753 |
]. |
1754 |
||
329 | 1755 |
" |
1756 |
if there is lots of room at the end (> 50%), |
|
1757 |
shift instead of growing. This helps collections |
|
1758 |
which get elements removed at the end and added at front. |
|
1759 |
" |
|
33 | 1760 |
oldSize > (sz * 2) ifTrue:[ |
11275
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1761 |
startIndex := oldSize // 4. |
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1762 |
startIndex > 1 ifTrue:[ |
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1763 |
contentsArray |
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1764 |
replaceFrom:startIndex to:(startIndex + sz - 1) |
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1765 |
with:contentsArray startingAt:1. |
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1766 |
contentsArray from:1 to:(startIndex - 1) put:nil. |
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1767 |
firstIndex := startIndex. |
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1768 |
lastIndex := startIndex + sz - 1. |
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1769 |
^ self |
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1770 |
] |
33 | 1771 |
]. |
2 | 1772 |
newSize := oldSize * 2. |
253 | 1773 |
newContents := Array basicNew:newSize. |
77 | 1774 |
newContents |
11275
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1775 |
replaceFrom:(oldSize + 1) to:newSize |
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1776 |
with:contentsArray startingAt:1. |
1 | 1777 |
contentsArray := newContents. |
1778 |
firstIndex := firstIndex + oldSize. |
|
1779 |
lastIndex := lastIndex + oldSize |
|
506
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1780 |
|
11275
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1781 |
"Created: / 08-11-1995 / 12:47:49 / cg" |
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1782 |
"Modified: / 22-10-2008 / 17:10:13 / cg" |
1 | 1783 |
! |
1784 |
||
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1785 |
makeRoomAtIndex:whereToMakeEmptySlot |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1786 |
"grow the contents for inserting at whereToMakeEmptySlot. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1787 |
The whereToMakeEmptySlot argument must be a physical index within the contentsArray. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1788 |
If there is (plenty of) room at either end, elements are shifted inplace to create |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1789 |
an empty slot; otherwise, a new contentsArray is allocated. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1790 |
Since this changes the logical size, the modified index is returned. |
1 | 1791 |
i.e. |
1792 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 -> #(1 2 nil 3 4 5 6) |
|
1793 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 -> #(nil 1 2 3 4 5 6) |
|
1794 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 -> #(1 2 3 4 5 6 nil)" |
|
1795 |
||
1796 |
|newContents |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1797 |
newSize "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1798 |
oldSize "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1799 |
first "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1800 |
last "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1801 |
index "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1802 |
shiftLeft shiftRight| |
1 | 1803 |
|
1804 |
oldSize := contentsArray size. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1805 |
index := whereToMakeEmptySlot. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1806 |
first := firstIndex. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1807 |
last := lastIndex. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1808 |
|
19791 | 1809 |
(first > 1) ifTrue:[ |
11275
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1810 |
"there is room at the beginning" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1811 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1812 |
shiftLeft := true. |
33 | 1813 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1814 |
index == first ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1815 |
"/ so, we'd have to copy all elements before that index |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1816 |
"/ one slot towards the containers beginning ... |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1817 |
"/ if there is also space at the end, AND the number of |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1818 |
"/ elements after the index is smaller than the number before, |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1819 |
"/ copy the remaining elements. To copy the least possible number. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1820 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1821 |
(last - index) < (index - first) ifTrue:[ |
19791 | 1822 |
last < oldSize ifTrue:[ |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1823 |
shiftLeft := false. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1824 |
shiftRight := true. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1825 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1826 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1827 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1828 |
] ifFalse:[ |
19791 | 1829 |
last < oldSize ifTrue:[ |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1830 |
shiftRight := true |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1831 |
] |
33 | 1832 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1833 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1834 |
shiftLeft == true ifTrue:[ |
11275
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1835 |
"there is room at the beginning" |
33 | 1836 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1837 |
index == first ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1838 |
contentsArray |
11275
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1839 |
replaceFrom:(first - 1) to:(index - 2) |
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1840 |
with:contentsArray startingAt:first. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1841 |
contentsArray at:index-1 put:nil. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1842 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1843 |
firstIndex := first - 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1844 |
^ index - 1 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1845 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1846 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1847 |
shiftRight == true ifTrue:[ |
11275
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1848 |
"there is room at the end" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1849 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1850 |
last := last + 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1851 |
index == last ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1852 |
contentsArray |
11275
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1853 |
replaceFrom:(index + 1) to:last |
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1854 |
with:contentsArray startingAt:index. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1855 |
contentsArray at:index put:nil |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1856 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1857 |
lastIndex := last. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1858 |
^ index |
33 | 1859 |
]. |
1860 |
||
19791 | 1861 |
"/ no space at either end |
1862 |
oldSize < MinContentsArraySize ifTrue:[ |
|
18879 | 1863 |
newSize := MinContentsArraySize |
1864 |
] ifFalse:[ |
|
1865 |
newSize := oldSize * 2. |
|
1866 |
]. |
|
253 | 1867 |
newContents := Array basicNew:newSize. |
11271 | 1868 |
index == first ifTrue:[ |
1869 |
"/ if there is a lot at the end (> 50), make all new space at the beginning. |
|
1870 |
"/ otherwise make 3/4 of the new space to the beginning, 1/4 to the end |
|
18879 | 1871 |
oldSize ~~ 0 ifTrue:[ |
1872 |
(last < (oldSize - 50)) ifTrue:[ |
|
1873 |
lastIndex := newSize - (oldSize-last). |
|
1874 |
firstIndex := lastIndex - (last - first). |
|
1875 |
] ifFalse:[ |
|
1876 |
firstIndex := oldSize * 3 // 4. |
|
19791 | 1877 |
firstIndex < 2 ifTrue:[firstIndex := 2]. "/ pathological case (was explicitly allocated with size<MinSize |
18879 | 1878 |
lastIndex := firstIndex + (last - first). |
1879 |
]. |
|
1880 |
newContents |
|
1881 |
replaceFrom:firstIndex to:lastIndex |
|
1882 |
with:contentsArray startingAt:first. |
|
11271 | 1883 |
]. |
1884 |
contentsArray := newContents. |
|
1885 |
firstIndex := firstIndex - 1. |
|
1886 |
||
1887 |
^ firstIndex. |
|
1888 |
] ifFalse:[ |
|
18879 | 1889 |
oldSize ~~ 0 ifTrue:[ |
1890 |
newContents |
|
1891 |
replaceFrom:1 to:(index - first) |
|
1892 |
with:contentsArray startingAt:first. |
|
11271 | 1893 |
|
18879 | 1894 |
index <= last ifTrue:[ |
1895 |
newContents |
|
1896 |
replaceFrom:(index - first + 2) to:(last - first + 2) |
|
1897 |
with:contentsArray startingAt:index. |
|
1898 |
]. |
|
11271 | 1899 |
]. |
1900 |
contentsArray := newContents. |
|
1901 |
firstIndex := 1. |
|
1902 |
lastIndex := last - first + 2. |
|
1903 |
||
1904 |
"/ return the modified index |
|
1905 |
^ index - (first - firstIndex). |
|
1 | 1906 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1907 |
|
11275
2883195d540e
makeRoomAtIndex: there is no need to do an early reallocation
Claus Gittinger <cg@exept.de>
parents:
11271
diff
changeset
|
1908 |
"Modified: / 22-10-2008 / 17:11:06 / cg" |
1 | 1909 |
! |
1910 |
||
2222 | 1911 |
makeRoomAtIndex:whereToMakeEmptySlots for:howMany |
1912 |
"grow the contents for inserting at whereToMakeEmptySlot. |
|
1913 |
The whereToMakeEmptySlot argument must be a physical index within the contentsArray. |
|
1914 |
If there is (plenty of) room at either end, elements are shifted inplace to create |
|
1915 |
an empty slot; otherwise, a new contentsArray is allocated. |
|
1916 |
Since this changes the logical size, the modified index is returned. |
|
1917 |
i.e. |
|
1918 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 for:2 -> #(1 2 nil nil 3 4 5 6) |
|
1919 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 for:2 -> #(nil nil 1 2 3 4 5 6) |
|
1920 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 for:2 -> #(1 2 3 4 5 6 nil nil)" |
|
1921 |
||
1922 |
|newContents |
|
1923 |
newSize "{ Class:SmallInteger }" |
|
1924 |
oldSize "{ Class:SmallInteger }" |
|
1925 |
oneFourthOfSize "{ Class:SmallInteger }" |
|
1926 |
first "{ Class:SmallInteger }" |
|
1927 |
last "{ Class:SmallInteger }" |
|
1928 |
index "{ Class:SmallInteger }" |
|
1929 |
shiftLeft shiftRight| |
|
1930 |
||
1931 |
oldSize := contentsArray size. |
|
19792 | 1932 |
oneFourthOfSize := (oldSize // 4). |
1933 |
oneFourthOfSize == 0 ifTrue:[oneFourthOfSize := 1]. |
|
2222 | 1934 |
index := whereToMakeEmptySlots. |
1935 |
first := firstIndex. |
|
1936 |
last := lastIndex. |
|
1937 |
||
2549 | 1938 |
shiftLeft := shiftRight := false. |
2222 | 1939 |
((first > howMany) and:[first > oneFourthOfSize]) ifTrue:[ |
1940 |
"there is room (>25%) at the beginning" |
|
1941 |
shiftLeft := true. |
|
1942 |
] ifFalse:[ |
|
2549 | 1943 |
((last + howMany) <= oldSize |
1944 |
and:[last < (oneFourthOfSize * 3)]) ifTrue:[ |
|
2222 | 1945 |
shiftRight := true |
1946 |
] |
|
1947 |
]. |
|
1948 |
||
1949 |
shiftLeft == true ifTrue:[ |
|
2549 | 1950 |
"there is room at the beginning" |
2222 | 1951 |
|
1952 |
index == first ifFalse:[ |
|
1953 |
contentsArray |
|
1954 |
replaceFrom:(first - howMany) |
|
1955 |
to:(index - howMany - 1) |
|
1956 |
with:contentsArray |
|
1957 |
startingAt:first. |
|
1958 |
contentsArray from:index-howMany to:index-1 put:nil. |
|
1959 |
]. |
|
1960 |
firstIndex := first - howMany. |
|
1961 |
^ index - howMany |
|
1962 |
]. |
|
1963 |
||
1964 |
shiftRight == true ifTrue:[ |
|
2549 | 1965 |
"there is room at the end" |
2222 | 1966 |
|
1967 |
last := last + howMany. |
|
1968 |
index == last ifFalse:[ |
|
1969 |
contentsArray |
|
1970 |
replaceFrom:(index + howMany) |
|
1971 |
to:last |
|
1972 |
with:contentsArray |
|
1973 |
startingAt:index. |
|
1974 |
contentsArray from:index to:index+howMany-1 put:nil |
|
1975 |
]. |
|
1976 |
lastIndex := last. |
|
1977 |
^ index |
|
1978 |
]. |
|
17167 | 1979 |
newSize := (oldSize * 2). |
2222 | 1980 |
[newSize < (oldSize+howMany)] whileTrue:[ |
17167 | 1981 |
newSize := (newSize * 2) max:howMany |
2222 | 1982 |
]. |
1983 |
||
1984 |
newContents := Array basicNew:newSize. |
|
18879 | 1985 |
oldSize ~~ 0 ifTrue:[ |
1986 |
index > first ifTrue:[ |
|
1987 |
newContents |
|
1988 |
replaceFrom:1 |
|
1989 |
to:index - first |
|
1990 |
with:contentsArray |
|
1991 |
startingAt:first. |
|
1992 |
]. |
|
1993 |
index <= last ifTrue:[ |
|
1994 |
newContents |
|
1995 |
replaceFrom:index - first + howMany + 1 |
|
1996 |
to:(last - first + howMany + 1) |
|
1997 |
with:contentsArray |
|
1998 |
startingAt:(index). |
|
1999 |
]. |
|
2222 | 2000 |
]. |
2001 |
contentsArray := newContents. |
|
2002 |
firstIndex := 1. |
|
2003 |
lastIndex := last - first + howMany + 1. |
|
2004 |
||
2005 |
"/ return the modified index |
|
2006 |
^ index - (first - firstIndex). |
|
2007 |
||
2549 | 2008 |
"Modified: 15.4.1997 / 12:34:16 / cg" |
2222 | 2009 |
! |
2010 |
||
606 | 2011 |
makeRoomAtLast |
2012 |
"grow/shift the contents for more room at the end. |
|
2013 |
Does not change the logical size. |
|
2014 |
i.e. |
|
2015 |
#(1 2 3 4 5 6) -> #(1 2 3 4 5 6 nil)" |
|
2016 |
||
2017 |
|newContents |
|
2018 |
oldSize "{ Class:SmallInteger }" |
|
2019 |
newSize "{ Class:SmallInteger }" |
|
2020 |
startIndex "{ Class:SmallInteger }" |
|
2021 |
sz "{ Class:SmallInteger }"| |
|
2022 |
||
2023 |
oldSize := contentsArray size. |
|
2024 |
sz := lastIndex - firstIndex + 1. |
|
2025 |
||
2026 |
" |
|
2027 |
if there is lots of room at the beginning (> 50%), |
|
2028 |
shift instead of growing. This helps collections which get |
|
2029 |
elements removed at front and added at the end. |
|
2030 |
" |
|
2031 |
oldSize > (sz * 2) ifTrue:[ |
|
11269 | 2032 |
startIndex := firstIndex // 4. |
2033 |
startIndex == 0 ifTrue:[ |
|
2034 |
startIndex := 1 |
|
2035 |
]. |
|
2036 |
contentsArray |
|
2037 |
replaceFrom:startIndex to:startIndex + sz - 1 |
|
2038 |
with:contentsArray startingAt:firstIndex. |
|
2039 |
contentsArray from:startIndex + sz to:lastIndex put:nil. |
|
2040 |
firstIndex := startIndex. |
|
2041 |
lastIndex := startIndex + sz - 1. |
|
2042 |
^ self |
|
606 | 2043 |
]. |
18879 | 2044 |
oldSize == 0 ifTrue:[ |
2045 |
newSize := MinContentsArraySize |
|
2046 |
] ifFalse:[ |
|
2047 |
newSize := oldSize * 2. |
|
2048 |
]. |
|
606 | 2049 |
newContents := Array basicNew:newSize. |
18879 | 2050 |
oldSize ~~ 0 ifTrue:[ |
2051 |
newContents |
|
2052 |
replaceFrom:1 to:oldSize |
|
2053 |
with:contentsArray startingAt:1. |
|
2054 |
]. |
|
606 | 2055 |
contentsArray := newContents |
11269 | 2056 |
|
2057 |
"Modified: / 22-10-2008 / 11:50:28 / cg" |
|
606 | 2058 |
! |
2059 |
||
2060 |
setFirstIndex:newFirstIndex lastIndex:newLastIndex |
|
2061 |
"set first and last index" |
|
2062 |
||
2063 |
firstIndex := newFirstIndex. |
|
2064 |
lastIndex := newLastIndex. |
|
6088
db3fc1ec9c1a
added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
5874
diff
changeset
|
2065 |
! |
db3fc1ec9c1a
added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
5874
diff
changeset
|
2066 |
|
db3fc1ec9c1a
added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
5874
diff
changeset
|
2067 |
setIndices |
db3fc1ec9c1a
added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
5874
diff
changeset
|
2068 |
"added for VW compatibility: set the indices for an empty collection" |
db3fc1ec9c1a
added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
5874
diff
changeset
|
2069 |
|
db3fc1ec9c1a
added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
5874
diff
changeset
|
2070 |
firstIndex := 1. |
db3fc1ec9c1a
added #setIndices for VW compatibility
Claus Gittinger <cg@exept.de>
parents:
5874
diff
changeset
|
2071 |
lastIndex := 0. |
606 | 2072 |
! ! |
2073 |
||
7260 | 2074 |
!OrderedCollection methodsFor:'private-accessing'! |
606 | 2075 |
|
2076 |
contentsArray |
|
4153 | 2077 |
"return the orderedCollections underlying contentsArray. |
2078 |
The actual elements are found here starting at firstIndex, |
|
2079 |
and ending at lastIndex." |
|
2080 |
||
606 | 2081 |
^ contentsArray |
2082 |
! |
|
2083 |
||
2084 |
firstIndex |
|
4153 | 2085 |
"return the index of my first element in my underlying contentsArray. |
2086 |
The actual elements are found starting this index, |
|
2087 |
and ending at lastIndex." |
|
2088 |
||
606 | 2089 |
^ firstIndex |
2090 |
! ! |
|
2091 |
||
2092 |
!OrderedCollection methodsFor:'queries'! |
|
2093 |
||
2094 |
capacity |
|
2095 |
"return the number of elements, that the receiver is |
|
2096 |
prepared to take. |
|
2097 |
Not used by the system; added for ST-80 compatibility." |
|
2098 |
||
2099 |
^ contentsArray size |
|
2100 |
! |
|
2101 |
||
2102 |
size |
|
2103 |
"return the number of elements in the collection" |
|
2104 |
||
2105 |
^ lastIndex - firstIndex + 1 |
|
2106 |
! ! |
|
2107 |
||
3095 | 2108 |
!OrderedCollection methodsFor:'searching'! |
1 | 2109 |
|
2110 |
identityIndexOf:anObject |
|
33 | 2111 |
"return the index of anObject or 0 if not found. Compare using ==" |
1 | 2112 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2113 |
|index| |
1 | 2114 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2115 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2116 |
identityIndexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2117 |
startingAt:firstIndex |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2118 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2119 |
index == 0 ifTrue:[^ 0]. |
1 | 2120 |
^ index - firstIndex + 1 |
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2121 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2122 |
"Modified: 12.4.1996 / 17:58:25 / cg" |
1 | 2123 |
! |
2124 |
||
2125 |
identityIndexOf:anObject startingAt:startIndex |
|
33 | 2126 |
"return the index of anObject, starting search at startIndex. |
2127 |
Compare using ==; return 0 if not found in the collection" |
|
1 | 2128 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2129 |
|index| |
1 | 2130 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2131 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2132 |
identityIndexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2133 |
startingAt:(startIndex + firstIndex - 1) |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2134 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2135 |
index == 0 ifTrue:[^ 0]. |
1 | 2136 |
^ index - firstIndex + 1 |
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2137 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2138 |
"Modified: 12.4.1996 / 17:58:19 / cg" |
1 | 2139 |
! |
2140 |
||
2141 |
indexOf:anObject |
|
33 | 2142 |
"return the index of anObject or 0 if not found in the collection. |
2143 |
Compare using =" |
|
1 | 2144 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2145 |
|index| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2146 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2147 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2148 |
indexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2149 |
startingAt:firstIndex |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2150 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2151 |
index == 0 ifTrue:[^ 0]. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2152 |
^ index - firstIndex + 1 |
1 | 2153 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2154 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2155 |
|c| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2156 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2157 |
c := OrderedCollection new:10000. |
12035 | 2158 |
c add:10; add:20; add:30. |
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2159 |
c indexOf:99 |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2160 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2161 |
|
12035 | 2162 |
" |
2163 |
|c| |
|
2164 |
||
2165 |
c := OrderedCollection new:10000. |
|
2166 |
c add:10; add:20; add:30. |
|
2167 |
c indexOf:30 |
|
2168 |
" |
|
2169 |
||
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2170 |
"Modified: 12.4.1996 / 17:57:54 / cg" |
1 | 2171 |
! |
2172 |
||
12036 | 2173 |
indexOf:anObject ifAbsent:exceptionValue |
2174 |
"return the index of anObject or 0 if not found in the collection. |
|
2175 |
Compare using = |
|
2176 |
If the receiver does not contain anElement, |
|
2177 |
return the result of evaluating the argument, exceptionBlock." |
|
2178 |
||
2179 |
|index| |
|
2180 |
||
2181 |
index := contentsArray |
|
2182 |
indexOf:anObject |
|
2183 |
startingAt:firstIndex |
|
2184 |
endingAt:lastIndex. |
|
2185 |
index == 0 ifTrue:[^ exceptionValue value]. |
|
2186 |
^ index - firstIndex + 1 |
|
2187 |
||
2188 |
" |
|
2189 |
|c| |
|
2190 |
||
2191 |
c := OrderedCollection new:10000. |
|
2192 |
c add:10; add:20; add:30. |
|
2193 |
c indexOf:99 ifAbsent:'nope' |
|
2194 |
" |
|
2195 |
||
2196 |
" |
|
2197 |
|c| |
|
2198 |
||
2199 |
c := OrderedCollection new:10000. |
|
2200 |
c add:10; add:20; add:30. |
|
2201 |
c indexOf:30 ifAbsent:'nope' |
|
2202 |
" |
|
2203 |
! |
|
2204 |
||
1 | 2205 |
indexOf:anObject startingAt:startIndex |
33 | 2206 |
"return the index of anObject, starting search at startIndex. |
2207 |
Compare using =; return 0 if not found in the collection" |
|
1 | 2208 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2209 |
|index| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2210 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2211 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2212 |
indexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2213 |
startingAt:(startIndex + firstIndex - 1) |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2214 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2215 |
index == 0 ifTrue:[^ 0]. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2216 |
^ index - firstIndex + 1 |
1 | 2217 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2218 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2219 |
|c| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2220 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2221 |
c := OrderedCollection new:10000. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2222 |
c add:1; add:2; add:3. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2223 |
c indexOf:4 startingAt:5 |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2224 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2225 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
2226 |
"Modified: 12.4.1996 / 17:58:53 / cg" |
1 | 2227 |
! ! |
2228 |
||
3095 | 2229 |
!OrderedCollection methodsFor:'testing'! |
2230 |
||
2231 |
includes:anObject |
|
2232 |
"return true if anObject is in the collection. Compare using =" |
|
2233 |
||
2234 |
^ (contentsArray |
|
2235 |
indexOf:anObject |
|
2236 |
startingAt:firstIndex |
|
2237 |
endingAt:lastIndex) ~~ 0 |
|
2238 |
||
2239 |
"Modified: 12.4.1996 / 17:57:27 / cg" |
|
2240 |
! |
|
2241 |
||
2242 |
includesIdentical:anObject |
|
2243 |
"return true if anObject is in the collection. Compare using ==" |
|
2244 |
||
2245 |
^ (contentsArray |
|
2246 |
identityIndexOf:anObject |
|
2247 |
startingAt:firstIndex |
|
2248 |
endingAt:lastIndex) ~~ 0 |
|
2249 |
||
2250 |
"Modified: 12.4.1996 / 17:57:09 / cg" |
|
5401 | 2251 |
! |
2252 |
||
5556 | 2253 |
isEmpty |
2254 |
"return true, if the receiver has no elements" |
|
2255 |
||
2256 |
^ lastIndex < firstIndex |
|
2257 |
! |
|
2258 |
||
17705 | 2259 |
isEmptyOrNil |
2260 |
"return true, if the receiver has no elements" |
|
2261 |
||
2262 |
^ lastIndex < firstIndex |
|
2263 |
! |
|
2264 |
||
5556 | 2265 |
isFixedSize |
2266 |
"return true if the receiver cannot grow - this will vanish once |
|
2267 |
Arrays and Strings learn how to grow ..." |
|
2268 |
||
2269 |
^ false |
|
2270 |
! |
|
2271 |
||
5401 | 2272 |
isOrderedCollection |
2273 |
"return true, if the receiver is some kind of ordered collection (or list etc); |
|
2274 |
true is returned here - the method is only redefined in Object." |
|
2275 |
||
2276 |
^ true |
|
2277 |
||
2278 |
||
5556 | 2279 |
! |
2280 |
||
2281 |
notEmpty |
|
2282 |
"return true, if the receiver has any elements" |
|
2283 |
||
2284 |
^ lastIndex >= firstIndex |
|
3095 | 2285 |
! ! |
2286 |
||
15660 | 2287 |
!OrderedCollection methodsFor:'tuning'! |
2288 |
||
2289 |
quickSortFrom:inBegin to:inEnd |
|
2290 |
self class == OrderedCollection ifTrue:[ |
|
2291 |
"/ because array-at/at:put: is much faster, this speeds up sorting by |
|
2292 |
"/ up to 30% |
|
2293 |
contentsArray |
|
2294 |
quickSortFrom:(firstIndex + inBegin - 1) |
|
2295 |
to:(firstIndex + inEnd - 1) |
|
2296 |
] ifFalse:[ |
|
2297 |
super quickSortFrom:inBegin to:inEnd |
|
2298 |
]. |
|
2299 |
! |
|
2300 |
||
2301 |
quickSortFrom:inBegin to:inEnd sortBlock:sortBlock |
|
2302 |
self class == OrderedCollection ifTrue:[ |
|
2303 |
"/ because array-at/at:put: is much faster, this speeds up sorting by |
|
2304 |
"/ up to 30% |
|
2305 |
contentsArray |
|
2306 |
quickSortFrom:(firstIndex + inBegin - 1) |
|
2307 |
to:(firstIndex + inEnd - 1) |
|
2308 |
sortBlock:sortBlock |
|
2309 |
] ifFalse:[ |
|
2310 |
super quickSortFrom:inBegin to:inEnd sortBlock:sortBlock |
|
2311 |
] |
|
2312 |
! ! |
|
2313 |
||
629 | 2314 |
!OrderedCollection class methodsFor:'documentation'! |
2315 |
||
2316 |
version |
|
18811 | 2317 |
^ '$Header$' |
16305 | 2318 |
! |
2319 |
||
2320 |
version_CVS |
|
18811 | 2321 |
^ '$Header$' |
629 | 2322 |
! ! |
14616 | 2323 |
|
18879 | 2324 |
|
2325 |
OrderedCollection initialize! |