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