author | Claus Gittinger <cg@exept.de> |
Sat, 01 Feb 1997 12:37:19 +0100 | |
changeset 2354 | 652d17daeeb5 |
parent 2348 | 523e9d6e0049 |
child 2355 | 9476ba4d810d |
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 |
|
606 | 210 |
|idx| |
387 | 211 |
|
606 | 212 |
idx := anInteger + firstIndex - 1. |
213 |
((anInteger < 1) or:[idx > lastIndex]) ifTrue:[ |
|
214 |
^ self subscriptBoundsError:anInteger |
|
215 |
] ifFalse:[ |
|
216 |
^ contentsArray at:idx |
|
217 |
] |
|
360 | 218 |
! |
219 |
||
606 | 220 |
at:anInteger put:anObject |
1221 | 221 |
"set the element at index, to be anInteger. |
222 |
Return anObject (sigh)." |
|
1 | 223 |
|
606 | 224 |
|idx| |
1 | 225 |
|
606 | 226 |
idx := anInteger + firstIndex - 1. |
227 |
((anInteger < 1) or:[idx > lastIndex]) ifTrue:[ |
|
1221 | 228 |
^ self subscriptBoundsError:anInteger |
606 | 229 |
] ifFalse:[ |
1221 | 230 |
^ contentsArray at:idx put:anObject |
606 | 231 |
] |
1221 | 232 |
|
233 |
"Modified: 19.4.1996 / 11:31:16 / 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 |
]. |
606 | 304 |
idx := lastIndex := idx + 1. |
305 |
contentsArray at: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 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
314 |
"Modified: 12.4.1996 / 13:26:57 / 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 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
408 |
|idx| |
348 | 409 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
410 |
idx := self makeRoomAtIndex:(index + firstIndex - 1). |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
411 |
"/ notice: the above may change firstIndex |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
412 |
contentsArray at:idx put:anObject. |
348 | 413 |
|
414 |
" |
|
606 | 415 |
|c| |
416 |
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
|
417 |
c add:'here' beforeIndex:3 |
606 | 418 |
" |
419 |
" |
|
420 |
|c| |
|
421 |
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
|
422 |
c add:'here' beforeIndex:1 |
606 | 423 |
" |
424 |
" |
|
425 |
|c| |
|
426 |
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
|
427 |
c add:'here' beforeIndex:5 |
348 | 428 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
429 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
430 |
"Modified: 12.4.1996 / 16:47:31 / cg" |
1 | 431 |
! |
432 |
||
2222 | 433 |
addAll:aCollection beforeIndex:index |
434 |
"insert all elements of the argument, anObject to become located at index. |
|
435 |
The collection may be unordered, but then order of the sliced-in elements |
|
436 |
is undefined. |
|
437 |
Return the receiver." |
|
438 |
||
439 |
|idx count| |
|
440 |
||
441 |
aCollection isSequenceable ifTrue:[ |
|
442 |
"/ we are lucky - that thing can count & do bulk copies |
|
443 |
||
444 |
count := aCollection size. |
|
445 |
idx := self makeRoomAtIndex:(index + firstIndex) for:count. |
|
446 |
"/ notice: the above may change firstIndex |
|
447 |
contentsArray replaceFrom:idx to:(idx + count - 1) with:aCollection startingAt:1. |
|
448 |
^ self |
|
449 |
]. |
|
450 |
||
451 |
idx := index. |
|
452 |
aCollection do:[:element | |
|
453 |
self add:element beforeIndex:idx. |
|
454 |
idx := idx + 1. |
|
455 |
]. |
|
456 |
||
457 |
" |
|
458 |
|c| |
|
459 |
c := #(1 2 3 4) asOrderedCollection. |
|
460 |
c addAll:'here' beforeIndex:3 |
|
461 |
" |
|
462 |
" |
|
463 |
|c| |
|
464 |
c := #(1 2 3 4) asOrderedCollection. |
|
465 |
c addAll:'here' beforeIndex:1 |
|
466 |
" |
|
467 |
" |
|
468 |
|c| |
|
469 |
c := #(1 2 3 4) asOrderedCollection. |
|
470 |
c addAll:'here' beforeIndex:5 |
|
471 |
" |
|
472 |
||
473 |
" |
|
474 |
|c| |
|
475 |
c := #(1 2 3 4) asOrderedCollection. |
|
476 |
c addAll:('hello' asSet) beforeIndex:3 |
|
477 |
" |
|
478 |
||
479 |
"Modified: 22.1.1997 / 04:00:27 / cg" |
|
480 |
! |
|
481 |
||
606 | 482 |
addFirst:anObject |
483 |
"add the argument, anObject to the beginning of the collection. |
|
484 |
Return the argument, anObject." |
|
56 | 485 |
|
606 | 486 |
(firstIndex == 1) ifTrue:[ |
487 |
self makeRoomAtFront |
|
348 | 488 |
]. |
606 | 489 |
firstIndex := firstIndex - 1. |
490 |
contentsArray at:firstIndex put:anObject. |
|
491 |
^ anObject |
|
56 | 492 |
|
493 |
" |
|
606 | 494 |
|c| |
495 |
c := #(1 2 3 4) asOrderedCollection. |
|
496 |
c addFirst:'here'. |
|
497 |
c |
|
498 |
" |
|
499 |
||
500 |
" |
|
501 |
|c| |
|
502 |
c := #() asOrderedCollection. |
|
503 |
c addFirst:'here'. |
|
504 |
c |
|
56 | 505 |
" |
506 |
! |
|
507 |
||
1 | 508 |
remove:anObject ifAbsent:exceptionBlock |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
509 |
"remove the first element which is equal to anObject; |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
510 |
if found, remove and return it; |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
511 |
if not, return the value from evaluating exceptionBlock. |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
512 |
Used equality compare (=) to search for the element." |
1 | 513 |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
514 |
|index retVal| |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
515 |
|
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
516 |
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
|
517 |
index ~~ 0 ifTrue:[ |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
518 |
"/ mhmh - should we use removeAtIndex: here ? |
1 | 519 |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
520 |
retVal := contentsArray at:index. |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
521 |
|
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
522 |
index == firstIndex ifTrue:[ |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
523 |
"/ nil it (helps GC) |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
524 |
contentsArray at:firstIndex put:nil. |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
525 |
firstIndex := firstIndex + 1. |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
526 |
] ifFalse:[ |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
527 |
index ~~ lastIndex ifTrue:[ |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
528 |
"/ shuffle |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
529 |
|
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
530 |
contentsArray |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
531 |
replaceFrom:index |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
532 |
to:(contentsArray size - 1) |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
533 |
with:contentsArray |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
534 |
startingAt:(index + 1). |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
535 |
]. |
1496 | 536 |
|
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
537 |
"/ nil it (helps GC) |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
538 |
contentsArray at:lastIndex put:nil. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
539 |
lastIndex := lastIndex - 1. |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
540 |
]. |
1496 | 541 |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
542 |
firstIndex > lastIndex ifTrue:[ |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
543 |
"reset to avoid ever growing" |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
544 |
firstIndex := 1. |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
545 |
lastIndex := 0 |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
546 |
]. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
547 |
^ retVal |
1 | 548 |
]. |
549 |
^ exceptionBlock value |
|
348 | 550 |
|
551 |
" |
|
1496 | 552 |
#(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:[self halt] |
553 |
#(1 2 3 4 5) asOrderedCollection remove:3 ifAbsent:[self halt] |
|
554 |
||
348 | 555 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection remove:3 ifAbsent:'oops' |
556 |
#(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:'oops' |
|
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
557 |
#(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
|
558 |
#(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' |
348 | 559 |
" |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
560 |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
561 |
"Modified: 1.2.1997 / 12:29:55 / cg" |
1 | 562 |
! |
563 |
||
114 | 564 |
removeAll |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
565 |
"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
|
566 |
Returns the receiver." |
114 | 567 |
|
568 |
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
|
569 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
570 |
"Modified: 12.4.1996 / 13:34:19 / cg" |
114 | 571 |
! |
572 |
||
348 | 573 |
removeAllSuchThat:aBlock |
574 |
"remove all elements that meet a test criteria as specified in aBlock. |
|
575 |
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
|
576 |
for which it returns true, are removed. |
1496 | 577 |
Return a collection containing the removed elements. |
578 |
||
579 |
Performance hint: |
|
580 |
if a big number of objects is removed this way, |
|
581 |
it may be better to rebuild a new collection via #select:, |
|
582 |
since removing elements out of the middle is somewhat slow |
|
583 |
due to the need to copy over remaining elements within the collection." |
|
348 | 584 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
585 |
"/ this is a q&d implementation (possibly slow). |
348 | 586 |
|
587 |
|runIndex removed element| |
|
588 |
||
589 |
removed := self species new. |
|
590 |
||
591 |
runIndex := 1. |
|
592 |
[runIndex <= self size] whileTrue:[ |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
593 |
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
|
594 |
(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
|
595 |
removed add:element. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
596 |
self removeAtIndex:runIndex |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
597 |
] ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
598 |
runIndex := runIndex + 1 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
599 |
] |
348 | 600 |
]. |
601 |
^ removed |
|
602 |
||
603 |
" |
|
604 |
|coll| |
|
605 |
||
606 |
coll := OrderedCollection withAll:(1 to:10). |
|
1422 | 607 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
608 |
Transcript showCR:coll |
|
348 | 609 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
610 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
611 |
"Modified: 12.4.1996 / 13:37:01 / cg" |
348 | 612 |
! |
613 |
||
606 | 614 |
removeFirst |
1496 | 615 |
"remove the first element from the collection; return the element." |
202 | 616 |
|
606 | 617 |
|anObject | |
13 | 618 |
|
606 | 619 |
firstIndex > lastIndex ifTrue:[ |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
620 |
"error if collection is empty" |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
621 |
^ self emptyCollectionError. |
606 | 622 |
]. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
623 |
|
606 | 624 |
anObject := contentsArray at:firstIndex. |
1496 | 625 |
|
626 |
"/ nil it out, to allow GC to reclaim it. |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
627 |
contentsArray at:firstIndex put:nil. |
1496 | 628 |
|
606 | 629 |
firstIndex := firstIndex + 1. |
1 | 630 |
|
606 | 631 |
firstIndex > lastIndex ifTrue:[ |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
632 |
"reset to avoid ever growing" |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
633 |
firstIndex := 1. |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
634 |
lastIndex := 0 |
1 | 635 |
]. |
636 |
^ anObject |
|
13 | 637 |
|
638 |
" |
|
606 | 639 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself |
640 |
OrderedCollection new removeFirst |
|
641 |
(SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself |
|
93 | 642 |
" |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
643 |
|
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
644 |
"Modified: 1.2.1997 / 12:30:50 / cg" |
606 | 645 |
! |
646 |
||
647 |
removeFirst:n |
|
648 |
"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
|
649 |
Return a collection containing the removed elements." |
606 | 650 |
|
651 |
|mySize ret| |
|
652 |
||
653 |
mySize := self size. |
|
654 |
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
|
655 |
"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
|
656 |
^ self notEnoughElementsError. |
606 | 657 |
]. |
658 |
||
659 |
ret := Array new:n. |
|
660 |
ret replaceFrom:1 to:n with:contentsArray startingAt:firstIndex. |
|
1496 | 661 |
|
606 | 662 |
"/ |
663 |
"/ nil-out contents array, to not keep elements from being GC'd |
|
664 |
"/ |
|
665 |
contentsArray from:firstIndex to:firstIndex + n - 1 put:nil. |
|
666 |
firstIndex := firstIndex + n. |
|
667 |
||
668 |
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
|
669 |
"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
|
670 |
firstIndex := 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
671 |
lastIndex := 0 |
606 | 672 |
]. |
673 |
^ ret |
|
93 | 674 |
|
675 |
" |
|
606 | 676 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:2; yourself |
677 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:0; yourself |
|
678 |
OrderedCollection new removeFirst:2 |
|
679 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:6 |
|
680 |
(SortedCollection withAll:#(5 4 3 2 1)) removeFirst:2; yourself |
|
13 | 681 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
682 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
683 |
"Modified: 12.4.1996 / 13:37:39 / cg" |
1 | 684 |
! |
685 |
||
606 | 686 |
removeFromIndex:startIndex toIndex:stopIndex |
687 |
"remove the elements stored under startIndex up to and including |
|
688 |
the elements under stopIndex. |
|
689 |
Return the receiver. |
|
690 |
Returning the receiver here is a historic leftover - it may change. |
|
691 |
Please use yourself in a cascade, if you need the receivers value |
|
692 |
when using this method." |
|
693 |
||
694 |
|nDeleted| |
|
1 | 695 |
|
606 | 696 |
(startIndex < 1 |
697 |
or:[stopIndex > self size]) ifTrue:[ |
|
698 |
^ self notEnoughElementsError |
|
699 |
]. |
|
700 |
nDeleted := stopIndex - startIndex + 1. |
|
701 |
contentsArray |
|
702 |
replaceFrom:(firstIndex + startIndex - 1) |
|
703 |
to:(lastIndex - nDeleted) |
|
704 |
with:contentsArray |
|
705 |
startingAt:(firstIndex + stopIndex). |
|
706 |
||
1496 | 707 |
"/ nil out rest (helps GC) |
606 | 708 |
contentsArray |
709 |
from:(lastIndex - nDeleted + 1) |
|
710 |
to:lastIndex |
|
711 |
put:nil. |
|
712 |
||
713 |
lastIndex := lastIndex - nDeleted. |
|
714 |
firstIndex > lastIndex ifTrue:[ |
|
715 |
"reset to avoid ever growing" |
|
716 |
firstIndex := 1. |
|
717 |
lastIndex := 0 |
|
718 |
] |
|
13 | 719 |
|
720 |
" |
|
606 | 721 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:3 toIndex:6 |
722 |
#(1 2 3 4 5) asOrderedCollection removeFromIndex:3 toIndex:6 |
|
217 | 723 |
" |
724 |
! |
|
725 |
||
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
726 |
removeIdentical:anObject ifAbsent:exceptionBlock |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
727 |
"remove the first element which is identical to anObject; |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
728 |
if found, remove and return it; |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
729 |
if not, return the value from evaluating exceptionBlock. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
730 |
Used identity compare (==) to search for the element." |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
731 |
|
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
732 |
|index| |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
733 |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
734 |
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
|
735 |
index ~~ 0 ifTrue:[ |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
736 |
"/ mhmh - should we use removeAtIndex: here ? |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
737 |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
738 |
index == firstIndex ifTrue:[ |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
739 |
"/ nil it (helps GC) |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
740 |
contentsArray at:firstIndex put:nil. |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
741 |
firstIndex := firstIndex + 1. |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
742 |
] ifFalse:[ |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
743 |
index ~~ lastIndex ifTrue:[ |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
744 |
"/ shuffle |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
745 |
|
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
746 |
contentsArray |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
747 |
replaceFrom:index |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
748 |
to:(contentsArray size - 1) |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
749 |
with:contentsArray |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
750 |
startingAt:(index + 1). |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
751 |
]. |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
752 |
|
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
753 |
"/ nil it (helps GC) |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
754 |
contentsArray at:lastIndex put:nil. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
755 |
lastIndex := lastIndex - 1. |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
756 |
]. |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
757 |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
758 |
firstIndex > lastIndex ifTrue:[ |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
759 |
"reset to avoid ever growing" |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
760 |
firstIndex := 1. |
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
761 |
lastIndex := 0 |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
762 |
]. |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
763 |
^ anObject |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
764 |
]. |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
765 |
^ exceptionBlock value |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
766 |
|
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
767 |
" |
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
768 |
#(1.0 2.0 3.0 4.0 5.0) asOrderedCollection remove:4 ifAbsent:'oops' |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
769 |
#(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
770 |
" |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
771 |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
772 |
"Modified: 1.2.1997 / 12:29:47 / cg" |
2348
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
773 |
! |
523e9d6e0049
added #removeIdentical:ifAbsent:
Claus Gittinger <cg@exept.de>
parents:
2222
diff
changeset
|
774 |
|
606 | 775 |
removeLast |
776 |
"remove the last element from the collection; return the element" |
|
777 |
||
778 |
|anObject | |
|
217 | 779 |
|
606 | 780 |
firstIndex > lastIndex ifTrue:[ |
781 |
"error if collection is empty" |
|
782 |
^ self emptyCollectionError. |
|
783 |
]. |
|
784 |
anObject := contentsArray at:lastIndex. |
|
1496 | 785 |
|
786 |
"/ nil it (helps GC) |
|
606 | 787 |
contentsArray at:lastIndex put:nil. |
788 |
lastIndex := lastIndex - 1. |
|
789 |
||
790 |
firstIndex > lastIndex ifTrue:[ |
|
791 |
"reset to avoid ever growing" |
|
792 |
firstIndex := 1. |
|
793 |
lastIndex := 0 |
|
794 |
]. |
|
795 |
^ anObject |
|
217 | 796 |
|
797 |
" |
|
606 | 798 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast; yourself |
799 |
OrderedCollection new removeLast |
|
800 |
(SortedCollection withAll:#(5 4 3 2 1)) removeLast; yourself |
|
217 | 801 |
" |
13 | 802 |
! |
803 |
||
606 | 804 |
removeLast:n |
805 |
"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
|
806 |
Return a collection of removed elements." |
606 | 807 |
|
808 |
|mySize ret| |
|
809 |
||
810 |
mySize := self size. |
|
811 |
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
|
812 |
"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
|
813 |
^ self notEnoughElementsError. |
606 | 814 |
]. |
13 | 815 |
|
606 | 816 |
ret := Array new:n. |
817 |
ret replaceFrom:1 to:n with:contentsArray startingAt:lastIndex - n + 1. |
|
1496 | 818 |
|
606 | 819 |
"/ |
820 |
"/ nil-out contents array, to not keep elements from being GC'd |
|
821 |
"/ |
|
822 |
contentsArray from:lastIndex - n + 1 to:lastIndex put:nil. |
|
823 |
lastIndex := lastIndex - n. |
|
13 | 824 |
|
606 | 825 |
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
|
826 |
"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
|
827 |
firstIndex := 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
828 |
lastIndex := 0 |
13 | 829 |
]. |
606 | 830 |
^ ret |
13 | 831 |
|
832 |
" |
|
606 | 833 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:2; yourself |
834 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:0; yourself |
|
835 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:6; yourself |
|
836 |
(SortedCollection withAll:#(5 4 3 2 1)) removeLast:2; yourself |
|
13 | 837 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
838 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
839 |
"Modified: 12.4.1996 / 13:39:12 / cg" |
1 | 840 |
! ! |
841 |
||
606 | 842 |
!OrderedCollection methodsFor:'converting'! |
1 | 843 |
|
1170 | 844 |
asArray |
845 |
"return the receiver as an array." |
|
846 |
||
847 |
|newArray sz| |
|
848 |
||
849 |
sz := self size. |
|
850 |
newArray := Array new:sz. |
|
851 |
newArray replaceFrom:1 to:sz with:contentsArray startingAt:firstIndex. |
|
852 |
^ newArray |
|
853 |
||
854 |
" |
|
855 |
|o rnd| |
|
856 |
||
857 |
o := OrderedCollection new. |
|
858 |
rnd := Random new. |
|
859 |
10000 timesRepeat:[ |
|
860 |
o add:rnd next. |
|
861 |
]. |
|
862 |
Time millisecondsToRun:[o asArray] |
|
863 |
" |
|
864 |
||
865 |
"Modified: 13.4.1996 / 12:10:56 / cg" |
|
866 |
! |
|
867 |
||
606 | 868 |
asOrderedCollection |
869 |
"return the receiver as an ordered collection" |
|
1 | 870 |
|
606 | 871 |
"could be an instance of a subclass..." |
872 |
self class == OrderedCollection ifTrue:[ |
|
873 |
^ self |
|
874 |
]. |
|
875 |
^ super asOrderedCollection |
|
1 | 876 |
! ! |
877 |
||
606 | 878 |
!OrderedCollection methodsFor:'copying'! |
13 | 879 |
|
606 | 880 |
, aCollection |
881 |
"return a new collection formed from concatenating the receiver with |
|
882 |
the argument" |
|
1 | 883 |
|
606 | 884 |
|newCollection| |
1 | 885 |
|
606 | 886 |
newCollection := self copyEmpty:(self size + aCollection size). |
887 |
self do:[:element | |
|
888 |
newCollection add:element |
|
42 | 889 |
]. |
606 | 890 |
aCollection do:[:element | |
891 |
newCollection add:element |
|
892 |
]. |
|
893 |
^ newCollection |
|
42 | 894 |
|
253 | 895 |
" |
606 | 896 |
#(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection |
897 |
#(1 2 3) asSortedCollection , #(99 101 100) asSortedCollection |
|
253 | 898 |
" |
42 | 899 |
! |
900 |
||
606 | 901 |
copy |
902 |
"return a new OrderedCollection containing the elements of the receiver." |
|
903 |
||
904 |
"redefinition is a consequence of the implementation with a |
|
905 |
separate array - otherwise we get a shallow copy of the |
|
906 |
contents array, which is not what we want here" |
|
907 |
||
908 |
^ self copyFrom:1 to:self size |
|
909 |
! |
|
910 |
||
911 |
copyEmpty |
|
912 |
"return a copy of the receiver without any elements." |
|
913 |
||
914 |
^ self copyEmpty:10 |
|
915 |
! |
|
916 |
||
917 |
copyFrom:start to:stop |
|
918 |
"return a new OrderedCollection containing the elements |
|
919 |
from start to stop." |
|
42 | 920 |
|
606 | 921 |
|newCollection sz| |
922 |
||
923 |
sz := stop - start + 1. |
|
924 |
newCollection := self copyEmptyAndGrow:sz. "must grow, otherwise replace fails" |
|
925 |
newCollection replaceFrom:1 to:sz with:self startingAt:start. |
|
926 |
^ newCollection |
|
927 |
! |
|
928 |
||
929 |
copyWith:newElement |
|
930 |
"return a new collection containing the receivers elements |
|
931 |
and the single new element, newElement. |
|
932 |
This is different from concatentation, which expects another collection |
|
933 |
as argument, but equivalent to copy-and-addLast." |
|
934 |
||
935 |
|newCollection mySize newSize| |
|
936 |
||
937 |
mySize := self size. |
|
938 |
newSize := mySize + 1. |
|
939 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
|
940 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
941 |
newCollection at:newSize put:newElement. |
|
942 |
^newCollection |
|
42 | 943 |
|
253 | 944 |
" |
606 | 945 |
#(1 2 3 4 5) copyWith:$a |
946 |
'abcdefg' copyWith:$h |
|
947 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
948 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
949 |
" |
|
950 |
! |
|
951 |
||
952 |
postCopy |
|
953 |
"have to copy the contentsArray too" |
|
954 |
||
955 |
contentsArray := contentsArray shallowCopy |
|
956 |
! ! |
|
957 |
||
958 |
!OrderedCollection methodsFor:'enumerating'! |
|
959 |
||
960 |
collect:aBlock |
|
961 |
"evaluate the argument, aBlock for every element in the collection |
|
962 |
and return a collection of the results" |
|
963 |
||
964 |
|newCollection |
|
965 |
start "{ Class:SmallInteger }" |
|
966 |
stop "{ Class:SmallInteger }" | |
|
967 |
||
968 |
newCollection := self copyEmpty:(self size). |
|
969 |
stop := lastIndex. |
|
970 |
start := firstIndex. |
|
971 |
start to:stop do:[:index | |
|
972 |
newCollection add:(aBlock value:(contentsArray at:index)). |
|
973 |
]. |
|
974 |
^ newCollection |
|
975 |
||
253 | 976 |
" |
606 | 977 |
#(1 2 3 4) asOrderedCollection collect:[:i | i * i] |
978 |
#(1 2 3 4) asOrderedCollection collect:[:i | i even] |
|
979 |
" |
|
980 |
! |
|
981 |
||
982 |
do:aBlock |
|
983 |
"evaluate the argument, aBlock for every element in the collection." |
|
984 |
||
985 |
contentsArray from:firstIndex to:lastIndex do:aBlock |
|
986 |
! |
|
987 |
||
988 |
keysAndValuesDo:aTwoArgBlock |
|
989 |
"evaluate the argument, aBlock for every element in the collection, |
|
990 |
passing both index and element as arguments." |
|
991 |
||
992 |
|start "{ Class:SmallInteger }" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
993 |
stop "{ Class:SmallInteger }" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
994 |
idx "{ Class:SmallInteger }" | |
606 | 995 |
|
996 |
stop := lastIndex. |
|
997 |
start := firstIndex. |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
998 |
idx := 1. |
606 | 999 |
start to:stop do:[:index | |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1000 |
aTwoArgBlock value:idx value:(contentsArray at:index). |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1001 |
idx := idx + 1. |
606 | 1002 |
] |
1003 |
||
1004 |
" |
|
1005 |
#(10 20 30 40) asOrderedCollection keysAndValuesDo:[:index :value | |
|
1422 | 1006 |
Transcript show:index; show:' '; showCR:value |
606 | 1007 |
] |
1008 |
" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1009 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1010 |
|oc| |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1011 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1012 |
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
|
1013 |
oc removeFirst; removeFirst. |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1014 |
oc keysAndValuesDo:[:index :value | |
1422 | 1015 |
Transcript show:index; show:' '; showCR:value |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1016 |
] |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1017 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1018 |
|
606 | 1019 |
! |
1020 |
||
679 | 1021 |
keysAndValuesReverseDo:aTwoArgBlock |
1022 |
"evaluate the argument, aBlock for every element in the collection, |
|
1023 |
passing both index and element as arguments." |
|
1024 |
||
1025 |
|start "{ Class:SmallInteger }" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1026 |
stop "{ Class:SmallInteger }" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1027 |
idx "{ Class:SmallInteger }"| |
679 | 1028 |
|
1029 |
stop := lastIndex. |
|
1030 |
start := firstIndex. |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1031 |
idx := (stop - start + 1). |
679 | 1032 |
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
|
1033 |
aTwoArgBlock value:idx value:(contentsArray at:index). |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1034 |
idx := idx - 1. |
679 | 1035 |
] |
1036 |
||
1037 |
" |
|
1038 |
#(10 20 30 40) asOrderedCollection keysAndValuesReverseDo:[:index :value | |
|
1422 | 1039 |
Transcript show:index; show:' '; showCR:value |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1040 |
] |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1041 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1042 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1043 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1044 |
|oc| |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1045 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1046 |
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
|
1047 |
oc removeFirst; removeFirst. |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
1048 |
oc keysAndValuesReverseDo:[:index :value | |
1422 | 1049 |
Transcript show:index; show:' '; showCR:value |
679 | 1050 |
] |
1051 |
" |
|
1071 | 1052 |
! |
1053 |
||
1054 |
reverseDo:aBlock |
|
1055 |
"evaluate the argument, aBlock for every element in the collection |
|
1056 |
procesing elements in reverse direction (i.e. starting with the last)" |
|
1057 |
||
1058 |
contentsArray from:firstIndex to:lastIndex reverseDo:aBlock |
|
1 | 1059 |
! ! |
1060 |
||
56 | 1061 |
!OrderedCollection methodsFor:'filling & replacing'! |
13 | 1062 |
|
1063 |
replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
1170 | 1064 |
"replace elements in the receiver between index start and stop, |
1065 |
with elements taken from replacementCollection starting at repStart. |
|
1066 |
Redefined - can be done faster" |
|
13 | 1067 |
|
1068 |
|end| |
|
1069 |
||
1070 |
end := stop + firstIndex - 1. |
|
1071 |
((start >= 1) and:[end <= lastIndex]) ifTrue:[ |
|
1170 | 1072 |
aCollection class == self class ifTrue:[ |
1073 |
contentsArray |
|
1074 |
replaceFrom:(start + firstIndex - 1) |
|
1075 |
to:end |
|
1076 |
with:aCollection contentsArray |
|
1077 |
startingAt:(repStart + aCollection firstIndex - 1). |
|
1078 |
] ifFalse:[ |
|
1079 |
contentsArray |
|
1080 |
replaceFrom:(start + firstIndex - 1) |
|
1081 |
to:end |
|
1082 |
with:aCollection |
|
1083 |
startingAt:repStart. |
|
1084 |
]. |
|
1085 |
^ self |
|
13 | 1086 |
]. |
1087 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
293 | 1088 |
|
1089 |
" |
|
1090 |
|c| |
|
1091 |
||
1092 |
c := #(1 2 3 4 5 6) asOrderedCollection. |
|
1093 |
c replaceFrom:3 to:6 with:c startingAt:2. |
|
1094 |
c |
|
1095 |
" |
|
1096 |
" |
|
1097 |
|c| |
|
1098 |
||
1099 |
c := #(1 2 3 4 5 6) asOrderedCollection. |
|
1100 |
c replaceFrom:3 to:5 with:c startingAt:4. |
|
1101 |
c |
|
1102 |
" |
|
1170 | 1103 |
|
1104 |
"Modified: 13.4.1996 / 12:16:24 / cg" |
|
13 | 1105 |
! ! |
1106 |
||
606 | 1107 |
!OrderedCollection methodsFor:'grow & shrink'! |
77 | 1108 |
|
606 | 1109 |
grow:newSize |
1110 |
"grow the receiver to newSize" |
|
77 | 1111 |
|
606 | 1112 |
|oldSize newContents oldLast newLast| |
77 | 1113 |
|
606 | 1114 |
oldSize := lastIndex - firstIndex + 1. |
1115 |
newSize ~~ oldSize ifTrue:[ |
|
1116 |
newLast := firstIndex + newSize - 1. |
|
1117 |
newSize < oldSize ifTrue:[ |
|
1118 |
oldLast := lastIndex. |
|
1119 |
lastIndex := newLast. |
|
1120 |
" |
|
1121 |
nil out rest, to give GC a chance to reclaim things |
|
1122 |
" |
|
1123 |
contentsArray from:lastIndex + 1 to:oldLast put:nil. |
|
1124 |
] ifFalse:[ |
|
1125 |
newLast <= contentsArray size ifTrue:[ |
|
1126 |
lastIndex := newLast. |
|
1127 |
^ self |
|
1128 |
]. |
|
77 | 1129 |
|
606 | 1130 |
newContents := Array basicNew:newSize. |
1131 |
newContents replaceFrom:1 to:oldSize with:contentsArray startingAt:firstIndex. |
|
1132 |
contentsArray := newContents. |
|
1133 |
firstIndex := 1. |
|
1134 |
lastIndex := newSize |
|
1135 |
] |
|
1136 |
] |
|
1137 |
! ! |
|
77 | 1138 |
|
606 | 1139 |
!OrderedCollection methodsFor:'inspecting'! |
1140 |
||
1141 |
inspectorClass |
|
1142 |
"redefined to launch an OrderedCollectionInspector |
|
1143 |
(instead of the default InspectorView)." |
|
1144 |
||
1145 |
^ OrderedCollectionInspectorView |
|
77 | 1146 |
|
1147 |
" |
|
606 | 1148 |
(OrderedCollection withAll:#(3 2 1)) inspect |
1149 |
(OrderedCollection withAll:#(3 2 1)) removeFirst; yourself; inspect |
|
1150 |
#(0 8 15 3 99 2) asSortedCollection inspect |
|
77 | 1151 |
" |
1152 |
! ! |
|
1153 |
||
1 | 1154 |
!OrderedCollection methodsFor:'private'! |
1155 |
||
606 | 1156 |
initContents:size |
1157 |
"setup the receiver-collection to hold size entries" |
|
33 | 1158 |
|
606 | 1159 |
contentsArray := Array basicNew:size. |
1160 |
firstIndex := 1. |
|
1161 |
lastIndex := 0 |
|
1 | 1162 |
! |
1163 |
||
1164 |
makeRoomAtFront |
|
282 | 1165 |
"grow/shift the contents for more room at the beginning. |
1166 |
Does not change the logical size. |
|
1167 |
i.e. |
|
1168 |
#(1 2 3 4 5 6) -> #(nil 1 2 3 4 5 6)" |
|
1 | 1169 |
|
1170 |
|newContents |
|
33 | 1171 |
oldSize "{ Class:SmallInteger }" |
1172 |
newSize "{ Class:SmallInteger }" |
|
1173 |
startIndex "{ Class:SmallInteger }" |
|
1174 |
sz "{ Class:SmallInteger }"| |
|
1 | 1175 |
|
1176 |
oldSize := contentsArray size. |
|
506
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1177 |
sz := lastIndex - firstIndex + 1. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1178 |
|
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1179 |
((oldSize == 0) or:[sz == 0]) ifTrue:[ |
253 | 1180 |
contentsArray := Array basicNew:3. |
159 | 1181 |
firstIndex := 2. lastIndex := 1. |
1182 |
^ self |
|
93 | 1183 |
]. |
1184 |
||
329 | 1185 |
" |
1186 |
if there is lots of room at the end (> 50%), |
|
1187 |
shift instead of growing. This helps collections |
|
1188 |
which get elements removed at the end and added at front. |
|
1189 |
" |
|
33 | 1190 |
oldSize > (sz * 2) ifTrue:[ |
508 | 1191 |
startIndex := oldSize // 4. |
1192 |
startIndex > 1 ifTrue:[ |
|
506
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1193 |
contentsArray |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1194 |
replaceFrom:startIndex |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1195 |
to:startIndex + sz - 1 |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1196 |
with:contentsArray |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1197 |
startingAt:1. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1198 |
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
|
1199 |
firstIndex := startIndex. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1200 |
lastIndex := startIndex + sz - 1. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1201 |
^ self |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1202 |
] |
33 | 1203 |
]. |
2 | 1204 |
newSize := oldSize * 2. |
329 | 1205 |
newSize == 0 ifTrue:[ newSize := 3]. |
253 | 1206 |
newContents := Array basicNew:newSize. |
77 | 1207 |
newContents |
159 | 1208 |
replaceFrom:(oldSize + 1) |
1209 |
to:newSize |
|
1210 |
with:contentsArray |
|
1211 |
startingAt:1. |
|
1 | 1212 |
contentsArray := newContents. |
1213 |
firstIndex := firstIndex + oldSize. |
|
1214 |
lastIndex := lastIndex + oldSize |
|
506
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1215 |
|
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1216 |
"Created: 8.11.1995 / 12:47:49 / cg" |
1 | 1217 |
! |
1218 |
||
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1219 |
makeRoomAtIndex:whereToMakeEmptySlot |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1220 |
"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
|
1221 |
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
|
1222 |
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
|
1223 |
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
|
1224 |
Since this changes the logical size, the modified index is returned. |
1 | 1225 |
i.e. |
1226 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 -> #(1 2 nil 3 4 5 6) |
|
1227 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 -> #(nil 1 2 3 4 5 6) |
|
1228 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 -> #(1 2 3 4 5 6 nil)" |
|
1229 |
||
1230 |
|newContents |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1231 |
newSize "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1232 |
oldSize "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1233 |
oneFourthOfSize "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1234 |
first "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1235 |
last "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1236 |
index "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1237 |
shiftLeft shiftRight| |
1 | 1238 |
|
1239 |
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
|
1240 |
oneFourthOfSize := oldSize // 4. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1241 |
index := whereToMakeEmptySlot. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1242 |
first := firstIndex. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1243 |
last := lastIndex. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1244 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1245 |
((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
|
1246 |
"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
|
1247 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1248 |
shiftLeft := true. |
33 | 1249 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1250 |
index == first ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1251 |
"/ 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
|
1252 |
"/ 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
|
1253 |
"/ 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
|
1254 |
"/ 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
|
1255 |
"/ 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
|
1256 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1257 |
(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
|
1258 |
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
|
1259 |
shiftLeft := false. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1260 |
shiftRight := true. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1261 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1262 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1263 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1264 |
] ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1265 |
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
|
1266 |
shiftRight := true |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1267 |
] |
33 | 1268 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1269 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1270 |
shiftLeft == true ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1271 |
"there is room (>25%) at the beginning" |
33 | 1272 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1273 |
index == first ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1274 |
contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1275 |
replaceFrom:(first - 1) |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1276 |
to:(index - 2) |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1277 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1278 |
startingAt:first. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1279 |
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
|
1280 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1281 |
firstIndex := first - 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1282 |
^ index - 1 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1283 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1284 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1285 |
shiftRight == true ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1286 |
"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
|
1287 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1288 |
last := last + 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1289 |
index == last ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1290 |
contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1291 |
replaceFrom:(index + 1) |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1292 |
to:last |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1293 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1294 |
startingAt:index. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1295 |
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
|
1296 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1297 |
lastIndex := last. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1298 |
^ index |
33 | 1299 |
]. |
1300 |
||
1301 |
newSize := oldSize * 2. |
|
253 | 1302 |
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
|
1303 |
index ~~ first ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1304 |
newContents |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1305 |
replaceFrom:1 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1306 |
to:index - first |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1307 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1308 |
startingAt:first. |
1 | 1309 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1310 |
index <= last ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1311 |
newContents |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1312 |
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
|
1313 |
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
|
1314 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1315 |
startingAt:index. |
1 | 1316 |
]. |
1317 |
contentsArray := newContents. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1318 |
firstIndex := 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1319 |
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
|
1320 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1321 |
"/ 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
|
1322 |
^ index - (first - firstIndex). |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1323 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1324 |
"Modified: 12.4.1996 / 17:18:58 / cg" |
1 | 1325 |
! |
1326 |
||
2222 | 1327 |
makeRoomAtIndex:whereToMakeEmptySlots for:howMany |
1328 |
"grow the contents for inserting at whereToMakeEmptySlot. |
|
1329 |
The whereToMakeEmptySlot argument must be a physical index within the contentsArray. |
|
1330 |
If there is (plenty of) room at either end, elements are shifted inplace to create |
|
1331 |
an empty slot; otherwise, a new contentsArray is allocated. |
|
1332 |
Since this changes the logical size, the modified index is returned. |
|
1333 |
i.e. |
|
1334 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 for:2 -> #(1 2 nil nil 3 4 5 6) |
|
1335 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 for:2 -> #(nil nil 1 2 3 4 5 6) |
|
1336 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 for:2 -> #(1 2 3 4 5 6 nil nil)" |
|
1337 |
||
1338 |
|newContents |
|
1339 |
newSize "{ Class:SmallInteger }" |
|
1340 |
oldSize "{ Class:SmallInteger }" |
|
1341 |
oneFourthOfSize "{ Class:SmallInteger }" |
|
1342 |
first "{ Class:SmallInteger }" |
|
1343 |
last "{ Class:SmallInteger }" |
|
1344 |
index "{ Class:SmallInteger }" |
|
1345 |
shiftLeft shiftRight| |
|
1346 |
||
1347 |
oldSize := contentsArray size. |
|
1348 |
oneFourthOfSize := oldSize // 4. |
|
1349 |
index := whereToMakeEmptySlots. |
|
1350 |
first := firstIndex. |
|
1351 |
last := lastIndex. |
|
1352 |
||
1353 |
((first > howMany) and:[first > oneFourthOfSize]) ifTrue:[ |
|
1354 |
"there is room (>25%) at the beginning" |
|
1355 |
||
1356 |
shiftLeft := true. |
|
1357 |
] ifFalse:[ |
|
1358 |
last < (oneFourthOfSize * 3) ifTrue:[ |
|
1359 |
shiftRight := true |
|
1360 |
] |
|
1361 |
]. |
|
1362 |
||
1363 |
shiftLeft == true ifTrue:[ |
|
1364 |
"there is room (>25%) at the beginning" |
|
1365 |
||
1366 |
index == first ifFalse:[ |
|
1367 |
contentsArray |
|
1368 |
replaceFrom:(first - howMany) |
|
1369 |
to:(index - howMany - 1) |
|
1370 |
with:contentsArray |
|
1371 |
startingAt:first. |
|
1372 |
contentsArray from:index-howMany to:index-1 put:nil. |
|
1373 |
]. |
|
1374 |
firstIndex := first - howMany. |
|
1375 |
^ index - howMany |
|
1376 |
]. |
|
1377 |
||
1378 |
shiftRight == true ifTrue:[ |
|
1379 |
"there is room (>25%) at the end" |
|
1380 |
||
1381 |
last := last + howMany. |
|
1382 |
index == last ifFalse:[ |
|
1383 |
contentsArray |
|
1384 |
replaceFrom:(index + howMany) |
|
1385 |
to:last |
|
1386 |
with:contentsArray |
|
1387 |
startingAt:index. |
|
1388 |
contentsArray from:index to:index+howMany-1 put:nil |
|
1389 |
]. |
|
1390 |
lastIndex := last. |
|
1391 |
^ index |
|
1392 |
]. |
|
1393 |
||
1394 |
newSize := oldSize * 2. |
|
1395 |
[newSize < (oldSize+howMany)] whileTrue:[ |
|
1396 |
newSize := newSize * 2 |
|
1397 |
]. |
|
1398 |
||
1399 |
newContents := Array basicNew:newSize. |
|
1400 |
index > first ifTrue:[ |
|
1401 |
newContents |
|
1402 |
replaceFrom:1 |
|
1403 |
to:index - first |
|
1404 |
with:contentsArray |
|
1405 |
startingAt:first. |
|
1406 |
]. |
|
1407 |
index <= last ifTrue:[ |
|
1408 |
newContents |
|
1409 |
replaceFrom:index - first + howMany + 1 |
|
1410 |
to:(last - first + howMany + 1) |
|
1411 |
with:contentsArray |
|
1412 |
startingAt:(index). |
|
1413 |
]. |
|
1414 |
contentsArray := newContents. |
|
1415 |
firstIndex := 1. |
|
1416 |
lastIndex := last - first + howMany + 1. |
|
1417 |
||
1418 |
"/ return the modified index |
|
1419 |
^ index - (first - firstIndex). |
|
1420 |
||
1421 |
"Modified: 22.1.1997 / 03:51:33 / cg" |
|
1422 |
! |
|
1423 |
||
606 | 1424 |
makeRoomAtLast |
1425 |
"grow/shift the contents for more room at the end. |
|
1426 |
Does not change the logical size. |
|
1427 |
i.e. |
|
1428 |
#(1 2 3 4 5 6) -> #(1 2 3 4 5 6 nil)" |
|
1429 |
||
1430 |
|newContents |
|
1431 |
oldSize "{ Class:SmallInteger }" |
|
1432 |
newSize "{ Class:SmallInteger }" |
|
1433 |
startIndex "{ Class:SmallInteger }" |
|
1434 |
sz "{ Class:SmallInteger }"| |
|
1435 |
||
1436 |
oldSize := contentsArray size. |
|
1437 |
sz := lastIndex - firstIndex + 1. |
|
1438 |
||
1439 |
" |
|
1440 |
if there is lots of room at the beginning (> 50%), |
|
1441 |
shift instead of growing. This helps collections which get |
|
1442 |
elements removed at front and added at the end. |
|
1443 |
" |
|
1444 |
oldSize > (sz * 2) ifTrue:[ |
|
1445 |
startIndex := firstIndex // 4. |
|
1446 |
startIndex == 0 ifTrue:[ |
|
1447 |
startIndex := 1 |
|
1448 |
]. |
|
1449 |
contentsArray |
|
1450 |
replaceFrom:startIndex |
|
1451 |
to:startIndex + sz - 1 |
|
1452 |
with:contentsArray |
|
1453 |
startingAt:firstIndex. |
|
1454 |
contentsArray from:startIndex + sz to:lastIndex put:nil. |
|
1455 |
firstIndex := startIndex. |
|
1456 |
lastIndex := startIndex + sz - 1. |
|
1457 |
^ self |
|
1458 |
]. |
|
1459 |
newSize := oldSize * 2. |
|
1460 |
newSize == 0 ifTrue:[newSize := 3]. |
|
1461 |
newContents := Array basicNew:newSize. |
|
1462 |
newContents |
|
1463 |
replaceFrom:1 |
|
1464 |
to:oldSize |
|
1465 |
with:contentsArray |
|
1466 |
startingAt:1. |
|
1467 |
contentsArray := newContents |
|
1468 |
! |
|
1469 |
||
1470 |
setFirstIndex:newFirstIndex lastIndex:newLastIndex |
|
1471 |
"set first and last index" |
|
1472 |
||
1473 |
firstIndex := newFirstIndex. |
|
1474 |
lastIndex := newLastIndex. |
|
1475 |
! ! |
|
1476 |
||
1477 |
!OrderedCollection methodsFor:'private accessing'! |
|
1478 |
||
1479 |
contentsArray |
|
1480 |
^ contentsArray |
|
1481 |
! |
|
1482 |
||
1483 |
firstIndex |
|
1484 |
^ firstIndex |
|
1485 |
! ! |
|
1486 |
||
1487 |
!OrderedCollection methodsFor:'queries'! |
|
1488 |
||
1489 |
capacity |
|
1490 |
"return the number of elements, that the receiver is |
|
1491 |
prepared to take. |
|
1492 |
Not used by the system; added for ST-80 compatibility." |
|
1493 |
||
1494 |
^ contentsArray size |
|
1495 |
! |
|
1496 |
||
1497 |
isEmpty |
|
1498 |
"return true, if the receiver has no elements" |
|
1499 |
||
1500 |
^ lastIndex < firstIndex |
|
1501 |
! |
|
1502 |
||
1503 |
isFixedSize |
|
1504 |
"return true if the receiver cannot grow - this will vanish once |
|
1505 |
Arrays and Strings learn how to grow ..." |
|
40 | 1506 |
|
606 | 1507 |
^ false |
1508 |
! |
|
1509 |
||
1510 |
notEmpty |
|
1511 |
"return true, if the receiver has any elements" |
|
1512 |
||
1513 |
^ lastIndex >= firstIndex |
|
1514 |
! |
|
1515 |
||
1516 |
size |
|
1517 |
"return the number of elements in the collection" |
|
1518 |
||
1519 |
^ lastIndex - firstIndex + 1 |
|
1520 |
! ! |
|
1521 |
||
1 | 1522 |
!OrderedCollection methodsFor:'testing'! |
1523 |
||
1524 |
identityIndexOf:anObject |
|
33 | 1525 |
"return the index of anObject or 0 if not found. Compare using ==" |
1 | 1526 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1527 |
|index| |
1 | 1528 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1529 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1530 |
identityIndexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1531 |
startingAt:firstIndex |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1532 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1533 |
index == 0 ifTrue:[^ 0]. |
1 | 1534 |
^ index - firstIndex + 1 |
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1535 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1536 |
"Modified: 12.4.1996 / 17:58:25 / cg" |
1 | 1537 |
! |
1538 |
||
1539 |
identityIndexOf:anObject startingAt:startIndex |
|
33 | 1540 |
"return the index of anObject, starting search at startIndex. |
1541 |
Compare using ==; return 0 if not found in the collection" |
|
1 | 1542 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1543 |
|index| |
1 | 1544 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1545 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1546 |
identityIndexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1547 |
startingAt:(startIndex + firstIndex - 1) |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1548 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1549 |
index == 0 ifTrue:[^ 0]. |
1 | 1550 |
^ index - firstIndex + 1 |
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1551 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1552 |
"Modified: 12.4.1996 / 17:58:19 / cg" |
1 | 1553 |
! |
1554 |
||
606 | 1555 |
includes:anObject |
1556 |
"return true if anObject is in the collection. Compare using =" |
|
1557 |
||
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1558 |
^ (contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1559 |
indexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1560 |
startingAt:firstIndex |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1561 |
endingAt:lastIndex) ~~ 0 |
606 | 1562 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1563 |
"Modified: 12.4.1996 / 17:57:27 / cg" |
606 | 1564 |
! |
1565 |
||
1071 | 1566 |
includesIdentical:anObject |
1567 |
"return true if anObject is in the collection. Compare using ==" |
|
1568 |
||
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1569 |
^ (contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1570 |
identityIndexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1571 |
startingAt:firstIndex |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1572 |
endingAt:lastIndex) ~~ 0 |
1071 | 1573 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1574 |
"Modified: 12.4.1996 / 17:57:09 / cg" |
1071 | 1575 |
! |
1576 |
||
1 | 1577 |
indexOf:anObject |
33 | 1578 |
"return the index of anObject or 0 if not found in the collection. |
1579 |
Compare using =" |
|
1 | 1580 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1581 |
|index| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1582 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1583 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1584 |
indexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1585 |
startingAt:firstIndex |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1586 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1587 |
index == 0 ifTrue:[^ 0]. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1588 |
^ index - firstIndex + 1 |
1 | 1589 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1590 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1591 |
|c| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1592 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1593 |
c := OrderedCollection new:10000. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1594 |
c add:1; add:2; add:3. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1595 |
c indexOf:99 |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1596 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1597 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1598 |
"Modified: 12.4.1996 / 17:57:54 / cg" |
1 | 1599 |
! |
1600 |
||
1601 |
indexOf:anObject startingAt:startIndex |
|
33 | 1602 |
"return the index of anObject, starting search at startIndex. |
1603 |
Compare using =; return 0 if not found in the collection" |
|
1 | 1604 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1605 |
|index| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1606 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1607 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1608 |
indexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1609 |
startingAt:(startIndex + firstIndex - 1) |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1610 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1611 |
index == 0 ifTrue:[^ 0]. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1612 |
^ index - firstIndex + 1 |
1 | 1613 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1614 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1615 |
|c| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1616 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1617 |
c := OrderedCollection new:10000. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1618 |
c add:1; add:2; add:3. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1619 |
c indexOf:4 startingAt:5 |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1620 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1621 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1622 |
"Modified: 12.4.1996 / 17:58:53 / cg" |
1 | 1623 |
! ! |
1624 |
||
629 | 1625 |
!OrderedCollection class methodsFor:'documentation'! |
1626 |
||
1627 |
version |
|
2354
652d17daeeb5
faster remove: if its the first or last element
Claus Gittinger <cg@exept.de>
parents:
2348
diff
changeset
|
1628 |
^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.57 1997-02-01 11:37:19 cg Exp $' |
629 | 1629 |
! ! |