1181344Sdfr(*
2181344Sdfr    Title:      Standard Basis Library: Mono_Array2 signature and structures.
3208291Suqs    Author:     David Matthews
4181344Sdfr    Copyright   David Matthews 2000
5181344Sdfr
6181344Sdfr    This library is free software; you can redistribute it and/or
7181344Sdfr    modify it under the terms of the GNU Lesser General Public
8181344Sdfr    License as published by the Free Software Foundation; either
9181344Sdfr    version 2.1 of the License, or (at your option) any later version.
10181344Sdfr    
11181344Sdfr    This library is distributed in the hope that it will be useful,
12181344Sdfr    but WITHOUT ANY WARRANTY; without even the implied warranty of
13181344Sdfr    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14181344Sdfr    Lesser General Public License for more details.
15181344Sdfr    
16181344Sdfr    You should have received a copy of the GNU Lesser General Public
17181344Sdfr    License along with this library; if not, write to the Free Software
18181344Sdfr    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19181344Sdfr*)
20181344Sdfr
21181344Sdfr(* G&R 2004 status: no change. *)
22181344Sdfr
23181344Sdfrsignature MONO_ARRAY2 =
24181344Sdfrsig
25181344Sdfr    eqtype array
26181344Sdfr    type elem
27203026Sgavin    type vector
28181344Sdfr    type region = {
29181344Sdfr                 base : array,
30181344Sdfr                 row : int,
31181344Sdfr                 col : int,
32181344Sdfr                 nrows : int option,
33181344Sdfr                 ncols : int option
34181344Sdfr               }
35181344Sdfr    datatype traversal = datatype Array2.traversal
36181344Sdfr    val array : int * int * elem -> array
37181344Sdfr    val fromList : elem list list -> array
38181344Sdfr    val tabulate : traversal -> int * int * (int * int -> elem) -> array
39181344Sdfr    val sub : array * int * int -> elem
40181344Sdfr    val update : array * int * int * elem -> unit
41181344Sdfr    val dimensions : array -> int * int
42181344Sdfr    val nCols      : array -> int
43181344Sdfr    val nRows      : array -> int
44181344Sdfr    val row: array * int -> vector
45181344Sdfr    val column: array * int -> vector
46181344Sdfr    val copy: {
47181344Sdfr                src : region,
48181344Sdfr                dst : array,
49181344Sdfr                dst_row : int,
50181344Sdfr                dst_col : int
51181344Sdfr              } -> unit
52181344Sdfr    val appi: Array2.traversal -> (int * int * elem -> unit) -> region -> unit
53181344Sdfr    val app: Array2.traversal -> (elem -> unit) -> array -> unit
54181344Sdfr    val modifyi: Array2.traversal -> (int * int * elem -> elem) -> region -> unit
55181344Sdfr    val modify: Array2.traversal -> (elem -> elem) -> array -> unit
56181344Sdfr    val foldi: Array2.traversal -> (int * int * elem * 'b -> 'b) -> 'b -> region -> 'b
57181344Sdfr    val fold: Array2.traversal -> (elem * 'b -> 'b) -> 'b -> array -> 'b
58181344Sdfrend;
59181344Sdfr
60181344Sdfr(* All these are simply defined in terms of Array2, at least for the
61181344Sdfr   moment. *)
62181344Sdfrstructure Word8Array2 : MONO_ARRAY2
63181344Sdfr    where type vector = Word8Vector.vector
64181344Sdfr    where type elem = Word8.word =
65236668Sjoelstruct
66181344Sdfr    open Array2
67181344Sdfr    type elem = Word8.word
68181344Sdfr    type vector = Word8Vector.vector
69203097Sschweikh    type elem = Word8.word
70181344Sdfr    type array = elem array
71181344Sdfr    type region = elem region
72181344Sdfr    (* Have to redefine row and column in terms of Word8Vector. *)
73203097Sschweikh    fun row(a, i) =
74181344Sdfr        Word8Vector.tabulate(nCols a, fn j => sub(a, i, j))
75181344Sdfr    and column(a, j) =
76181344Sdfr        Word8Vector.tabulate(nRows a, fn i => sub(a, i, j))
77181344Sdfrend;
78181344Sdfr
79181344Sdfrstructure CharArray2 : MONO_ARRAY2
80181344Sdfr    where type vector = CharVector.vector
81181344Sdfr    where type elem = char =
82181344Sdfrstruct
83181344Sdfr    open Array2
84181344Sdfr    type elem = char
85181344Sdfr    type vector = CharVector.vector
86181344Sdfr    type array = elem array
87181344Sdfr    type region = elem region
88181344Sdfr    fun row(a, i) =
89181344Sdfr        CharVector.tabulate(nCols a, fn j => sub(a, i, j))
90181344Sdfr    and column(a, j) =
91181344Sdfr        CharVector.tabulate(nRows a, fn i => sub(a, i, j))
92181344Sdfrend;
93181344Sdfr
94181344Sdfrstructure BoolArray2 : MONO_ARRAY2
95181344Sdfr    where type vector = BoolVector.vector
96181344Sdfr    where type elem = bool =
97181344Sdfrstruct
98181344Sdfr    open Array2
99181344Sdfr    type elem = bool
100181344Sdfr    type vector = BoolVector.vector
101181344Sdfr    type array = elem array
102181344Sdfr    type region = elem region
103203025Sgavin    fun row(a, i) =
104181344Sdfr        BoolVector.tabulate(nCols a, fn j => sub(a, i, j))
105181344Sdfr    and column(a, j) =
106181344Sdfr        BoolVector.tabulate(nRows a, fn i => sub(a, i, j))
107181344Sdfrend;
108181344Sdfr
109181344Sdfrstructure IntArray2 : MONO_ARRAY2
110181344Sdfr    where type vector = IntVector.vector
111181344Sdfr    where type elem = int =
112181344Sdfrstruct
113181344Sdfr    open Array2
114181344Sdfr    type elem = int
115181344Sdfr    type vector = IntVector.vector
116    type array = elem array
117    type region = elem region
118    fun row(a, i) =
119        IntVector.tabulate(nCols a, fn j => sub(a, i, j))
120    and column(a, j) =
121        IntVector.tabulate(nRows a, fn i => sub(a, i, j))
122end;
123
124structure RealArray2 : MONO_ARRAY2
125    where type vector = RealVector.vector
126    where type elem = real =
127struct
128    open Array2
129    type elem = real
130    type vector = RealVector.vector
131    type array = elem array
132    type region = elem region
133    fun row(a, i) =
134        RealVector.tabulate(nCols a, fn j => sub(a, i, j))
135    and column(a, j) =
136        RealVector.tabulate(nRows a, fn i => sub(a, i, j))
137end;
138