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