SortedCollection.st
changeset 14310 b1787ec39fd8
parent 14309 602e61cb64a3
child 16000 74d0c38d1834
child 18011 deb0c3355881
--- a/SortedCollection.st	Fri Aug 10 20:51:43 2012 +0200
+++ b/SortedCollection.st	Fri Aug 10 21:28:52 2012 +0200
@@ -502,7 +502,9 @@
      Notice, that quicksort degenerates to a veeery slow bubble-like
      sort when a collection of already sorted elements is given to it.
      Therefore, adding chunks is better done by sorting the chunk and merging
-     it in, instead of resorting the receiver."
+     it in, instead of resorting the receiver.
+
+     aSortedCollection must be sorted by the same sortBlock as myself!!"
 
     |newContentsArray
      contentsArray2
@@ -516,32 +518,30 @@
      end1Reached end2Reached
      el1 el2 |
 
+    n2 := aSortedCollection size.
+    n2 == 0 ifTrue:[
+        ^ self.
+    ].
+
+    aSortedCollection isSortedCollection ifTrue:[
+        contentsArray2 := aSortedCollection contentsArray.
+        srcIndex2 := aSortedCollection firstIndex.
+    ] ifFalse:[
+        contentsArray2 := aSortedCollection.
+        srcIndex2 := 1.
+    ].
+
     n1 := self size.
-    n2 := aSortedCollection size.
-
     n1 == 0 ifTrue:[
-        ^ aSortedCollection.
-    ].
-    n2 == 0 ifTrue:[
+        firstIndex := srcIndex2.
+        lastIndex := firstIndex + n2 - 1.
+        contentsArray := contentsArray2 copy.
         ^ self.
     ].
 
     newContentsArray := Array new:(n1 + n2).
     destIndex := 1.
 
-    (aSortedCollection isSortedBy:sortBlock) ifTrue:[
-        aSortedCollection isSortedCollection ifTrue:[
-            contentsArray2 := aSortedCollection contentsArray.
-            srcIndex2 := aSortedCollection firstIndex.
-        ] ifFalse:[
-            contentsArray2 := aSortedCollection.
-            srcIndex2 := 1.
-        ]
-    ] ifFalse:[
-        contentsArray2 := aSortedCollection.
-        srcIndex2 := 1.
-    ].
-
     last2 := srcIndex2 + n2 -1.
 
     srcIndex1 := firstIndex.
@@ -753,45 +753,6 @@
 
 !SortedCollection methodsFor:'private'!
 
-indexForInserting:anObject
-    "search the index at which to insert anObject.
-     Can also be used to search for an existing element
-     by checking if the element at the returned index is the one we look for.
-     Uses a binarySearch since we can depend on the elements being in sorted order.
-     The returned index is a physical one, for accessing contentsArray."
-
-    |low    "{ Class: SmallInteger}"
-     high   "{ Class: SmallInteger}"
-     middle "{ Class: SmallInteger}"
-     element|
-
-    "
-     we can of course use a binary search - since the elements are sorted
-    "
-    low := firstIndex.
-    high := lastIndex.
-    [low > high] whileFalse:[
-        middle := (low + high) // 2.
-        element := contentsArray at:middle.
-        (sortBlock value:element value:anObject) ifTrue:[
-            "middleelement is smaller than object"
-            low := middle + 1
-        ] ifFalse:[
-            high := middle - 1
-        ]
-    ].
-    ^ low
-
-    "
-     #(1 2 3 4 7 99 1313 981989 898989898) asSortedCollection indexForInserting:50
-
-     #(1.0 2.0 3 4 7 49.0 51.0 99 1313 981989 898989898) asSortedCollection indexForInserting:50
-
-    "
-
-    "Modified: 12.4.1996 / 13:22:03 / cg"
-!
-
 setSortBlock: aSortBlock
     "set the sortblock without resorting - private only"
 
@@ -873,6 +834,45 @@
     "
 !
 
+indexForInserting:anObject
+    "search the index at which to insert anObject.
+     Can also be used to search for an existing element
+     by checking if the element at the returned index is the one we look for.
+     Uses a binarySearch since we can depend on the elements being in sorted order.
+     The returned index is a physical one, for accessing contentsArray."
+
+    |low    "{ Class: SmallInteger}"
+     high   "{ Class: SmallInteger}"
+     middle "{ Class: SmallInteger}"
+     element|
+
+    "
+     we can of course use a binary search - since the elements are sorted
+    "
+    low := firstIndex.
+    high := lastIndex.
+    [low > high] whileFalse:[
+        middle := (low + high) // 2.
+        element := contentsArray at:middle.
+        (sortBlock value:element value:anObject) ifTrue:[
+            "middleelement is smaller than object"
+            low := middle + 1
+        ] ifFalse:[
+            high := middle - 1
+        ]
+    ].
+    ^ low
+
+    "
+     #(1 2 3 4 7 99 1313 981989 898989898) asSortedCollection indexForInserting:50
+
+     #(1.0 2.0 3 4 7 49.0 51.0 99 1313 981989 898989898) asSortedCollection indexForInserting:50
+
+    "
+
+    "Modified: 12.4.1996 / 13:22:03 / cg"
+!
+
 indexOf:anElement
     "Return the index of anElement within the receiver.
      If the receiver does not contain anElement, return 0."
@@ -990,11 +990,11 @@
 !SortedCollection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.75 2012-08-10 18:51:43 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.76 2012-08-10 19:28:52 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.75 2012-08-10 18:51:43 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SortedCollection.st,v 1.76 2012-08-10 19:28:52 stefan Exp $'
 ! !
 
 SortedCollection initialize!