Skip to Content
ReferenceLibrary

grid

The grid module.

Generated from spec/std/grid.cp by cpl doc --format json.

Dir4

pub struct Dir4(k: Int)

One of the four directions up, right, down, left (clockwise, rows growing downwards).

Source

Dir4.up

fn up() -> Dir4

Up: one row towards row 0, delta (-1, 0).

  • cost: O(1)
  • traps: never

Examples:

  • Dir4.up().delta == (-1, 0)

Source

Dir4.right

fn right() -> Dir4

Right: one column higher, delta (0, 1).

  • cost: O(1)
  • traps: never

Examples:

  • Dir4.right().delta == (0, 1)

Source

Dir4.down

fn down() -> Dir4

Down: one row higher (rows grow downwards), delta (1, 0).

  • cost: O(1)
  • traps: never

Examples:

  • Dir4.down().delta == (1, 0)

Source

Dir4.left

fn left() -> Dir4

Left: one column lower, delta (0, -1).

  • cost: O(1)
  • traps: never

Examples:

  • Dir4.left().delta == (0, -1)

Source

Dir4.all

fn all() -> List<Dir4>

All four directions, clockwise from up. The list is fresh each call, but has a fixed length of four, so the cost is constant.

  • cost: O(1)
  • order: up, right, down, left
  • traps: never

Examples:

  • Dir4.all() == [Dir4.up(), Dir4.right(), Dir4.down(), Dir4.left()]

Source

Dir4.from

fn from(letters: Str, ch: Char) -> ?Dir4

The direction a letter stands for, given the letters for up, right, down and left in that

Matching is exact (case-sensitive). If a letter appears more than once in letters, the first position wins.

  • order: Dir4.from("URDL", ch), Dir4.from("NESW", ch). None for another letter.
  • cost: O(1)
  • ties: a repeated letter maps to its first position in letters
  • traps: letters.len != 4

Examples:

  • Dir4.from("URDL", 'D') == Some(Dir4.down())
  • Dir4.from("NESW", 'W') == Some(Dir4.left())
  • Dir4.from("URDL", 'x') == None

Source

Dir4.cw

fn cw(self) -> Dir4

A quarter turn clockwise: up to right, right to down, down to left, left to up.

  • cost: O(1)
  • traps: never

Examples:

  • Dir4.up().cw == Dir4.right()
  • Dir4.left().cw == Dir4.up()

Source

Dir4.ccw

fn ccw(self) -> Dir4

A quarter turn counter-clockwise, the inverse of cw.

  • cost: O(1)
  • traps: never

Examples:

  • Dir4.up().ccw == Dir4.left()
  • Dir4.down().cw.ccw == Dir4.down()

Source

Dir4.back

fn back(self) -> Dir4

The opposite direction, a half turn: its delta is the negated delta.

  • cost: O(1)
  • traps: never

Examples:

  • Dir4.up().back == Dir4.down()
  • Dir4.right().back == Dir4.left()

Source

Dir4.delta

fn delta(self) -> (Int, Int)

The change in (row, column) of one step.

  • cost: O(1)
  • traps: never

Examples:

  • Dir4.all().map(d => d.delta) == [(-1, 0), (0, 1), (1, 0), (0, -1)]

Source

Dir8

pub struct Dir8(k: Int)

One of the eight directions, clockwise from up: the four of Dir4 and the diagonals between.

Source

Dir8.all

fn all() -> List<Dir8>

All eight directions, clockwise from up: up, up-right, right, down-right, down, down-left, left, up-left. Dir8 has no named constructors; index this list.

  • cost: O(1)
  • order: clockwise from up
  • traps: never

Examples:

  • Dir8.all().map(d => d.delta) == [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

Source

Dir8.from

fn from(letters: Str, ch: Char) -> ?Dir8

The direction whose letter is ch in letters, listed in Dir8.all() order, e.g. Dir8.from("NOEKSJWH", ch) style alphabets. None for another letter.

Matching is exact (case-sensitive). If a letter appears more than once in letters, the first position wins.

  • cost: O(1)
  • ties: a repeated letter maps to its first position in letters
  • traps: letters.len != 8

Examples:

  • Dir8.from("abcdefgh", 'c') == Some(Dir8.all()[2])
  • Dir8.from("abcdefgh", 'x') == None

Source

Dir8.cw

fn cw(self) -> Dir8

An eighth of a turn clockwise: the next direction in Dir8.all(), wrapping from up-left to up.

  • cost: O(1)
  • traps: never

Examples:

  • Dir8.all()[0].cw.delta == (-1, 1)
  • Dir8.all()[7].cw == Dir8.all()[0]

Source

Dir8.ccw

fn ccw(self) -> Dir8

An eighth of a turn counter-clockwise, the inverse of cw.

  • cost: O(1)
  • traps: never

Examples:

  • Dir8.all()[0].ccw == Dir8.all()[7]

Source

Dir8.back

fn back(self) -> Dir8

The opposite direction, four eighths round: its delta is the negated delta.

  • cost: O(1)
  • traps: never

Examples:

  • Dir8.all()[1].back.delta == (1, -1)

Source

Dir8.delta

fn delta(self) -> (Int, Int)

The change in (row, column) of one step; each part is -1, 0 or 1.

  • cost: O(1)
  • traps: never

Examples:

  • Dir8.all()[3].delta == (1, 1)

Source

On this page