added #copyFrom:to:
authorClaus Gittinger <cg@exept.de>
Sat, 18 May 1996 19:29:02 +0200
changeset 352 e6902ee52702
parent 351 c7c0c33286b8
child 353 d1b7f731a331
added #copyFrom:to:
RunArray.st
--- a/RunArray.st	Sat May 18 17:49:46 1996 +0200
+++ b/RunArray.st	Sat May 18 19:29:02 1996 +0200
@@ -219,8 +219,8 @@
 
 !RunArray methodsFor:'accessing'!
 
-at:anInteger 
-    "Answer the element at index anInteger. 
+at:index 
+    "Answer the element at index. 
      at: is used by a knowledgeable client to access an existing element 
      This is a pretty dumb thing to do to a runArray and it is 
      not at all efficient (think of that as a discouragement)."
@@ -228,22 +228,22 @@
     |position "{ Class: SmallInteger }"
      nRuns    "{ Class: SmallInteger }"|
 
-    (anInteger > 0) ifTrue:[
+    (index > 0) ifTrue:[
         position := 1.
         nRuns := contentsArray size.
         1 to:nRuns by:2 do:[:runIndex |
             |runLen|
 
             runLen := contentsArray at:runIndex.
-            anInteger >= position ifTrue:[
-                anInteger < (position + runLen) ifTrue:[
+            index >= position ifTrue:[
+                index < (position + runLen) ifTrue:[
                     ^ contentsArray at:(runIndex + 1)
                 ].
             ].
             position := position + runLen
         ]
     ].
-    ^ self errorInvalidKey:anInteger
+    ^ self subscriptBoundsError:index
 
     "
      |c|
@@ -262,7 +262,7 @@
      c at:10.  
     "
 
-    "Modified: 11.5.1996 / 13:33:50 / cg"
+    "Modified: 18.5.1996 / 14:54:52 / cg"
 !
 
 at:index put:anObject 
@@ -297,7 +297,7 @@
         ]
     ].
     runIndex isNil ifTrue:[
-        ^ self errorInvalidKey:index
+        ^ self subscriptBoundsError:index
     ].
 
     val := contentsArray at:(runIndex + 1).
@@ -504,7 +504,7 @@
      Transcript showCR:c.   
     "
 
-    "Modified: 12.5.1996 / 15:55:32 / cg"
+    "Modified: 18.5.1996 / 19:26:43 / cg"
 !
 
 size
@@ -675,6 +675,63 @@
     "Modified: 14.5.1996 / 14:59:12 / cg"
 ! !
 
+!RunArray methodsFor:'copying'!
+
+copyFrom:start to:stop
+    "return a new collection, containing the elements from start to stop"
+
+    |runStart runNext newRuns idx copying|
+
+    (contentsArray notNil 
+    and:[stop >= start]) ifTrue:[
+        newRuns := self species new.
+        runStart := 1.
+        idx := start.
+        copying := false.
+        contentsArray pairWiseDo:[:len :val | 
+            runNext := runStart + len.
+        
+            copying ifFalse:[
+                idx >= runStart ifTrue:[
+                    copying := true
+                ]
+            ].
+            copying ifTrue:[
+                idx < runNext ifTrue:[
+                    "/ found the first run
+
+                    stop < runNext ifTrue:[
+                        newRuns add:val withOccurrences:(stop-idx+1).
+                        ^ newRuns
+                    ].
+
+                    newRuns add:val withOccurrences:(runNext-idx).
+                    idx := runNext.
+                ].
+            ].
+            runStart := runNext.
+        ]
+    ].
+
+    ^ super copyFrom:start to:stop  "/ for the error report
+
+    "
+     |r|
+     r := RunArray withAll:#(1 2 3 3 3 3 4 4 4 5 6 7 7 7 7 7 7 7).
+     r copyFrom:1 to:10  
+
+     |r|
+     r := RunArray withAll:#(1 2 3 3 3 3 3 3 4 5 6 7 7 7 7 7 7 7).
+     r copyFrom:4 to:10       
+
+     |r|
+     r := RunArray withAll:#(1 2 3 3 3 3 3 3 4 5 6 7 7 7 7 7 7 7).
+     r copyFrom:1 to:20      
+    "
+
+    "Modified: 18.5.1996 / 19:28:47 / cg"
+! !
+
 !RunArray methodsFor:'enumerating'!
 
 do:aBlock 
@@ -875,5 +932,5 @@
 !RunArray class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/RunArray.st,v 1.10 1996-05-18 15:32:47 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/RunArray.st,v 1.11 1996-05-18 17:29:02 cg Exp $'
 ! !