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