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