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