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