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