RunArray.st
changeset 892 90a4022c52a1
parent 871 b44445dc4ac6
child 895 799219e60ad6
--- a/RunArray.st	Sat Jul 15 16:41:47 2000 +0200
+++ b/RunArray.st	Mon Jul 17 12:47:31 2000 +0200
@@ -27,6 +27,8 @@
 
 
 
+"{ Package: 'stx:libbasic2' }"
+
 SequenceableCollection subclass:#RunArray
 	instanceVariableNames:'contentsArray'
 	classVariableNames:''
@@ -682,6 +684,103 @@
     "Modified: 11.5.1996 / 13:34:53 / cg"
 ! !
 
+!RunArray methodsFor:'comparing'!
+
+= aCollection
+    "return true, if the argument contains the same elements as the receiver.
+     Optimized, especially for the common case, that collection is another runArray"
+
+    |otherContents idx1 idx2 runCount1 runValue1 runCount2 runValue2|
+
+    aCollection class == self class ifFalse:[
+        aCollection isSequenceable ifTrue:[
+            idx1 := 1.
+            self do:[:element |
+                idx1 > aCollection size ifTrue:[^ false].
+                element = (aCollection at:idx1) ifFalse:[^ false].
+                idx1 := idx1+1.
+            ].
+            idx1 > aCollection size ifFalse:[^ false].
+            ^ true.
+        ].
+
+        idx1 := 1.
+        runCount1 := 0.
+
+        aCollection do:[:element |
+            runCount1 == 0 ifTrue:[
+                idx1+1 > contentsArray size ifTrue:[^ false].
+
+                runCount1 := contentsArray at:idx1.
+                runValue1 := contentsArray at:idx1+1.
+                idx1 := idx1+2.
+            ].
+            runValue1 = element ifFalse:[^ false].
+            runCount1 := runCount1 - 1.
+        ].
+        runCount1 ~~ 0 ifTrue:[^ false].
+        idx1+1 > contentsArray size ifFalse:[^ false].
+        ^ true
+    ].
+    otherContents := aCollection getContentsArray.
+    otherContents = contentsArray ifTrue:[^ true].
+
+    idx1 := 1.
+    runCount1 := contentsArray at:idx1.
+    runValue1 := contentsArray at:idx1+1.
+    idx1 := idx1+2.
+
+    idx2 := 1.
+    runCount2 := otherContents at:idx2.
+    runValue2 := otherContents at:idx2+1.
+    idx2 := idx2+2.
+
+    [true] whileTrue:[
+        runValue1 = runValue2 ifFalse:[^ false].
+        runCount1 == runCount2 ifTrue:[
+            idx1 := idx1 + 2.
+            idx2 := idx2 + 2.
+            idx1 > contentsArray size ifTrue:[
+                idx2 > otherContents size ifTrue:[^ true].
+                ^ false.
+            ] ifFalse:[
+                idx2 > otherContents size ifTrue:[^ false].
+            ].
+        ] ifFalse:[
+            runCount2 > runCount1 ifTrue:[
+                runCount2 := runCount2 - runCount1.
+                idx1+1 > contentsArray size ifTrue:[^ false].
+                runCount1 := contentsArray at:idx1.
+                runValue1 := contentsArray at:idx1+1.
+                idx1 := idx1 + 2.
+            ] ifFalse:[
+                runCount1 := runCount1 - runCount2.
+                idx2+1 > otherContents size ifTrue:[^ false].
+                runCount2 := otherContents at:idx2.
+                runValue2 := otherContents at:idx2+1.
+                idx2 := idx2 + 2.
+            ]
+        ]
+    ].
+
+    "
+     'hello' asText sameStringAndEmphasisAs: 'hello' asText
+     'hello' asText sameStringAndEmphasisAs: 'hello' asText allBold
+     'hello' asText allBold sameStringAndEmphasisAs: 'hello' asText allBold
+     'hello1' asText allBold sameStringAndEmphasisAs: 'hello' asText allBold  
+     'hello' asText allBold sameStringAndEmphasisAs: 'hello1' asText allBold  
+     ('hello' asText allBold , ' ') sameStringAndEmphasisAs: 'hello ' asText allBold  
+     ('hello ' asText allBold) sameStringAndEmphasisAs: ('hello' asText allBold , ' ')  
+     ('hello' asText allBold , ' ') sameStringAndEmphasisAs: ('hello' asText allBold , ' ')  
+     'hello' asRunArray = 'hello' asRunArray 
+     'hello1' asRunArray = 'hello' asRunArray 
+     'hello' asRunArray = 'hello1' asRunArray 
+     'hello' asRunArray = 'hello' asArray      
+     'hello1' asRunArray = 'hello' asArray     
+     'hello' asRunArray = 'hello1' asArray     
+    "
+! !
+
 !RunArray methodsFor:'converting'!
 
 asOrderedCollection
@@ -875,6 +974,10 @@
     "Modified: / 30.10.1997 / 15:49:54 / cg"
 !
 
+getContentsArray
+    ^ contentsArray
+!
+
 isEmpty
     "Am I empty or not. Returns a boolean"
 
@@ -1145,5 +1248,5 @@
 !RunArray class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/RunArray.st,v 1.15 2000-01-22 19:29:13 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/RunArray.st,v 1.16 2000-07-17 10:47:10 cg Exp $'
 ! !