1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 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 |
OrderedCollection subclass:#SortedCollection
|
159
|
14 |
instanceVariableNames:'sortBlock'
|
|
15 |
classVariableNames:'DefaultSortBlock'
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Collections-Sequenceable'
|
1
|
18 |
!
|
|
19 |
|
77
|
20 |
!SortedCollection class methodsFor:'documentation'!
|
1
|
21 |
|
88
|
22 |
copyright
|
|
23 |
"
|
|
24 |
COPYRIGHT (c) 1993 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 |
|
77
|
36 |
documentation
|
|
37 |
"
|
|
38 |
I keep my elements sorted. The sort order is defined by a sortblock,
|
|
39 |
a two-argument block which, when given two elements of the collection,
|
|
40 |
should return true if the element given as first arg has to come before the
|
|
41 |
element given as second arg.
|
|
42 |
|
|
43 |
Thus a sortBlock of [:a :b | a < b] defines ascending sort-order,
|
|
44 |
while [:a :b | a > b] defines descening order.
|
|
45 |
The default sortBlock for SortedCollections is the first one.
|
|
46 |
"
|
|
47 |
! !
|
1
|
48 |
|
|
49 |
!SortedCollection class methodsFor:'initialization'!
|
|
50 |
|
|
51 |
initialize
|
|
52 |
DefaultSortBlock := [:a :b | a < b ]
|
|
53 |
|
|
54 |
"SortedCollection initialize"
|
|
55 |
! !
|
|
56 |
|
|
57 |
!SortedCollection class methodsFor:'instance creation'!
|
|
58 |
|
|
59 |
new
|
2
|
60 |
"return a new sortedCollection, the sorting is done using
|
|
61 |
a compare for a > b, in ascending order"
|
|
62 |
|
1
|
63 |
^ super new setSortBlock:DefaultSortBlock
|
|
64 |
!
|
|
65 |
|
|
66 |
new:size
|
2
|
67 |
"return a new sortedCollection with preallocated size.
|
|
68 |
The sorting is done using a compare for a > b, in ascending order"
|
|
69 |
|
1
|
70 |
^ (super new:size) setSortBlock:DefaultSortBlock
|
|
71 |
!
|
|
72 |
|
|
73 |
sortBlock:aBlock
|
2
|
74 |
"set the sort-block"
|
|
75 |
|
1
|
76 |
^ super new setSortBlock:aBlock
|
329
|
77 |
!
|
|
78 |
|
|
79 |
withAll:aCollection sortBlock:aBlock
|
|
80 |
"initialize from aCollection and set the sort-block"
|
|
81 |
|
|
82 |
^ (self sortBlock:aBlock) addAll:aCollection
|
|
83 |
|
|
84 |
"
|
|
85 |
SortedCollection withAll:#(1 2 3 4 5 6 7 8 9 0)
|
|
86 |
sortBlock:[:a :b | a > b]
|
|
87 |
"
|
1
|
88 |
! !
|
|
89 |
|
|
90 |
!SortedCollection methodsFor:'adding & removing'!
|
|
91 |
|
607
|
92 |
add:anObject
|
|
93 |
"add the argument, anObject at the proper place in the
|
|
94 |
receiver. Returns the argument, anObject."
|
2
|
95 |
|
607
|
96 |
|index|
|
1
|
97 |
|
607
|
98 |
lastIndex < firstIndex "i.e. self size == 0" ifTrue:[
|
|
99 |
super add:anObject
|
|
100 |
] ifFalse:[
|
|
101 |
index := self indexForInserting:anObject.
|
|
102 |
self makeRoomAtIndex:index.
|
|
103 |
contentsArray at:index put:anObject
|
|
104 |
].
|
|
105 |
^ anObject
|
2
|
106 |
|
607
|
107 |
"
|
|
108 |
|c| #(7 3 9 10 99) asSortedCollection add:5; yourself
|
|
109 |
#(7 3 9 10 99) asSortedCollection add:1; yourself
|
|
110 |
#(7 3 9 10 99) asSortedCollection add:1000; yourself
|
|
111 |
"
|
1
|
112 |
!
|
|
113 |
|
|
114 |
add:newObject after:oldObject
|
2
|
115 |
"catch this - its not allowed for sortedCollections"
|
|
116 |
|
1
|
117 |
self shouldNotImplement
|
|
118 |
!
|
|
119 |
|
|
120 |
add:newObject before:oldObject
|
2
|
121 |
"catch this - its not allowed for sortedCollections"
|
|
122 |
|
1
|
123 |
self shouldNotImplement
|
|
124 |
!
|
|
125 |
|
|
126 |
addAll:aCollection
|
|
127 |
"add all elements of the argument, aCollection to the
|
|
128 |
receiver"
|
|
129 |
|
|
130 |
|mySize "{ Class: SmallInteger }"
|
|
131 |
otherSize "{ Class: SmallInteger }"
|
|
132 |
dstIndex "{ Class: SmallInteger }"
|
|
133 |
newSize newContents|
|
|
134 |
|
|
135 |
"if aCollection is bigger than a threshhold, its faster
|
359
|
136 |
to add all en-bloque and resort the whole collection.
|
|
137 |
Question: what is a good treshhold ?"
|
1
|
138 |
|
|
139 |
mySize := self size.
|
|
140 |
otherSize := aCollection size.
|
|
141 |
((mySize == 0) or:[otherSize > 5]) ifTrue:[
|
159
|
142 |
newSize := mySize + otherSize.
|
|
143 |
newContents := Array new:newSize.
|
|
144 |
newContents replaceFrom:1 to:mySize with:contentsArray startingAt:1.
|
359
|
145 |
aCollection isSequenceable ifTrue:[
|
159
|
146 |
"maybe we can do it in one big move"
|
|
147 |
newContents replaceFrom:(mySize + 1) to:newSize with:aCollection startingAt:1.
|
|
148 |
] ifFalse:[
|
|
149 |
dstIndex := mySize + 1.
|
|
150 |
aCollection do:[:element |
|
|
151 |
newContents at:dstIndex put:element.
|
|
152 |
dstIndex := dstIndex + 1
|
|
153 |
]
|
|
154 |
].
|
|
155 |
firstIndex := 1.
|
|
156 |
lastIndex := newSize.
|
|
157 |
contentsArray := newContents.
|
|
158 |
contentsArray sort:sortBlock.
|
|
159 |
^ self
|
1
|
160 |
].
|
|
161 |
super addAll:aCollection
|
|
162 |
|
77
|
163 |
"
|
|
164 |
#(7 3 9 10 99) asSortedCollection addAll:#(77 0 1 16 5)
|
|
165 |
"
|
1
|
166 |
!
|
|
167 |
|
607
|
168 |
addFirst:anObject
|
|
169 |
"catch this - its not allowed for sortedCollections"
|
|
170 |
|
|
171 |
self shouldNotImplement
|
|
172 |
!
|
1
|
173 |
|
607
|
174 |
addLast:anObject
|
|
175 |
"catch this - its not allowed for sortedCollections"
|
|
176 |
|
|
177 |
self shouldNotImplement
|
|
178 |
!
|
1
|
179 |
|
607
|
180 |
at:index put:anObject
|
|
181 |
"catch this - its not allowed for sortedCollections"
|
|
182 |
|
|
183 |
self shouldNotImplement
|
|
184 |
! !
|
|
185 |
|
|
186 |
!SortedCollection methodsFor:'converting'!
|
|
187 |
|
|
188 |
asSortedCollection
|
|
189 |
"return the receiver as a sorted collection"
|
|
190 |
|
|
191 |
"could be an instance of a subclass..."
|
|
192 |
self class == SortedCollection ifTrue:[
|
|
193 |
^ self
|
1
|
194 |
].
|
607
|
195 |
^ super asSortedCollection
|
1
|
196 |
! !
|
|
197 |
|
95
|
198 |
!SortedCollection methodsFor:'copying'!
|
|
199 |
|
607
|
200 |
copyEmpty:size
|
|
201 |
"return a copy of the receiver with no elements, but
|
|
202 |
the same size. This method has been be redefined to
|
|
203 |
preserve the sortBlock."
|
|
204 |
|
|
205 |
^ (self species new:size) sortBlock:sortBlock
|
|
206 |
!
|
|
207 |
|
159
|
208 |
postCopyFrom:anOriginal
|
|
209 |
"sent after a copy or when a new collection species has been created.
|
95
|
210 |
The new collection should have the same sortBlock as the original."
|
|
211 |
|
159
|
212 |
sortBlock := anOriginal sortBlock
|
95
|
213 |
|
|
214 |
"
|
|
215 |
#(4 7 1 99 -1 17) asSortedCollection inspect
|
159
|
216 |
#(4 7 1 99 -1 17) asSortedCollection copy inspect
|
95
|
217 |
(#(4 7 1 99 -1 17) asSortedCollection sortBlock:[:a :b | a > b]) inspect
|
159
|
218 |
(#(4 7 1 99 -1 17) asSortedCollection sortBlock:[:a :b | a > b]) copy inspect
|
|
219 |
(#(4 7 1 99 -1 17) asSortedCollection select:[:e| e even]) inspect
|
95
|
220 |
"
|
|
221 |
! !
|
|
222 |
|
216
|
223 |
!SortedCollection methodsFor:'enumerating'!
|
184
|
224 |
|
|
225 |
collect:aBlock
|
|
226 |
"evaluate the argument, aBlock for every element in the collection
|
|
227 |
and return a collection of the results. Redefined to return an OrderedCollection;
|
|
228 |
see X3J20 spec. (SortedCollection>>collect: should return an OrderedCollection)"
|
|
229 |
|
|
230 |
|newCollection
|
|
231 |
start "{ Class:SmallInteger }"
|
|
232 |
stop "{ Class:SmallInteger }" |
|
|
233 |
|
|
234 |
newCollection := OrderedCollection new:(self size).
|
|
235 |
stop := lastIndex.
|
|
236 |
start := firstIndex.
|
|
237 |
start to:stop do:[:index |
|
|
238 |
newCollection add:(aBlock value:(contentsArray at:index)).
|
|
239 |
].
|
|
240 |
^ newCollection
|
|
241 |
! !
|
|
242 |
|
607
|
243 |
!SortedCollection methodsFor:'instance protocol'!
|
|
244 |
|
|
245 |
sortBlock
|
|
246 |
"return the block used for sorting"
|
|
247 |
|
|
248 |
^ sortBlock
|
|
249 |
!
|
|
250 |
|
|
251 |
sortBlock:aSortBlock
|
|
252 |
"change the sort criteria for a sorted collection, resort the elements of
|
|
253 |
the collection, and return the receiver. The argument, aSortBlock must
|
|
254 |
be a two-argument block which returns true if its arg1 has to come before
|
|
255 |
its arg2 in the collection."
|
|
256 |
|
|
257 |
sortBlock := aSortBlock.
|
|
258 |
lastIndex > firstIndex ifTrue:[
|
|
259 |
contentsArray quickSortFrom:firstIndex to:lastIndex sortBlock:aSortBlock
|
|
260 |
]
|
|
261 |
|
|
262 |
"
|
|
263 |
#(9 8 7 6 5 4 3) asSortedCollection
|
|
264 |
#(9 8 7 6 5 4 3) asSortedCollection sortBlock:[:a : b | a < b]
|
|
265 |
#(9 8 7 6 5 4 3) asSortedCollection sortBlock:[:a : b | a > b]
|
|
266 |
#($f $G $z $Y $o $H) asSortedCollection
|
|
267 |
#($f $G $z $Y $o $H) asSortedCollection sortBlock:[:a : b | a asUppercase < b asUppercase]
|
|
268 |
"
|
|
269 |
! !
|
|
270 |
|
|
271 |
!SortedCollection methodsFor:'private'!
|
|
272 |
|
|
273 |
indexForInserting:anObject
|
|
274 |
"search the index at which to insert anObject. Can also be used
|
|
275 |
to search for an existing element by checking if the element at
|
|
276 |
the returned index is the one we look for.
|
|
277 |
The returned index is a physical one, for accessing contentsArray."
|
|
278 |
|
|
279 |
|low "{ Class: SmallInteger}"
|
|
280 |
high "{ Class: SmallInteger}"
|
|
281 |
middle "{ Class: SmallInteger}"
|
|
282 |
element|
|
|
283 |
|
|
284 |
"
|
|
285 |
we can of course use a binary search - since the elements are
|
|
286 |
sorted
|
|
287 |
"
|
|
288 |
low := firstIndex.
|
|
289 |
high := lastIndex.
|
|
290 |
[low <= high] whileTrue:[
|
|
291 |
middle := (low + high) // 2.
|
|
292 |
element := contentsArray at:middle.
|
|
293 |
(sortBlock value:element value:anObject) ifTrue:[
|
|
294 |
"middleelement is smaller than object"
|
|
295 |
low := middle + 1
|
|
296 |
] ifFalse:[
|
|
297 |
high := middle - 1
|
|
298 |
]
|
|
299 |
].
|
|
300 |
^ low
|
|
301 |
|
|
302 |
"
|
|
303 |
#(1 2 3 4 7 99 1313 981989 898989898) asSortedCollection indexForInserting:50
|
|
304 |
#(1.0 2.0 3 4 7 49.0 51.0 99 1313 981989 898989898) asSortedCollection indexForInserting:50
|
|
305 |
"
|
|
306 |
|
|
307 |
!
|
|
308 |
|
|
309 |
setSortBlock: aSortBlock
|
|
310 |
"set the sortblock without resorting - private only"
|
|
311 |
|
|
312 |
sortBlock := aSortBlock
|
|
313 |
! !
|
|
314 |
|
|
315 |
!SortedCollection methodsFor:'searching'!
|
|
316 |
|
|
317 |
after:anObject
|
|
318 |
"return the element after the argument, anObject; or nil if there is none.
|
|
319 |
If anObject is contained multiple times in the collection, return the
|
|
320 |
the first element which is non-equal to anObject.
|
|
321 |
If the receiver does not contain anObject, report an error"
|
|
322 |
|
|
323 |
^ self after:anObject ifAbsent:[self errorValueNotFound:anObject]
|
|
324 |
|
|
325 |
"
|
|
326 |
#(7 3 9 10 99) asSortedCollection after:50
|
|
327 |
#(7 3 9 10 99) asSortedCollection after:1
|
|
328 |
#(7 3 9 10 99) asSortedCollection after:10
|
|
329 |
#(7 3 9 10 99) asSortedCollection after:7
|
|
330 |
#(7 3 9 10 99) asSortedCollection after:99
|
|
331 |
#(7 10 3 10 9 10 10 99) asSortedCollection after:9
|
|
332 |
#(7 10 3 10 9 10 10 99) asSortedCollection after:10
|
|
333 |
"
|
|
334 |
!
|
|
335 |
|
|
336 |
after:anObject ifAbsent:exceptionBlock
|
|
337 |
"return the element after the argument, anObject; or nil if there is none.
|
|
338 |
If anObject is contained multiple times in the collection, return the
|
|
339 |
the first element which is non-equal to anObject.
|
|
340 |
If the receiver does not contain anObject, return the result from evaluating
|
|
341 |
exceptionBlock."
|
|
342 |
|
|
343 |
|index "{ Class: SmallInteger }"|
|
|
344 |
|
|
345 |
index := self indexForInserting:anObject.
|
|
346 |
((index < firstIndex)
|
|
347 |
or:[(contentsArray at:index) ~= anObject]) ifTrue:[^ exceptionBlock value].
|
|
348 |
|
|
349 |
"skip multiple occurences of the same ..."
|
|
350 |
|
|
351 |
[(index <= lastIndex) and:[(contentsArray at:index) = anObject]] whileTrue:[
|
|
352 |
index := index + 1
|
|
353 |
].
|
|
354 |
(index > lastIndex) ifTrue:[^ nil].
|
|
355 |
^ contentsArray at:index
|
|
356 |
!
|
|
357 |
|
|
358 |
before:anObject
|
|
359 |
"return the element before the argument, anObject; or nil if there is none.
|
|
360 |
If the receiver does not contain anObject, report an error"
|
|
361 |
|
|
362 |
^ self before:anObject ifAbsent:[self errorValueNotFound:anObject]
|
|
363 |
|
|
364 |
"
|
|
365 |
#(7 3 9 10 99) asSortedCollection before:50
|
|
366 |
#(7 3 9 10 99) asSortedCollection before:1
|
|
367 |
#(7 3 9 10 99) asSortedCollection before:1000
|
|
368 |
#(7 3 9 10 99) asSortedCollection before:10
|
|
369 |
#(7 3 9 10 99) asSortedCollection before:7
|
|
370 |
#(7 3 9 10 99) asSortedCollection before:99
|
|
371 |
#(7 10 3 10 9 10 10 99) asSortedCollection before:9
|
|
372 |
#(7 10 3 10 9 10 10 99) asSortedCollection before:10
|
|
373 |
"
|
|
374 |
!
|
|
375 |
|
|
376 |
before:anObject ifAbsent:exceptionBlock
|
|
377 |
"return the element before the argument, anObject; or nil if there is none.
|
|
378 |
If the receiver does not contain anObject, return the result from evaluating
|
|
379 |
exceptionBlock."
|
|
380 |
|
|
381 |
|index "{ Class: SmallInteger }"|
|
|
382 |
|
|
383 |
index := self indexForInserting:anObject.
|
|
384 |
((index <= firstIndex)
|
|
385 |
or:[(contentsArray at:index) ~= anObject]) ifTrue:[^ exceptionBlock value].
|
|
386 |
|
|
387 |
^ contentsArray at:index - 1
|
|
388 |
|
|
389 |
"
|
|
390 |
#(7 3 9 10 99) asSortedCollection before:50
|
|
391 |
#(7 3 9 10 99) asSortedCollection before:1
|
|
392 |
#(7 3 9 10 99) asSortedCollection before:10
|
|
393 |
#(7 3 9 10 99) asSortedCollection before:7
|
|
394 |
#(7 3 9 10 99) asSortedCollection before:99
|
|
395 |
#(7 10 3 10 9 10 10 99) asSortedCollection before:9
|
|
396 |
#(7 10 3 10 9 10 10 99) asSortedCollection before:10
|
|
397 |
"
|
|
398 |
! !
|
|
399 |
|
1
|
400 |
!SortedCollection methodsFor:'testing'!
|
|
401 |
|
|
402 |
includes:anObject
|
|
403 |
"return true, if the argument, anObject is in the collection.
|
62
|
404 |
Redefined, since due to being sorted, the inclusion check can
|
2
|
405 |
be done with log-n compares i.e. much faster."
|
1
|
406 |
|
32
|
407 |
|index "{ Class: SmallInteger }"|
|
1
|
408 |
|
77
|
409 |
index := self indexForInserting:anObject.
|
32
|
410 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ false].
|
|
411 |
^ (contentsArray at:index) = anObject
|
2
|
412 |
|
77
|
413 |
"
|
|
414 |
#(7 3 9 10 99) asSortedCollection includes:50
|
|
415 |
#(7 3 9 10 99) asSortedCollection includes:10
|
|
416 |
"
|
1
|
417 |
!
|
|
418 |
|
|
419 |
occurrencesOf:anObject
|
|
420 |
"return how many times the argument, anObject is in the collection.
|
62
|
421 |
Redefined, since due to being sorted, the range of checked objects
|
2
|
422 |
can be limited i.e. it can be done much faster."
|
1
|
423 |
|
|
424 |
|index "{ Class: SmallInteger }"
|
|
425 |
tally "{ Class: SmallInteger }" |
|
|
426 |
|
77
|
427 |
index := self indexForInserting:anObject.
|
32
|
428 |
((index < firstIndex) or:[index > lastIndex]) ifTrue:[^ 0].
|
|
429 |
|
1
|
430 |
tally := 0.
|
32
|
431 |
[(index <= lastIndex) and:[(contentsArray at:index) = anObject]] whileTrue:[
|
159
|
432 |
tally := tally + 1.
|
|
433 |
index := index + 1
|
1
|
434 |
].
|
|
435 |
^ tally
|
2
|
436 |
|
77
|
437 |
"
|
|
438 |
#(7 3 9 10 99) asSortedCollection occurrencesOf:50
|
|
439 |
#(7 3 9 10 99) asSortedCollection occurrencesOf:10
|
|
440 |
#(7 10 3 10 9 10 10 99) asSortedCollection occurrencesOf:10
|
|
441 |
"
|
|
442 |
! !
|
|
443 |
|
629
|
444 |
!SortedCollection class methodsFor:'documentation'!
|
|
445 |
|
|
446 |
version
|
|
447 |
^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.23 1995-11-23 17:06:40 cg Exp $'
|
|
448 |
! !
|
607
|
449 |
SortedCollection initialize!
|