[Squeak] been dazed by SortedCollection

Patrick Logan patrickl at servio.gemstone.com
Sun Aug 30 05:15:00 UTC 1998


It's late, but here goes...

    I've been dazed and confused ((C)Led Zeppelin) by
    SortedCollection.

        Transcript cr; show: (collection copyFrom: 1 to: 5) printString

    Got SortedCollection (6 7 8 9 10 ), although expected
    SortedCollection (10 9 8 7 6 ).

Because there are a number of *copy* methods that do not give
SortedCollection a chance to set the sortBlock for the new collection.

    Here is my first aid.

        targetCollection sortBlock: sortBlock.

This will resort the collection using your desired sortBlock. It is
inefficient, but it will work. Better to set the sortBlock before
adding elements. For example (same could be applied to a number of
other copy methods)...

OrderedCollection>>copyFrom: start to: end

  | copy |
  end < start ifTrue: [^self copyEmpty].
  copy := self copyEmptySize: end -start + 1.
  start to: end do:
    [ :n |
     copy add: (self at: n)].
  ^copy

....where...

OrderedCollection>>copyEmptySize: n

  ^self species new: n

....and...

SortedCollection>>copyEmptySize: n

  ^(self species new: n)
      sortBlock: self sortBlock;
      yourself

Untested because I have to get back to testing Java... 8*(

-- 
Patrick Logan                 mailto:patrickl at gemstone.com
Voice 503-533-3365            Fax   503-629-8556
Gemstone Systems, Inc         http://www.gemstone.com





More information about the Squeak-dev mailing list