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