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 |