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