The random instance generator and other improvements.
[match/match.git] / program / ArrayStuff.hs
diff --git a/program/ArrayStuff.hs b/program/ArrayStuff.hs
new file mode 100644 (file)
index 0000000..abf14c7
--- /dev/null
@@ -0,0 +1,18 @@
+module ArrayStuff where
+import Data.Ix
+import Data.Array.IArray
+
+funcArray lohi f = listArray lohi $ map f $ range lohi
+
+transposeArray arr =
+       let swap (x, y) = (y, x) in
+       let (lo, hi) = bounds arr in
+       ixmap (swap lo, swap hi) swap arr
+
+array2DtoListOfLists arr =
+       let ((xlo, ylo), (xhi, yhi)) = bounds arr in
+       map (\x -> map (\y -> arr ! (x, y)) $ range (ylo, yhi)) $ range (xlo, xhi)
+
+-- Use instead of amap when the array implementation needs to change.
+-- E.g., mapping an unboxed array to an array whose elements must be boxed.
+amap2 f arr = funcArray (bounds arr) (\i -> f (arr ! i))