Crates.io | block-grid |
lib.rs | block-grid |
version | 0.1.3 |
source | src |
created_at | 2020-11-01 20:41:15.242299 |
updated_at | 2022-01-05 07:32:46.818403 |
description | A quick, cache-conscious, tiled 2D array. |
homepage | https://github.com/gunvirranu/block-grid |
repository | https://github.com/gunvirranu/block-grid |
max_upload_size | |
id | 307642 |
size | 63,088 |
block-grid
gives you a fixed size, two-dimensional array, with a blocked memory representation. This has the sweet benefit of being much more cache-friendly if you're often accessing nearby coordinates.
(row, col): (usize, usize)
Block
and BlockMut
no_std
and serde
supportuse block_grid::{BlockGrid, CoordsIterator, U2};
fn main() {
let data: Vec<_> = (0..(4 * 6)).collect();
// Construct from row-major ordered data
let grid = BlockGrid::<usize, U2>::from_row_major(4, 6, &data).unwrap();
// The 2D grid looks like:
// +-----------------------+
// | 0 1 | 2 3 | 4 5 |
// | 6 7 | 8 9 | 10 11 |
// |-------+-------+-------|
// | 12 13 | 14 15 | 16 17 |
// | 18 19 | 20 21 | 22 23 |
// +-----------------------+
// Indexing
assert_eq!(grid[(1, 3)], 9);
// Access raw array
let first_five = &grid.raw()[..5];
assert_eq!(first_five, &[0, 1, 6, 7, 2]);
// Iterate over blocks, and access the last
let block = grid.block_iter().last().unwrap();
assert_eq!(block[(0, 1)], 17);
// Iterate in row-major order
for (i, &x) in grid.row_major_iter().enumerate() {
assert_eq!(x, i);
}
// Iterate in memory order, with coordinates
for ((row, col), &x) in grid.each_iter().coords() {
assert_eq!(row * 6 + col, x);
}
}
TODO: Stuff about caches
See CHANGELOG.md
.
block-grid
is licensed under the MIT license.
If your access patterns suit a typical row-major memory representation, you can still use block-grid
! If you truly desire alternatives, however, check out array2d
, imgref
, grid
, or toodee
. The last two support dynamic resizing. For matrices and linear algebra, there's also nalgebra
.