1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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
|
|
14 |
instanceVariableNames:'contentsArray firstIndex lastIndex'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Sequenceable'
|
|
18 |
!
|
|
19 |
|
|
20 |
OrderedCollection comment:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
77
|
25 |
$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.12 1994-05-17 10:08:27 claus Exp $
|
33
|
26 |
written spring 89 by claus
|
|
27 |
'!
|
|
28 |
|
|
29 |
!OrderedCollection class methodsFor:'documentation'!
|
|
30 |
|
|
31 |
documentation
|
|
32 |
"
|
68
|
33 |
OrderedCollection have ordered elements. Insertion and removal at both ends
|
|
34 |
is possible - therefore they can be used for queues and stacks.
|
1
|
35 |
|
68
|
36 |
Instance variables:
|
1
|
37 |
|
68
|
38 |
contentsArray <Array> the actual contents
|
|
39 |
firstIndex <SmallInteger> index of first valid element
|
|
40 |
lastIndex <SmallInteger> index of last valid element
|
33
|
41 |
"
|
|
42 |
! !
|
1
|
43 |
|
|
44 |
!OrderedCollection class methodsFor:'instance creation'!
|
|
45 |
|
|
46 |
new:size
|
|
47 |
"create a new OrderedCollection"
|
|
48 |
|
|
49 |
^ (self basicNew) initContents:size
|
|
50 |
!
|
|
51 |
|
|
52 |
new
|
|
53 |
"create a new OrderedCollection"
|
|
54 |
|
|
55 |
^ (self basicNew) initContents:10
|
|
56 |
! !
|
|
57 |
|
13
|
58 |
!OrderedCollection methodsFor:'queries'!
|
1
|
59 |
|
|
60 |
size
|
|
61 |
"return the number of elements in the collection"
|
|
62 |
|
|
63 |
^ lastIndex - firstIndex + 1
|
|
64 |
!
|
|
65 |
|
|
66 |
isFixedSize
|
|
67 |
"return true if the receiver cannot grow - this will vanish once
|
|
68 |
Arrays and Strings learn how to grow ..."
|
|
69 |
|
|
70 |
^ false
|
|
71 |
! !
|
|
72 |
|
|
73 |
!OrderedCollection methodsFor:'copying'!
|
|
74 |
|
|
75 |
, aCollection
|
|
76 |
"return a new collection formed from concatenating the receiver with
|
|
77 |
the argument"
|
|
78 |
|
|
79 |
|newCollection|
|
|
80 |
|
|
81 |
newCollection := self species new:(self size + aCollection size).
|
|
82 |
self do:[:element |
|
|
83 |
newCollection add:element
|
|
84 |
].
|
|
85 |
aCollection do:[:element |
|
|
86 |
newCollection add:element
|
|
87 |
].
|
|
88 |
^ newCollection
|
2
|
89 |
|
|
90 |
"#(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection"
|
13
|
91 |
!
|
|
92 |
|
68
|
93 |
copyWith:newElement
|
|
94 |
"return a new collection consisting of a copy of the receivers elements
|
|
95 |
plus the argument."
|
|
96 |
|
|
97 |
|newCollection mySize newSize|
|
|
98 |
|
|
99 |
mySize := self size.
|
|
100 |
newSize := mySize + 1.
|
|
101 |
newCollection := self species new:newSize.
|
|
102 |
newCollection grow:newSize.
|
|
103 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1.
|
|
104 |
newCollection at:newSize put:newElement.
|
|
105 |
^newCollection
|
|
106 |
|
|
107 |
"#(1 2 3 4 5) copyWith:$a"
|
|
108 |
"'abcdefg' copyWith:$h"
|
|
109 |
"'abcdefg' copyWith:'123'" "-- will fail: string cannot be stored into string"
|
|
110 |
"'abcdefg' copyWith:1" "-- will fail: integer cannot be stored into string"
|
|
111 |
!
|
|
112 |
|
13
|
113 |
copy
|
|
114 |
"return a new OrderedCollection containing the elements of the receiver."
|
|
115 |
|
|
116 |
"redefinition is a consequence of the implementation with a
|
|
117 |
separate array - otherwise we get a shallow copy of the
|
|
118 |
contents array, which is not what we want here"
|
|
119 |
|
|
120 |
^ self copyFrom:1 to:self size
|
|
121 |
!
|
|
122 |
|
|
123 |
copyFrom:start to:stop
|
|
124 |
"return a new OrderedCollection containing the elements
|
|
125 |
from start to stop."
|
|
126 |
|
|
127 |
|newCollection sz|
|
|
128 |
|
|
129 |
sz := stop - start + 1.
|
|
130 |
newCollection := self species new:sz.
|
|
131 |
newCollection grow:sz.
|
|
132 |
newCollection replaceFrom:1 to:sz with:self startingAt:start.
|
|
133 |
^ newCollection
|
1
|
134 |
! !
|
|
135 |
|
|
136 |
!OrderedCollection methodsFor:'adding & removing'!
|
|
137 |
|
|
138 |
removeFirst
|
|
139 |
"remove the first element from the collection; return the element"
|
|
140 |
|
|
141 |
|anObject |
|
|
142 |
|
42
|
143 |
firstIndex > lastIndex ifTrue:[
|
|
144 |
"error if collection is empty"
|
|
145 |
self subscriptBoundsError.
|
|
146 |
^ nil
|
|
147 |
].
|
1
|
148 |
anObject := contentsArray at:firstIndex.
|
13
|
149 |
contentsArray at:firstIndex put:nil.
|
1
|
150 |
firstIndex := firstIndex + 1.
|
13
|
151 |
firstIndex > lastIndex ifTrue:[
|
|
152 |
"reset to avoid ever growing"
|
|
153 |
firstIndex := 1.
|
|
154 |
lastIndex := 0
|
|
155 |
].
|
1
|
156 |
^ anObject
|
42
|
157 |
|
|
158 |
"(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself"
|
|
159 |
"(SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself"
|
|
160 |
"(SortedCollection new) removeFirst"
|
1
|
161 |
!
|
|
162 |
|
|
163 |
removeLast
|
|
164 |
"remove the last element from the collection; return the element"
|
|
165 |
|
|
166 |
|anObject |
|
|
167 |
|
42
|
168 |
firstIndex > lastIndex ifTrue:[
|
|
169 |
"error if collection is empty"
|
|
170 |
self subscriptBoundsError.
|
|
171 |
^ nil
|
|
172 |
].
|
1
|
173 |
anObject := contentsArray at:lastIndex.
|
13
|
174 |
contentsArray at:lastIndex put:nil.
|
1
|
175 |
lastIndex := lastIndex - 1.
|
13
|
176 |
firstIndex > lastIndex ifTrue:[
|
|
177 |
"reset to avoid ever growing"
|
|
178 |
firstIndex := 1.
|
|
179 |
lastIndex := 0
|
|
180 |
].
|
1
|
181 |
^ anObject
|
42
|
182 |
|
|
183 |
"(OrderedCollection withAll:#(1 2 3 4 5)) removeLast; yourself"
|
|
184 |
"(SortedCollection withAll:#(5 4 3 2 1)) removeLast; yourself"
|
1
|
185 |
!
|
|
186 |
|
56
|
187 |
removeFromIndex:startIndex to:stopIndex
|
|
188 |
"remove the elements stored under startIndex up to and including
|
|
189 |
the elements under stopIndex.
|
|
190 |
return the receiver."
|
|
191 |
|
|
192 |
|nDeleted|
|
|
193 |
|
|
194 |
nDeleted := stopIndex - startIndex + 1.
|
|
195 |
contentsArray replaceFrom:(firstIndex + startIndex)
|
|
196 |
to:(lastIndex - nDeleted)
|
|
197 |
with:contentsArray
|
|
198 |
startingAt:(firstIndex + stopIndex + 1).
|
|
199 |
contentsArray from:(lastIndex - nDeleted + 1)
|
|
200 |
to:lastIndex
|
|
201 |
put:nil.
|
|
202 |
|
|
203 |
lastIndex := lastIndex - nDeleted.
|
|
204 |
firstIndex > lastIndex ifTrue:[
|
|
205 |
"reset to avoid ever growing"
|
|
206 |
firstIndex := 1.
|
|
207 |
lastIndex := 0
|
|
208 |
]
|
|
209 |
|
|
210 |
"
|
|
211 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:3 to:6
|
|
212 |
"
|
|
213 |
!
|
|
214 |
|
1
|
215 |
remove:anObject ifAbsent:exceptionBlock
|
|
216 |
"remove the first occurrence of anObject from the collection;
|
|
217 |
return the value of exceptionBlock if anObject is not in
|
|
218 |
the collection"
|
|
219 |
|
|
220 |
|index "{ Class:SmallInteger }"|
|
|
221 |
|
|
222 |
index := firstIndex.
|
|
223 |
[index <= lastIndex] whileTrue:[
|
|
224 |
anObject = (contentsArray at:index) ifTrue:[
|
|
225 |
contentsArray replaceFrom:index to:(contentsArray size - 1)
|
|
226 |
with:contentsArray startingAt:(index + 1).
|
13
|
227 |
contentsArray at:lastIndex put:nil.
|
1
|
228 |
lastIndex := lastIndex - 1.
|
13
|
229 |
firstIndex > lastIndex ifTrue:[
|
|
230 |
"reset to avoid ever growing"
|
|
231 |
firstIndex := 1.
|
|
232 |
lastIndex := 0
|
|
233 |
].
|
1
|
234 |
^ anObject
|
|
235 |
].
|
|
236 |
index := index + 1
|
|
237 |
].
|
|
238 |
^ exceptionBlock value
|
|
239 |
!
|
|
240 |
|
|
241 |
add:anObject
|
|
242 |
"add the argument, anObject to the end of the collection
|
|
243 |
Return the argument, anObject."
|
|
244 |
|
|
245 |
(lastIndex == contentsArray size) ifTrue:[
|
|
246 |
self makeRoomAtLast
|
|
247 |
].
|
|
248 |
lastIndex := lastIndex + 1.
|
|
249 |
contentsArray at:lastIndex put:anObject.
|
|
250 |
^ anObject
|
13
|
251 |
|
|
252 |
"
|
|
253 |
|c|
|
|
254 |
c := #(4 3 2 1) asOrderedCollection.
|
|
255 |
c add:'here'
|
|
256 |
"
|
1
|
257 |
!
|
|
258 |
|
|
259 |
addFirst:anObject
|
|
260 |
"add the argument, anObject to the beginning of the collection.
|
|
261 |
Return the argument, anObject."
|
|
262 |
|
|
263 |
(firstIndex == 1) ifTrue:[
|
|
264 |
self makeRoomAtFront
|
|
265 |
].
|
|
266 |
firstIndex := firstIndex - 1.
|
|
267 |
contentsArray at:firstIndex put:anObject.
|
|
268 |
^ anObject
|
13
|
269 |
|
|
270 |
"
|
|
271 |
|c|
|
|
272 |
c := #(4 3 2 1) asOrderedCollection.
|
|
273 |
c addFirst:'here'
|
|
274 |
"
|
1
|
275 |
!
|
|
276 |
|
|
277 |
add:anObject beforeIndex:index
|
|
278 |
"insert the argument, anObject to become located at index.
|
|
279 |
Return the argument, anObject."
|
|
280 |
|
|
281 |
self makeRoomAtIndex:(index - firstIndex + 1).
|
|
282 |
contentsArray at:(index - firstIndex + 1) put:anObject.
|
13
|
283 |
|
|
284 |
"
|
|
285 |
|c|
|
|
286 |
c := #(4 3 2 1) asOrderedCollection.
|
|
287 |
c add:'here' beforeIndex:3
|
|
288 |
"
|
|
289 |
!
|
|
290 |
|
|
291 |
add:newObject after:oldObject
|
|
292 |
"insert the argument, newObject after oldObject.
|
|
293 |
If oldObject is not in the receiver, report an error,
|
|
294 |
otherwise return the argument, anObject."
|
|
295 |
|
|
296 |
|idx|
|
|
297 |
|
|
298 |
idx := self indexOf:oldObject.
|
|
299 |
idx ~~ 0 ifTrue:[
|
|
300 |
^ self add:newObject beforeIndex:(idx + 1).
|
|
301 |
].
|
|
302 |
self errorNotFound
|
|
303 |
|
|
304 |
"
|
|
305 |
|c|
|
|
306 |
c := #(4 3 2 1) asOrderedCollection.
|
33
|
307 |
c add:'here' after:3.
|
|
308 |
c add:'here' after:5
|
13
|
309 |
"
|
|
310 |
!
|
|
311 |
|
|
312 |
|
|
313 |
add:newObject before:oldObject
|
|
314 |
"insert the argument, newObject before oldObject.
|
|
315 |
If oldObject is not in the receiver, report an error,
|
|
316 |
otherwise return the argument, anObject."
|
|
317 |
|
|
318 |
|idx|
|
|
319 |
|
|
320 |
idx := self indexOf:oldObject.
|
|
321 |
idx ~~ 0 ifTrue:[
|
|
322 |
^ self add:newObject beforeIndex:idx.
|
|
323 |
].
|
|
324 |
self errorNotFound
|
|
325 |
|
|
326 |
"
|
|
327 |
|c|
|
|
328 |
c := #(4 3 2 1) asOrderedCollection.
|
33
|
329 |
c add:'here' before:3.
|
|
330 |
c add:'here' before:5
|
13
|
331 |
"
|
1
|
332 |
! !
|
|
333 |
|
|
334 |
!OrderedCollection methodsFor:'grow & shrink'!
|
|
335 |
|
|
336 |
grow:newSize
|
13
|
337 |
"grow the receiver to newSize"
|
1
|
338 |
|
33
|
339 |
|newContents oldLast|
|
1
|
340 |
|
|
341 |
newSize <= (lastIndex - firstIndex + 1) ifTrue:[
|
33
|
342 |
oldLast := lastIndex.
|
|
343 |
lastIndex := firstIndex + newSize - 1.
|
|
344 |
contentsArray from:lastIndex + 1 to:oldLast put:nil.
|
1
|
345 |
] ifFalse:[
|
|
346 |
newContents := Array new:newSize.
|
|
347 |
newContents replaceFrom:1 to:(lastIndex - firstIndex + 1) with:contentsArray.
|
|
348 |
contentsArray := newContents.
|
|
349 |
firstIndex := 1.
|
|
350 |
lastIndex := newSize
|
|
351 |
]
|
|
352 |
! !
|
|
353 |
|
|
354 |
!OrderedCollection methodsFor:'accessing'!
|
|
355 |
|
|
356 |
at:anInteger
|
|
357 |
"return the element at index, anInteger"
|
|
358 |
|
13
|
359 |
|idx|
|
|
360 |
|
|
361 |
idx := anInteger + firstIndex - 1.
|
|
362 |
((anInteger < 1) or:[idx > lastIndex]) ifTrue:[
|
2
|
363 |
self subscriptBoundsError
|
1
|
364 |
] ifFalse:[
|
13
|
365 |
^ contentsArray at:idx
|
1
|
366 |
]
|
|
367 |
!
|
|
368 |
|
|
369 |
at:anInteger put:anObject
|
|
370 |
"set the element at index, to be anInteger"
|
|
371 |
|
13
|
372 |
|idx|
|
|
373 |
|
|
374 |
idx := anInteger + firstIndex - 1.
|
|
375 |
((anInteger < 1) or:[idx > lastIndex]) ifTrue:[
|
2
|
376 |
self subscriptBoundsError
|
1
|
377 |
] ifFalse:[
|
13
|
378 |
^ contentsArray at:idx put:anObject
|
1
|
379 |
]
|
42
|
380 |
!
|
|
381 |
|
|
382 |
first
|
|
383 |
"return the first element"
|
|
384 |
|
|
385 |
firstIndex <= lastIndex ifTrue:[
|
|
386 |
^ contentsArray at:firstIndex
|
|
387 |
].
|
|
388 |
"error if collection is empty"
|
|
389 |
self subscriptBoundsError
|
|
390 |
|
|
391 |
"(OrderedCollection withAll:#(1 2 3 4 5)) first"
|
|
392 |
"(SortedCollection withAll:#(5 4 3 2 1)) first"
|
|
393 |
!
|
|
394 |
|
|
395 |
last
|
|
396 |
"return the last element"
|
|
397 |
|
|
398 |
firstIndex <= lastIndex ifTrue:[
|
|
399 |
^ contentsArray at:lastIndex
|
|
400 |
].
|
|
401 |
"error if collection is empty"
|
|
402 |
self subscriptBoundsError
|
|
403 |
|
|
404 |
"(OrderedCollection withAll:#(1 2 3 4 5)) last"
|
|
405 |
"(SortedCollection withAll:#(5 4 3 2 1)) last"
|
1
|
406 |
! !
|
|
407 |
|
56
|
408 |
!OrderedCollection methodsFor:'filling & replacing'!
|
13
|
409 |
|
|
410 |
replaceFrom:start to:stop with:aCollection startingAt:repStart
|
|
411 |
"redefined - can be done faster"
|
|
412 |
|
|
413 |
|end|
|
|
414 |
|
|
415 |
end := stop + firstIndex - 1.
|
|
416 |
((start >= 1) and:[end <= lastIndex]) ifTrue:[
|
77
|
417 |
contentsArray
|
|
418 |
replaceFrom:(start + firstIndex - 1)
|
|
419 |
to:end
|
|
420 |
with:aCollection
|
|
421 |
startingAt:repStart.
|
|
422 |
^ self
|
13
|
423 |
].
|
|
424 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
|
|
425 |
! !
|
|
426 |
|
77
|
427 |
!OrderedCollection methodsFor:'searching'!
|
|
428 |
|
|
429 |
after:anObject
|
|
430 |
"return the element, after anObject.
|
|
431 |
If anObject is not in the receiver, report an error."
|
|
432 |
|
|
433 |
^ self after:anObject ifAbsent:[self errorNotFound]
|
|
434 |
|
|
435 |
"
|
|
436 |
#(4 3 2 1) asOrderedCollection after:3.
|
|
437 |
#(4 3 2 1) asOrderedCollection after:5
|
|
438 |
#(4 3 2 1) asOrderedCollection after:1
|
|
439 |
"
|
|
440 |
!
|
|
441 |
|
|
442 |
after:anObject ifAbsent:exceptionBlock
|
|
443 |
"return the element after the argument anObject, nil if there is none.
|
|
444 |
If anObject is not in the receiver, return the result from evaluating exceptionBlock."
|
|
445 |
|
|
446 |
|idx|
|
|
447 |
|
|
448 |
idx := self indexOf:anObject.
|
|
449 |
idx ~~ 0 ifTrue:[
|
|
450 |
idx == lastIndex ifTrue:[^ nil].
|
|
451 |
^ self at:(idx + 1).
|
|
452 |
].
|
|
453 |
^ exceptionBlock value
|
|
454 |
|
|
455 |
"
|
|
456 |
#(4 3 2 1) asOrderedCollection after:3.
|
|
457 |
#(4 3 2 1) asOrderedCollection after:5
|
|
458 |
#(4 3 2 1) asOrderedCollection after:1
|
|
459 |
"
|
|
460 |
!
|
|
461 |
|
|
462 |
before:anObject
|
|
463 |
"return the element before the argument, anObject.
|
|
464 |
If anObject is not in the receiver, report an error."
|
|
465 |
|
|
466 |
^ self before:anObject ifAbsent:[self errorNotFound]
|
|
467 |
|
|
468 |
"
|
|
469 |
#(4 3 2 1) asOrderedCollection before:3.
|
|
470 |
#(4 3 2 1) asOrderedCollection before:4
|
|
471 |
#(4 3 2 1) asOrderedCollection before:0
|
|
472 |
"
|
|
473 |
!
|
|
474 |
|
|
475 |
before:anObject ifAbsent:exceptionBlock
|
|
476 |
"return the element before the argument anObject, nil if there is none.
|
|
477 |
If anObject is not in the receiver, return the result from evaluating exceptionBlock."
|
|
478 |
|
|
479 |
|idx|
|
|
480 |
|
|
481 |
idx := self indexOf:anObject.
|
|
482 |
idx ~~ 0 ifTrue:[
|
|
483 |
idx == firstIndex ifTrue:[^ nil].
|
|
484 |
^ self at:(idx - 1).
|
|
485 |
].
|
|
486 |
^ exceptionBlock value
|
|
487 |
|
|
488 |
"
|
|
489 |
#(4 3 2 1) asOrderedCollection before:3.
|
|
490 |
#(4 3 2 1) asOrderedCollection before:5
|
|
491 |
#(4 3 2 1) asOrderedCollection before:1
|
|
492 |
#(4 3 2 1) asOrderedCollection before:4
|
|
493 |
"
|
|
494 |
! !
|
|
495 |
|
1
|
496 |
!OrderedCollection methodsFor:'private'!
|
|
497 |
|
|
498 |
setFirstIndex:newFirstIndex lastIndex:newLastIndex
|
13
|
499 |
"set first and last index"
|
|
500 |
|
1
|
501 |
firstIndex := newFirstIndex.
|
|
502 |
lastIndex := newLastIndex.
|
|
503 |
!
|
|
504 |
|
|
505 |
makeRoomAtLast
|
33
|
506 |
"grow/shift the contents for more room at the end"
|
1
|
507 |
|
|
508 |
|newContents
|
33
|
509 |
oldSize "{ Class:SmallInteger }"
|
|
510 |
newSize "{ Class:SmallInteger }"
|
|
511 |
startIndex "{ Class:SmallInteger }"
|
|
512 |
sz "{ Class:SmallInteger }"|
|
1
|
513 |
|
|
514 |
oldSize := contentsArray size.
|
33
|
515 |
sz := self size.
|
|
516 |
|
|
517 |
"if there is lots of room at the beginning (> 50%), shift instead of growing"
|
|
518 |
oldSize > (sz * 2) ifTrue:[
|
|
519 |
startIndex := firstIndex // 4.
|
77
|
520 |
contentsArray
|
|
521 |
replaceFrom:startIndex
|
|
522 |
to:startIndex + sz - 1
|
|
523 |
with:contentsArray
|
|
524 |
startingAt:firstIndex.
|
33
|
525 |
contentsArray from:startIndex + sz to:lastIndex put:nil.
|
|
526 |
firstIndex := startIndex.
|
|
527 |
lastIndex := startIndex + sz - 1.
|
|
528 |
^ self
|
|
529 |
].
|
10
|
530 |
newSize := oldSize * 2.
|
|
531 |
newSize == 0 ifTrue:[ newSize := 1].
|
|
532 |
newContents := Array new:newSize.
|
1
|
533 |
newContents replaceFrom:1 to:oldSize with:contentsArray.
|
|
534 |
contentsArray := newContents
|
|
535 |
!
|
|
536 |
|
|
537 |
makeRoomAtFront
|
33
|
538 |
"grow/shift the contents for more room at the beginning"
|
1
|
539 |
|
|
540 |
|newContents
|
33
|
541 |
oldSize "{ Class:SmallInteger }"
|
|
542 |
newSize "{ Class:SmallInteger }"
|
|
543 |
startIndex "{ Class:SmallInteger }"
|
|
544 |
sz "{ Class:SmallInteger }"|
|
1
|
545 |
|
|
546 |
oldSize := contentsArray size.
|
33
|
547 |
sz := self size.
|
|
548 |
|
|
549 |
"if there is lots of room at the end (> 50%), shift instead of growing"
|
|
550 |
oldSize > (sz * 2) ifTrue:[
|
|
551 |
startIndex := oldSize // 4.
|
77
|
552 |
contentsArray
|
|
553 |
replaceFrom:startIndex
|
|
554 |
to:startIndex + sz - 1
|
|
555 |
with:contentsArray
|
|
556 |
startingAt:1.
|
33
|
557 |
contentsArray from:1 to:(startIndex - 1) put:nil.
|
|
558 |
firstIndex := startIndex.
|
|
559 |
lastIndex := startIndex + sz - 1.
|
|
560 |
^ self
|
|
561 |
].
|
2
|
562 |
newSize := oldSize * 2.
|
10
|
563 |
newSize == 0 ifTrue:[ newSize := 1].
|
2
|
564 |
newContents := Array new:newSize.
|
77
|
565 |
newContents
|
|
566 |
replaceFrom:(oldSize + 1)
|
|
567 |
to:newSize
|
|
568 |
with:contentsArray
|
|
569 |
startingAt:1.
|
1
|
570 |
contentsArray := newContents.
|
|
571 |
firstIndex := firstIndex + oldSize.
|
|
572 |
lastIndex := lastIndex + oldSize
|
|
573 |
!
|
|
574 |
|
|
575 |
makeRoomAtIndex:index
|
|
576 |
"grow the contents for inserting at index
|
|
577 |
i.e.
|
|
578 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 -> #(1 2 nil 3 4 5 6)
|
|
579 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 -> #(nil 1 2 3 4 5 6)
|
|
580 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 -> #(1 2 3 4 5 6 nil)"
|
|
581 |
|
|
582 |
|newContents
|
|
583 |
newSize "{ Class:SmallInteger }"
|
|
584 |
oldSize "{ Class:SmallInteger }" |
|
|
585 |
|
|
586 |
oldSize := contentsArray size.
|
68
|
587 |
((firstIndex > 1) and:[firstIndex > (oldSize // 4)]) ifTrue:[
|
77
|
588 |
"there is room (>25%) at the beginning"
|
33
|
589 |
|
|
590 |
index == 1 ifFalse:[
|
77
|
591 |
contentsArray
|
|
592 |
replaceFrom:(firstIndex - 1)
|
|
593 |
to:(index - 1)
|
|
594 |
with:contentsArray
|
|
595 |
startingAt:firstIndex.
|
33
|
596 |
contentsArray at:index put:nil.
|
|
597 |
].
|
|
598 |
firstIndex := firstIndex - 1.
|
|
599 |
^ self
|
|
600 |
].
|
|
601 |
lastIndex < (oldSize * 3 // 4) ifTrue:[
|
77
|
602 |
"there is room (>25%) at the end"
|
33
|
603 |
|
|
604 |
index == (lastIndex + 1) ifFalse:[
|
77
|
605 |
contentsArray
|
|
606 |
replaceFrom:(index + 1)
|
|
607 |
to:(lastIndex + 1)
|
|
608 |
with:contentsArray
|
|
609 |
startingAt:index.
|
33
|
610 |
contentsArray at:index put:nil
|
|
611 |
].
|
|
612 |
lastIndex := lastIndex + 1.
|
|
613 |
^ self
|
|
614 |
].
|
|
615 |
|
|
616 |
newSize := oldSize * 2.
|
1
|
617 |
newContents := Array new:newSize.
|
|
618 |
index == 1 ifFalse:[
|
33
|
619 |
newContents replaceFrom:1 to:index-1
|
|
620 |
with:contentsArray startingAt:1.
|
1
|
621 |
].
|
|
622 |
index == newSize ifFalse:[
|
77
|
623 |
newContents
|
|
624 |
replaceFrom:index + 1
|
|
625 |
to:oldSize + 1
|
|
626 |
with:contentsArray
|
|
627 |
startingAt:index.
|
1
|
628 |
].
|
|
629 |
contentsArray := newContents.
|
|
630 |
lastIndex := lastIndex + 1
|
|
631 |
!
|
|
632 |
|
|
633 |
initContents:size
|
40
|
634 |
"setup the receiver-collection to hold size entries"
|
|
635 |
|
1
|
636 |
contentsArray := Array new:size.
|
|
637 |
firstIndex := 1.
|
|
638 |
lastIndex := 0
|
|
639 |
! !
|
|
640 |
|
|
641 |
!OrderedCollection methodsFor:'testing'!
|
|
642 |
|
|
643 |
includes:anObject
|
33
|
644 |
"return true if anObject is in the collection. Compare using ="
|
1
|
645 |
|
33
|
646 |
|index "{ Class:SmallInteger }"|
|
1
|
647 |
|
13
|
648 |
index := contentsArray indexOf:anObject startingAt:firstIndex.
|
33
|
649 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ false].
|
|
650 |
^ true
|
1
|
651 |
!
|
|
652 |
|
|
653 |
identityIndexOf:anObject
|
33
|
654 |
"return the index of anObject or 0 if not found. Compare using =="
|
1
|
655 |
|
33
|
656 |
|index "{ Class:SmallInteger }"|
|
1
|
657 |
|
13
|
658 |
index := contentsArray identityIndexOf:anObject startingAt:firstIndex.
|
33
|
659 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ 0].
|
1
|
660 |
^ index - firstIndex + 1
|
|
661 |
!
|
|
662 |
|
|
663 |
identityIndexOf:anObject startingAt:startIndex
|
33
|
664 |
"return the index of anObject, starting search at startIndex.
|
|
665 |
Compare using ==; return 0 if not found in the collection"
|
1
|
666 |
|
33
|
667 |
|index "{ Class:SmallInteger }"|
|
1
|
668 |
|
|
669 |
index := contentsArray identityIndexOf:anObject startingAt:(startIndex + firstIndex - 1).
|
33
|
670 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ 0].
|
1
|
671 |
^ index - firstIndex + 1
|
|
672 |
!
|
|
673 |
|
|
674 |
indexOf:anObject
|
33
|
675 |
"return the index of anObject or 0 if not found in the collection.
|
|
676 |
Compare using ="
|
1
|
677 |
|
33
|
678 |
|index "{ Class:SmallInteger }"|
|
1
|
679 |
|
13
|
680 |
index := contentsArray indexOf:anObject startingAt:firstIndex.
|
33
|
681 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ 0].
|
1
|
682 |
^ index - firstIndex + 1
|
|
683 |
!
|
|
684 |
|
|
685 |
indexOf:anObject startingAt:startIndex
|
33
|
686 |
"return the index of anObject, starting search at startIndex.
|
|
687 |
Compare using =; return 0 if not found in the collection"
|
1
|
688 |
|
33
|
689 |
|index "{ Class:SmallInteger }"|
|
1
|
690 |
|
|
691 |
index := contentsArray indexOf:anObject startingAt:(startIndex + firstIndex - 1).
|
33
|
692 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ 0].
|
1
|
693 |
^ index - firstIndex + 1
|
|
694 |
! !
|
|
695 |
|
13
|
696 |
!OrderedCollection methodsFor:'converting'!
|
|
697 |
|
|
698 |
asOrderedCollection
|
|
699 |
"return the receiver as an ordered collection"
|
|
700 |
|
|
701 |
"could be an instance of a subclass..."
|
|
702 |
self class == OrderedCollection ifTrue:[
|
|
703 |
^ self
|
|
704 |
].
|
|
705 |
^ super asOrderedCollection
|
|
706 |
! !
|
|
707 |
|
1
|
708 |
!OrderedCollection methodsFor:'enumeration'!
|
|
709 |
|
|
710 |
do:aBlock
|
|
711 |
"evaluate the argument, aBlock for every element in the collection."
|
|
712 |
|
|
713 |
contentsArray from:firstIndex to:lastIndex do:aBlock
|
|
714 |
!
|
|
715 |
|
42
|
716 |
reverseDo:aBlock
|
|
717 |
"evaluate the argument, aBlock for every element in the collection
|
|
718 |
procesing elements in reverse direction (i.e. starting with the last)"
|
|
719 |
|
|
720 |
contentsArray from:firstIndex to:lastIndex reverseDo:aBlock
|
|
721 |
!
|
|
722 |
|
3
|
723 |
keysAndValuesDo:aTwoArgBlock
|
|
724 |
"evaluate the argument, aBlock for every element in the collection,
|
|
725 |
passing both index and element as arguments."
|
|
726 |
|
33
|
727 |
|start "{ Class:SmallInteger }"
|
|
728 |
stop "{ Class:SmallInteger }" |
|
|
729 |
|
|
730 |
stop := lastIndex.
|
|
731 |
start := firstIndex.
|
|
732 |
start to:stop do:[:index |
|
3
|
733 |
aTwoArgBlock value:index value:(contentsArray at:index)
|
|
734 |
]
|
|
735 |
!
|
|
736 |
|
1
|
737 |
collect:aBlock
|
|
738 |
"evaluate the argument, aBlock for every element in the collection
|
|
739 |
and return a collection of the results"
|
|
740 |
|
|
741 |
|newCollection
|
33
|
742 |
start "{ Class:SmallInteger }"
|
|
743 |
stop "{ Class:SmallInteger }" |
|
1
|
744 |
|
|
745 |
newCollection := (self species new).
|
33
|
746 |
stop := lastIndex.
|
|
747 |
start := firstIndex.
|
|
748 |
start to:stop do:[:index |
|
1
|
749 |
newCollection add:(aBlock value:(contentsArray at:index)).
|
|
750 |
].
|
|
751 |
^ newCollection
|
|
752 |
! !
|
33
|
753 |
|
|
754 |
!OrderedCollection methodsFor:'misc'!
|
|
755 |
|
|
756 |
inspect
|
|
757 |
"redefined to launch a SequenceableCollectionInspector on the receiver
|
|
758 |
(instead of the default InspectorView)."
|
|
759 |
|
|
760 |
OrderedCollectionInspectorView isNil ifTrue:[
|
|
761 |
super inspect
|
|
762 |
] ifFalse:[
|
|
763 |
OrderedCollectionInspectorView openOn:self
|
|
764 |
]
|
|
765 |
|
|
766 |
"(OrderedCollection withAll:#(3 2 1)) inspect"
|
|
767 |
"(OrderedCollection withAll:#(3 2 1)) removeFirst; yourself; inspect"
|
|
768 |
"#(0 8 15 3 99 2) asSortedCollection inspect"
|
|
769 |
! !
|