OrderedCollection.st
changeset 2362 23574f29c8af
parent 2361 dd95b53674e9
child 2364 788423a8d67f
--- a/OrderedCollection.st	Sat Feb 08 19:00:05 1997 +0100
+++ b/OrderedCollection.st	Sat Feb 08 19:22:27 1997 +0100
@@ -509,7 +509,7 @@
     "remove the first element which is equal to anObject;
      if found, remove and return it; 
      if not, return the value from evaluating exceptionBlock.
-     Used equality compare (=) to search for the element."
+     Uses equality compare (=) to search for the element."
 
     |index retVal|
 
@@ -532,7 +532,7 @@
      #(1.0 2.0 3.0 4.0 5.0) asOrderedCollection removeIdentical:4 ifAbsent:'oops' 
     "
 
-    "Modified: 8.2.1997 / 18:59:22 / cg"
+    "Modified: 8.2.1997 / 19:17:19 / cg"
 !
 
 removeAll
@@ -551,23 +551,25 @@
      Return a collection containing the removed elements.
 
      Performance hint:
-	if a big number of objects is removed this way,
+        if a large number of objects is to be removed this way,
         it may be better to rebuild a new collection via #select:,
         since removing elements out of the middle is somewhat slow
-	due to the need to copy over remaining elements within the collection."
+        due to the need to copy over remaining elements within the collection."
 
     "/ this is a q&d implementation (possibly slow).
 
-    |runIndex removed element|
+    |runIndex removed element sz|
 
     removed := self species new.
 
     runIndex := 1.
-    [runIndex <= self size] whileTrue:[
+    sz := self size.
+    [runIndex <= sz] whileTrue:[
         element := self at:runIndex.
         (aBlock value:element) ifTrue:[
             removed add:element.
-            self removeAtIndex:runIndex
+            self removeAtIndex:runIndex.
+            sz := sz - 1.
         ] ifFalse:[
             runIndex := runIndex + 1
         ]
@@ -582,31 +584,34 @@
      Transcript showCR:coll
     "
 
-    "Modified: 12.4.1996 / 13:37:01 / cg"
+    "Modified: 8.2.1997 / 19:19:00 / cg"
 !
 
 removeFirst
     "remove the first element from the collection; return the element."
 
-    |anObject |
+    |anObject fI "{ Class: SmallInteger }" |
 
-    firstIndex > lastIndex ifTrue:[
+    fI := firstIndex.
+
+    fI > lastIndex ifTrue:[
         "error if collection is empty"
         ^ self emptyCollectionError.
     ].
 
-    anObject := contentsArray at:firstIndex.
+    anObject := contentsArray at:fI.
 
     "/ nil it out, to allow GC to reclaim it.
-    contentsArray at:firstIndex put:nil.
+    contentsArray at:fI put:nil.
 
-    firstIndex := firstIndex + 1.
+    fI := fI + 1.
 
-    firstIndex > lastIndex ifTrue:[
+    fI > lastIndex ifTrue:[
         "reset to avoid ever growing"
-        firstIndex := 1.
+        fI := 1.
         lastIndex := 0 
     ].
+    firstIndex := fI.
     ^ anObject
 
     "
@@ -615,14 +620,14 @@
      (SortedCollection withAll:#(5 4 3 2 1)) removeFirst; yourself
     "
 
-    "Modified: 1.2.1997 / 12:30:50 / cg"
+    "Modified: 8.2.1997 / 19:14:39 / cg"
 !
 
 removeFirst:n
     "remove the first n elements from the collection; 
      Return a collection containing the removed elements."
 
-    |mySize ret|
+    |mySize ret newFirstIndex|
 
     mySize := self size.
     mySize < n ifTrue:[
@@ -636,14 +641,15 @@
     "/
     "/ nil-out contents array, to not keep elements from being GC'd
     "/
-    contentsArray from:firstIndex to:firstIndex + n - 1 put:nil.
-    firstIndex := firstIndex + n.
+    newFirstIndex := firstIndex + n.
+    contentsArray from:firstIndex to:newFirstIndex - 1 put:nil.
 
-    firstIndex > lastIndex ifTrue:[
+    newFirstIndex > lastIndex ifTrue:[
         "reset to avoid ever growing"
-        firstIndex := 1.
+        newFirstIndex := 1.
         lastIndex := 0 
     ].
+    firstIndex := newFirstIndex.
     ^ ret
 
     "
@@ -654,7 +660,7 @@
      (SortedCollection withAll:#(5 4 3 2 1)) removeFirst:2; yourself  
     "
 
-    "Modified: 12.4.1996 / 13:37:39 / cg"
+    "Modified: 8.2.1997 / 19:20:18 / cg"
 !
 
 removeFromIndex:startIndex toIndex:stopIndex
@@ -665,21 +671,24 @@
      Please use yourself in a cascade, if you need the receivers value
      when using this method."
 
-    |nDeleted|
+    |nDeleted "{ Class: SmallInteger }" 
+     newLastIndex|
 
     (startIndex < 1
     or:[stopIndex > self size]) ifTrue:[
         ^ self notEnoughElementsError
     ].
 
-    stopIndex < startIndex ifTrue:[
+    nDeleted := stopIndex - startIndex + 1.
+    nDeleted < 0 ifTrue:[
         "/ mhmh - what should be done here ?
         ^ self error:'bad index range'
     ].
+    nDeleted == 0 ifTrue:[^ self].
 
-    nDeleted := stopIndex - startIndex + 1.
-
+    "/
     "/ can be done faster, when removing the first elements
+    "/
     startIndex == firstIndex ifTrue:[
         "/ nil out (helps GC)
         contentsArray
@@ -688,7 +697,9 @@
             put:nil.
         firstIndex := firstIndex + nDeleted
     ] ifFalse:[
-        "/ can be done faster, when removinf the last elements
+        "/
+        "/ can be done faster, when removing the last elements
+        "/
         stopIndex == lastIndex ifTrue:[
             "/ nil out (helps GC)
             contentsArray
@@ -697,24 +708,27 @@
                 put:nil.
             lastIndex := lastIndex - nDeleted
         ] ifFalse:[
+            "/
             "/ must shuffle
             "/ TODO:
             "/    for big collections, try to copy the smallest
             "/    possible number of elements
 
+            newLastIndex := lastIndex - nDeleted.
+
             contentsArray 
                 replaceFrom:(firstIndex + startIndex - 1)
-                to:(lastIndex - nDeleted)
+                to:newLastIndex
                 with:contentsArray 
                 startingAt:(firstIndex + stopIndex).
 
             "/ nil out rest (helps GC)
             contentsArray
-                from:(lastIndex - nDeleted + 1)
+                from:(newLastIndex + 1)
                 to:lastIndex
                 put:nil.
 
-            lastIndex := lastIndex - nDeleted.
+            lastIndex := newLastIndex.
         ]
     ].
 
@@ -732,7 +746,7 @@
      #(1 2 3 4 5) asOrderedCollection removeFromIndex:3 toIndex:6
     "
 
-    "Modified: 1.2.1997 / 12:53:58 / cg"
+    "Modified: 8.2.1997 / 19:21:32 / cg"
 !
 
 removeIdentical:anObject ifAbsent:exceptionBlock
@@ -1617,5 +1631,5 @@
 !OrderedCollection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.59 1997-02-08 18:00:05 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/OrderedCollection.st,v 1.60 1997-02-08 18:22:27 cg Exp $'
 ! !