package grid

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module GridSource

Grids aka two-dimensional arrays

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).

Sourcetype 'a t = 'a array array
Sourcetype position = int * int

A position is an ordered pair (i,j), where i is the row and j is the column. Rows and columns are 0-based.

Sourceval height : 'a t -> int

Returns the number of rows.

Sourceval width : 'a t -> int

Returns the number of columns.

Sourceval size : 'a t -> int * int

both height and width, in that order

Sourceval 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.

Sourceval init : int -> int -> (position -> 'a) -> 'a t

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.

Sourceval get : 'a t -> position -> 'a

get g p returns the value at position p. Raises Invalid_argument if the position is out of bounds.

Sourceval set : 'a t -> position -> 'a -> unit

set g p v sets the value at position p, with v. Raises Invalid_argument if the position is out of bounds.

Sourceval inside : 'a t -> position -> bool

inside g p indicates whether position p is a legal position in g

Sourceval north : position -> position

the position above in the grid

Sourceval north_west : position -> position

the position above left in the grid

Sourceval west : position -> position

the position to the left in the grid

Sourceval south_west : position -> position

the position below left in the grid

Sourceval south : position -> position

the position below in the grid

Sourceval south_east : position -> position

the position below right in the grid

Sourceval east : position -> position

the position to the right in the grid

Sourceval north_east : position -> position

the position above right in the grid

Sourcetype direction =
  1. | N
  2. | NW
  3. | W
  4. | SW
  5. | S
  6. | SE
  7. | E
  8. | NE
Sourceval rotate_left : 'a t -> 'a t

rotate_left g returns a new grid that is the left rotation of g

Sourceval rotate_right : 'a t -> 'a t

rotate_right g returns a new grid that is the right rotation of g

Sourceval map : (position -> 'a -> 'b) -> 'a t -> 'b t
Sourceval iter4 : (position -> 'a -> unit) -> 'a t -> position -> unit

iter4 f g p applies function f on the four neightbors of position p (provided they exist)

Sourceval iter8 : (position -> 'a -> unit) -> 'a t -> position -> unit

iter4 f g p applies function f on the eight neightbors of position p (provided they exist)

Sourceval iter : (position -> 'a -> unit) -> 'a t -> unit

iter f g applies function f at each position of the grid g

Sourceval fold : (position -> 'a -> 'acc -> 'acc) -> 'a t -> 'acc -> 'acc

fold f g folds function f over each position of the grid g

Sourceval print : ?bol:(Format.formatter -> int -> unit) -> ?eol:(Format.formatter -> int -> unit) -> ?sep:(Format.formatter -> position -> unit) -> (Format.formatter -> position -> 'a -> unit) -> 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.

Sourceval print_chars : Format.formatter -> char t -> unit

prints a grid of characters using Format.pp_print_char

Sourceval read : 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.

OCaml

Innovation. Community. Security.