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