Commit | Line | Data |
---|---|---|
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 | ||
8 | structure Array2: ARRAY2 = | |
9 | struct | |
10 | ||
11 | open Array2 | |
12 | ||
13 | type 'a t = 'a array | |
14 | ||
15 | fun 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 | ||
31 | fun layout f a = List.layout (List.layout f) (toList a) | |
32 | ||
33 | fun wholeRegion a : 'a region = | |
34 | {base = a, row = 0, col = 0, nrows = NONE, ncols = NONE} | |
35 | ||
36 | fun 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 | ||
47 | fun 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 | ||
52 | fun forall (a, f) = foralli (a, f o #3) | |
53 | ||
54 | fun tabulate (r, c, f) = Pervasive.Array2.tabulate RowMajor (r, c, f) | |
55 | ||
56 | fun foreachi (a, f) = | |
57 | foldi RowMajor (fn (r, c, a, ()) => f (r, c, a)) () (wholeRegion a) | |
58 | ||
59 | fun foreach (a, f) = foreachi (a, f o #3) | |
60 | ||
61 | fun copy a = tabulate (nRows a, nCols a, fn (r, c) => sub (a, r, c)) | |
62 | ||
63 | fun new (r, c, x) = tabulate (r, c, fn _ => x) | |
64 | ||
65 | end |