612 |
613 |
613 remove:anObject ifAbsent:exceptionBlock |
614 remove:anObject ifAbsent:exceptionBlock |
614 "remove the first element which is equal to anObject; |
615 "remove the first element which is equal to anObject; |
615 if found, remove and return it; |
616 if found, remove and return it; |
616 if not, return the value from evaluating exceptionBlock. |
617 if not, return the value from evaluating exceptionBlock. |
|
618 Destructive: modifies the receiver. |
617 Uses equality compare (=) to search for the element." |
619 Uses equality compare (=) to search for the element." |
618 |
620 |
619 |index retVal| |
621 |index retVal| |
620 |
622 |
621 index := self indexOf:anObject. |
623 index := self indexOf:anObject. |
657 |
660 |
658 removeAllSuchThat:aBlock |
661 removeAllSuchThat:aBlock |
659 "remove all elements that meet a test criteria as specified in aBlock. |
662 "remove all elements that meet a test criteria as specified in aBlock. |
660 The argument, aBlock is evaluated for successive elements and all those, |
663 The argument, aBlock is evaluated for successive elements and all those, |
661 for which it returns true, are removed. |
664 for which it returns true, are removed. |
|
665 Destructive: modifies the receiver. |
662 Return a collection containing the removed elements. |
666 Return a collection containing the removed elements. |
663 |
667 |
664 Performance hint: |
668 Performance: |
665 if a large number of objects is to be removed this way, |
669 this is an O(N) algorithm. |
|
670 |
|
671 If a large number of objects is to be removed this way, |
666 it may be better to rebuild a new collection via #select:, |
672 it may be better to rebuild a new collection via #select:, |
667 since removing elements out of the middle is somewhat slow |
673 since removing elements out of the middle is somewhat slow |
668 due to the need to copy over remaining elements within the collection." |
674 due to the need to copy over remaining elements within the collection." |
669 |
675 |
670 "/ this is a q&d implementation (possibly slow). |
676 "/ this is a q&d implementation (possibly slow). |
671 |
677 |
672 |runIndex removed element sz| |
678 |removed element dstIndex| |
673 |
679 |
674 removed := OrderedCollection new. |
680 removed := OrderedCollection new. |
675 |
681 |
676 runIndex := 1. |
682 dstIndex := firstIndex. |
677 sz := self size. |
683 firstIndex to: lastIndex do:[:srcIndex | |
678 [runIndex <= sz] whileTrue:[ |
684 element := contentsArray at:srcIndex. |
679 element := self at:runIndex. |
|
680 (aBlock value:element) ifTrue:[ |
685 (aBlock value:element) ifTrue:[ |
681 removed add:element. |
686 removed add:element |
682 self removeAtIndex:runIndex. |
|
683 sz := sz - 1. |
|
684 ] ifFalse:[ |
687 ] ifFalse:[ |
685 runIndex := runIndex + 1 |
688 contentsArray at:dstIndex put:element. |
686 ] |
689 dstIndex := dstIndex + 1. |
687 ]. |
690 ]. |
|
691 ]. |
|
692 contentsArray from:dstIndex to:lastIndex put:nil. |
|
693 lastIndex := dstIndex - 1. |
688 ^ removed |
694 ^ removed |
689 |
695 |
690 " |
696 " |
691 |coll| |
697 |coll| |
692 |
698 |
693 coll := OrderedCollection withAll:(1 to:10). |
699 coll := OrderedCollection withAll:(1 to:10). |
694 Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
700 Transcript showCR:(coll removeAllSuchThat:[:el | el even]). |
695 Transcript showCR:coll |
701 Transcript showCR:coll |
696 " |
702 " |
|
703 " |
|
704 |coll1 coll2| |
|
705 |
|
706 coll1 := OrderedCollection withAll:(1 to:1000). |
|
707 Transcript show:'(1000) - '; showCR:( |
|
708 Time millisecondsToRun:[ |
|
709 100 timesRepeat:[ coll1 copy removeAllSuchThat:[:el | el even] ] |
|
710 ]). |
|
711 |
|
712 coll2 := OrderedCollection withAll:(1 to:10000). |
|
713 Transcript show:'(10000) - '; showCR:( |
|
714 Time millisecondsToRun:[ |
|
715 100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el even] ] |
|
716 ]). |
|
717 |
|
718 coll2 := OrderedCollection withAll:(1 to:50000). |
|
719 Transcript show:'(50000) - '; showCR:( |
|
720 Time millisecondsToRun:[ |
|
721 100 timesRepeat:[ coll2 copy removeAllSuchThat:[:el | el even] ] |
|
722 ]). |
|
723 " |
697 |
724 |
698 "Modified: 8.2.1997 / 19:19:00 / cg" |
725 "Modified: 8.2.1997 / 19:19:00 / cg" |
699 ! |
726 ! |
700 |
727 |
701 removeFirst |
728 removeFirst |
702 "remove the first element from the collection; return the element. |
729 "remove the first element from the collection; return the element. |
703 If there is no element in the receiver collection, raise an error." |
730 If there is no element in the receiver collection, raise an error. |
|
731 Destructive: modifies the receiver" |
704 |
732 |
705 |anObject |
733 |anObject |
706 fI "{ Class: SmallInteger }" | |
734 fI "{ Class: SmallInteger }" | |
707 |
735 |
708 fI := firstIndex. |
736 fI := firstIndex. |
825 ! |
855 ! |
826 |
856 |
827 removeFromIndex:startIndex toIndex:stopIndex |
857 removeFromIndex:startIndex toIndex:stopIndex |
828 "remove the elements stored under startIndex up to and including |
858 "remove the elements stored under startIndex up to and including |
829 the elements under stopIndex. |
859 the elements under stopIndex. |
|
860 Destructive: modifies the receiver |
830 Return the receiver. |
861 Return the receiver. |
831 Returning the receiver here is a historic leftover - it may change. |
862 Returning the receiver here is a historic leftover - it may change. |
832 Please use yourself in a cascade, if you need the receiver's value |
863 Please use yourself in a cascade, if you need the receiver's value |
833 when using this method." |
864 when using this method." |
834 |
865 |
918 |
949 |
919 removeIdentical:anObject ifAbsent:exceptionBlock |
950 removeIdentical:anObject ifAbsent:exceptionBlock |
920 "remove the first element which is identical to anObject; |
951 "remove the first element which is identical to anObject; |
921 if found, remove and return it; |
952 if found, remove and return it; |
922 if not, return the value from evaluating exceptionBlock. |
953 if not, return the value from evaluating exceptionBlock. |
|
954 Destructive: modifies the receiver. |
923 Uses identity compare (==) to search for the element." |
955 Uses identity compare (==) to search for the element." |
924 |
956 |
925 |index| |
957 |index| |
926 |
958 |
927 index := contentsArray identityIndexOf:anObject startingAt:firstIndex endingAt:lastIndex. |
959 index := contentsArray identityIndexOf:anObject startingAt:firstIndex endingAt:lastIndex. |
964 " |
996 " |
965 |
997 |
966 "Modified: 8.2.1997 / 18:57:43 / cg" |
998 "Modified: 8.2.1997 / 18:57:43 / cg" |
967 ! |
999 ! |
968 |
1000 |
|
1001 removeIndices:aSortedCollectionOfIndices |
|
1002 "remove all elements stored in any of aSortedCollectionOfIndices, |
|
1003 which must be sorted and sequenceable. |
|
1004 Destructive: modifies the receiver. |
|
1005 Returns a collection of removed elements. |
|
1006 |
|
1007 Performance: |
|
1008 this is an O(N) algorithm." |
|
1009 |
|
1010 |removed element dstIndex indexIndex nextIndex numIndices| |
|
1011 |
|
1012 removed := OrderedCollection new. |
|
1013 |
|
1014 dstIndex := firstIndex. |
|
1015 numIndices := aSortedCollectionOfIndices size. |
|
1016 indexIndex := 1. |
|
1017 nextIndex := aSortedCollectionOfIndices at:1. |
|
1018 firstIndex to: lastIndex do:[:srcIndex | |
|
1019 element := contentsArray at:srcIndex. |
|
1020 srcIndex == nextIndex ifTrue:[ |
|
1021 removed add:element. |
|
1022 indexIndex := indexIndex + 1. |
|
1023 indexIndex > numIndices ifTrue:[ |
|
1024 nextIndex := nil. |
|
1025 ] ifFalse:[ |
|
1026 nextIndex := aSortedCollectionOfIndices at:indexIndex. |
|
1027 ]. |
|
1028 ] ifFalse:[ |
|
1029 contentsArray at:dstIndex put:element. |
|
1030 dstIndex := dstIndex + 1. |
|
1031 ]. |
|
1032 ]. |
|
1033 contentsArray from:dstIndex to:lastIndex put:nil. |
|
1034 lastIndex := dstIndex - 1. |
|
1035 ^ removed |
|
1036 |
|
1037 " |
|
1038 |coll| |
|
1039 |
|
1040 coll := OrderedCollection withAll:(1 to:10). |
|
1041 Transcript showCR:(coll removeIndices:#(1 5 7)). |
|
1042 Transcript showCR:coll |
|
1043 " |
|
1044 ! |
|
1045 |
969 removeLast |
1046 removeLast |
970 "remove the last element from the collection; return the element" |
1047 "remove the last element from the collection. |
|
1048 Return the removed element. |
|
1049 Destructive: modifies the receiver" |
971 |
1050 |
972 |anObject |
1051 |anObject |
973 idx "{ Class: SmallInteger }" | |
1052 idx "{ Class: SmallInteger }" | |
974 |
1053 |
975 idx := lastIndex. |
1054 idx := lastIndex. |