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