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