SequenceableCollectionSorter.st
changeset 183 01781ca31dad
parent 182 2ba5785a441e
child 184 9200f060f2dc
equal deleted inserted replaced
182:2ba5785a441e 183:01781ca31dad
     9 
     9 
    10 documentation
    10 documentation
    11 "
    11 "
    12     a SequenceableCollectionSorter allows for anything which responds to
    12     a SequenceableCollectionSorter allows for anything which responds to
    13     keyed at/atPut messages to be sorted just like a SequenceableCollection.
    13     keyed at/atPut messages to be sorted just like a SequenceableCollection.
    14     Since the access messages can be customized, even non collection (or
    14     Since the access messages can be customized, even non collections
    15     collection simulators) can be sorted with this.
    15     (or collection simulators, models etc.) can be sorted with these sorters.
    16     (use #atSelector: / #putSelector: and #sizeSelector: for customization).
    16     (use #atSelector: / #putSelector: and #sizeSelector: for customization).
    17 
    17 
    18     As with collection sorting, the sortBlock can be specified and defaults to
    18     As with collection sorting, the sortBlock can be specified and defaults to
    19     a block which compares using #< messages.
    19     a block which compares using #< messages.
    20 "
    20 "
   152     b := begin.
   152     b := begin.
   153     e := end.
   153     e := end.
   154     middleElement := collection at:((b + e) // 2).
   154     middleElement := collection at:((b + e) // 2).
   155 
   155 
   156     [b < e] whileTrue:[
   156     [b < e] whileTrue:[
   157         [b < end and:[(collection at:b) < middleElement]] whileTrue:[b := b + 1].
   157         [b < end and:[(collection at:b) <= middleElement]] whileTrue:[b := b + 1].
   158         [e > begin and:[middleElement < (collection at:e)]] whileTrue:[e := e - 1].
   158         [e > begin and:[middleElement <= (collection at:e)]] whileTrue:[e := e - 1].
   159 
   159 
   160         (b <= e) ifTrue:[
   160         (b <= e) ifTrue:[
   161             (b == e) ifFalse:[
   161             (b == e) ifFalse:[
   162                 temp1 := collection at:b. temp2 := collection at:e. 
   162                 temp1 := collection at:b. temp2 := collection at:e. 
   163                 collection at:b put:temp2. collection at:e put:temp1
   163                 collection at:b put:temp2. collection at:e put:temp1
   167         ]
   167         ]
   168     ].
   168     ].
   169     (begin < e) ifTrue:[self defaultSort:begin to:e].
   169     (begin < e) ifTrue:[self defaultSort:begin to:e].
   170     (b < end) ifTrue:[self defaultSort:b to:end]
   170     (b < end) ifTrue:[self defaultSort:b to:end]
   171 
   171 
   172     "Modified: 6.2.1996 / 15:42:51 / cg"
       
   173     "Created: 6.2.1996 / 15:44:37 / cg"
   172     "Created: 6.2.1996 / 15:44:37 / cg"
       
   173     "Modified: 6.2.1996 / 18:00:56 / cg"
   174 !
   174 !
   175 
   175 
   176 nonDefaultSort:inBegin to:inEnd
   176 nonDefaultSort:inBegin to:inEnd
   177     "actual sort worker for sorting when a non default sortBlock
   177     "actual sort worker for sorting when a non default sortBlock
   178      or access selectors are used."
   178      or access selectors are used."
   248 ! !
   248 ! !
   249 
   249 
   250 !SequenceableCollectionSorter class methodsFor:'documentation'!
   250 !SequenceableCollectionSorter class methodsFor:'documentation'!
   251 
   251 
   252 version
   252 version
   253     ^ '$Header: /cvs/stx/stx/libbasic2/SequenceableCollectionSorter.st,v 1.1 1996-02-06 17:49:00 cg Exp $'
   253     ^ '$Header: /cvs/stx/stx/libbasic2/SequenceableCollectionSorter.st,v 1.2 1996-02-06 19:01:35 cg Exp $'
   254 ! !
   254 ! !
   255 SequenceableCollectionSorter initialize!
   255 SequenceableCollectionSorter initialize!