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