Module Agrid

Introduction

type +'a t = 'a Flex_array.t Flex_array.t

The type of grids. This is an immutable data structure. Values of type 'a t can be compared using structural equality = (provided the elements of type 'a can).

Using grids

val width : 'a t -> int

width g returns the width of g, that is to say its number of columns.

val height : 'a t -> int

height g returns the height of g, that is to say its number of rows.

val dim : 'a t -> int * int

dim g returns a pair in which the first element is the width of g and the second one its height.

val get : 'a t -> x:int -> y:int -> 'a

get g ~x ~y returns the element at width-index ~x and height-index ~y in grid g. Index start at 0.

val get_row : 'a t -> int -> 'a Flex_array.t

get_row g y returns the row at height-index y in grid g. Index start at 0.

val get_col : 'a t -> int -> 'a Flex_array.t

get_col g x returns the column at width-index x in grid g. Index start at 0.

Building grids

val empty : 'a t

The empty grid.

val of_array : 'a array array -> 'a t

of_array a returns a grid from the two dimensional array a with the same height, width and order.

val of_list : 'a list list -> 'a t

of_list l returns a grid from the two dimensional list l with the same height, width and order.

val set : 'a t -> x:int -> y:int -> 'a -> 'a t

set g ~x ~y v returns a new grid where all elements are identical to those of g, except at width-index x and height-index y where the element is v.

val cons_row : 'a Flex_array.t -> 'a t -> 'a t

cons_row row grid returns a new grid obtained by appending the row row at the height-axis front of grid g.

val snoc_row : 'a t -> 'a Flex_array.t -> 'a t

snow_row grid row returns a new grid obtained by appending the row row at the height-axis end of grid g.

val cons_col : 'a Flex_array.t -> 'a t -> 'a t

cons_col col grid returns a new grid obtained by appending the column col at the width-axis front of grid g.

val snoc_col : 'a t -> 'a Flex_array.t -> 'a t

snow_col grid col returns a new grid obtained by appending the column col at the width-axis end of grid g.

val tail_row : 'a t -> 'a t

tail_row g returns a new grid obtained by removing the row at the height-axis front of grid g.

val liat_row : 'a t -> 'a t

liat_row g returns a new grid obtained by removing the row at the height-axis end of grid g.

val tail_col : 'a t -> 'a t

tail_col g returns a new grid obtained by removing the column at the width-axis front of grid g.

val liat_col : 'a t -> 'a t

liat_col g returns a new grid obtained by removing the column at the width-axis end of grid g.

Iterators

val iter : ('a -> unit) -> 'a t -> unit

iter f g applies function f in turn to all the elements of g. It goes row by row. It is equivalent to f (get g ~x:0 ~y:0); f (get g ~x:1 ~y:0); ...; f (get g ~x:(w - 1) ~y:0); ...; f (get g ~x:0 ~y:(h - 1)); f (get g ~x:1 ~y:(h -1)); ...; f (get g ~x:(w - 1) ~y:(h - 1)) where w and h are respectively the width and the height of g but runs faster.

val iteri : (x:int -> y:int -> 'a -> unit) -> 'a t -> unit

Same as iter, but the function is applied with the width-index and height-index of the element as first and second argument, and the element itself as third argument.

val map : ('a -> 'b) -> 'a t -> 'b t

map f g returns a grid with the same dimension than g but where each element x has been replaced by the value of f x.

val mapi : (x:int -> y:int -> 'a -> 'b) -> 'a t -> 'b t

Same as map, but the function is applied with the width-index and height-index of the element as first and second arguement, and the element itself as third argument.

val fold : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b

fold f acc g computes Flex_array.fold (Flex_array.fold f) acc g.

val pp : ?⁠pp_sep_row:(Stdlib.Format.formatter -> unit -> unit) -> ?⁠pp_sep_col:(Stdlib.Format.formatter -> unit -> unit) -> (Stdlib.Format.formatter -> 'a -> unit) -> Stdlib.Format.formatter -> 'a t -> unit

pp ?pp_sep_row ?pp_sep_col pp_v fmt g prints items of grid g using pp_v to print each item, calling pp_sep_row between rows and pp_sep_col between items (i.e. between each column). pp_sep_row defaults to Format.pp_print_cut and pp_print_col defaults to fun fmt () -> Format.fprintf fmt ";". Does nothing on empty grids.