#buffer #multi-dimensional #array #array-index #index

xdbuf

Provides a reusable multi-dimensional buffer

1 unstable release

0.3.0 Jul 10, 2024
0.2.0 Jul 10, 2024
0.1.2 Jul 9, 2024
0.1.1 Jul 9, 2024
0.1.0 Jul 9, 2024

#517 in Math

MIT/Apache

39KB
338 lines

English | 日本語

xdbuf

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.

Usage

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(())
}

License

Licensed under either of

at your option.

(The English in the README and documentation comments has been translated into English by DeepL and ChatGPT.)

Dependencies

~135KB