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