SortedCollection reversed...

Ned Konz ned at bike-nomad.com
Thu Oct 11 17:38:41 UTC 2001


On Thursday 11 October 2001 02:41 am, goran.hultgren at bluefish.se wrote:
> Hmmm, doing some archeology here:
>
> 'abd' asSortedCollection reversed -->  a SortedCollection($d $b $a)
>
> ...as expected but:
>
> 'abd' asSortedCollection reversed reSort --> a SortedCollection($a $b
> $d)
>
> ...ok, so reversed (obvious, if you check the implementation of
> reversed) is just a temporary state for SortedCollections. Hmm, is that
> logical/intentional/intuitive/good? (I checked in Dolphin and they have
> changed "self species" to "OrderedCollection" so that you actually do not
> get a SortedCollection back. They noted the trickiness in maintaining an
> inverted sortBlock.)
>
> 'abd' asSortedCollection reversed add: $c; yourself --> a
> SortedCollection($d $b $a $c)
>
> ...not sorted anymore in either direction! Actually, it tried to put $c
> in the right place but fails since it is reversed.
>
> Questions:
>
> - First, why is there both #reverse and #reversed ? Both are called in
> numerous places and one is just implemented by calling the other. If
> #reverse was done "inplace" I could understand the difference in name
> (as reverse sounds like an imperative), but it isn't.
>
> - Ok, if we reimplement reverse/reversed for SortedCollections so that
> it remembers that it is reversed, how should it be done?
>
> - Sidequestion: If a class without subclasses implements #species as for
> example CharacterSet does (returning the class CharacterSet), what is
> the point in that? I mean, what is the difference from just inheriting
> Object>>species? Well, if it isn't in anticipation of new subclasses of
> course... :-)
>
> Thoughts:
>
> I experimented with wrapping the sortBlock in a reversing block - [:a :b
>
> | block value: b value: a ] - but I couldn't really figure out how to
>
> get it working without adding some state to SortedCollection in order
> for the SortedCollection to know when it has been reversed so that it
> could "unwrap" the sortblock again. A simpler approach is to just add
> another sortmethod and a boolean flag telling when being reversed, but
> that would mean another instvar.
>
> Anyway, just some ramblings... Set me straight! :-)
>
> regards, Gšran

Why not just:

reversed
	| newSortBlock newCollection |
	newCollection _ self class new: self size.
	newSortBlock _ sortBlock ifNil: [ [ :a :b | b < a  ] ]
		ifNotNil: [ [ :a :b | sortBlock value: b value: a  ] ].
	newCollection sortBlock: newSortBlock fixTemps.
	newCollection addAll: self.
	^newCollection.

I don't know why you have to keep track of whether it's been reversed before, 
unless you're going to re-reverse the output of reversed.

I tried to subclass BlockContext to make a ReversedBlockContext (that would 
define, for instance,

ReversedBlockContext>>value: a value: b
        ^super value: b value: a

But it blew up the VM when I actually tried to evaluate one.

I figured that you could have a method

BlockContext>>reversed
     ^self as: ReversedBlockContext

and use that in SortedCollection>>reversed (which would allow un-reversal).

-- 
Ned Konz
currently: Stanwood, WA
email:     ned at bike-nomad.com
homepage:  http://bike-nomad.com




More information about the Squeak-dev mailing list