#multi-dimensional #array #buffer #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

#640 in Math

Download history 16/week @ 2024-07-25 2/week @ 2024-08-01 1/week @ 2024-09-12 4/week @ 2024-09-19 5/week @ 2024-09-26 2/week @ 2024-10-03

120 downloads per month

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