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