Library
Module
Module type
Parameter
Class
Class type
Following conventions in mathematics, grids are considered rows first. Thus in the following, height
refers to the first dimension and width
to the second dimension.
0 1 j width-1 +---+---+---+---+---+--+ 0 | | | | | | | +---+---+---+---+---+--+ 1 | | | | | | | +---+---+---+---+---+--+ i | | | | X | | | +---+---+---+---+---+--+ height-1 | | | | | | | +---+---+---+---+---+--+
Following OCaml conventions, indices are 0-based.
For the width to be well-defined, some of the following functions assume a positive number of rows (and, to be consistent, a positive number of columns).
A position is an ordered pair (i,j)
, where i
is the row and j
is the column. Rows and columns are 0-based.
val height : 'a t -> int
Returns the number of rows.
val width : 'a t -> int
Returns the number of columns.
val size : 'a t -> int * int
both height and width, in that order
val make : int -> int -> 'a -> 'a t
make h w v
returns a new grid with height h
and width w
, where all values are equal to v
. Raises Invalid_argument
if h<1
or w<1
.
For h>=1
and w>=1
, this is equivalent to Array.make_matrix h w v
.
init h w f
returns a new grid with height h
and width w
, where the value at position p
is f p
. Raises Invalid_argument
if h<1
or w<1
.
get g p
returns the value at position p
. Raises Invalid_argument
if the position is out of bounds.
set g p v
sets the value at position p
, with v
. Raises Invalid_argument
if the position is out of bounds.
inside g p
indicates whether position p
is a legal position in g
map f g
returns a fresh grid, with the size of the grid g
and where the value at position p
is given by f p (get g p)
The following functions that iterate or fold over the neighbors of p
all begin by calling f
on the cell north of p
and rotate clockwise.
iter4 f g p
applies function f
on the four neighbors of position p
(provided they exist)
iter8 f g p
applies function f
on the eight neighbors of position p
(provided they exist)
fold4 f g p
folds function f
on the four neighbors of position p
(provided they exist)
fold8 f g p
folds function f
on the eight neighbors of position p
(provided they exist)
iter
and fold
both begin at the top left of the grid and move left to right in each row from top to bottom.
iter f g
applies function f
at each position of the grid g
fold f g
folds function f
over each position of the grid g
find f g
returns a position in g
where f
holds, or raises Not_found
if there is none
val print :
?bol:(Stdlib.Format.formatter -> int -> unit) ->
?eol:(Stdlib.Format.formatter -> int -> unit) ->
?sep:(Stdlib.Format.formatter -> position -> unit) ->
(Stdlib.Format.formatter -> position -> 'a -> unit) ->
Stdlib.Format.formatter ->
'a t ->
unit
print pp fmt g
prints the grid g
on formatter fmt
, using function pp
to print each element.
Function bol
is called at the beginning of each line, and is passed the line number. The default function does nothing.
Function eol
is called at the end of each line, and is passed the line number. The default function calls Format.pp_print_newline
.
Function sep
is printed between two consecutive element on a given row (and is passed the position of the left one). The default function does nothing.
val print_chars : Stdlib.Format.formatter -> char t -> unit
prints a grid of characters using Format.pp_print_char
val read : Stdlib.in_channel -> char t
read c
reads a grid of characters from the input channel c
. Raises Invalid_argument
if the lines do not have the same length, or there is no line at all.