aboutsummaryrefslogtreecommitdiffhomepage
path: root/libslang/slsh/lib/arrayfuns.sl
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2011-10-14 04:55:05 +0200
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2011-10-14 04:55:05 +0200
commit6aa0e0017d7d0cddc006da885946934b06949a91 (patch)
tree66b688ec32e2f91266db760b1762f2a50cc52036 /libslang/slsh/lib/arrayfuns.sl
parenta966db5b71328f6adf9dd767e64b322a3bd7ed9c (diff)
downloaderlang-slang-fork-6aa0e0017d7d0cddc006da885946934b06949a91.tar.gz
include libslang-1.4.9 and automatically build it and link erlang-slang against it
few (erlang) people will still have libslang-1.4.9 installed or spend time to get it to link against the driver
Diffstat (limited to 'libslang/slsh/lib/arrayfuns.sl')
-rw-r--r--libslang/slsh/lib/arrayfuns.sl50
1 files changed, 50 insertions, 0 deletions
diff --git a/libslang/slsh/lib/arrayfuns.sl b/libslang/slsh/lib/arrayfuns.sl
new file mode 100644
index 0000000..17d9a49
--- /dev/null
+++ b/libslang/slsh/lib/arrayfuns.sl
@@ -0,0 +1,50 @@
+%!%+
+%\function{reverse}
+%\synopsis{Reverse the elements of a 1-d array}
+%\usage{Array_Type reverse (Array_Type A)}
+%\description
+% The \slfun{reverse} function reverses the elements of a 1-d array and
+% returns the result.
+%\seealso{shift}
+%!%-
+public define reverse (a)
+{
+ variable i = length (a);
+ if (i <= 1)
+ return a;
+
+ i--;
+ __tmp(a)[[i:0:-1]];
+}
+
+
+%!%+
+%\function{shift}
+%\synopsis{Shift the elements of a 1-d array}
+%\usage{Array_Type shift (Array_Type A, Int_Type n)}
+%\description
+% The \slfun{shift} function shifts the elements of an array by a specified amount
+% and returns the result. If \exmp{n} is positive, the ith element of the array
+% will be shifted to the position \exmp{i-n} of the array. Elements for
+% which \exmp{i-n} is less than 0 will be moved to the end of the array.
+%\example
+%#v+
+% A = [1,2,3,4,5,6,7,8,9];
+% B = shift (A, 3); % ==> B = [4,5,6,7,8,9,1,2,3];
+% C = shift (A, -1); % ==> C = [9,1,2,3,4,5,6,7,8];
+%#v-
+%\notes
+% It many ways \exmp{rotate} would be a better name for this function.
+%\seealso{reverse, transpose}
+%!%-
+public define shift (x, n)
+{
+ variable len = length(x);
+ variable i = [0:len-1];
+
+ % allow n to be negative and large
+ n = len + n mod len;
+ return x[(i + n)mod len];
+}
+
+provide ("arrayfuns");