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