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