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