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