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