+copyWithoutAll:
authorClaus Gittinger <cg@exept.de>
Tue, 30 Mar 2004 20:46:42 +0200
changeset 8276 9720407471d6
parent 8275 1f2d4a880b69
child 8277 357428ca03c5
+copyWithoutAll:
SequenceableCollection.st
--- a/SequenceableCollection.st	Tue Mar 30 20:46:20 2004 +0200
+++ b/SequenceableCollection.st	Tue Mar 30 20:46:42 2004 +0200
@@ -433,6 +433,53 @@
     "
 !
 
+copyWithoutAll:elementsToSkip
+    "return a new collection consisting of a copy of the receiver, with
+     ALL elements equal to any in elementsToSkip are left out.
+     No error is reported, if any in elementsToSkip is not in the collection."
+
+    |n         "{ Class: SmallInteger }"
+     sz        "{ Class: SmallInteger }"
+     srcIndex  "{ Class: SmallInteger }"
+     dstIndex  "{ Class: SmallInteger }"
+     skipIndex "{ Class: SmallInteger }"
+     copy l|
+
+    "the code below may look like overkill, 
+     however, for big collections its better to move data
+     around in big chunks"
+
+    n := self occurrencesOfAny:elementsToSkip.
+    n == 0 ifTrue:[^ self copy].
+
+    sz := self size.
+    copy := self copyEmptyAndGrow:(sz - n).
+
+    srcIndex := 1.
+    dstIndex := 1.
+
+    n timesRepeat:[
+        skipIndex := self indexOfAny:elementsToSkip startingAt:srcIndex.
+        l := skipIndex - srcIndex.
+        l ~~ 0 ifTrue:[
+            copy replaceFrom:dstIndex to:(dstIndex + l - 1) 
+                        with:self startingAt:srcIndex.
+            dstIndex := dstIndex + l
+        ].
+        srcIndex := skipIndex + 1
+    ].
+    l := sz - srcIndex.
+    copy replaceFrom:dstIndex to:(dstIndex + l)
+                with:self startingAt:srcIndex.
+    ^ copy
+
+    "
+     #($a $b $c $d $e $f $g) copyWithoutAll:#($d $b $f)
+     'abcdefghi' copyWithoutAll:'hai'    
+     #(90 80 70 80 60 45 80 50) copyWithoutAll:#(80 70 45)
+    "
+!
+
 copyWithoutFirst
     ^ self copyFrom:2
 !
@@ -6381,7 +6428,7 @@
 !SequenceableCollection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.205 2004-03-17 16:15:57 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.206 2004-03-30 18:46:42 cg Exp $'
 ! !
 
 SequenceableCollection initialize!