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