faster #removeFromIndex:toIndex.
authorClaus Gittinger <cg@exept.de>
Sat, 01 Feb 1997 12:54:38 +0100
changeset 2355 9476ba4d810d
parent 2354 652d17daeeb5
child 2356 7f7118456ed0
faster #removeFromIndex:toIndex.
OrdColl.st
OrderedCollection.st
--- a/OrdColl.st	Sat Feb 01 12:37:19 1997 +0100
+++ b/OrdColl.st	Sat Feb 01 12:54:38 1997 +0100
@@ -695,32 +695,70 @@
 
     (startIndex < 1
     or:[stopIndex > self size]) ifTrue:[
-	^ self notEnoughElementsError
+        ^ self notEnoughElementsError
     ].
+
+    stopIndex < startIndex ifTrue:[
+        "/ mhmh - what should be done here ?
+        ^ self error:'bad index range'
+    ].
+
     nDeleted := stopIndex - startIndex + 1.
-    contentsArray 
-	replaceFrom:(firstIndex + startIndex - 1)
-	to:(lastIndex - nDeleted)
-	with:contentsArray 
-	startingAt:(firstIndex + stopIndex).
 
-    "/ nil out rest (helps GC)
-    contentsArray
-	from:(lastIndex - nDeleted + 1)
-	to:lastIndex
-	put:nil.
+    "/ can be done faster, when removing the first elements
+    startIndex == firstIndex ifTrue:[
+        "/ nil out (helps GC)
+        contentsArray
+            from:firstIndex
+            to:firstIndex + nDeleted - 1
+            put:nil.
+        firstIndex := firstIndex + nDeleted
+    ] ifFalse:[
+        "/ can be done faster, when removinf the last elements
+        stopIndex == lastIndex ifTrue:[
+            "/ nil out (helps GC)
+            contentsArray
+                from:lastIndex - nDeleted + 1
+                to:lastIndex
+                put:nil.
+            lastIndex := lastIndex - nDeleted
+        ] ifFalse:[
+            "/ must shuffle
+            "/ TODO:
+            "/    for big collections, try to copy the smallest
+            "/    possible number of elements
 
-    lastIndex := lastIndex - nDeleted.
+            contentsArray 
+                replaceFrom:(firstIndex + startIndex - 1)
+                to:(lastIndex - nDeleted)
+                with:contentsArray 
+                startingAt:(firstIndex + stopIndex).
+
+            "/ nil out rest (helps GC)
+            contentsArray
+                from:(lastIndex - nDeleted + 1)
+                to:lastIndex
+                put:nil.
+
+            lastIndex := lastIndex - nDeleted.
+        ]
+    ].
+
     firstIndex > lastIndex ifTrue:[
-	"reset to avoid ever growing"
-	firstIndex := 1.
-	lastIndex := 0 
+        "reset to avoid ever growing"
+        firstIndex := 1.
+        lastIndex := 0 
     ]
 
     "
      #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:3 toIndex:6
+     #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:6 toIndex:8
+     #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:1 toIndex:3
+     #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:6 toIndex:9
      #(1 2 3 4 5) asOrderedCollection removeFromIndex:3 toIndex:6
     "
+
+    "Modified: 1.2.1997 / 12:53:58 / cg"
 !
 
 removeIdentical:anObject ifAbsent:exceptionBlock
@@ -1625,5 +1663,5 @@
 !OrderedCollection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Attic/OrdColl.st,v 1.57 1997-02-01 11:37:19 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Attic/OrdColl.st,v 1.58 1997-02-01 11:54:38 cg Exp $'
 ! !
--- a/OrderedCollection.st	Sat Feb 01 12:37:19 1997 +0100
+++ b/OrderedCollection.st	Sat Feb 01 12:54:38 1997 +0100
@@ -695,32 +695,70 @@
 
     (startIndex < 1
     or:[stopIndex > self size]) ifTrue:[
-	^ self notEnoughElementsError
+        ^ self notEnoughElementsError
     ].
+
+    stopIndex < startIndex ifTrue:[
+        "/ mhmh - what should be done here ?
+        ^ self error:'bad index range'
+    ].
+
     nDeleted := stopIndex - startIndex + 1.
-    contentsArray 
-	replaceFrom:(firstIndex + startIndex - 1)
-	to:(lastIndex - nDeleted)
-	with:contentsArray 
-	startingAt:(firstIndex + stopIndex).
 
-    "/ nil out rest (helps GC)
-    contentsArray
-	from:(lastIndex - nDeleted + 1)
-	to:lastIndex
-	put:nil.
+    "/ can be done faster, when removing the first elements
+    startIndex == firstIndex ifTrue:[
+        "/ nil out (helps GC)
+        contentsArray
+            from:firstIndex
+            to:firstIndex + nDeleted - 1
+            put:nil.
+        firstIndex := firstIndex + nDeleted
+    ] ifFalse:[
+        "/ can be done faster, when removinf the last elements
+        stopIndex == lastIndex ifTrue:[
+            "/ nil out (helps GC)
+            contentsArray
+                from:lastIndex - nDeleted + 1
+                to:lastIndex
+                put:nil.
+            lastIndex := lastIndex - nDeleted
+        ] ifFalse:[
+            "/ must shuffle
+            "/ TODO:
+            "/    for big collections, try to copy the smallest
+            "/    possible number of elements
 
-    lastIndex := lastIndex - nDeleted.
+            contentsArray 
+                replaceFrom:(firstIndex + startIndex - 1)
+                to:(lastIndex - nDeleted)
+                with:contentsArray 
+                startingAt:(firstIndex + stopIndex).
+
+            "/ nil out rest (helps GC)
+            contentsArray
+                from:(lastIndex - nDeleted + 1)
+                to:lastIndex
+                put:nil.
+
+            lastIndex := lastIndex - nDeleted.
+        ]
+    ].
+
     firstIndex > lastIndex ifTrue:[
-	"reset to avoid ever growing"
-	firstIndex := 1.
-	lastIndex := 0 
+        "reset to avoid ever growing"
+        firstIndex := 1.
+        lastIndex := 0 
     ]
 
     "
      #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:3 toIndex:6
+     #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:6 toIndex:8
+     #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:1 toIndex:3
+     #(1 2 3 4 5 6 7 8 9) asOrderedCollection removeFromIndex:6 toIndex:9
      #(1 2 3 4 5) asOrderedCollection removeFromIndex:3 toIndex:6
     "
+
+    "Modified: 1.2.1997 / 12:53:58 / cg"
 !
 
 removeIdentical:anObject ifAbsent:exceptionBlock
@@ -1625,5 +1663,5 @@
 !OrderedCollection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.57 1997-02-01 11:37:19 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.58 1997-02-01 11:54:38 cg Exp $'
 ! !