664 |
664 |
665 removeFirst |
665 removeFirst |
666 "remove the first element from the collection; return the element. |
666 "remove the first element from the collection; return the element. |
667 If there is no element in the receiver collection, raise an error." |
667 If there is no element in the receiver collection, raise an error." |
668 |
668 |
669 |anObject fI "{ Class: SmallInteger }" | |
669 |anObject |
|
670 fI "{ Class: SmallInteger }" | |
670 |
671 |
671 fI := firstIndex. |
672 fI := firstIndex. |
672 |
673 |
673 fI > lastIndex ifTrue:[ |
674 fI > lastIndex ifTrue:[ |
674 "error if collection is empty" |
675 "error if collection is empty" |
694 (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself |
695 (OrderedCollection withAll:#(1 2 3 4 5)) removeFirst; yourself |
695 OrderedCollection new removeFirst |
696 OrderedCollection new removeFirst |
696 (SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself |
697 (SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself |
697 " |
698 " |
698 |
699 |
699 "Modified: / 30.7.1998 / 13:19:59 / cg" |
700 "Modified: / 5.2.1999 / 23:22:58 / cg" |
700 ! |
701 ! |
701 |
702 |
702 removeFirst:n |
703 removeFirst:n |
703 "remove the first n elements from the collection; |
704 "remove the first n elements from the collection; |
704 Return a collection containing the removed elements." |
705 Return a collection containing the removed elements." |
793 Return the receiver. |
794 Return the receiver. |
794 Returning the receiver here is a historic leftover - it may change. |
795 Returning the receiver here is a historic leftover - it may change. |
795 Please use yourself in a cascade, if you need the receivers value |
796 Please use yourself in a cascade, if you need the receivers value |
796 when using this method." |
797 when using this method." |
797 |
798 |
798 |nDeleted "{ Class: SmallInteger }" |
799 |nDeleted "{ Class: SmallInteger }" |
|
800 fI "{ Class: SmallInteger }" |
|
801 lI "{ Class: SmallInteger }" |
799 newLastIndex sz| |
802 newLastIndex sz| |
800 |
803 |
801 sz := self size. |
804 sz := self size. |
802 |
805 |
803 (startIndex < 1 or:[stopIndex > sz]) ifTrue:[ |
806 (startIndex < 1 or:[stopIndex > sz]) ifTrue:[ |
808 nDeleted < 0 ifTrue:[ |
811 nDeleted < 0 ifTrue:[ |
809 "/ mhmh - what should be done here ? |
812 "/ mhmh - what should be done here ? |
810 ^ self error:'bad index range' |
813 ^ self error:'bad index range' |
811 ]. |
814 ]. |
812 nDeleted == 0 ifTrue:[^ self]. |
815 nDeleted == 0 ifTrue:[^ self]. |
|
816 |
|
817 fI := firstIndex. |
|
818 lI := lastIndex. |
813 |
819 |
814 "/ |
820 "/ |
815 "/ can be done faster, when removing the first elements |
821 "/ can be done faster, when removing the first elements |
816 "/ |
822 "/ |
817 startIndex == 1 ifTrue:[ |
823 startIndex == 1 ifTrue:[ |
818 "/ nil out (helps GC) |
824 "/ nil out (helps GC) |
819 contentsArray |
825 contentsArray |
820 from:firstIndex |
826 from:fI |
821 to:firstIndex + nDeleted - 1 |
827 to:fI + nDeleted - 1 |
822 put:nil. |
828 put:nil. |
823 firstIndex := firstIndex + nDeleted |
829 firstIndex := fI := fI + nDeleted |
824 ] ifFalse:[ |
830 ] ifFalse:[ |
825 "/ |
831 "/ |
826 "/ can be done faster, when removing the last elements |
832 "/ can be done faster, when removing the last elements |
827 "/ |
833 "/ |
828 stopIndex == sz ifTrue:[ |
834 stopIndex == sz ifTrue:[ |
829 "/ nil out (helps GC) |
835 "/ nil out (helps GC) |
830 contentsArray |
836 contentsArray |
831 from:lastIndex - nDeleted + 1 |
837 from:lI - nDeleted + 1 |
832 to:lastIndex |
838 to:lI |
833 put:nil. |
839 put:nil. |
834 lastIndex := lastIndex - nDeleted |
840 lastIndex := lI := lI - nDeleted |
835 ] ifFalse:[ |
841 ] ifFalse:[ |
836 "/ |
842 "/ |
837 "/ must shuffle |
843 "/ must shuffle |
838 "/ TODO: |
844 "/ TODO: |
839 "/ for big collections, try to copy the smallest |
845 "/ for big collections, try to copy the smallest |
840 "/ possible number of elements |
846 "/ possible number of elements |
841 |
847 |
842 newLastIndex := lastIndex - nDeleted. |
848 newLastIndex := lI - nDeleted. |
843 |
849 |
844 contentsArray |
850 contentsArray |
845 replaceFrom:(firstIndex + startIndex - 1) |
851 replaceFrom:(fI + startIndex - 1) |
846 to:newLastIndex |
852 to:newLastIndex |
847 with:contentsArray |
853 with:contentsArray |
848 startingAt:(firstIndex + stopIndex). |
854 startingAt:(fI + stopIndex). |
849 |
855 |
850 "/ nil out rest (helps GC) |
856 "/ nil out rest (helps GC) |
851 contentsArray |
857 contentsArray |
852 from:(newLastIndex + 1) |
858 from:(newLastIndex + 1) |
853 to:lastIndex |
859 to:lI |
854 put:nil. |
860 put:nil. |
855 |
861 |
856 lastIndex := newLastIndex. |
862 lastIndex := lI := newLastIndex. |
857 ] |
863 ] |
858 ]. |
864 ]. |
859 |
865 |
860 firstIndex > lastIndex ifTrue:[ |
866 fI > lI ifTrue:[ |
861 "reset to avoid ever growing" |
867 "reset to avoid ever growing" |
862 firstIndex := 1. |
868 firstIndex := 1. |
863 lastIndex := 0 |
869 lastIndex := 0 |
864 ] |
870 ] |
865 |
871 |
869 #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:1 toIndex:3 |
875 #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:1 toIndex:3 |
870 #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:6 toIndex:9 |
876 #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:6 toIndex:9 |
871 #(1 2 3 4 5) asOrderedCollection removeFromIndex:3 toIndex:6 |
877 #(1 2 3 4 5) asOrderedCollection removeFromIndex:3 toIndex:6 |
872 " |
878 " |
873 |
879 |
874 "Modified: 8.2.1997 / 19:31:05 / cg" |
880 "Modified: / 5.2.1999 / 23:22:07 / cg" |
875 ! |
881 ! |
876 |
882 |
877 removeIdentical:anObject ifAbsent:exceptionBlock |
883 removeIdentical:anObject ifAbsent:exceptionBlock |
878 "remove the first element which is identical to anObject; |
884 "remove the first element which is identical to anObject; |
879 if found, remove and return it; |
885 if found, remove and return it; |