Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / array2.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 1999-2006, 2008 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8structure Array2: ARRAY2 =
9struct
10
11open Array2
12
13type 'a t = 'a array
14
15fun toList a =
16 let
17 fun loop (r, ac) =
18 if r < 0
19 then ac
20 else loop (r - 1,
21 let
22 fun loop (c, ac) =
23 if c < 0
24 then ac
25 else loop (c - 1, sub (a, r, c) :: ac)
26 in loop (nCols a - 1, [])
27 end :: ac)
28 in loop (nRows a - 1, [])
29 end
30
31fun layout f a = List.layout (List.layout f) (toList a)
32
33fun wholeRegion a : 'a region =
34 {base = a, row = 0, col = 0, nrows = NONE, ncols = NONE}
35
36fun foralli (a, f) =
37 let exception False
38 in (appi RowMajor (fn (r, c, x) =>
39 if f (r, c, x)
40 then ()
41 else raise False)
42 (wholeRegion a)
43 ; true)
44 handle False => false
45 end
46
47fun equals (a, a', f) =
48 nRows a = nRows a'
49 andalso nCols a = nCols a'
50 andalso foralli (a, fn (r, c, x) => f (x, sub (a', r, c)))
51
52fun forall (a, f) = foralli (a, f o #3)
53
54fun tabulate (r, c, f) = Pervasive.Array2.tabulate RowMajor (r, c, f)
55
56fun foreachi (a, f) =
57 foldi RowMajor (fn (r, c, a, ()) => f (r, c, a)) () (wholeRegion a)
58
59fun foreach (a, f) = foreachi (a, f o #3)
60
61fun copy a = tabulate (nRows a, nCols a, fn (r, c) => sub (a, r, c))
62
63fun new (r, c, x) = tabulate (r, c, fn _ => x)
64
65end