SortedCollection.st
changeset 4596 66a082fbc5b3
parent 3340 300d88cf8b14
child 5028 1f7e943ce46e
equal deleted inserted replaced
4595:24446fd5d3e1 4596:66a082fbc5b3
   638      #(7 3 9 10 99) asSortedCollection before:7   
   638      #(7 3 9 10 99) asSortedCollection before:7   
   639      #(7 3 9 10 99) asSortedCollection before:99   
   639      #(7 3 9 10 99) asSortedCollection before:99   
   640      #(7 10 3 10 9 10 10 99) asSortedCollection before:9  
   640      #(7 10 3 10 9 10 10 99) asSortedCollection before:9  
   641      #(7 10 3 10 9 10 10 99) asSortedCollection before:10   
   641      #(7 10 3 10 9 10 10 99) asSortedCollection before:10   
   642     "
   642     "
       
   643 !
       
   644 
       
   645 indexOf:anElement
       
   646     "Return the index of anElement within the receiver. 
       
   647      If the receiver does not contain anElement, return 0."
       
   648 
       
   649     ^ self indexOf:anElement ifAbsent:0
       
   650 !
       
   651 
       
   652 indexOf:anElement ifAbsent:exceptionBlock
       
   653     "Return the index of anElement within the receiver. 
       
   654      If the receiver does not contain anElement, 
       
   655      return the result of evaluating the argument, exceptionBlock."
       
   656 
       
   657     |insertionIndex index "{ Class: SmallInteger }" 
       
   658      obj|
       
   659 
       
   660     firstIndex > lastIndex ifTrue:[
       
   661         "/ empty
       
   662         ^ exceptionBlock value
       
   663     ].
       
   664 
       
   665     "/ if I am small, the inherited linear search is faster ...
       
   666     (lastIndex - firstIndex) < 20 ifTrue:[
       
   667         ^ super indexOf:anElement ifAbsent:exceptionBlock
       
   668     ].
       
   669 
       
   670     insertionIndex := self indexForInserting:anElement.
       
   671     insertionIndex > lastIndex ifTrue:[
       
   672         insertionIndex := lastIndex
       
   673     ] ifFalse:[
       
   674         insertionIndex < firstIndex ifTrue:[
       
   675             insertionIndex := firstIndex
       
   676         ]
       
   677     ].
       
   678 
       
   679     index := insertionIndex.
       
   680     [index >= firstIndex
       
   681     and:[obj := contentsArray basicAt:index.
       
   682             anElement = obj ifTrue: [^ index - firstIndex + 1].
       
   683             [sortBlock value:anElement value:obj]]]
       
   684                     whileTrue: [index := index - 1].
       
   685 
       
   686     index := insertionIndex.
       
   687     [index <= lastIndex
       
   688     and: [obj := contentsArray basicAt: index.
       
   689             anElement = obj ifTrue: [^ index - firstIndex + 1].
       
   690             [sortBlock value:obj value:anElement]]]
       
   691                     whileTrue: [index := index + 1].
       
   692 
       
   693     ^exceptionBlock value
       
   694 
       
   695     "
       
   696      #('aa' 'bb' 'cc' 'dd') asSortedCollection indexOf:'bb'
       
   697      #('aa' 'bb' 'cc' 'dd' 'aa' 'bb' 'cc' 'dd') asSortedCollection indexOf:'bb' 
       
   698 
       
   699      |allSyms indices|
       
   700      allSyms := Symbol allInstances asSortedCollection. 
       
   701      Time millisecondsToRun:[
       
   702          indices := allSyms collect:[:el | allSyms indexOf:el]. 
       
   703      ].
       
   704      indices = (1 to:allSyms size)
       
   705     "
   643 ! !
   706 ! !
   644 
   707 
   645 !SortedCollection methodsFor:'testing'!
   708 !SortedCollection methodsFor:'testing'!
   646 
   709 
   647 includes:anObject
   710 includes:anObject
   693 ! !
   756 ! !
   694 
   757 
   695 !SortedCollection class methodsFor:'documentation'!
   758 !SortedCollection class methodsFor:'documentation'!
   696 
   759 
   697 version
   760 version
   698     ^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.38 1998-03-13 16:24:04 cg Exp $'
   761     ^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.39 1999-08-12 10:33:40 cg Exp $'
   699 ! !
   762 ! !
   700 SortedCollection initialize!
   763 SortedCollection initialize!