20 releases (breaking)

0.15.0 Sep 22, 2024
0.14.0 May 15, 2024
0.13.0 Dec 18, 2023
0.12.0 Nov 22, 2023
0.1.1 Dec 23, 2017

#156 in Data structures

Download history 27305/week @ 2024-07-19 25959/week @ 2024-07-26 22012/week @ 2024-08-02 25915/week @ 2024-08-09 22686/week @ 2024-08-16 25710/week @ 2024-08-23 23358/week @ 2024-08-30 21781/week @ 2024-09-06 18580/week @ 2024-09-13 23779/week @ 2024-09-20 24204/week @ 2024-09-27 25628/week @ 2024-10-04 21684/week @ 2024-10-11 19137/week @ 2024-10-18 18257/week @ 2024-10-25 16452/week @ 2024-11-01

78,877 downloads per month
Used in 297 crates (13 directly)

MIT license

120KB
2.5K SLoC

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.

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 crate is a container for all kind of data. If you need to perform matrix operations, you are better off 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.

Memory layout

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 by default.

If you need a specific memory layout, please seek the *_with_order constructors. You should also take note that some transformation methods change the internal memory layout, like transpose.

This choice is important, because operations on rows are faster with a row-major memory layout. Likewise, operations on columns are faster with column-major memory layout.

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

~175KB