13 releases (7 breaking)

0.8.1 Aug 17, 2022
0.7.0 Aug 6, 2022
0.6.0 Nov 5, 2021
0.4.0 Jul 23, 2021
0.1.1 Dec 23, 2017

#178 in Data structures

Download history 73/week @ 2022-06-08 85/week @ 2022-06-15 105/week @ 2022-06-22 68/week @ 2022-06-29 128/week @ 2022-07-06 147/week @ 2022-07-13 94/week @ 2022-07-20 73/week @ 2022-07-27 196/week @ 2022-08-03 283/week @ 2022-08-10 288/week @ 2022-08-17 131/week @ 2022-08-24 143/week @ 2022-08-31 249/week @ 2022-09-07 166/week @ 2022-09-14 137/week @ 2022-09-21

715 downloads per month
Used in 2 crates

MIT license

42KB
837 lines

Grid

docs crates.io build status license

Data structure grid for rust. Provide a two dimensional data structure for rust that is easy to use and fast. Most of the functionality provided by the std::vec::Vec type for one dimensional vectors is implemented for two dimensions in this crate.

To use grid with no_std import the library such as:

grid = { version = "*", default-features = false }

lib.rs:

Two Dimensional Grid

Continuous growable 2D data structure. The purpose of this crate is to provide an universal data structure that is faster, uses less memory, and is easier to use than a naive Vec<Vec<T>> solution.

Similar to C-like arrays grid uses a flat 1D Vec<T> data structure to have a continuous memory data layout. See also this explanation of why you should probably use a one-dimensional array approach.

Note that this crate uses a row-major memory layout. Therefore, grid.push_row() is way faster then the grid.push_col() operation.

This crate will always provide a 2D data structure. If you need three or more dimensions take a look at the ndarray library. The grid create is a container for all kind of data. If you need to perform matrix operations, you are better of with a linear algebra lib, such as cgmath or nalgebra. No other dependencies except for the std lib are used. Most of the functions std::Vec<T> offer are also implemented in grid and slightly modified for a 2D data object.

Examples

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

Dependencies

~21KB