Skip to content

A dynamically-sized two-dimensional vec written in Rust

Notifications You must be signed in to change notification settings

good-praxis/vecgrid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vecgrid

dependency status Build Status

Vecgrid provides a dynamically sized two-dimensional vector. It is more efficient and is easier to use than a vector of vectors, i.e. Vec<Vec<T>>.

This is beneficial when using a grid-like structure, which is common in image processing, game development, and other situations. Vecgrid cannot be used when rows or columns might have different lengths⁠—all rows and columns must be the same length.

Vecgrid is a fork of the statically-sized two-dimensional array library Array2D.

Roadmap

This project attemps to extend the upstream project in an opinionated fashion, by adding mutable iterators and dynamic resizing of the inner collection. Here's how that is going:

  • row_iter_mut
  • column_iter_mut
  • rows_iter_mut
  • columns_iter_mut
  • elements_row_major_iter_mut
  • elements_column_major_iter_mut
  • insert_row
  • insert_column
  • insert_rows
  • insert_columns
  • remove_row
  • remove_column
  • remove_rows
  • remove_columns
  • append_rows
  • append_columns
  • extend_rows
  • extend_columns

Upstream code might be refactored along the way to make use of optimizations or to align approaches across the crate. Code deprecated upstream from before the inital release of this crate is dropped, future deprecated upstream code may or may not be deprecated in this crate in kind. A release of a major version of this crate indicates maturity surpassing active tracking of the upstream repository, but until then changes will be synced as they happen.

How to use Vecgrid

Creating a Vecgrid

A Vecgrid can be created in many different ways. These include:

Extending a Vecgrid

Since Vecgrids are dynamically sized, it is possible to extend them:

  • Providing singular rows of matching length alongside row indices to insert_row, or providing a mutable slice of rows to insert_rows.
  • Append the grid, either with matching length rows via append_rows... or future additions!
  • Remove singular or consecutive rows via remove_row and remove_rows respectively.

Accessing data from a Vecgrid

Vecgrid supports several forms of indexing:

Vecgrid also supports several forms of iteration. You can iterate through:

Extracting all data from a Vecgrid

A Vecgrid can be converted back into a Vec through several methods. You can extract the data as:

Examples

use vecgrid::{Vecgrid, Error};

pub fn main() -> Result<(), Error> {
    // Create a vecgrid filled with the same element.
    let prefilled = Vecgrid::filled_with(42, 2, 3);
    assert_eq!(prefilled.num_rows(), 2);
    assert_eq!(prefilled.num_columns(), 3);
    assert_eq!(prefilled[(0, 0)], 42);

    // Create a vecgrid from the given rows. You can also use columns
    // with the `columns` function
    let rows = vec![vec![1, 2, 3], vec![4, 5, 6]];
    let from_rows = Vecgrid::from_rows(&rows)?;
    assert_eq!(from_rows.num_rows(), 2);
    assert_eq!(from_rows.num_columns(), 3);
    assert_eq!(from_rows[(1, 1)], 5);

    // Create  vecgrid from a flat Vec of elements in row major or
    // column major order.
    let column_major = vec![1, 4, 2, 5, 3, 6];
    let from_column_major =
        Vecgrid::from_column_major(&column_major, 2, 3)?;
    assert_eq!(from_column_major.num_rows(), 2);
    assert_eq!(from_column_major.num_columns(), 3);
    assert_eq!(from_column_major[(1, 1)], 5);

    // Implements `Eq` if the element type does.
    assert_eq!(from_rows, from_column_major);

    // Index into a vecgrid using a tuple of usize to access or alter
    // the vecgrid.
    let rows = vec![vec![1, 2, 3], vec![4, 5, 6]];
    let mut vecgrid = Vecgrid::from_rows(&rows)?;
    vecgrid[(1, 1)] = 100;

    // Convert the vecgrid back into a nested Vec using `as_rows` or
    // `as_columns`.
    let vecgrid_rows = vecgrid.as_rows();
    assert_eq!(vecgrid_rows, vec![vec![1, 2, 3], vec![4, 100, 6]]);

    // Convert the vecgrid back into a flat Vec using `as_row_major` or
    // `as_column_major`.
    let vecgrid_column_major = vecgrid.as_column_major();
    assert_eq!(vecgrid_column_major, vec![1, 4, 2, 100, 3, 6]);

    // Iterate over a single row or column
    println!("First column:");
    for element in vecgrid.column_iter(0)? {
        println!("{}", element);
    }

    // Iterate over all rows or columns.
    println!("All elements:");
    for row_iter in vecgrid.rows_iter() {
        for element in row_iter {
            print!("{} ", element);
        }
        println!();
    }

    Ok(())
}

Acknowledgement

This library is made possible thanks to the excellent groundwork laid down in Array2D by author HarrisonMc555, as well as contributor to the upstream project tylerjw. Array2D has been published under the MIT license.

License: MIT

About

A dynamically-sized two-dimensional vec written in Rust

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages