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