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