correction [was: Re: [FIX] SortedCollectionFix-sr]
Stephan Rudlof
squeak-dev at lists.squeakfoundation.org
Fri Sep 27 16:15:24 UTC 2002
I wrote:
<...>
> Dictionary>>keysSortedSafely
> "Answer a SortedCollection containing the receiver's keys."
> | sortedKeys |
> sortedKeys _ SortedCollection new: self size.
> sortedKeys sortBlock:
> [:x :y | "Should really be use <obj, string, num> compareSafely..."
> ((x isString and: [y isString])
> or: [x isNumber and: [y isNumber]])
> ifTrue: [x < y]
> ifFalse: [x class == y class
> ifTrue: [x printString < y printString]
> ifFalse: [x class name < y class name]]].
> self keysDo: [:each | sortedKeys addLast: each].
> ^ sortedKeys reSort
>
> This seems to be safe, since >>reSort is called in every case, but using
> SortedCollection>>addAll: seems to be better here.
>
> But let's take a look at
> SortedCollection>>
> addAll: aCollection
> aCollection size > (self size // 3)
> ifTrue:
> [aCollection do: [:each | super addLast: each].
> self reSort]
> ifFalse: [aCollection do: [:each | self add: each]].
> ^ aCollection
>
> Now I see a problem: the implementor (sma) of Dictionary>>keysSortedSafely
> possibly has seen, that in his case each element would be added one by one,
> instead of adding all unsorted followed by a reSort. And then he has tried
> to optimize it...
This statement is wrong!
Calling SortedCollection>>addAll: with a temporarily constructed
OrderedCollection containing the keys unsorted would lead to the "self
reSort" case. Don't know, what I've seen here.
Sorry,
Stephan
<...>
More information about the Squeak-dev
mailing list
|