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