author | Claus Gittinger <cg@exept.de> |
Tue, 07 Jan 1997 12:02:05 +0100 | |
changeset 2072 | e84dbf5e5424 |
parent 1496 | bea70dc24829 |
child 2166 | 73a9098ea254 |
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 |
|
606 | 184 |
at:anInteger |
185 |
"return the element at index, anInteger" |
|
1 | 186 |
|
606 | 187 |
|idx| |
387 | 188 |
|
606 | 189 |
idx := anInteger + firstIndex - 1. |
190 |
((anInteger < 1) or:[idx > lastIndex]) ifTrue:[ |
|
191 |
^ self subscriptBoundsError:anInteger |
|
192 |
] ifFalse:[ |
|
193 |
^ contentsArray at:idx |
|
194 |
] |
|
360 | 195 |
! |
196 |
||
606 | 197 |
at:anInteger put:anObject |
1221 | 198 |
"set the element at index, to be anInteger. |
199 |
Return anObject (sigh)." |
|
1 | 200 |
|
606 | 201 |
|idx| |
1 | 202 |
|
606 | 203 |
idx := anInteger + firstIndex - 1. |
204 |
((anInteger < 1) or:[idx > lastIndex]) ifTrue:[ |
|
1221 | 205 |
^ self subscriptBoundsError:anInteger |
606 | 206 |
] ifFalse:[ |
1221 | 207 |
^ contentsArray at:idx put:anObject |
606 | 208 |
] |
1221 | 209 |
|
210 |
"Modified: 19.4.1996 / 11:31:16 / cg" |
|
202 | 211 |
! |
212 |
||
606 | 213 |
first |
214 |
"return the first element" |
|
1 | 215 |
|
606 | 216 |
firstIndex <= lastIndex ifTrue:[ |
217 |
^ contentsArray at:firstIndex |
|
1 | 218 |
]. |
606 | 219 |
"error if collection is empty" |
220 |
^ self emptyCollectionError |
|
2 | 221 |
|
93 | 222 |
" |
606 | 223 |
(OrderedCollection withAll:#(1 2 3 4 5)) first |
224 |
(SortedCollection withAll:#(5 4 3 2 1)) first |
|
93 | 225 |
" |
13 | 226 |
! |
227 |
||
606 | 228 |
last |
229 |
"return the last element" |
|
68 | 230 |
|
606 | 231 |
firstIndex <= lastIndex ifTrue:[ |
232 |
^ contentsArray at:lastIndex |
|
233 |
]. |
|
234 |
"error if collection is empty" |
|
235 |
^ self emptyCollectionError |
|
68 | 236 |
|
93 | 237 |
" |
606 | 238 |
(OrderedCollection withAll:#(1 2 3 4 5)) last |
239 |
(SortedCollection withAll:#(5 4 3 2 1)) last |
|
93 | 240 |
" |
1 | 241 |
! ! |
242 |
||
243 |
!OrderedCollection methodsFor:'adding & removing'! |
|
244 |
||
606 | 245 |
add:anObject |
246 |
"add the argument, anObject to the end of the collection |
|
247 |
Return the argument, anObject." |
|
248 |
||
249 |
|idx "{ Class:SmallInteger }"| |
|
1 | 250 |
|
606 | 251 |
idx := lastIndex. |
1 | 252 |
|
606 | 253 |
(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
|
254 |
self makeRoomAtLast. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
255 |
idx := lastIndex. |
42 | 256 |
]. |
606 | 257 |
idx := lastIndex := idx + 1. |
258 |
contentsArray at:idx put:anObject. |
|
1 | 259 |
^ anObject |
42 | 260 |
|
348 | 261 |
" |
606 | 262 |
|c| |
263 |
c := #(4 3 2 1) asOrderedCollection. |
|
264 |
c add:'here' |
|
348 | 265 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
266 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
267 |
"Modified: 12.4.1996 / 13:26:57 / cg" |
1 | 268 |
! |
269 |
||
606 | 270 |
add:newObject after:oldObject |
271 |
"insert the argument, newObject after oldObject. |
|
272 |
If oldObject is not in the receiver, report an error, |
|
273 |
otherwise return the argument, anObject." |
|
1 | 274 |
|
606 | 275 |
|idx| |
1 | 276 |
|
606 | 277 |
idx := self indexOf:oldObject. |
278 |
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
|
279 |
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
|
280 |
^ newObject |
42 | 281 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
282 |
^ self errorValueNotFound:oldObject |
42 | 283 |
|
348 | 284 |
" |
606 | 285 |
|c| |
286 |
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
|
287 |
c add:'here' after:3; yourself. |
606 | 288 |
" |
289 |
" |
|
290 |
|c| |
|
291 |
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
|
292 |
c add:'here' after:1; yourself. |
606 | 293 |
" |
294 |
" |
|
295 |
|c| |
|
296 |
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
|
297 |
c add:'here' after:5; yourself |
606 | 298 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
299 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
300 |
"Modified: 12.4.1996 / 13:51:56 / cg" |
606 | 301 |
! |
302 |
||
303 |
add:anObject afterIndex:index |
|
304 |
"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
|
305 |
Return the receiver (sigh - ST-80 compatibility)." |
606 | 306 |
|
307 |
^ self add:anObject beforeIndex:(index + 1) |
|
308 |
||
309 |
" |
|
310 |
|c| |
|
311 |
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
|
312 |
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
|
313 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
314 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
315 |
|c| |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
316 |
c := #(4 3 2 1) asOrderedCollection. |
606 | 317 |
c add:'here' afterIndex:4 |
348 | 318 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
319 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
320 |
|c| |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
321 |
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
|
322 |
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
|
323 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
324 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
325 |
|c| |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
326 |
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
|
327 |
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
|
328 |
" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
329 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
330 |
"Modified: 12.4.1996 / 13:52:41 / cg" |
348 | 331 |
! |
332 |
||
606 | 333 |
add:newObject before:oldObject |
334 |
"insert the argument, newObject before oldObject. |
|
335 |
If oldObject is not in the receiver, report an error, |
|
336 |
otherwise return the argument, anObject." |
|
348 | 337 |
|
606 | 338 |
|idx| |
348 | 339 |
|
606 | 340 |
idx := self indexOf:oldObject. |
341 |
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
|
342 |
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
|
343 |
^ newObject |
348 | 344 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
345 |
^ self errorValueNotFound:oldObject |
348 | 346 |
|
347 |
" |
|
606 | 348 |
|c| |
349 |
c := #(4 3 2 1) asOrderedCollection. |
|
350 |
c add:'here' before:3. |
|
351 |
c add:'here' before:5 |
|
348 | 352 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
353 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
354 |
"Modified: 12.4.1996 / 13:25:47 / cg" |
348 | 355 |
! |
356 |
||
606 | 357 |
add:anObject beforeIndex:index |
358 |
"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
|
359 |
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
|
360 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
361 |
|idx| |
348 | 362 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
363 |
idx := self makeRoomAtIndex:(index + firstIndex - 1). |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
364 |
"/ notice: the above may change firstIndex |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
365 |
contentsArray at:idx put:anObject. |
348 | 366 |
|
367 |
" |
|
606 | 368 |
|c| |
369 |
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
|
370 |
c add:'here' beforeIndex:3 |
606 | 371 |
" |
372 |
" |
|
373 |
|c| |
|
374 |
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
|
375 |
c add:'here' beforeIndex:1 |
606 | 376 |
" |
377 |
" |
|
378 |
|c| |
|
379 |
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
|
380 |
c add:'here' beforeIndex:5 |
348 | 381 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
382 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
383 |
"Modified: 12.4.1996 / 16:47:31 / cg" |
1 | 384 |
! |
385 |
||
606 | 386 |
addFirst:anObject |
387 |
"add the argument, anObject to the beginning of the collection. |
|
388 |
Return the argument, anObject." |
|
56 | 389 |
|
606 | 390 |
(firstIndex == 1) ifTrue:[ |
391 |
self makeRoomAtFront |
|
348 | 392 |
]. |
606 | 393 |
firstIndex := firstIndex - 1. |
394 |
contentsArray at:firstIndex put:anObject. |
|
395 |
^ anObject |
|
56 | 396 |
|
397 |
" |
|
606 | 398 |
|c| |
399 |
c := #(1 2 3 4) asOrderedCollection. |
|
400 |
c addFirst:'here'. |
|
401 |
c |
|
402 |
" |
|
403 |
||
404 |
" |
|
405 |
|c| |
|
406 |
c := #() asOrderedCollection. |
|
407 |
c addFirst:'here'. |
|
408 |
c |
|
56 | 409 |
" |
410 |
! |
|
411 |
||
1 | 412 |
remove:anObject ifAbsent:exceptionBlock |
407 | 413 |
"remove the first occurrence of anObject from the collection |
414 |
and return it. |
|
415 |
If its not in the receiver collection, return the value of exceptionBlock." |
|
1 | 416 |
|
417 |
|index "{ Class:SmallInteger }"| |
|
418 |
||
419 |
index := firstIndex. |
|
420 |
[index <= lastIndex] whileTrue:[ |
|
159 | 421 |
anObject = (contentsArray at:index) ifTrue:[ |
329 | 422 |
contentsArray |
423 |
replaceFrom:index |
|
424 |
to:(contentsArray size - 1) |
|
425 |
with:contentsArray |
|
426 |
startingAt:(index + 1). |
|
1496 | 427 |
|
428 |
"/ nil it (helps GC) |
|
159 | 429 |
contentsArray at:lastIndex put:nil. |
1496 | 430 |
|
159 | 431 |
lastIndex := lastIndex - 1. |
432 |
firstIndex > lastIndex ifTrue:[ |
|
433 |
"reset to avoid ever growing" |
|
434 |
firstIndex := 1. |
|
435 |
lastIndex := 0 |
|
436 |
]. |
|
437 |
^ anObject |
|
438 |
]. |
|
439 |
index := index + 1 |
|
1 | 440 |
]. |
441 |
^ exceptionBlock value |
|
348 | 442 |
|
443 |
" |
|
1496 | 444 |
#(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:[self halt] |
445 |
#(1 2 3 4 5) asOrderedCollection remove:3 ifAbsent:[self halt] |
|
446 |
||
348 | 447 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection remove:3 ifAbsent:'oops' |
448 |
#(1 2 3 4 5) asOrderedCollection remove:9 ifAbsent:'oops' |
|
449 |
" |
|
1 | 450 |
! |
451 |
||
114 | 452 |
removeAll |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
453 |
"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
|
454 |
Returns the receiver." |
114 | 455 |
|
456 |
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
|
457 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
458 |
"Modified: 12.4.1996 / 13:34:19 / cg" |
114 | 459 |
! |
460 |
||
348 | 461 |
removeAllSuchThat:aBlock |
462 |
"remove all elements that meet a test criteria as specified in aBlock. |
|
463 |
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
|
464 |
for which it returns true, are removed. |
1496 | 465 |
Return a collection containing the removed elements. |
466 |
||
467 |
Performance hint: |
|
468 |
if a big number of objects is removed this way, |
|
469 |
it may be better to rebuild a new collection via #select:, |
|
470 |
since removing elements out of the middle is somewhat slow |
|
471 |
due to the need to copy over remaining elements within the collection." |
|
348 | 472 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
473 |
"/ this is a q&d implementation (possibly slow). |
348 | 474 |
|
475 |
|runIndex removed element| |
|
476 |
||
477 |
removed := self species new. |
|
478 |
||
479 |
runIndex := 1. |
|
480 |
[runIndex <= self size] whileTrue:[ |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
481 |
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
|
482 |
(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
|
483 |
removed add:element. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
484 |
self removeAtIndex:runIndex |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
485 |
] ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
486 |
runIndex := runIndex + 1 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
487 |
] |
348 | 488 |
]. |
489 |
^ removed |
|
490 |
||
491 |
" |
|
492 |
|coll| |
|
493 |
||
494 |
coll := OrderedCollection withAll:(1 to:10). |
|
1422 | 495 |
Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
496 |
Transcript showCR:coll |
|
348 | 497 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
498 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
499 |
"Modified: 12.4.1996 / 13:37:01 / cg" |
348 | 500 |
! |
501 |
||
606 | 502 |
removeFirst |
1496 | 503 |
"remove the first element from the collection; return the element." |
202 | 504 |
|
606 | 505 |
|anObject | |
13 | 506 |
|
606 | 507 |
firstIndex > lastIndex ifTrue:[ |
508 |
"error if collection is empty" |
|
509 |
^ self emptyCollectionError. |
|
510 |
]. |
|
511 |
anObject := contentsArray at:firstIndex. |
|
1496 | 512 |
|
513 |
"/ nil it out, to allow GC to reclaim it. |
|
514 |
||
606 | 515 |
contentsArray at:firstIndex put:nil. |
516 |
firstIndex := firstIndex + 1. |
|
1 | 517 |
|
606 | 518 |
firstIndex > lastIndex ifTrue:[ |
519 |
"reset to avoid ever growing" |
|
520 |
firstIndex := 1. |
|
521 |
lastIndex := 0 |
|
1 | 522 |
]. |
523 |
^ anObject |
|
13 | 524 |
|
525 |
" |
|
606 | 526 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself |
527 |
OrderedCollection new removeFirst |
|
528 |
(SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself |
|
93 | 529 |
" |
606 | 530 |
! |
531 |
||
532 |
removeFirst:n |
|
533 |
"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
|
534 |
Return a collection containing the removed elements." |
606 | 535 |
|
536 |
|mySize ret| |
|
537 |
||
538 |
mySize := self size. |
|
539 |
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
|
540 |
"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
|
541 |
^ self notEnoughElementsError. |
606 | 542 |
]. |
543 |
||
544 |
ret := Array new:n. |
|
545 |
ret replaceFrom:1 to:n with:contentsArray startingAt:firstIndex. |
|
1496 | 546 |
|
606 | 547 |
"/ |
548 |
"/ nil-out contents array, to not keep elements from being GC'd |
|
549 |
"/ |
|
550 |
contentsArray from:firstIndex to:firstIndex + n - 1 put:nil. |
|
551 |
firstIndex := firstIndex + n. |
|
552 |
||
553 |
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
|
554 |
"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
|
555 |
firstIndex := 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
556 |
lastIndex := 0 |
606 | 557 |
]. |
558 |
^ ret |
|
93 | 559 |
|
560 |
" |
|
606 | 561 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:2; yourself |
562 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:0; yourself |
|
563 |
OrderedCollection new removeFirst:2 |
|
564 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst:6 |
|
565 |
(SortedCollection withAll:#(5 4 3 2 1)) removeFirst:2; yourself |
|
13 | 566 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
567 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
568 |
"Modified: 12.4.1996 / 13:37:39 / cg" |
1 | 569 |
! |
570 |
||
606 | 571 |
removeFromIndex:startIndex toIndex:stopIndex |
572 |
"remove the elements stored under startIndex up to and including |
|
573 |
the elements under stopIndex. |
|
574 |
Return the receiver. |
|
575 |
Returning the receiver here is a historic leftover - it may change. |
|
576 |
Please use yourself in a cascade, if you need the receivers value |
|
577 |
when using this method." |
|
578 |
||
579 |
|nDeleted| |
|
1 | 580 |
|
606 | 581 |
(startIndex < 1 |
582 |
or:[stopIndex > self size]) ifTrue:[ |
|
583 |
^ self notEnoughElementsError |
|
584 |
]. |
|
585 |
nDeleted := stopIndex - startIndex + 1. |
|
586 |
contentsArray |
|
587 |
replaceFrom:(firstIndex + startIndex - 1) |
|
588 |
to:(lastIndex - nDeleted) |
|
589 |
with:contentsArray |
|
590 |
startingAt:(firstIndex + stopIndex). |
|
591 |
||
1496 | 592 |
"/ nil out rest (helps GC) |
606 | 593 |
contentsArray |
594 |
from:(lastIndex - nDeleted + 1) |
|
595 |
to:lastIndex |
|
596 |
put:nil. |
|
597 |
||
598 |
lastIndex := lastIndex - nDeleted. |
|
599 |
firstIndex > lastIndex ifTrue:[ |
|
600 |
"reset to avoid ever growing" |
|
601 |
firstIndex := 1. |
|
602 |
lastIndex := 0 |
|
603 |
] |
|
13 | 604 |
|
605 |
" |
|
606 | 606 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:3 toIndex:6 |
607 |
#(1 2 3 4 5) asOrderedCollection removeFromIndex:3 toIndex:6 |
|
217 | 608 |
" |
609 |
! |
|
610 |
||
606 | 611 |
removeLast |
612 |
"remove the last element from the collection; return the element" |
|
613 |
||
614 |
|anObject | |
|
217 | 615 |
|
606 | 616 |
firstIndex > lastIndex ifTrue:[ |
617 |
"error if collection is empty" |
|
618 |
^ self emptyCollectionError. |
|
619 |
]. |
|
620 |
anObject := contentsArray at:lastIndex. |
|
1496 | 621 |
|
622 |
"/ nil it (helps GC) |
|
606 | 623 |
contentsArray at:lastIndex put:nil. |
624 |
lastIndex := lastIndex - 1. |
|
625 |
||
626 |
firstIndex > lastIndex ifTrue:[ |
|
627 |
"reset to avoid ever growing" |
|
628 |
firstIndex := 1. |
|
629 |
lastIndex := 0 |
|
630 |
]. |
|
631 |
^ anObject |
|
217 | 632 |
|
633 |
" |
|
606 | 634 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast; yourself |
635 |
OrderedCollection new removeLast |
|
636 |
(SortedCollection withAll:#(5 4 3 2 1)) removeLast; yourself |
|
217 | 637 |
" |
13 | 638 |
! |
639 |
||
606 | 640 |
removeLast:n |
641 |
"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
|
642 |
Return a collection of removed elements." |
606 | 643 |
|
644 |
|mySize ret| |
|
645 |
||
646 |
mySize := self size. |
|
647 |
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
|
648 |
"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
|
649 |
^ self notEnoughElementsError. |
606 | 650 |
]. |
13 | 651 |
|
606 | 652 |
ret := Array new:n. |
653 |
ret replaceFrom:1 to:n with:contentsArray startingAt:lastIndex - n + 1. |
|
1496 | 654 |
|
606 | 655 |
"/ |
656 |
"/ nil-out contents array, to not keep elements from being GC'd |
|
657 |
"/ |
|
658 |
contentsArray from:lastIndex - n + 1 to:lastIndex put:nil. |
|
659 |
lastIndex := lastIndex - n. |
|
13 | 660 |
|
606 | 661 |
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
|
662 |
"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
|
663 |
firstIndex := 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
664 |
lastIndex := 0 |
13 | 665 |
]. |
606 | 666 |
^ ret |
13 | 667 |
|
668 |
" |
|
606 | 669 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:2; yourself |
670 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:0; yourself |
|
671 |
(OrderedCollection withAll:#(1 2 3 4 5)) removeLast:6; yourself |
|
672 |
(SortedCollection withAll:#(5 4 3 2 1)) removeLast:2; yourself |
|
13 | 673 |
" |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
674 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
675 |
"Modified: 12.4.1996 / 13:39:12 / cg" |
1 | 676 |
! ! |
677 |
||
606 | 678 |
!OrderedCollection methodsFor:'converting'! |
1 | 679 |
|
1170 | 680 |
asArray |
681 |
"return the receiver as an array." |
|
682 |
||
683 |
|newArray sz| |
|
684 |
||
685 |
sz := self size. |
|
686 |
newArray := Array new:sz. |
|
687 |
newArray replaceFrom:1 to:sz with:contentsArray startingAt:firstIndex. |
|
688 |
^ newArray |
|
689 |
||
690 |
" |
|
691 |
|o rnd| |
|
692 |
||
693 |
o := OrderedCollection new. |
|
694 |
rnd := Random new. |
|
695 |
10000 timesRepeat:[ |
|
696 |
o add:rnd next. |
|
697 |
]. |
|
698 |
Time millisecondsToRun:[o asArray] |
|
699 |
" |
|
700 |
||
701 |
"Modified: 13.4.1996 / 12:10:56 / cg" |
|
702 |
! |
|
703 |
||
606 | 704 |
asOrderedCollection |
705 |
"return the receiver as an ordered collection" |
|
1 | 706 |
|
606 | 707 |
"could be an instance of a subclass..." |
708 |
self class == OrderedCollection ifTrue:[ |
|
709 |
^ self |
|
710 |
]. |
|
711 |
^ super asOrderedCollection |
|
1 | 712 |
! ! |
713 |
||
606 | 714 |
!OrderedCollection methodsFor:'copying'! |
13 | 715 |
|
606 | 716 |
, aCollection |
717 |
"return a new collection formed from concatenating the receiver with |
|
718 |
the argument" |
|
1 | 719 |
|
606 | 720 |
|newCollection| |
1 | 721 |
|
606 | 722 |
newCollection := self copyEmpty:(self size + aCollection size). |
723 |
self do:[:element | |
|
724 |
newCollection add:element |
|
42 | 725 |
]. |
606 | 726 |
aCollection do:[:element | |
727 |
newCollection add:element |
|
728 |
]. |
|
729 |
^ newCollection |
|
42 | 730 |
|
253 | 731 |
" |
606 | 732 |
#(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection |
733 |
#(1 2 3) asSortedCollection , #(99 101 100) asSortedCollection |
|
253 | 734 |
" |
42 | 735 |
! |
736 |
||
606 | 737 |
copy |
738 |
"return a new OrderedCollection containing the elements of the receiver." |
|
739 |
||
740 |
"redefinition is a consequence of the implementation with a |
|
741 |
separate array - otherwise we get a shallow copy of the |
|
742 |
contents array, which is not what we want here" |
|
743 |
||
744 |
^ self copyFrom:1 to:self size |
|
745 |
! |
|
746 |
||
747 |
copyEmpty |
|
748 |
"return a copy of the receiver without any elements." |
|
749 |
||
750 |
^ self copyEmpty:10 |
|
751 |
! |
|
752 |
||
753 |
copyFrom:start to:stop |
|
754 |
"return a new OrderedCollection containing the elements |
|
755 |
from start to stop." |
|
42 | 756 |
|
606 | 757 |
|newCollection sz| |
758 |
||
759 |
sz := stop - start + 1. |
|
760 |
newCollection := self copyEmptyAndGrow:sz. "must grow, otherwise replace fails" |
|
761 |
newCollection replaceFrom:1 to:sz with:self startingAt:start. |
|
762 |
^ newCollection |
|
763 |
! |
|
764 |
||
765 |
copyWith:newElement |
|
766 |
"return a new collection containing the receivers elements |
|
767 |
and the single new element, newElement. |
|
768 |
This is different from concatentation, which expects another collection |
|
769 |
as argument, but equivalent to copy-and-addLast." |
|
770 |
||
771 |
|newCollection mySize newSize| |
|
772 |
||
773 |
mySize := self size. |
|
774 |
newSize := mySize + 1. |
|
775 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails" |
|
776 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1. |
|
777 |
newCollection at:newSize put:newElement. |
|
778 |
^newCollection |
|
42 | 779 |
|
253 | 780 |
" |
606 | 781 |
#(1 2 3 4 5) copyWith:$a |
782 |
'abcdefg' copyWith:$h |
|
783 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string |
|
784 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string |
|
785 |
" |
|
786 |
! |
|
787 |
||
788 |
postCopy |
|
789 |
"have to copy the contentsArray too" |
|
790 |
||
791 |
contentsArray := contentsArray shallowCopy |
|
792 |
! ! |
|
793 |
||
794 |
!OrderedCollection methodsFor:'enumerating'! |
|
795 |
||
796 |
collect:aBlock |
|
797 |
"evaluate the argument, aBlock for every element in the collection |
|
798 |
and return a collection of the results" |
|
799 |
||
800 |
|newCollection |
|
801 |
start "{ Class:SmallInteger }" |
|
802 |
stop "{ Class:SmallInteger }" | |
|
803 |
||
804 |
newCollection := self copyEmpty:(self size). |
|
805 |
stop := lastIndex. |
|
806 |
start := firstIndex. |
|
807 |
start to:stop do:[:index | |
|
808 |
newCollection add:(aBlock value:(contentsArray at:index)). |
|
809 |
]. |
|
810 |
^ newCollection |
|
811 |
||
253 | 812 |
" |
606 | 813 |
#(1 2 3 4) asOrderedCollection collect:[:i | i * i] |
814 |
#(1 2 3 4) asOrderedCollection collect:[:i | i even] |
|
815 |
" |
|
816 |
! |
|
817 |
||
818 |
do:aBlock |
|
819 |
"evaluate the argument, aBlock for every element in the collection." |
|
820 |
||
821 |
contentsArray from:firstIndex to:lastIndex do:aBlock |
|
822 |
! |
|
823 |
||
824 |
keysAndValuesDo:aTwoArgBlock |
|
825 |
"evaluate the argument, aBlock for every element in the collection, |
|
826 |
passing both index and element as arguments." |
|
827 |
||
828 |
|start "{ Class:SmallInteger }" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
829 |
stop "{ Class:SmallInteger }" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
830 |
idx "{ Class:SmallInteger }" | |
606 | 831 |
|
832 |
stop := lastIndex. |
|
833 |
start := firstIndex. |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
834 |
idx := 1. |
606 | 835 |
start to:stop do:[:index | |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
836 |
aTwoArgBlock value:idx value:(contentsArray at:index). |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
837 |
idx := idx + 1. |
606 | 838 |
] |
839 |
||
840 |
" |
|
841 |
#(10 20 30 40) asOrderedCollection keysAndValuesDo:[:index :value | |
|
1422 | 842 |
Transcript show:index; show:' '; showCR:value |
606 | 843 |
] |
844 |
" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
845 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
846 |
|oc| |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
847 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
848 |
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
|
849 |
oc removeFirst; removeFirst. |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
850 |
oc keysAndValuesDo:[:index :value | |
1422 | 851 |
Transcript show:index; show:' '; showCR:value |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
852 |
] |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
853 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
854 |
|
606 | 855 |
! |
856 |
||
679 | 857 |
keysAndValuesReverseDo:aTwoArgBlock |
858 |
"evaluate the argument, aBlock for every element in the collection, |
|
859 |
passing both index and element as arguments." |
|
860 |
||
861 |
|start "{ Class:SmallInteger }" |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
862 |
stop "{ Class:SmallInteger }" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
863 |
idx "{ Class:SmallInteger }"| |
679 | 864 |
|
865 |
stop := lastIndex. |
|
866 |
start := firstIndex. |
|
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
867 |
idx := (stop - start + 1). |
679 | 868 |
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
|
869 |
aTwoArgBlock value:idx value:(contentsArray at:index). |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
870 |
idx := idx - 1. |
679 | 871 |
] |
872 |
||
873 |
" |
|
874 |
#(10 20 30 40) asOrderedCollection keysAndValuesReverseDo:[:index :value | |
|
1422 | 875 |
Transcript show:index; show:' '; showCR:value |
1090
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
876 |
] |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
877 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
878 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
879 |
" |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
880 |
|oc| |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
881 |
|
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
882 |
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
|
883 |
oc removeFirst; removeFirst. |
def6e50a2d13
oops - keysAndValuesDo was wrong, iff elements have been removed upFront recently
ca
parents:
1071
diff
changeset
|
884 |
oc keysAndValuesReverseDo:[:index :value | |
1422 | 885 |
Transcript show:index; show:' '; showCR:value |
679 | 886 |
] |
887 |
" |
|
1071 | 888 |
! |
889 |
||
890 |
reverseDo:aBlock |
|
891 |
"evaluate the argument, aBlock for every element in the collection |
|
892 |
procesing elements in reverse direction (i.e. starting with the last)" |
|
893 |
||
894 |
contentsArray from:firstIndex to:lastIndex reverseDo:aBlock |
|
1 | 895 |
! ! |
896 |
||
56 | 897 |
!OrderedCollection methodsFor:'filling & replacing'! |
13 | 898 |
|
899 |
replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
1170 | 900 |
"replace elements in the receiver between index start and stop, |
901 |
with elements taken from replacementCollection starting at repStart. |
|
902 |
Redefined - can be done faster" |
|
13 | 903 |
|
904 |
|end| |
|
905 |
||
906 |
end := stop + firstIndex - 1. |
|
907 |
((start >= 1) and:[end <= lastIndex]) ifTrue:[ |
|
1170 | 908 |
aCollection class == self class ifTrue:[ |
909 |
contentsArray |
|
910 |
replaceFrom:(start + firstIndex - 1) |
|
911 |
to:end |
|
912 |
with:aCollection contentsArray |
|
913 |
startingAt:(repStart + aCollection firstIndex - 1). |
|
914 |
] ifFalse:[ |
|
915 |
contentsArray |
|
916 |
replaceFrom:(start + firstIndex - 1) |
|
917 |
to:end |
|
918 |
with:aCollection |
|
919 |
startingAt:repStart. |
|
920 |
]. |
|
921 |
^ self |
|
13 | 922 |
]. |
923 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart |
|
293 | 924 |
|
925 |
" |
|
926 |
|c| |
|
927 |
||
928 |
c := #(1 2 3 4 5 6) asOrderedCollection. |
|
929 |
c replaceFrom:3 to:6 with:c startingAt:2. |
|
930 |
c |
|
931 |
" |
|
932 |
" |
|
933 |
|c| |
|
934 |
||
935 |
c := #(1 2 3 4 5 6) asOrderedCollection. |
|
936 |
c replaceFrom:3 to:5 with:c startingAt:4. |
|
937 |
c |
|
938 |
" |
|
1170 | 939 |
|
940 |
"Modified: 13.4.1996 / 12:16:24 / cg" |
|
13 | 941 |
! ! |
942 |
||
606 | 943 |
!OrderedCollection methodsFor:'grow & shrink'! |
77 | 944 |
|
606 | 945 |
grow:newSize |
946 |
"grow the receiver to newSize" |
|
77 | 947 |
|
606 | 948 |
|oldSize newContents oldLast newLast| |
77 | 949 |
|
606 | 950 |
oldSize := lastIndex - firstIndex + 1. |
951 |
newSize ~~ oldSize ifTrue:[ |
|
952 |
newLast := firstIndex + newSize - 1. |
|
953 |
newSize < oldSize ifTrue:[ |
|
954 |
oldLast := lastIndex. |
|
955 |
lastIndex := newLast. |
|
956 |
" |
|
957 |
nil out rest, to give GC a chance to reclaim things |
|
958 |
" |
|
959 |
contentsArray from:lastIndex + 1 to:oldLast put:nil. |
|
960 |
] ifFalse:[ |
|
961 |
newLast <= contentsArray size ifTrue:[ |
|
962 |
lastIndex := newLast. |
|
963 |
^ self |
|
964 |
]. |
|
77 | 965 |
|
606 | 966 |
newContents := Array basicNew:newSize. |
967 |
newContents replaceFrom:1 to:oldSize with:contentsArray startingAt:firstIndex. |
|
968 |
contentsArray := newContents. |
|
969 |
firstIndex := 1. |
|
970 |
lastIndex := newSize |
|
971 |
] |
|
972 |
] |
|
973 |
! ! |
|
77 | 974 |
|
606 | 975 |
!OrderedCollection methodsFor:'inspecting'! |
976 |
||
977 |
inspectorClass |
|
978 |
"redefined to launch an OrderedCollectionInspector |
|
979 |
(instead of the default InspectorView)." |
|
980 |
||
981 |
^ OrderedCollectionInspectorView |
|
77 | 982 |
|
983 |
" |
|
606 | 984 |
(OrderedCollection withAll:#(3 2 1)) inspect |
985 |
(OrderedCollection withAll:#(3 2 1)) removeFirst; yourself; inspect |
|
986 |
#(0 8 15 3 99 2) asSortedCollection inspect |
|
77 | 987 |
" |
988 |
! ! |
|
989 |
||
1 | 990 |
!OrderedCollection methodsFor:'private'! |
991 |
||
606 | 992 |
initContents:size |
993 |
"setup the receiver-collection to hold size entries" |
|
33 | 994 |
|
606 | 995 |
contentsArray := Array basicNew:size. |
996 |
firstIndex := 1. |
|
997 |
lastIndex := 0 |
|
1 | 998 |
! |
999 |
||
1000 |
makeRoomAtFront |
|
282 | 1001 |
"grow/shift the contents for more room at the beginning. |
1002 |
Does not change the logical size. |
|
1003 |
i.e. |
|
1004 |
#(1 2 3 4 5 6) -> #(nil 1 2 3 4 5 6)" |
|
1 | 1005 |
|
1006 |
|newContents |
|
33 | 1007 |
oldSize "{ Class:SmallInteger }" |
1008 |
newSize "{ Class:SmallInteger }" |
|
1009 |
startIndex "{ Class:SmallInteger }" |
|
1010 |
sz "{ Class:SmallInteger }"| |
|
1 | 1011 |
|
1012 |
oldSize := contentsArray size. |
|
506
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1013 |
sz := lastIndex - firstIndex + 1. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1014 |
|
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1015 |
((oldSize == 0) or:[sz == 0]) ifTrue:[ |
253 | 1016 |
contentsArray := Array basicNew:3. |
159 | 1017 |
firstIndex := 2. lastIndex := 1. |
1018 |
^ self |
|
93 | 1019 |
]. |
1020 |
||
329 | 1021 |
" |
1022 |
if there is lots of room at the end (> 50%), |
|
1023 |
shift instead of growing. This helps collections |
|
1024 |
which get elements removed at the end and added at front. |
|
1025 |
" |
|
33 | 1026 |
oldSize > (sz * 2) ifTrue:[ |
508 | 1027 |
startIndex := oldSize // 4. |
1028 |
startIndex > 1 ifTrue:[ |
|
506
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1029 |
contentsArray |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1030 |
replaceFrom:startIndex |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1031 |
to:startIndex + sz - 1 |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1032 |
with:contentsArray |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1033 |
startingAt:1. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1034 |
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
|
1035 |
firstIndex := startIndex. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1036 |
lastIndex := startIndex + sz - 1. |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1037 |
^ self |
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1038 |
] |
33 | 1039 |
]. |
2 | 1040 |
newSize := oldSize * 2. |
329 | 1041 |
newSize == 0 ifTrue:[ newSize := 3]. |
253 | 1042 |
newContents := Array basicNew:newSize. |
77 | 1043 |
newContents |
159 | 1044 |
replaceFrom:(oldSize + 1) |
1045 |
to:newSize |
|
1046 |
with:contentsArray |
|
1047 |
startingAt:1. |
|
1 | 1048 |
contentsArray := newContents. |
1049 |
firstIndex := firstIndex + oldSize. |
|
1050 |
lastIndex := lastIndex + oldSize |
|
506
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1051 |
|
984b32fe635a
fixed makeRoomAtFront if underlying arrays size is <= 3
Claus Gittinger <cg@exept.de>
parents:
407
diff
changeset
|
1052 |
"Created: 8.11.1995 / 12:47:49 / cg" |
1 | 1053 |
! |
1054 |
||
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1055 |
makeRoomAtIndex:whereToMakeEmptySlot |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1056 |
"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
|
1057 |
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
|
1058 |
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
|
1059 |
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
|
1060 |
Since this changes the logical size, the modified index is returned. |
1 | 1061 |
i.e. |
1062 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 -> #(1 2 nil 3 4 5 6) |
|
1063 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 -> #(nil 1 2 3 4 5 6) |
|
1064 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 -> #(1 2 3 4 5 6 nil)" |
|
1065 |
||
1066 |
|newContents |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1067 |
newSize "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1068 |
oldSize "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1069 |
oneFourthOfSize "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1070 |
first "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1071 |
last "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1072 |
index "{ Class:SmallInteger }" |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1073 |
shiftLeft shiftRight| |
1 | 1074 |
|
1075 |
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
|
1076 |
oneFourthOfSize := oldSize // 4. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1077 |
index := whereToMakeEmptySlot. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1078 |
first := firstIndex. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1079 |
last := lastIndex. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1080 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1081 |
((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
|
1082 |
"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
|
1083 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1084 |
shiftLeft := true. |
33 | 1085 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1086 |
index == first ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1087 |
"/ 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
|
1088 |
"/ 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
|
1089 |
"/ 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
|
1090 |
"/ 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
|
1091 |
"/ 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
|
1092 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1093 |
(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
|
1094 |
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
|
1095 |
shiftLeft := false. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1096 |
shiftRight := true. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1097 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1098 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1099 |
] |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1100 |
] ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1101 |
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
|
1102 |
shiftRight := true |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1103 |
] |
33 | 1104 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1105 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1106 |
shiftLeft == true ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1107 |
"there is room (>25%) at the beginning" |
33 | 1108 |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1109 |
index == first ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1110 |
contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1111 |
replaceFrom:(first - 1) |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1112 |
to:(index - 2) |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1113 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1114 |
startingAt:first. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1115 |
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
|
1116 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1117 |
firstIndex := first - 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1118 |
^ index - 1 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1119 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1120 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1121 |
shiftRight == true ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1122 |
"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
|
1123 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1124 |
last := last + 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1125 |
index == last ifFalse:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1126 |
contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1127 |
replaceFrom:(index + 1) |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1128 |
to:last |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1129 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1130 |
startingAt:index. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1131 |
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
|
1132 |
]. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1133 |
lastIndex := last. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1134 |
^ index |
33 | 1135 |
]. |
1136 |
||
1137 |
newSize := oldSize * 2. |
|
253 | 1138 |
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
|
1139 |
index ~~ first ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1140 |
newContents |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1141 |
replaceFrom:1 |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1142 |
to:index - first |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1143 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1144 |
startingAt:first. |
1 | 1145 |
]. |
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1146 |
index <= last ifTrue:[ |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1147 |
newContents |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1148 |
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
|
1149 |
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
|
1150 |
with:contentsArray |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1151 |
startingAt:index. |
1 | 1152 |
]. |
1153 |
contentsArray := newContents. |
|
1161
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1154 |
firstIndex := 1. |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1155 |
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
|
1156 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1157 |
"/ 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
|
1158 |
^ index - (first - firstIndex). |
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1159 |
|
aad722dea51a
return values compatible to ST-80; added comments; fixed & tuned #makeRoomAtIndex:
Claus Gittinger <cg@exept.de>
parents:
1157
diff
changeset
|
1160 |
"Modified: 12.4.1996 / 17:18:58 / cg" |
1 | 1161 |
! |
1162 |
||
606 | 1163 |
makeRoomAtLast |
1164 |
"grow/shift the contents for more room at the end. |
|
1165 |
Does not change the logical size. |
|
1166 |
i.e. |
|
1167 |
#(1 2 3 4 5 6) -> #(1 2 3 4 5 6 nil)" |
|
1168 |
||
1169 |
|newContents |
|
1170 |
oldSize "{ Class:SmallInteger }" |
|
1171 |
newSize "{ Class:SmallInteger }" |
|
1172 |
startIndex "{ Class:SmallInteger }" |
|
1173 |
sz "{ Class:SmallInteger }"| |
|
1174 |
||
1175 |
oldSize := contentsArray size. |
|
1176 |
sz := lastIndex - firstIndex + 1. |
|
1177 |
||
1178 |
" |
|
1179 |
if there is lots of room at the beginning (> 50%), |
|
1180 |
shift instead of growing. This helps collections which get |
|
1181 |
elements removed at front and added at the end. |
|
1182 |
" |
|
1183 |
oldSize > (sz * 2) ifTrue:[ |
|
1184 |
startIndex := firstIndex // 4. |
|
1185 |
startIndex == 0 ifTrue:[ |
|
1186 |
startIndex := 1 |
|
1187 |
]. |
|
1188 |
contentsArray |
|
1189 |
replaceFrom:startIndex |
|
1190 |
to:startIndex + sz - 1 |
|
1191 |
with:contentsArray |
|
1192 |
startingAt:firstIndex. |
|
1193 |
contentsArray from:startIndex + sz to:lastIndex put:nil. |
|
1194 |
firstIndex := startIndex. |
|
1195 |
lastIndex := startIndex + sz - 1. |
|
1196 |
^ self |
|
1197 |
]. |
|
1198 |
newSize := oldSize * 2. |
|
1199 |
newSize == 0 ifTrue:[newSize := 3]. |
|
1200 |
newContents := Array basicNew:newSize. |
|
1201 |
newContents |
|
1202 |
replaceFrom:1 |
|
1203 |
to:oldSize |
|
1204 |
with:contentsArray |
|
1205 |
startingAt:1. |
|
1206 |
contentsArray := newContents |
|
1207 |
! |
|
1208 |
||
1209 |
setFirstIndex:newFirstIndex lastIndex:newLastIndex |
|
1210 |
"set first and last index" |
|
1211 |
||
1212 |
firstIndex := newFirstIndex. |
|
1213 |
lastIndex := newLastIndex. |
|
1214 |
! ! |
|
1215 |
||
1216 |
!OrderedCollection methodsFor:'private accessing'! |
|
1217 |
||
1218 |
contentsArray |
|
1219 |
^ contentsArray |
|
1220 |
! |
|
1221 |
||
1222 |
firstIndex |
|
1223 |
^ firstIndex |
|
1224 |
! ! |
|
1225 |
||
1226 |
!OrderedCollection methodsFor:'queries'! |
|
1227 |
||
1228 |
capacity |
|
1229 |
"return the number of elements, that the receiver is |
|
1230 |
prepared to take. |
|
1231 |
Not used by the system; added for ST-80 compatibility." |
|
1232 |
||
1233 |
^ contentsArray size |
|
1234 |
! |
|
1235 |
||
1236 |
isEmpty |
|
1237 |
"return true, if the receiver has no elements" |
|
1238 |
||
1239 |
^ lastIndex < firstIndex |
|
1240 |
! |
|
1241 |
||
1242 |
isFixedSize |
|
1243 |
"return true if the receiver cannot grow - this will vanish once |
|
1244 |
Arrays and Strings learn how to grow ..." |
|
40 | 1245 |
|
606 | 1246 |
^ false |
1247 |
! |
|
1248 |
||
1249 |
notEmpty |
|
1250 |
"return true, if the receiver has any elements" |
|
1251 |
||
1252 |
^ lastIndex >= firstIndex |
|
1253 |
! |
|
1254 |
||
1255 |
size |
|
1256 |
"return the number of elements in the collection" |
|
1257 |
||
1258 |
^ lastIndex - firstIndex + 1 |
|
1259 |
! ! |
|
1260 |
||
1261 |
!OrderedCollection methodsFor:'searching'! |
|
1262 |
||
1263 |
after:anObject ifAbsent:exceptionBlock |
|
1264 |
"return the element after the argument anObject, nil if there is none. |
|
1265 |
If anObject is not in the receiver, return the result from evaluating exceptionBlock." |
|
1266 |
||
1267 |
|idx| |
|
1268 |
||
1269 |
idx := self indexOf:anObject. |
|
1270 |
idx ~~ 0 ifTrue:[ |
|
1367 | 1271 |
idx == lastIndex ifTrue:[^ nil]. |
1272 |
^ self at:(idx + 1). |
|
606 | 1273 |
]. |
1274 |
^ exceptionBlock value |
|
1275 |
||
1276 |
" |
|
1277 |
#(4 3 2 1) asOrderedCollection after:3. |
|
1367 | 1278 |
#(4 3 3 1) asOrderedCollection after:3. <- what should be returned here ? |
606 | 1279 |
#(4 3 2 1) asOrderedCollection after:5 |
1280 |
#(4 3 2 1) asOrderedCollection after:1 |
|
1281 |
" |
|
1282 |
||
1367 | 1283 |
"Modified: 10.5.1996 / 14:07:28 / cg" |
606 | 1284 |
! |
1285 |
||
1286 |
before:anObject ifAbsent:exceptionBlock |
|
1287 |
"return the element before the argument anObject, nil if there is none. |
|
1288 |
If anObject is not in the receiver, return the result from evaluating exceptionBlock." |
|
1289 |
||
1290 |
|idx| |
|
1291 |
||
1292 |
idx := self indexOf:anObject. |
|
1293 |
idx ~~ 0 ifTrue:[ |
|
1367 | 1294 |
idx == firstIndex ifTrue:[^ nil]. |
1295 |
^ self at:(idx - 1). |
|
606 | 1296 |
]. |
1297 |
^ exceptionBlock value |
|
1298 |
||
1299 |
" |
|
1300 |
#(4 3 2 1) asOrderedCollection before:3. |
|
1301 |
#(4 3 2 1) asOrderedCollection before:5 |
|
1302 |
#(4 3 2 1) asOrderedCollection before:1 |
|
1367 | 1303 |
#(4 2 1 1) asOrderedCollection before:1 |
606 | 1304 |
#(4 3 2 1) asOrderedCollection before:4 |
1305 |
" |
|
1367 | 1306 |
|
1307 |
"Modified: 10.5.1996 / 14:07:55 / cg" |
|
1 | 1308 |
! ! |
1309 |
||
1310 |
!OrderedCollection methodsFor:'testing'! |
|
1311 |
||
1312 |
identityIndexOf:anObject |
|
33 | 1313 |
"return the index of anObject or 0 if not found. Compare using ==" |
1 | 1314 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1315 |
|index| |
1 | 1316 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1317 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1318 |
identityIndexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1319 |
startingAt:firstIndex |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1320 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1321 |
index == 0 ifTrue:[^ 0]. |
1 | 1322 |
^ index - firstIndex + 1 |
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1323 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1324 |
"Modified: 12.4.1996 / 17:58:25 / cg" |
1 | 1325 |
! |
1326 |
||
1327 |
identityIndexOf:anObject startingAt:startIndex |
|
33 | 1328 |
"return the index of anObject, starting search at startIndex. |
1329 |
Compare using ==; return 0 if not found in the collection" |
|
1 | 1330 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1331 |
|index| |
1 | 1332 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1333 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1334 |
identityIndexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1335 |
startingAt:(startIndex + firstIndex - 1) |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1336 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1337 |
index == 0 ifTrue:[^ 0]. |
1 | 1338 |
^ index - firstIndex + 1 |
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1339 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1340 |
"Modified: 12.4.1996 / 17:58:19 / cg" |
1 | 1341 |
! |
1342 |
||
606 | 1343 |
includes:anObject |
1344 |
"return true if anObject is in the collection. Compare using =" |
|
1345 |
||
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1346 |
^ (contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1347 |
indexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1348 |
startingAt:firstIndex |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1349 |
endingAt:lastIndex) ~~ 0 |
606 | 1350 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1351 |
"Modified: 12.4.1996 / 17:57:27 / cg" |
606 | 1352 |
! |
1353 |
||
1071 | 1354 |
includesIdentical:anObject |
1355 |
"return true if anObject is in the collection. Compare using ==" |
|
1356 |
||
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1357 |
^ (contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1358 |
identityIndexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1359 |
startingAt:firstIndex |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1360 |
endingAt:lastIndex) ~~ 0 |
1071 | 1361 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1362 |
"Modified: 12.4.1996 / 17:57:09 / cg" |
1071 | 1363 |
! |
1364 |
||
1 | 1365 |
indexOf:anObject |
33 | 1366 |
"return the index of anObject or 0 if not found in the collection. |
1367 |
Compare using =" |
|
1 | 1368 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1369 |
|index| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1370 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1371 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1372 |
indexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1373 |
startingAt:firstIndex |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1374 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1375 |
index == 0 ifTrue:[^ 0]. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1376 |
^ index - firstIndex + 1 |
1 | 1377 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1378 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1379 |
|c| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1380 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1381 |
c := OrderedCollection new:10000. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1382 |
c add:1; add:2; add:3. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1383 |
c indexOf:99 |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1384 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1385 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1386 |
"Modified: 12.4.1996 / 17:57:54 / cg" |
1 | 1387 |
! |
1388 |
||
1389 |
indexOf:anObject startingAt:startIndex |
|
33 | 1390 |
"return the index of anObject, starting search at startIndex. |
1391 |
Compare using =; return 0 if not found in the collection" |
|
1 | 1392 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1393 |
|index| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1394 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1395 |
index := contentsArray |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1396 |
indexOf:anObject |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1397 |
startingAt:(startIndex + firstIndex - 1) |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1398 |
endingAt:lastIndex. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1399 |
index == 0 ifTrue:[^ 0]. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1400 |
^ index - firstIndex + 1 |
1 | 1401 |
|
1157
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1402 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1403 |
|c| |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1404 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1405 |
c := OrderedCollection new:10000. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1406 |
c add:1; add:2; add:3. |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1407 |
c indexOf:4 startingAt:5 |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1408 |
" |
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1409 |
|
c52d548036e2
faster search (limit to active range)
Claus Gittinger <cg@exept.de>
parents:
1106
diff
changeset
|
1410 |
"Modified: 12.4.1996 / 17:58:53 / cg" |
1 | 1411 |
! ! |
1412 |
||
629 | 1413 |
!OrderedCollection class methodsFor:'documentation'! |
1414 |
||
1415 |
version |
|
1496 | 1416 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/OrdColl.st,v 1.53 1996-06-29 09:44:00 cg Exp $' |
629 | 1417 |
! ! |