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