Crates.io | xdbuf |
lib.rs | xdbuf |
version | 0.3.0 |
source | src |
created_at | 2024-07-09 09:32:17.630084 |
updated_at | 2024-07-10 12:48:13.892537 |
description | Provides a reusable multi-dimensional buffer. |
homepage | https://crates.io/crates/xdbuf |
repository | https://github.com/azishio/xdbuf-rs |
max_upload_size | |
id | 1296856 |
size | 52,244 |
English | 日本語
Provides a reusable multidimensional buffer. It can be reinitialized to any size as long as the dimensions are the same, minimizing the number of memory reallocations.
Also provides a Walker
structure to traverse the array.
use xdbuf::{XDBuf, Walker, step2d};
fn main() -> Result<(), anyhow::Error> {
// Create an instance
let mut buf = XDBuf::new([5, 6], 0).unwrap();
assert_eq!(buf.get(0), Some(&0));
assert_eq!(buf.len(), 30);
// Reinitialize from an initial array
// [1, 2, 3,
// 4, 5, 6,
// 7, 8, 9]
let initial_vec = (1..=9).collect::<Vec<_>>();
buf.init_with_vec([3, 3], initial_vec).unwrap();
assert_eq!(buf.get(0), Some(&1));
assert_eq!(buf.get(8), Some(&9));
// Generate a `Walker` to traverse the array
let mut walker = buf.walker_from_m([1, 1])?;
assert_eq!(buf.get(walker.index_s()), Some(&5));
// Move down
assert_eq!(step2d::DOWN, [0, -1]);
walker.as_(&step2d::DOWN)?;
assert_eq!(buf.get(walker.index_s()), Some(&2));
// Move until the value is greater than or equal to 8
walker.as_until(|&value, _index| { value >= 8 })?;
assert_eq!(buf.get(walker.index_s()), Some(&8));
// Rewrite the value
buf.set(walker.index_s(), 100)?;
Ok(())
}
Licensed under either of
at your option.
(The English in the README and documentation comments has been translated into English by DeepL and ChatGPT.)