[][src]Struct pathfinding::matrix::Matrix

pub struct Matrix<C> {
    pub rows: usize,
    pub columns: usize,
    // some fields omitted
}

Matrix of an arbitrary type. Data are stored consecutively in memory, by rows. Raw data can be accessed using as_ref() or as_mut().

Fields

rows: usize

Rows

columns: usize

Columns

Implementations

impl<C: Clone> Matrix<C>[src]

pub fn new(rows: usize, columns: usize, value: C) -> Self[src]

Create new matrix with an initial value.

pub fn new_square(size: usize, value: C) -> Self[src]

Create new square matrix with initial value.

pub fn fill(&mut self, value: C)[src]

Fill with a known value.

pub fn slice(
    &self,
    rows: Range<usize>,
    columns: Range<usize>
) -> Result<Self, MatrixFormatError>
[src]

Return a copy of a sub-matrix, or return an error if the ranges are outside the original matrix.

#[must_use]pub fn rotated_cw(&self, times: usize) -> Self[src]

Return a copy of a matrix rotated clock-wise a number of times.

#[must_use]pub fn rotated_ccw(&self, times: usize) -> Self[src]

Return a copy of a matrix rotated counter-clock-wise a number of times.

#[must_use]pub fn flipped_lr(&self) -> Self[src]

Return a copy of the matrix flipped along the vertical axis.

#[must_use]pub fn flipped_ud(&self) -> Self[src]

Return a copy of the matrix flipped along the horizontal axis.

#[must_use]pub fn transposed(&self) -> Self[src]

Return a copy of the matrix after transposition.

pub fn extend(&mut self, row: &[C]) -> Result<(), MatrixFormatError>[src]

Extend the matrix in place by adding one full row. An error is returned if the row does not have the expected number of elements.

impl<C: Copy> Matrix<C>[src]

pub fn set_slice(&mut self, pos: &(usize, usize), slice: &Self)[src]

Replace a slice of the current matrix with the content of another one. Only the relevant cells will be extracted if the slice goes outside the original matrix.

impl<C> Matrix<C>[src]

pub fn from_vec(
    rows: usize,
    columns: usize,
    values: Vec<C>
) -> Result<Self, MatrixFormatError>
[src]

Create new matrix from vector values. The first value will be assigned to index (0, 0), the second one to index (0, 1), and so on. An error is returned instead if data length does not correspond to the announced size.

pub fn square_from_vec(values: Vec<C>) -> Result<Self, MatrixFormatError>[src]

Create new square matrix from vector values. The first value will be assigned to index (0, 0), the second one to index (0, 1), and so on. An error is returned if the number of values is not a square number.

#[must_use]pub fn new_empty(columns: usize) -> Self[src]

Create new empty matrix with a predefined number of columns. This is useful to gradually build the matrix and extend it later using extend and does not require a filler element compared to Matrix::new.

pub fn from_rows<IR, IC>(rows: IR) -> Result<Self, MatrixFormatError> where
    IR: IntoIterator<Item = IC>,
    IC: IntoIterator<Item = C>, 
[src]

Create a matrix from something convertible to an iterator on rows, each row being convertible to an iterator on columns.

An error will be returned if length of rows differ.

use pathfinding::matrix::*;

let input = "abc\ndef";
let matrix = Matrix::from_rows(input.lines().map(|l| l.chars()))?;
assert_eq!(matrix.rows, 2);
assert_eq!(matrix.columns, 3);
assert_eq!(matrix[&(1, 1)], 'e');

#[must_use]pub fn is_square(&self) -> bool[src]

Check if a matrix is a square one.

#[must_use]pub fn idx(&self, i: &(usize, usize)) -> usize[src]

Index in raw data of a given position.

Panics

This function panics if the coordinates do not designated a cell.

pub fn flip_lr(&mut self)[src]

Flip the matrix around the vertical axis.

pub fn flip_ud(&mut self)[src]

Flip the matrix around the horizontal axis.

pub fn rotate_cw(&mut self, times: usize)[src]

Rotate a square matrix clock-wise a number of times.

Panics

This function panics if the matrix is not square.

pub fn rotate_ccw(&mut self, times: usize)[src]

Rotate a square matrix counter-clock-wise a number of times.

Panics

This function panics if the matrix is not square.

pub fn neighbours(
    &self,
    index: &(usize, usize),
    diagonals: bool
) -> impl Iterator<Item = (usize, usize)>
[src]

Return an iterator on neighbours of a given matrix cell, with or without considering diagonals.

pub fn in_direction(
    &self,
    index: &(usize, usize),
    direction: (isize, isize)
) -> impl Iterator<Item = (usize, usize)>
[src]

Return an iterator of cells in a given direction starting from a given cell. Any direction (including with values greater than 1) can be given. The starting cell is not included in the results.

Examples

Starting from square (1, 1) in a 8×8 chessboard, move like a knight by steps of two rows down and one column right:

let m = Matrix::new_square(8, '.');
assert_eq!(m.in_direction(&(1, 1), (2, 1)).collect(),
           vec![(3, 2), (5, 3), (7, 4)]);

Starting from square (3, 2) in the same chessboard, move diagonally in the North-West direction:

let m = Matrix::new_square(8, '.');
assert_eq!(m.in_direction(&(3, 2), directions::NW).collect(),
           vec![(2, 1), (1, 0)]);

#[must_use]pub fn iter(&self) -> RowIterator<'_, C>

Notable traits for RowIterator<'a, C>

impl<'a, C> Iterator for RowIterator<'a, C> type Item = &'a [C];
[src]

Return an iterator on rows of the matrix.

pub fn indices(&self) -> impl Iterator<Item = (usize, usize)>[src]

Return an iterator on the Matrix indices, first row first.

#[must_use]pub fn values(&self) -> Iter<'_, C>[src]

Return an iterator on values, first row first.

#[must_use]pub fn values_mut(&mut self) -> IterMut<'_, C>[src]

Return a mutable iterator on values, first row first.

Trait Implementations

impl<C: Clone> Clone for Matrix<C>[src]

impl<C: Debug> Debug for Matrix<C>[src]

impl<C> Deref for Matrix<C>[src]

type Target = [C]

The resulting type after dereferencing.

impl<C> DerefMut for Matrix<C>[src]

impl<C: Eq> Eq for Matrix<C>[src]

impl<C: Hash> Hash for Matrix<C>[src]

impl<'a, C> Index<&'a (usize, usize)> for Matrix<C>[src]

type Output = C

The returned type after indexing.

impl<'a, C> IndexMut<&'a (usize, usize)> for Matrix<C>[src]

impl<'a, C> IntoIterator for &'a Matrix<C>[src]

type IntoIter = RowIterator<'a, C>

Which kind of iterator are we turning this into?

type Item = &'a [C]

The type of the elements being iterated over.

impl<C: Clone + Signed> Neg for Matrix<C>[src]

type Output = Self

The resulting type after applying the - operator.

impl<C: PartialEq> PartialEq<Matrix<C>> for Matrix<C>[src]

impl<C> StructuralEq for Matrix<C>[src]

impl<C> StructuralPartialEq for Matrix<C>[src]

impl<C: Copy> Weights<C> for Matrix<C>[src]

Auto Trait Implementations

impl<C> RefUnwindSafe for Matrix<C> where
    C: RefUnwindSafe

impl<C> Send for Matrix<C> where
    C: Send

impl<C> Sync for Matrix<C> where
    C: Sync

impl<C> Unpin for Matrix<C> where
    C: Unpin

impl<C> UnwindSafe for Matrix<C> where
    C: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<Q, K> Equivalent<K> for Q where
    K: Borrow<Q> + ?Sized,
    Q: Eq + ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.