[Squeak] been dazed by SortedCollection

Maurice Rabb m3rabb at stono.com
Mon Aug 31 17:26:11 UTC 1998


At 3:55 AM 8/31/98, NISHIHARA Satoshi wrote:

>and get result...
>1) 29672
>2) 29595
>3) 14330 (... super copyFrom: startIndex to: endIndex. ...)


The problem with the new versions is that we are forgeting special the
SortCollection protocol.

#add:           adds an element an resorts the list immediately
#addAll:        adds a collection and then resorts
#addLast:       adds an element without resorting (sneaky)
#resort         forces a resort


Try using:

copyFrom: startIndex to: endIndex
        "Answer a copy of the receiver that contains elements from position
        startIndex to endIndex, keeping the sortBlock from the receiver"

        | newCollection |
        newCollection _ self species new:
                (endIndex < startIndex
                        ifTrue: [0] ifFalse: [(endIndex + 1 - startIndex)]).
        newCollection sortBlock: self sortBlock.
        startIndex to: endIndex do:
                [:index | newCollection addLast: (self at: index)].
        ^newCollection reSort


>        | a b |
>        Transcript cr; show: (Time millisecondsToRun:
>                [a := SortedCollection new.
>                a sortBlock: [:element1 :element2 | element1 > element2].
>                1 to: 10000 do: [:i | a add: i].
>                100 timesRepeat: [b := a copyFrom: 1 to: 500].
>                ]) printString.
>        ^b


Likewise you should rewrite your test code to make use of #addAll: as well.

a := SortedCollection sortBlock: [:element1 :element2 | element1 > element2].
a addAll: (1 to: 10000).
Time millisecondsToRun:[100 timesRepeat: [b := a copyFrom: 1 to: 500]]

--Maurice

---------------------------------------------------------------------------
  Maurice Rabb    773.281.6003    Stono Technologies, LLC    Chicago, USA





More information about the Squeak-dev mailing list