| 1 | module ArrayStuff where |
| 2 | import Data.Ix |
| 3 | import Data.Array.IArray |
| 4 | |
| 5 | funcArray lohi f = listArray lohi $ map f $ range lohi |
| 6 | |
| 7 | constArray lohi v = listArray lohi $ repeat v |
| 8 | |
| 9 | transposeArray arr = |
| 10 | let swap (x, y) = (y, x) in |
| 11 | let (lo, hi) = bounds arr in |
| 12 | ixmap (swap lo, swap hi) swap arr |
| 13 | |
| 14 | array2DtoListOfLists arr = |
| 15 | let ((xlo, ylo), (xhi, yhi)) = bounds arr in |
| 16 | map (\x -> map (\y -> arr ! (x, y)) $ range (ylo, yhi)) $ range (xlo, xhi) |
| 17 | |
| 18 | listOfListsToArray2D ll = |
| 19 | listArray ((0, 0), (length ll - 1, length (head ll) - 1)) $ concat ll |
| 20 | |
| 21 | -- Use instead of amap when the array implementation needs to change. |
| 22 | -- E.g., mapping an unboxed array to an array whose elements must be boxed. |
| 23 | amap2 f arr = funcArray (bounds arr) (\i -> f (arr ! i)) |
| 24 | |
| 25 | -- Like amap2 but the mapping function is also passed the index. |
| 26 | aixmap f arr = funcArray (bounds arr) (\i -> f i (arr ! i)) |