SequenceableCollection.st
branchjv
changeset 18107 d46c13a0795b
parent 18105 3a3a3e0ac47f
parent 15813 e025be120fff
child 18120 e3a375d5f6a8
--- a/SequenceableCollection.st	Wed Nov 20 15:12:13 2013 +0000
+++ b/SequenceableCollection.st	Mon Nov 25 10:37:44 2013 +0000
@@ -1960,6 +1960,33 @@
     "
 !
 
+compareWith:aSequenceableCollection
+    "Compare the receiver with the argument and return 1 if the receiver is
+     greater, 0 if equal and -1 if less than the argument."
+
+    |mySize    "{ Class: SmallInteger }"
+     otherSize "{ Class: SmallInteger }"
+     n         "{ Class: SmallInteger }"
+     e1 e2|
+
+    mySize := self size.
+    otherSize := aSequenceableCollection size.
+    n := mySize min:otherSize.
+
+    1 to:n do:[:index |
+        e1 := self at:index.
+        e2 := aSequenceableCollection at:index.
+        e1 ~~ e2 ifTrue:[
+            "identity compare is faster"    
+            e1 > e2 ifTrue:[^ 1].
+            e1 < e2 ifTrue:[^ -1].
+        ].
+    ].
+    mySize > otherSize ifTrue:[^ 1].
+    mySize < otherSize ifTrue:[^ -1].
+    ^ 0
+!
+
 deepSameContentsAs:aCollection
     "return true, if the receiver and the arg have the same contents
      in both the named instance vars and any indexed instVars.
@@ -2079,45 +2106,6 @@
     "Modified: / 27.3.1998 / 17:33:49 / cg"
 !
 
-identicalContentsAs:aCollection
-    "return true if the receiver and aCollection represent collections
-     with identical contents. This is much like #=, but compares
-     elements using #== instead of #=."
-
-    ^ self
-	sameContentsAs:aCollection
-	whenComparedWith:[:a :b | a == b]
-
-"/    |index "{ Class: SmallInteger }"
-"/     stop  "{ Class: SmallInteger }" |
-"/
-"/    (aCollection == self) ifTrue:[^ true].
-"/    (aCollection size == self size) ifFalse:[^ false].
-"/    (aCollection isSequenceable) ifFalse:[^ aCollection identicalContentsAs:self].
-"/
-"/    stop := self size.
-"/    stop == (aCollection size) ifFalse:[^ false].
-"/
-"/    index := 1.
-"/    [index <= stop] whileTrue:[
-"/        (self at:index) == (aCollection at:index) ifFalse:[^ false].
-"/        index := index + 1
-"/    ].
-"/    ^ true
-
-    "
-     #(1 2 3 4 5) = #(1 2 3 4 5)
-     #(1 2 3 4 5) = #(1.0 2 3 4.0 5)
-     #($1 $2 $3 $4 $5) = '12345'
-
-     #(1 2 3 4 5) identicalContentsAs:#(1 2 3 4 5)
-     #(1 2 3 4 5) identicalContentsAs: #(1.0 2 3 4.0 5)
-     #($1 $2 $3 $4 $5) identicalContentsAs: '12345'
-    "
-
-    "Modified: / 31.10.2001 / 11:30:18 / cg"
-!
-
 isSameSequenceAs:otherCollection
     "Answer whether the receiver's size is the same as otherCollection's size,
      and each of the receiver's elements equal the corresponding element of otherCollection.
@@ -2125,6 +2113,7 @@
 
     | size |
 
+    (otherCollection == self) ifTrue:[^ true].
     (size := self size) = otherCollection size ifFalse: [^ false].
     otherCollection isSequenceable ifFalse: [^ false].
 
@@ -2140,29 +2129,24 @@
      Redefinded, so that SequenceableCollections are equivalent, 
      especially OrderedCollections with unused space"
 
-    |index "{ Class: SmallInteger }"
-     stop  "{ Class: SmallInteger }" |
+    |stop  "{ Class: SmallInteger }" |
 
     (aCollection == self) ifTrue:[^true].
     (aCollection isSequenceable) ifFalse:[
-        aCollection with:self do:[:e1 :e2 |
-            (e1 = e2) ifFalse:[^false].
-        ].
-        ^ true
+        ^ aCollection sameContentsAs:self.
     ].
 
     stop := self size.
     stop == (aCollection size) ifFalse:[^false].
 
-    index := 1.
-    [index <= stop] whileTrue:[
-        ((self at:index) = (aCollection at:index)) ifFalse:[^false].
-        index := index + 1
+    1 to:stop do:[:index|
+        ((self at:index) = (aCollection at:index)) ifFalse:[^ false].
     ].
     ^ true
 
     "
      #(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) copy
+     #(1 2 3 4 5) asSet sameContentsAs: #(1 2 3 4 5) copy
      #($1 $2 $3 $4 $5) sameContentsAs: #(1 2 3 4 5) 
      #($1 $2 $3 $4 $5) sameContentsAs: '12345'      
      #($1 $2 $3 $4 $5) sameContentsAs: '54321' asSortedCollection 
@@ -2179,27 +2163,25 @@
      and #identicalContentsAs: is the same as #sameContentsAs:whenComparedUsing:#==.
     "
 
-    |index "{ Class: SmallInteger }"
-     stop  "{ Class: SmallInteger }" |
+    |stop  "{ Class: SmallInteger }" |
 
     (aCollection == self) ifTrue:[^ true].
     (aCollection size == self size) ifFalse:[^ false].
     (aCollection isSequenceable) ifFalse:[
-	^ aCollection sameContentsAs:self whenComparedWith:compareBlock
+        ^ aCollection sameContentsAs:self whenComparedWith:compareBlock
     ].
 
     stop := self size.
     stop == (aCollection size) ifFalse:[^ false].
 
-    index := 1.
-    [index <= stop] whileTrue:[
-	(compareBlock value:(self at:index) value:(aCollection at:index)) ifFalse:[^ false].
-	index := index + 1
+    1 to:stop do:[:index|
+        (compareBlock value:(self at:index) value:(aCollection at:index)) ifFalse:[^ false].
     ].
     ^ true
 
     "
      #(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5)     whenComparedWith:[:a :b | a = b]
+     #(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5) asSet whenComparedWith:[:a :b | a = b]
      #(1 2 3 4 5) sameContentsAs: #(1 2 3 4 5)     whenComparedWith:[:a :b | a == b]
      #(1 2 3 4 5) sameContentsAs: #(1.0 2 3 4.0 5) whenComparedWith:[:a :b | a = b]
      #(1 2 3 4 5) sameContentsAs: #(1.0 2 3 4.0 5) whenComparedWith:[:a :b | a == b]
@@ -7211,11 +7193,13 @@
      End the search at endIndex or when an element is found.
      Return its index or 0 if none detected."
 
-    |start "{ Class: SmallInteger }"|
+    |start "{ Class: SmallInteger }" 
+     end "{ Class: SmallInteger }"|
 
     start := startIndex.
-    start to:endIndex by:-1 do:[:index |
-	(aBlock value:(self at:index)) ifTrue:[^ index].
+    end := endIndex.
+    start to:end by:-1 do:[:index |
+        (aBlock value:(self at:index)) ifTrue:[^ index].
     ].
     ^ 0
 
@@ -9233,11 +9217,11 @@
 !SequenceableCollection class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.353 2013-10-25 12:21:01 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.355 2013-11-14 15:32:31 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.353 2013-10-25 12:21:01 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.355 2013-11-14 15:32:31 stefan Exp $'
 ! !