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