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
|
159
|
14 |
instanceVariableNames:'contentsArray firstIndex lastIndex'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Sequenceable'
|
1
|
18 |
!
|
|
19 |
|
|
20 |
OrderedCollection comment:'
|
5
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
159
|
22 |
All Rights Reserved
|
93
|
23 |
|
345
|
24 |
$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.24 1995-05-16 17:08:13 claus Exp $
|
33
|
25 |
'!
|
|
26 |
|
|
27 |
!OrderedCollection class methodsFor:'documentation'!
|
|
28 |
|
88
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
159
|
32 |
All Rights Reserved
|
88
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
345
|
45 |
$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.24 1995-05-16 17:08:13 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
|
48 |
|
33
|
49 |
documentation
|
|
50 |
"
|
68
|
51 |
OrderedCollection have ordered elements. Insertion and removal at both ends
|
|
52 |
is possible - therefore they can be used for queues and stacks.
|
1
|
53 |
|
68
|
54 |
Instance variables:
|
1
|
55 |
|
68
|
56 |
contentsArray <Array> the actual contents
|
|
57 |
firstIndex <SmallInteger> index of first valid element
|
|
58 |
lastIndex <SmallInteger> index of last valid element
|
33
|
59 |
"
|
|
60 |
! !
|
1
|
61 |
|
|
62 |
!OrderedCollection class methodsFor:'instance creation'!
|
|
63 |
|
|
64 |
new:size
|
|
65 |
"create a new OrderedCollection"
|
|
66 |
|
|
67 |
^ (self basicNew) initContents:size
|
|
68 |
!
|
|
69 |
|
|
70 |
new
|
|
71 |
"create a new OrderedCollection"
|
|
72 |
|
|
73 |
^ (self basicNew) initContents:10
|
|
74 |
! !
|
|
75 |
|
293
|
76 |
!OrderedCollection methodsFor:'private accessing'!
|
|
77 |
|
|
78 |
contentsArray
|
|
79 |
^ contentsArray
|
|
80 |
!
|
|
81 |
|
|
82 |
firstIndex
|
|
83 |
^ firstIndex
|
|
84 |
! !
|
|
85 |
|
13
|
86 |
!OrderedCollection methodsFor:'queries'!
|
1
|
87 |
|
|
88 |
size
|
|
89 |
"return the number of elements in the collection"
|
|
90 |
|
|
91 |
^ lastIndex - firstIndex + 1
|
|
92 |
!
|
|
93 |
|
|
94 |
isFixedSize
|
|
95 |
"return true if the receiver cannot grow - this will vanish once
|
|
96 |
Arrays and Strings learn how to grow ..."
|
|
97 |
|
|
98 |
^ false
|
|
99 |
! !
|
|
100 |
|
|
101 |
!OrderedCollection methodsFor:'copying'!
|
|
102 |
|
202
|
103 |
copyEmpty
|
|
104 |
"return a copy of the receiver without any elements."
|
|
105 |
|
|
106 |
^ self copyEmpty:10
|
|
107 |
!
|
|
108 |
|
|
109 |
postCopy
|
159
|
110 |
"have to copy the contentsArray too"
|
|
111 |
|
|
112 |
contentsArray := contentsArray shallowCopy
|
|
113 |
!
|
|
114 |
|
1
|
115 |
, aCollection
|
|
116 |
"return a new collection formed from concatenating the receiver with
|
|
117 |
the argument"
|
|
118 |
|
|
119 |
|newCollection|
|
|
120 |
|
202
|
121 |
newCollection := self copyEmpty:(self size + aCollection size).
|
1
|
122 |
self do:[:element |
|
159
|
123 |
newCollection add:element
|
1
|
124 |
].
|
|
125 |
aCollection do:[:element |
|
159
|
126 |
newCollection add:element
|
1
|
127 |
].
|
|
128 |
^ newCollection
|
2
|
129 |
|
93
|
130 |
"
|
|
131 |
#(1 2 3) asOrderedCollection , #(4 5 6) asOrderedCollection
|
|
132 |
#(1 2 3) asSortedCollection , #(99 101 100) asSortedCollection
|
|
133 |
"
|
13
|
134 |
!
|
|
135 |
|
68
|
136 |
copyWith:newElement
|
229
|
137 |
"return a new collection containing the receivers elements
|
|
138 |
and the single new element, newElement.
|
|
139 |
This is different from concatentation, which expects another collection
|
|
140 |
as argument, but equivalent to copy-and-addLast."
|
68
|
141 |
|
|
142 |
|newCollection mySize newSize|
|
|
143 |
|
|
144 |
mySize := self size.
|
|
145 |
newSize := mySize + 1.
|
253
|
146 |
newCollection := self copyEmptyAndGrow:newSize. "must grow, otherwise replace fails"
|
68
|
147 |
newCollection replaceFrom:1 to:mySize with:self startingAt:1.
|
|
148 |
newCollection at:newSize put:newElement.
|
|
149 |
^newCollection
|
|
150 |
|
93
|
151 |
"
|
|
152 |
#(1 2 3 4 5) copyWith:$a
|
|
153 |
'abcdefg' copyWith:$h
|
|
154 |
'abcdefg' copyWith:'123' -- will fail: string cannot be stored into string
|
|
155 |
'abcdefg' copyWith:1 -- will fail: integer cannot be stored into string
|
|
156 |
"
|
68
|
157 |
!
|
|
158 |
|
13
|
159 |
copy
|
|
160 |
"return a new OrderedCollection containing the elements of the receiver."
|
|
161 |
|
|
162 |
"redefinition is a consequence of the implementation with a
|
|
163 |
separate array - otherwise we get a shallow copy of the
|
|
164 |
contents array, which is not what we want here"
|
|
165 |
|
|
166 |
^ self copyFrom:1 to:self size
|
|
167 |
!
|
|
168 |
|
|
169 |
copyFrom:start to:stop
|
|
170 |
"return a new OrderedCollection containing the elements
|
|
171 |
from start to stop."
|
|
172 |
|
|
173 |
|newCollection sz|
|
|
174 |
|
|
175 |
sz := stop - start + 1.
|
253
|
176 |
newCollection := self copyEmptyAndGrow:sz. "must grow, otherwise replace fails"
|
13
|
177 |
newCollection replaceFrom:1 to:sz with:self startingAt:start.
|
|
178 |
^ newCollection
|
1
|
179 |
! !
|
|
180 |
|
|
181 |
!OrderedCollection methodsFor:'adding & removing'!
|
|
182 |
|
|
183 |
removeFirst
|
|
184 |
"remove the first element from the collection; return the element"
|
|
185 |
|
|
186 |
|anObject |
|
|
187 |
|
42
|
188 |
firstIndex > lastIndex ifTrue:[
|
159
|
189 |
"error if collection is empty"
|
|
190 |
^ self emptyCollectionError.
|
42
|
191 |
].
|
1
|
192 |
anObject := contentsArray at:firstIndex.
|
13
|
193 |
contentsArray at:firstIndex put:nil.
|
1
|
194 |
firstIndex := firstIndex + 1.
|
329
|
195 |
|
13
|
196 |
firstIndex > lastIndex ifTrue:[
|
159
|
197 |
"reset to avoid ever growing"
|
|
198 |
firstIndex := 1.
|
|
199 |
lastIndex := 0
|
13
|
200 |
].
|
1
|
201 |
^ anObject
|
42
|
202 |
|
|
203 |
"(OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself"
|
|
204 |
"(SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself"
|
|
205 |
"(SortedCollection new) removeFirst"
|
1
|
206 |
!
|
|
207 |
|
|
208 |
removeLast
|
|
209 |
"remove the last element from the collection; return the element"
|
|
210 |
|
|
211 |
|anObject |
|
|
212 |
|
42
|
213 |
firstIndex > lastIndex ifTrue:[
|
159
|
214 |
"error if collection is empty"
|
|
215 |
^ self emptyCollectionError.
|
42
|
216 |
].
|
1
|
217 |
anObject := contentsArray at:lastIndex.
|
13
|
218 |
contentsArray at:lastIndex put:nil.
|
1
|
219 |
lastIndex := lastIndex - 1.
|
329
|
220 |
|
13
|
221 |
firstIndex > lastIndex ifTrue:[
|
159
|
222 |
"reset to avoid ever growing"
|
|
223 |
firstIndex := 1.
|
|
224 |
lastIndex := 0
|
13
|
225 |
].
|
1
|
226 |
^ anObject
|
42
|
227 |
|
|
228 |
"(OrderedCollection withAll:#(1 2 3 4 5)) removeLast; yourself"
|
|
229 |
"(SortedCollection withAll:#(5 4 3 2 1)) removeLast; yourself"
|
1
|
230 |
!
|
|
231 |
|
293
|
232 |
removeFromIndex:startIndex toIndex:stopIndex
|
56
|
233 |
"remove the elements stored under startIndex up to and including
|
|
234 |
the elements under stopIndex.
|
|
235 |
return the receiver."
|
|
236 |
|
|
237 |
|nDeleted|
|
|
238 |
|
|
239 |
nDeleted := stopIndex - startIndex + 1.
|
329
|
240 |
contentsArray
|
|
241 |
replaceFrom:(firstIndex + startIndex - 1)
|
|
242 |
to:(lastIndex - nDeleted)
|
|
243 |
with:contentsArray
|
|
244 |
startingAt:(firstIndex + stopIndex).
|
|
245 |
|
|
246 |
contentsArray
|
|
247 |
from:(lastIndex - nDeleted + 1)
|
|
248 |
to:lastIndex
|
|
249 |
put:nil.
|
56
|
250 |
|
|
251 |
lastIndex := lastIndex - nDeleted.
|
|
252 |
firstIndex > lastIndex ifTrue:[
|
159
|
253 |
"reset to avoid ever growing"
|
|
254 |
firstIndex := 1.
|
|
255 |
lastIndex := 0
|
56
|
256 |
]
|
|
257 |
|
|
258 |
"
|
293
|
259 |
#(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:3 toIndex:6
|
56
|
260 |
"
|
|
261 |
!
|
|
262 |
|
1
|
263 |
remove:anObject ifAbsent:exceptionBlock
|
|
264 |
"remove the first occurrence of anObject from the collection;
|
|
265 |
return the value of exceptionBlock if anObject is not in
|
|
266 |
the collection"
|
|
267 |
|
|
268 |
|index "{ Class:SmallInteger }"|
|
|
269 |
|
|
270 |
index := firstIndex.
|
|
271 |
[index <= lastIndex] whileTrue:[
|
159
|
272 |
anObject = (contentsArray at:index) ifTrue:[
|
329
|
273 |
contentsArray
|
|
274 |
replaceFrom:index
|
|
275 |
to:(contentsArray size - 1)
|
|
276 |
with:contentsArray
|
|
277 |
startingAt:(index + 1).
|
159
|
278 |
contentsArray at:lastIndex put:nil.
|
|
279 |
lastIndex := lastIndex - 1.
|
|
280 |
firstIndex > lastIndex ifTrue:[
|
|
281 |
"reset to avoid ever growing"
|
|
282 |
firstIndex := 1.
|
|
283 |
lastIndex := 0
|
|
284 |
].
|
|
285 |
^ anObject
|
|
286 |
].
|
|
287 |
index := index + 1
|
1
|
288 |
].
|
|
289 |
^ exceptionBlock value
|
|
290 |
!
|
|
291 |
|
114
|
292 |
removeAll
|
|
293 |
"remove all elements from the collection."
|
|
294 |
|
|
295 |
self initContents:10
|
|
296 |
!
|
|
297 |
|
1
|
298 |
add:anObject
|
|
299 |
"add the argument, anObject to the end of the collection
|
|
300 |
Return the argument, anObject."
|
|
301 |
|
202
|
302 |
|idx "{ Class:SmallInteger }"|
|
|
303 |
|
|
304 |
idx := lastIndex.
|
|
305 |
|
|
306 |
(idx == contentsArray size) ifTrue:[
|
|
307 |
self makeRoomAtLast.
|
|
308 |
idx := lastIndex.
|
1
|
309 |
].
|
202
|
310 |
idx := idx + 1.
|
|
311 |
contentsArray at:idx put:anObject.
|
|
312 |
lastIndex := idx.
|
1
|
313 |
^ anObject
|
13
|
314 |
|
|
315 |
"
|
|
316 |
|c|
|
|
317 |
c := #(4 3 2 1) asOrderedCollection.
|
|
318 |
c add:'here'
|
|
319 |
"
|
1
|
320 |
!
|
|
321 |
|
|
322 |
addFirst:anObject
|
|
323 |
"add the argument, anObject to the beginning of the collection.
|
|
324 |
Return the argument, anObject."
|
|
325 |
|
|
326 |
(firstIndex == 1) ifTrue:[
|
159
|
327 |
self makeRoomAtFront
|
1
|
328 |
].
|
|
329 |
firstIndex := firstIndex - 1.
|
|
330 |
contentsArray at:firstIndex put:anObject.
|
|
331 |
^ anObject
|
13
|
332 |
|
|
333 |
"
|
|
334 |
|c|
|
93
|
335 |
c := #(1 2 3 4) asOrderedCollection.
|
|
336 |
c addFirst:'here'.
|
|
337 |
c
|
|
338 |
"
|
|
339 |
|
|
340 |
"
|
|
341 |
|c|
|
|
342 |
c := #() asOrderedCollection.
|
|
343 |
c addFirst:'here'.
|
|
344 |
c
|
13
|
345 |
"
|
1
|
346 |
!
|
|
347 |
|
|
348 |
add:anObject beforeIndex:index
|
|
349 |
"insert the argument, anObject to become located at index.
|
|
350 |
Return the argument, anObject."
|
|
351 |
|
|
352 |
self makeRoomAtIndex:(index - firstIndex + 1).
|
|
353 |
contentsArray at:(index - firstIndex + 1) put:anObject.
|
13
|
354 |
|
|
355 |
"
|
|
356 |
|c|
|
|
357 |
c := #(4 3 2 1) asOrderedCollection.
|
|
358 |
c add:'here' beforeIndex:3
|
|
359 |
"
|
217
|
360 |
"
|
|
361 |
|c|
|
|
362 |
c := #(4 3 2 1) asOrderedCollection.
|
|
363 |
c add:'here' beforeIndex:1
|
|
364 |
"
|
|
365 |
"
|
|
366 |
|c|
|
|
367 |
c := #(4 3 2 1) asOrderedCollection.
|
|
368 |
c add:'here' beforeIndex:5
|
|
369 |
"
|
|
370 |
!
|
|
371 |
|
|
372 |
add:anObject afterIndex:index
|
|
373 |
"insert the argument, anObject to become located at index.
|
|
374 |
Return the argument, anObject."
|
|
375 |
|
|
376 |
^ self add:anObject beforeIndex:(index + 1)
|
|
377 |
|
|
378 |
"
|
|
379 |
|c|
|
|
380 |
c := #(4 3 2 1) asOrderedCollection.
|
|
381 |
c add:'here' afterIndex:4
|
|
382 |
"
|
13
|
383 |
!
|
|
384 |
|
|
385 |
add:newObject after:oldObject
|
|
386 |
"insert the argument, newObject after oldObject.
|
|
387 |
If oldObject is not in the receiver, report an error,
|
|
388 |
otherwise return the argument, anObject."
|
|
389 |
|
|
390 |
|idx|
|
|
391 |
|
|
392 |
idx := self indexOf:oldObject.
|
|
393 |
idx ~~ 0 ifTrue:[
|
159
|
394 |
^ self add:newObject beforeIndex:(idx + 1).
|
13
|
395 |
].
|
345
|
396 |
self errorNotFound:oldObject
|
13
|
397 |
|
|
398 |
"
|
|
399 |
|c|
|
|
400 |
c := #(4 3 2 1) asOrderedCollection.
|
217
|
401 |
c add:'here' after:3.
|
|
402 |
"
|
|
403 |
"
|
|
404 |
|c|
|
|
405 |
c := #(4 3 2 1) asOrderedCollection.
|
|
406 |
c add:'here' after:1.
|
|
407 |
"
|
|
408 |
"
|
|
409 |
|c|
|
|
410 |
c := #(4 3 2 1) asOrderedCollection.
|
33
|
411 |
c add:'here' after:5
|
13
|
412 |
"
|
|
413 |
!
|
|
414 |
|
|
415 |
|
|
416 |
add:newObject before:oldObject
|
|
417 |
"insert the argument, newObject before oldObject.
|
|
418 |
If oldObject is not in the receiver, report an error,
|
|
419 |
otherwise return the argument, anObject."
|
|
420 |
|
|
421 |
|idx|
|
|
422 |
|
|
423 |
idx := self indexOf:oldObject.
|
|
424 |
idx ~~ 0 ifTrue:[
|
159
|
425 |
^ self add:newObject beforeIndex:idx.
|
13
|
426 |
].
|
345
|
427 |
self errorNotFound:oldObject
|
13
|
428 |
|
|
429 |
"
|
|
430 |
|c|
|
|
431 |
c := #(4 3 2 1) asOrderedCollection.
|
33
|
432 |
c add:'here' before:3.
|
|
433 |
c add:'here' before:5
|
13
|
434 |
"
|
1
|
435 |
! !
|
|
436 |
|
|
437 |
!OrderedCollection methodsFor:'grow & shrink'!
|
|
438 |
|
|
439 |
grow:newSize
|
13
|
440 |
"grow the receiver to newSize"
|
1
|
441 |
|
253
|
442 |
|oldSize newContents oldLast newLast|
|
1
|
443 |
|
253
|
444 |
oldSize := lastIndex - firstIndex + 1.
|
|
445 |
newSize ~~ oldSize ifTrue:[
|
|
446 |
newLast := firstIndex + newSize - 1.
|
|
447 |
newSize < oldSize ifTrue:[
|
|
448 |
oldLast := lastIndex.
|
|
449 |
lastIndex := newLast.
|
|
450 |
"
|
|
451 |
nil out rest, to give GC a chance to reclaim things
|
|
452 |
"
|
|
453 |
contentsArray from:lastIndex + 1 to:oldLast put:nil.
|
|
454 |
] ifFalse:[
|
|
455 |
newLast <= contentsArray size ifTrue:[
|
|
456 |
lastIndex := newLast.
|
|
457 |
^ self
|
|
458 |
].
|
|
459 |
|
|
460 |
newContents := Array basicNew:newSize.
|
|
461 |
newContents replaceFrom:1 to:(lastIndex - firstIndex + 1) with:contentsArray.
|
|
462 |
contentsArray := newContents.
|
|
463 |
firstIndex := 1.
|
|
464 |
lastIndex := newSize
|
|
465 |
]
|
1
|
466 |
]
|
|
467 |
! !
|
|
468 |
|
|
469 |
!OrderedCollection methodsFor:'accessing'!
|
|
470 |
|
|
471 |
at:anInteger
|
|
472 |
"return the element at index, anInteger"
|
|
473 |
|
13
|
474 |
|idx|
|
|
475 |
|
|
476 |
idx := anInteger + firstIndex - 1.
|
|
477 |
((anInteger < 1) or:[idx > lastIndex]) ifTrue:[
|
159
|
478 |
^ self subscriptBoundsError:anInteger
|
1
|
479 |
] ifFalse:[
|
159
|
480 |
^ contentsArray at:idx
|
1
|
481 |
]
|
|
482 |
!
|
|
483 |
|
|
484 |
at:anInteger put:anObject
|
|
485 |
"set the element at index, to be anInteger"
|
|
486 |
|
13
|
487 |
|idx|
|
|
488 |
|
|
489 |
idx := anInteger + firstIndex - 1.
|
|
490 |
((anInteger < 1) or:[idx > lastIndex]) ifTrue:[
|
159
|
491 |
^ self subscriptBoundsError:anInteger
|
1
|
492 |
] ifFalse:[
|
159
|
493 |
^ contentsArray at:idx put:anObject
|
1
|
494 |
]
|
42
|
495 |
!
|
|
496 |
|
|
497 |
first
|
|
498 |
"return the first element"
|
|
499 |
|
|
500 |
firstIndex <= lastIndex ifTrue:[
|
159
|
501 |
^ contentsArray at:firstIndex
|
42
|
502 |
].
|
|
503 |
"error if collection is empty"
|
159
|
504 |
^ self emptyCollectionError
|
42
|
505 |
|
253
|
506 |
"
|
|
507 |
(OrderedCollection withAll:#(1 2 3 4 5)) first
|
|
508 |
(SortedCollection withAll:#(5 4 3 2 1)) first
|
|
509 |
"
|
42
|
510 |
!
|
|
511 |
|
|
512 |
last
|
|
513 |
"return the last element"
|
|
514 |
|
|
515 |
firstIndex <= lastIndex ifTrue:[
|
159
|
516 |
^ contentsArray at:lastIndex
|
42
|
517 |
].
|
|
518 |
"error if collection is empty"
|
159
|
519 |
^ self emptyCollectionError
|
42
|
520 |
|
253
|
521 |
"
|
|
522 |
(OrderedCollection withAll:#(1 2 3 4 5)) last
|
|
523 |
(SortedCollection withAll:#(5 4 3 2 1)) last
|
|
524 |
"
|
1
|
525 |
! !
|
|
526 |
|
56
|
527 |
!OrderedCollection methodsFor:'filling & replacing'!
|
13
|
528 |
|
|
529 |
replaceFrom:start to:stop with:aCollection startingAt:repStart
|
|
530 |
"redefined - can be done faster"
|
|
531 |
|
|
532 |
|end|
|
|
533 |
|
|
534 |
end := stop + firstIndex - 1.
|
|
535 |
((start >= 1) and:[end <= lastIndex]) ifTrue:[
|
293
|
536 |
aCollection class == self class ifTrue:[
|
|
537 |
contentsArray
|
|
538 |
replaceFrom:(start + firstIndex - 1)
|
|
539 |
to:end
|
|
540 |
with:aCollection contentsArray
|
|
541 |
startingAt:(repStart + aCollection firstIndex - 1).
|
|
542 |
] ifFalse:[
|
|
543 |
contentsArray
|
|
544 |
replaceFrom:(start + firstIndex - 1)
|
|
545 |
to:end
|
|
546 |
with:aCollection
|
|
547 |
startingAt:repStart.
|
|
548 |
].
|
159
|
549 |
^ self
|
13
|
550 |
].
|
|
551 |
^ super replaceFrom:start to:stop with:aCollection startingAt:repStart
|
293
|
552 |
|
|
553 |
"
|
|
554 |
|c|
|
|
555 |
|
|
556 |
c := #(1 2 3 4 5 6) asOrderedCollection.
|
|
557 |
c replaceFrom:3 to:6 with:c startingAt:2.
|
|
558 |
c
|
|
559 |
"
|
|
560 |
"
|
|
561 |
|c|
|
|
562 |
|
|
563 |
c := #(1 2 3 4 5 6) asOrderedCollection.
|
|
564 |
c replaceFrom:3 to:5 with:c startingAt:4.
|
|
565 |
c
|
|
566 |
"
|
13
|
567 |
! !
|
|
568 |
|
77
|
569 |
!OrderedCollection methodsFor:'searching'!
|
|
570 |
|
|
571 |
after:anObject
|
|
572 |
"return the element, after anObject.
|
|
573 |
If anObject is not in the receiver, report an error."
|
|
574 |
|
345
|
575 |
^ self after:anObject ifAbsent:[self errorNotFound:anObject]
|
77
|
576 |
|
|
577 |
"
|
|
578 |
#(4 3 2 1) asOrderedCollection after:3.
|
|
579 |
#(4 3 2 1) asOrderedCollection after:5
|
|
580 |
#(4 3 2 1) asOrderedCollection after:1
|
|
581 |
"
|
|
582 |
!
|
|
583 |
|
|
584 |
after:anObject ifAbsent:exceptionBlock
|
|
585 |
"return the element after the argument anObject, nil if there is none.
|
|
586 |
If anObject is not in the receiver, return the result from evaluating exceptionBlock."
|
|
587 |
|
|
588 |
|idx|
|
|
589 |
|
|
590 |
idx := self indexOf:anObject.
|
|
591 |
idx ~~ 0 ifTrue:[
|
159
|
592 |
idx == lastIndex ifTrue:[^ nil].
|
|
593 |
^ self at:(idx + 1).
|
77
|
594 |
].
|
|
595 |
^ exceptionBlock value
|
|
596 |
|
|
597 |
"
|
|
598 |
#(4 3 2 1) asOrderedCollection after:3.
|
|
599 |
#(4 3 2 1) asOrderedCollection after:5
|
|
600 |
#(4 3 2 1) asOrderedCollection after:1
|
|
601 |
"
|
|
602 |
!
|
|
603 |
|
|
604 |
before:anObject
|
|
605 |
"return the element before the argument, anObject.
|
|
606 |
If anObject is not in the receiver, report an error."
|
|
607 |
|
345
|
608 |
^ self before:anObject ifAbsent:[self errorNotFound:anObject]
|
77
|
609 |
|
|
610 |
"
|
|
611 |
#(4 3 2 1) asOrderedCollection before:3.
|
|
612 |
#(4 3 2 1) asOrderedCollection before:4
|
|
613 |
#(4 3 2 1) asOrderedCollection before:0
|
|
614 |
"
|
|
615 |
!
|
|
616 |
|
|
617 |
before:anObject ifAbsent:exceptionBlock
|
|
618 |
"return the element before the argument anObject, nil if there is none.
|
|
619 |
If anObject is not in the receiver, return the result from evaluating exceptionBlock."
|
|
620 |
|
|
621 |
|idx|
|
|
622 |
|
|
623 |
idx := self indexOf:anObject.
|
|
624 |
idx ~~ 0 ifTrue:[
|
159
|
625 |
idx == firstIndex ifTrue:[^ nil].
|
|
626 |
^ self at:(idx - 1).
|
77
|
627 |
].
|
|
628 |
^ exceptionBlock value
|
|
629 |
|
|
630 |
"
|
|
631 |
#(4 3 2 1) asOrderedCollection before:3.
|
|
632 |
#(4 3 2 1) asOrderedCollection before:5
|
|
633 |
#(4 3 2 1) asOrderedCollection before:1
|
|
634 |
#(4 3 2 1) asOrderedCollection before:4
|
|
635 |
"
|
|
636 |
! !
|
|
637 |
|
1
|
638 |
!OrderedCollection methodsFor:'private'!
|
|
639 |
|
|
640 |
setFirstIndex:newFirstIndex lastIndex:newLastIndex
|
13
|
641 |
"set first and last index"
|
|
642 |
|
1
|
643 |
firstIndex := newFirstIndex.
|
|
644 |
lastIndex := newLastIndex.
|
|
645 |
!
|
|
646 |
|
|
647 |
makeRoomAtLast
|
282
|
648 |
"grow/shift the contents for more room at the end.
|
|
649 |
Does not change the logical size.
|
|
650 |
i.e.
|
|
651 |
#(1 2 3 4 5 6) -> #(1 2 3 4 5 6 nil)"
|
1
|
652 |
|
|
653 |
|newContents
|
33
|
654 |
oldSize "{ Class:SmallInteger }"
|
|
655 |
newSize "{ Class:SmallInteger }"
|
|
656 |
startIndex "{ Class:SmallInteger }"
|
|
657 |
sz "{ Class:SmallInteger }"|
|
1
|
658 |
|
|
659 |
oldSize := contentsArray size.
|
253
|
660 |
sz := lastIndex - firstIndex + 1.
|
33
|
661 |
|
329
|
662 |
"
|
|
663 |
if there is lots of room at the beginning (> 50%),
|
|
664 |
shift instead of growing. This helps collections which get
|
|
665 |
elements removed at front and added at the end.
|
|
666 |
"
|
33
|
667 |
oldSize > (sz * 2) ifTrue:[
|
159
|
668 |
startIndex := firstIndex // 4.
|
|
669 |
contentsArray
|
|
670 |
replaceFrom:startIndex
|
|
671 |
to:startIndex + sz - 1
|
|
672 |
with:contentsArray
|
|
673 |
startingAt:firstIndex.
|
|
674 |
contentsArray from:startIndex + sz to:lastIndex put:nil.
|
|
675 |
firstIndex := startIndex.
|
|
676 |
lastIndex := startIndex + sz - 1.
|
|
677 |
^ self
|
33
|
678 |
].
|
10
|
679 |
newSize := oldSize * 2.
|
329
|
680 |
newSize == 0 ifTrue:[newSize := 3].
|
253
|
681 |
newContents := Array basicNew:newSize.
|
329
|
682 |
newContents
|
|
683 |
replaceFrom:1
|
|
684 |
to:oldSize
|
|
685 |
with:contentsArray
|
|
686 |
startingAt:1.
|
1
|
687 |
contentsArray := newContents
|
|
688 |
!
|
|
689 |
|
|
690 |
makeRoomAtFront
|
282
|
691 |
"grow/shift the contents for more room at the beginning.
|
|
692 |
Does not change the logical size.
|
|
693 |
i.e.
|
|
694 |
#(1 2 3 4 5 6) -> #(nil 1 2 3 4 5 6)"
|
1
|
695 |
|
|
696 |
|newContents
|
33
|
697 |
oldSize "{ Class:SmallInteger }"
|
|
698 |
newSize "{ Class:SmallInteger }"
|
|
699 |
startIndex "{ Class:SmallInteger }"
|
|
700 |
sz "{ Class:SmallInteger }"|
|
1
|
701 |
|
|
702 |
oldSize := contentsArray size.
|
93
|
703 |
oldSize == 0 ifTrue:[
|
253
|
704 |
contentsArray := Array basicNew:3.
|
159
|
705 |
firstIndex := 2. lastIndex := 1.
|
|
706 |
^ self
|
93
|
707 |
].
|
|
708 |
|
253
|
709 |
sz := lastIndex - firstIndex + 1.
|
33
|
710 |
|
329
|
711 |
"
|
|
712 |
if there is lots of room at the end (> 50%),
|
|
713 |
shift instead of growing. This helps collections
|
|
714 |
which get elements removed at the end and added at front.
|
|
715 |
"
|
33
|
716 |
oldSize > (sz * 2) ifTrue:[
|
159
|
717 |
startIndex := oldSize // 4.
|
|
718 |
contentsArray
|
|
719 |
replaceFrom:startIndex
|
|
720 |
to:startIndex + sz - 1
|
|
721 |
with:contentsArray
|
|
722 |
startingAt:1.
|
|
723 |
contentsArray from:1 to:(startIndex - 1) put:nil.
|
|
724 |
firstIndex := startIndex.
|
|
725 |
lastIndex := startIndex + sz - 1.
|
|
726 |
^ self
|
33
|
727 |
].
|
2
|
728 |
newSize := oldSize * 2.
|
329
|
729 |
newSize == 0 ifTrue:[ newSize := 3].
|
253
|
730 |
newContents := Array basicNew:newSize.
|
77
|
731 |
newContents
|
159
|
732 |
replaceFrom:(oldSize + 1)
|
|
733 |
to:newSize
|
|
734 |
with:contentsArray
|
|
735 |
startingAt:1.
|
1
|
736 |
contentsArray := newContents.
|
|
737 |
firstIndex := firstIndex + oldSize.
|
|
738 |
lastIndex := lastIndex + oldSize
|
|
739 |
!
|
|
740 |
|
|
741 |
makeRoomAtIndex:index
|
|
742 |
"grow the contents for inserting at index
|
282
|
743 |
Changes the logical size.
|
1
|
744 |
i.e.
|
|
745 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:3 -> #(1 2 nil 3 4 5 6)
|
|
746 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:1 -> #(nil 1 2 3 4 5 6)
|
|
747 |
#(1 2 3 4 5 6) asOrderedCollection makeRoomAtIndex:7 -> #(1 2 3 4 5 6 nil)"
|
|
748 |
|
|
749 |
|newContents
|
|
750 |
newSize "{ Class:SmallInteger }"
|
|
751 |
oldSize "{ Class:SmallInteger }" |
|
|
752 |
|
|
753 |
oldSize := contentsArray size.
|
68
|
754 |
((firstIndex > 1) and:[firstIndex > (oldSize // 4)]) ifTrue:[
|
159
|
755 |
"there is room (>25%) at the beginning"
|
33
|
756 |
|
159
|
757 |
index == 1 ifFalse:[
|
|
758 |
contentsArray
|
|
759 |
replaceFrom:(firstIndex - 1)
|
|
760 |
to:(index - 1)
|
|
761 |
with:contentsArray
|
|
762 |
startingAt:firstIndex.
|
|
763 |
contentsArray at:index put:nil.
|
|
764 |
].
|
|
765 |
firstIndex := firstIndex - 1.
|
|
766 |
^ self
|
33
|
767 |
].
|
|
768 |
lastIndex < (oldSize * 3 // 4) ifTrue:[
|
159
|
769 |
"there is room (>25%) at the end"
|
33
|
770 |
|
159
|
771 |
index == (lastIndex + 1) ifFalse:[
|
|
772 |
contentsArray
|
|
773 |
replaceFrom:(index + 1)
|
|
774 |
to:(lastIndex + 1)
|
|
775 |
with:contentsArray
|
|
776 |
startingAt:index.
|
|
777 |
contentsArray at:index put:nil
|
|
778 |
].
|
|
779 |
lastIndex := lastIndex + 1.
|
|
780 |
^ self
|
33
|
781 |
].
|
|
782 |
|
|
783 |
newSize := oldSize * 2.
|
253
|
784 |
newContents := Array basicNew:newSize.
|
329
|
785 |
index ~~ 1 ifTrue:[
|
|
786 |
newContents
|
|
787 |
replaceFrom:1
|
|
788 |
to:index-1
|
|
789 |
with:contentsArray
|
|
790 |
startingAt:1.
|
1
|
791 |
].
|
329
|
792 |
index ~~ newSize ifTrue:[
|
159
|
793 |
newContents
|
|
794 |
replaceFrom:index + 1
|
|
795 |
to:oldSize + 1
|
|
796 |
with:contentsArray
|
|
797 |
startingAt:index.
|
1
|
798 |
].
|
|
799 |
contentsArray := newContents.
|
|
800 |
lastIndex := lastIndex + 1
|
|
801 |
!
|
|
802 |
|
|
803 |
initContents:size
|
40
|
804 |
"setup the receiver-collection to hold size entries"
|
|
805 |
|
253
|
806 |
contentsArray := Array basicNew:size.
|
1
|
807 |
firstIndex := 1.
|
|
808 |
lastIndex := 0
|
|
809 |
! !
|
|
810 |
|
|
811 |
!OrderedCollection methodsFor:'testing'!
|
|
812 |
|
|
813 |
includes:anObject
|
33
|
814 |
"return true if anObject is in the collection. Compare using ="
|
1
|
815 |
|
33
|
816 |
|index "{ Class:SmallInteger }"|
|
1
|
817 |
|
13
|
818 |
index := contentsArray indexOf:anObject startingAt:firstIndex.
|
33
|
819 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ false].
|
|
820 |
^ true
|
1
|
821 |
!
|
|
822 |
|
|
823 |
identityIndexOf:anObject
|
33
|
824 |
"return the index of anObject or 0 if not found. Compare using =="
|
1
|
825 |
|
33
|
826 |
|index "{ Class:SmallInteger }"|
|
1
|
827 |
|
13
|
828 |
index := contentsArray identityIndexOf:anObject startingAt:firstIndex.
|
33
|
829 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ 0].
|
1
|
830 |
^ index - firstIndex + 1
|
|
831 |
!
|
|
832 |
|
|
833 |
identityIndexOf:anObject startingAt:startIndex
|
33
|
834 |
"return the index of anObject, starting search at startIndex.
|
|
835 |
Compare using ==; return 0 if not found in the collection"
|
1
|
836 |
|
33
|
837 |
|index "{ Class:SmallInteger }"|
|
1
|
838 |
|
|
839 |
index := contentsArray identityIndexOf:anObject startingAt:(startIndex + firstIndex - 1).
|
33
|
840 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ 0].
|
1
|
841 |
^ index - firstIndex + 1
|
|
842 |
!
|
|
843 |
|
|
844 |
indexOf:anObject
|
33
|
845 |
"return the index of anObject or 0 if not found in the collection.
|
|
846 |
Compare using ="
|
1
|
847 |
|
33
|
848 |
|index "{ Class:SmallInteger }"|
|
1
|
849 |
|
13
|
850 |
index := contentsArray indexOf:anObject startingAt:firstIndex.
|
33
|
851 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ 0].
|
1
|
852 |
^ index - firstIndex + 1
|
|
853 |
!
|
|
854 |
|
|
855 |
indexOf:anObject startingAt:startIndex
|
33
|
856 |
"return the index of anObject, starting search at startIndex.
|
|
857 |
Compare using =; return 0 if not found in the collection"
|
1
|
858 |
|
33
|
859 |
|index "{ Class:SmallInteger }"|
|
1
|
860 |
|
|
861 |
index := contentsArray indexOf:anObject startingAt:(startIndex + firstIndex - 1).
|
33
|
862 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ 0].
|
1
|
863 |
^ index - firstIndex + 1
|
|
864 |
! !
|
|
865 |
|
13
|
866 |
!OrderedCollection methodsFor:'converting'!
|
|
867 |
|
|
868 |
asOrderedCollection
|
|
869 |
"return the receiver as an ordered collection"
|
|
870 |
|
|
871 |
"could be an instance of a subclass..."
|
|
872 |
self class == OrderedCollection ifTrue:[
|
159
|
873 |
^ self
|
13
|
874 |
].
|
|
875 |
^ super asOrderedCollection
|
|
876 |
! !
|
|
877 |
|
217
|
878 |
!OrderedCollection methodsFor:'enumerating'!
|
1
|
879 |
|
|
880 |
do:aBlock
|
|
881 |
"evaluate the argument, aBlock for every element in the collection."
|
|
882 |
|
|
883 |
contentsArray from:firstIndex to:lastIndex do:aBlock
|
|
884 |
!
|
|
885 |
|
42
|
886 |
reverseDo:aBlock
|
|
887 |
"evaluate the argument, aBlock for every element in the collection
|
|
888 |
procesing elements in reverse direction (i.e. starting with the last)"
|
|
889 |
|
|
890 |
contentsArray from:firstIndex to:lastIndex reverseDo:aBlock
|
|
891 |
!
|
|
892 |
|
3
|
893 |
keysAndValuesDo:aTwoArgBlock
|
|
894 |
"evaluate the argument, aBlock for every element in the collection,
|
|
895 |
passing both index and element as arguments."
|
|
896 |
|
33
|
897 |
|start "{ Class:SmallInteger }"
|
|
898 |
stop "{ Class:SmallInteger }" |
|
|
899 |
|
|
900 |
stop := lastIndex.
|
|
901 |
start := firstIndex.
|
|
902 |
start to:stop do:[:index |
|
159
|
903 |
aTwoArgBlock value:index value:(contentsArray at:index)
|
3
|
904 |
]
|
282
|
905 |
|
|
906 |
"
|
|
907 |
#(10 20 30 40) asOrderedCollection keysAndValuesDo:[:index :value |
|
|
908 |
Transcript show:index; show:' '; showCr:value
|
|
909 |
]
|
|
910 |
"
|
3
|
911 |
!
|
|
912 |
|
1
|
913 |
collect:aBlock
|
|
914 |
"evaluate the argument, aBlock for every element in the collection
|
|
915 |
and return a collection of the results"
|
|
916 |
|
|
917 |
|newCollection
|
33
|
918 |
start "{ Class:SmallInteger }"
|
|
919 |
stop "{ Class:SmallInteger }" |
|
1
|
920 |
|
202
|
921 |
newCollection := self copyEmpty:(self size).
|
33
|
922 |
stop := lastIndex.
|
|
923 |
start := firstIndex.
|
|
924 |
start to:stop do:[:index |
|
159
|
925 |
newCollection add:(aBlock value:(contentsArray at:index)).
|
1
|
926 |
].
|
|
927 |
^ newCollection
|
282
|
928 |
|
|
929 |
"
|
|
930 |
#(1 2 3 4) asOrderedCollection collect:[:i | i * i]
|
|
931 |
#(1 2 3 4) asOrderedCollection collect:[:i | i even]
|
|
932 |
"
|
1
|
933 |
! !
|
33
|
934 |
|
282
|
935 |
!OrderedCollection methodsFor:'inspecting'!
|
33
|
936 |
|
282
|
937 |
inspectorClass
|
|
938 |
"redefined to launch an OrderedCollectionInspector
|
33
|
939 |
(instead of the default InspectorView)."
|
|
940 |
|
282
|
941 |
^ OrderedCollectionInspectorView
|
33
|
942 |
|
282
|
943 |
"
|
|
944 |
(OrderedCollection withAll:#(3 2 1)) inspect
|
|
945 |
(OrderedCollection withAll:#(3 2 1)) removeFirst; yourself; inspect
|
|
946 |
#(0 8 15 3 99 2) asSortedCollection inspect
|
|
947 |
"
|
33
|
948 |
! !
|