Struct geom3d::Grid[][src]

pub struct Grid<T> { /* fields omitted */ }
Expand description

Stores elements of a certain type in a 2D grid structure.

Uses a rust Vec<T> type to reference the grid data on the heap. Also the number of rows and columns are stored in the grid data structure.

The grid data is stored in a row-major memory layout.

Implementations

Init a grid of size rows x columns with default values of the given type. For example this will generate a 2x3 grid of zeros:

use grid::Grid;
let grid : Grid<u8> = Grid::new(2,2);
assert_eq!(grid[0][0], 0);

Init a grid of size rows x columns with the given data element.

Returns a grid from a vector with a given column length. The length of vec must be a multiple of cols.

For example:

use grid::Grid;
let grid = Grid::from_vec(vec![1,2,3,4,5,6], 3);
assert_eq!(grid.size(), (2, 3));

will create a grid with the following layout: [1,2,3] [4,5,6]

This example will fail, because vec.len() is not a multiple of cols:

use grid::Grid;
Grid::from_vec(vec![1,2,3,4,5], 3);

Returns a reference to an element, without performing bound checks. Generally not recommended, use with caution! Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.

Returns a mutable reference to an element, without performing bound checks. Generally not recommended, use with caution! Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.

Access a certain element in the grid. Returns None if an element beyond the grid bounds is tried to be accessed.

Mutable access to a certain element in the grid. Returns None if an element beyond the grid bounds is tried to be accessed.

Returns the size of the gird as a two element tuple. First element are the number of rows and the second the columns.

Returns the number of rows of the grid.

Returns the number of columns of the grid.

Returns true if the grid contains no elements. For example:

use grid::*;
let grid : Grid<u8> = grid![];
assert!(grid.is_empty());

Clears the grid.

Returns an iterator over the whole grid, starting from the first row and column.

use grid::*;
let grid: Grid<u8> = grid![[1,2][3,4]];
let mut iter = grid.iter();
assert_eq!(iter.next(), Some(&1));
assert_eq!(iter.next(), Some(&2));
assert_eq!(iter.next(), Some(&3));
assert_eq!(iter.next(), Some(&4));
assert_eq!(iter.next(), None);

Returns an mutable iterator over the whole grid that allows modifying each value.

use grid::*;
let mut grid: Grid<u8> = grid![[1,2][3,4]];
let mut iter = grid.iter_mut();
let next = iter.next();
assert_eq!(next, Some(&mut 1));
*next.unwrap() = 10;

Returns an iterator over a column.

Examples

use grid::*;
let grid: Grid<u8> = grid![[1, 2, 3][3, 4, 5]];
let mut col_iter = grid.iter_col(1);
assert_eq!(col_iter.next(), Some(&2));
assert_eq!(col_iter.next(), Some(&4));
assert_eq!(col_iter.next(), None);

Panics

Panics if the col index is out of bounds.

Returns a mutable iterator over a column.

Examples

use grid::*;
let mut grid: Grid<u8> = grid![[1, 2, 3][3, 4, 5]];
let mut col_iter = grid.iter_col_mut(1);
let next = col_iter.next();
assert_eq!(next, Some(&mut 2));
*next.unwrap() = 10;
assert_eq!(grid[0][1], 10);

Panics

Panics if the col index is out of bounds.

Returns an iterator over a row.

Examples

use grid::*;
let grid: Grid<u8> = grid![[1, 2, 3][3, 4, 5]];
let mut col_iter = grid.iter_row(1);
assert_eq!(col_iter.next(), Some(&3));
assert_eq!(col_iter.next(), Some(&4));
assert_eq!(col_iter.next(), Some(&5));
assert_eq!(col_iter.next(), None);

Panics

Panics if the row index is out of bounds.

Returns a mutable iterator over a row.

Examples

use grid::*;
let mut grid: Grid<u8> = grid![[1, 2, 3][3, 4, 5]];
let mut col_iter = grid.iter_row_mut(1);
let next = col_iter.next();
*next.unwrap() = 10;
assert_eq!(grid[1][0], 10);

Panics

Panics if the row index is out of bounds.

Add a new row to the grid.

Examples

use grid::*;
let mut grid: Grid<u8> = grid![[1, 2, 3][3, 4, 5]];
let row = vec![6,7,8];
grid.push_row(row);
assert_eq!(grid.rows(), 3);
assert_eq!(grid[2][0], 6);
assert_eq!(grid[2][1], 7);
assert_eq!(grid[2][2], 8);

Can also be used to init an empty grid:

use grid::*;
let mut grid: Grid<u8> = grid![];
let row = vec![1,2,3];
grid.push_row(row);
assert_eq!(grid.size(), (1, 3));

Panics

Panics if the grid is not empty and row.len() != grid.cols().

Add a new column to the grid.

Important: Please note that Grid uses a Row-Major memory layout. Therefore, the push_col() operation requires quite a lot of memory shifting and will be significantly slower compared to a push_row() operation.

Examples

use grid::*;
let mut grid: Grid<u8> = grid![[1, 2, 3][3, 4, 5]];
let col = vec![4,6];
grid.push_col(col);
assert_eq!(grid.cols(), 4);
assert_eq!(grid[0][3], 4);
assert_eq!(grid[1][3], 6);

Can also be used to init an empty grid:

use grid::*;
let mut grid: Grid<u8> = grid![];
let col = vec![1,2,3];
grid.push_col(col);
assert_eq!(grid.size(), (3, 1));

Panics

Panics if the grid is not empty and col.len() != grid.rows().

Removes the last row from a grid and returns it, or None if it is empty.

Examples

use grid::*;
let mut grid = grid![[1,2,3][4,5,6]];
assert_eq![grid.pop_row(), Some(vec![4,5,6])];
assert_eq![grid.pop_row(), Some(vec![1,2,3])];
assert_eq![grid.pop_row(), None];

Removes the last column from a grid and returns it, or None if it is empty.

Note that this operation is much slower than the pop_row() because the memory layout of Grid is row-major and removing a column requires a lot of move operations.

Examples

use grid::*;
let mut grid = grid![[1,2,3][4,5,6]];
assert_eq![grid.pop_col(), Some(vec![3,6])];
assert_eq![grid.pop_col(), Some(vec![2,5])];
assert_eq![grid.pop_col(), Some(vec![1,4])];
assert_eq![grid.pop_col(), None];

Insert a new row at the index and shifts all rows after down.

Examples

use grid::*;
let mut grid = grid![[1,2,3][4,5,6]];
grid.insert_row(1, vec![7,8,9]);
assert_eq!(grid[0], [1,2,3]);
assert_eq!(grid[1], [7,8,9]);
assert_eq!(grid[2], [4,5,6]);
assert_eq!(grid.size(), (3,3))

Insert a new column at the index.

Important! Insertion of columns is a lot slower than the lines insertion. This is because of the memory layout of the grid data structure.

Examples

use grid::*;
let mut grid = grid![[1,2,3][4,5,6]];
grid.insert_col(1, vec![9,9]);
assert_eq!(grid[0], [1,9,2,3]);
assert_eq!(grid[1], [4,9,5,6]);
assert_eq!(grid.size(), (2,4))

Returns a reference to the internal data structure of the grid.

Grid uses a row major layout. All rows are placed right after each other in the vector data structure.

Examples

use grid::*;
let grid = grid![[1,2,3][4,5,6]];
let flat = grid.flatten();
assert_eq!(flat, &vec![1,2,3,4,5,6]);

Converts self into a vector without clones or allocation.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Performs the mutable indexing (container[index]) operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait. Read more

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait. Read more

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s. Read more

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s. Read more

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.