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