changeset 276:39b5f3fed12d

Make slice return partial slices when range extents overlap vector ends
author Chris Cannam
date Sat, 25 May 2013 18:31:10 +0100
parents 2c3faf6a2820
children 678477bf617c
files yetilab/vector.yeti yetilab/vector/test/test_vector.yeti
diffstat 2 files changed, 15 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/yetilab/vector.yeti	Sat May 25 18:18:10 2013 +0100
+++ b/yetilab/vector.yeti	Sat May 25 18:31:10 2013 +0100
@@ -72,7 +72,15 @@
 
 //!!! doc note: argument order chosen for consistency with std module function
 slice v start end is ~double[] -> number -> number -> ~double[] =
-    Arrays#copyOfRange(v, start, end);
+    if start < 0 then slice v 0 end
+    elif start > length' v then slice v (length' v) end
+    else
+        if end < start then slice v start start
+        elif end > length' v then slice v start (length' v)
+        else
+            Arrays#copyOfRange(v, start, end);
+        fi
+    fi;
 
 resizedTo n v is number -> ~double[] -> ~double[] =
     Arrays#copyOf(v, n);
--- a/yetilab/vector/test/test_vector.yeti	Sat May 25 18:18:10 2013 +0100
+++ b/yetilab/vector/test/test_vector.yeti	Sat May 25 18:31:10 2013 +0100
@@ -78,9 +78,12 @@
 
 "slice": \(
     v = vec.fromList [1,2,3,4];
-    vec.equal (vec.slice v 0 4) v and (
-        vec.equal (vec.slice v 2 4) (vec.fromList [3,4])
-    )
+    vec.equal (vec.slice v 0 4) v and
+        vec.equal (vec.slice v 2 4) (vec.fromList [3,4]) and
+        vec.equal (vec.slice v (-1) 2) (vec.fromList [1,2]) and
+        vec.equal (vec.slice v 3 5) (vec.fromList [4]) and
+        vec.equal (vec.slice v 5 7) (vec.fromList []) and
+        vec.equal (vec.slice v 3 2) (vec.fromList [])
 ),
 
 "resizedTo": \(