matrix

package module
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2023 License: MIT Imports: 0 Imported by: 7

README

matrix

A package to manipulate rectangular matrices backed by a slice

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cell added in v0.0.4

type Cell[T any] struct {
	X, Y  int
	Value T
}

A Cell stores the cell coordinates and its value

type Matrix

type Matrix[T comparable] struct {
	// contains filtered or unexported fields
}

A Matrix is a 2-dimensional table stored as a 1-dimensional slice

func FromSlice added in v0.0.6

func FromSlice[T comparable](cols int, cart bool, sl []T) (m Matrix[T], ok bool)

FromSlice creates a new matrix with the content of the input slice. It requires the matrix `width` (cols) and the type of matrix. If the length of the input slice is not a multiple of the width the `ok` return boolean will be false.

func New

func New[T comparable](w, h int, cart bool) Matrix[T]

New creates a new Matrix given width, height and if it should use cartesian coordinates (0 at the bottom)

func NewLike added in v0.0.5

func NewLike[T comparable](m Matrix[T]) Matrix[T]

NewLike creates a new matrix with the same dimensions as the input matrix

func (Matrix[T]) Adjacent added in v0.0.4

func (m Matrix[T]) Adjacent(x, y int, wrap bool) []Cell[T]

Adjacent returns a list of Cell(s) adjacent to the one at x,y. If wrap is true coordinates that are outside the Matrix boundary will wrap around. For example if x=0 return add rightmost cell, if y=top add bottom cell.

func (Matrix[T]) Cartesian added in v0.0.5

func (m Matrix[T]) Cartesian() bool

Cartesian return true for cartesian coordinates (0 at the bottom) and false for computer coordinates (0 at the top)

func (Matrix[T]) Clone added in v0.0.2

func (m Matrix[T]) Clone() Matrix[T]

Clone creates a new matrix that is a copy of the input matrix

func (Matrix[T]) Column added in v0.0.9

func (m Matrix[T]) Column(x int) (col []T)

Column returns the list of values for the specified column

func (Matrix[T]) Copy added in v0.0.7

func (m Matrix[T]) Copy(x, y int, n Matrix[T])

Copy copies the matrix `n` into `m` starting at the coordinates `x,y`

func (Matrix[T]) Equals added in v0.0.6

func (m Matrix[T]) Equals(n Matrix[T]) bool

Equal returns true if the two matrices are equal

func (Matrix[T]) Fill added in v0.0.3

func (m Matrix[T]) Fill(v T)

Fill sets all cells of the matrix to the specified value

func (Matrix[T]) Fix

func (m Matrix[T]) Fix(y int) int

Fix converts the row number (y) from one coordinate system to the other (cartesian to computer or viceversa)

func (Matrix[T]) Get

func (m Matrix[T]) Get(x, y int) T

Get returns the value for the cell at x,y

func (Matrix[T]) Height

func (m Matrix[T]) Height() int

Height returns the matrix height (number of rows)

func (Matrix[T]) Hexagonal added in v0.0.8

func (m Matrix[T]) Hexagonal(x, y int, wrap bool) []Cell[T]

Hexagonal returns a list of Cell(s) adjacent to the one at x,y, according to the Hexagonal neighbourhood rules emulated on a rectangular grid (left, top-left, top, right, bottom-right, bottom)

func (Matrix[T]) Moore added in v0.0.8

func (m Matrix[T]) Moore(x, y int, wrap bool) []Cell[T]

Moore returns a list of Cell(s) adjacent to the one at x,y, according to the Moore neighbourhood (from x-1, y-1 to x+1, y+1)

func (Matrix[T]) Row

func (m Matrix[T]) Row(y int) []T

Row returns the list of values for the specified row

func (Matrix[T]) Set

func (m Matrix[T]) Set(x, y int, v T)

Set changes the value for the cell at x,y

func (Matrix[T]) Slice added in v0.0.5

func (m Matrix[T]) Slice() []T

Slice return the backing slice for the Matrix

func (Matrix[T]) Submatrix added in v0.0.6

func (m Matrix[T]) Submatrix(x, y, w, h int) Matrix[T]

Creates a new matrix that is a subset of the input matrix

func (Matrix[T]) Swap added in v0.0.9

func (m Matrix[T]) Swap(x, y, x1, y1 int)

Swap changes the value for the cell at x,y with the one for the cell at x1, y1

func (Matrix[T]) VonNewmann added in v0.0.8

func (m Matrix[T]) VonNewmann(x, y int, wrap bool) []Cell[T]

VonNewmann returns a list of Cell(s) adjacent to the one at x,y, according to the Von Newmann neighbourhood (above, below, left, righ)

func (Matrix[T]) Width

func (m Matrix[T]) Width() int

Width returns the matrix width (number of columns)

type Neighbourhood added in v0.0.8

type Neighbourhood int
const (
	MooreNeighbourhood Neighbourhood = iota
	VonNewmannNeighbourhood
	HexagonalNeighbourhood
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL