logo       

correction [was: Re: [FIX] SortedCollectionFix-sr]: msg#00743

lang.smalltalk.squeak.general

Subject: correction [was: Re: [FIX] SortedCollectionFix-sr]

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

<...>





<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise