15 unstable releases (4 breaking)
0.5.0 | Sep 9, 2023 |
---|---|
0.4.2 | Aug 6, 2023 |
0.3.0 | Feb 20, 2021 |
0.2.4 | Nov 19, 2020 |
0.1.2 | May 28, 2020 |
#743 in Algorithms
262 downloads per month
Used in 3 crates
170KB
3.5K
SLoC
toodee
TooDee
is a lightweight and high performance two-dimensional wrapper around a growable Vec
.
TooDeeView
and TooDeeViewMut
allow you create two-dimensional wrappers around a slice.
Core features
- Raw access to the underlying vector's slice via
data()
anddata_mut()
. - Creation of performant two-dimensional subsets using
view()
andview_mut()
. get_unchecked(Coordinate)
andget_unchecked_row(usize)
for faster (unsafe) access to cells or rows.- Most operations are implemented for both
TooDee
andTooDeeViewMut
structs - see below for how this pattern can be extended. - Get/set specific cells using indexing, e.g.,
let my_row = toodee[row]; my_row[col] = val;
. - Index with a
Coordinate
if you prefer, e.g.,toodee[(col, row)] = val
. - Index by row index (i.e., row major) to access row slices, e.g.,
&toodee[row]
. - Iteration, any which way -
rows()
,rows_mut()
,col()
,col_mut()
,cells()
,cells_mut()
. #[no_std]
compliant.- Can create a new
TooDeeView
from a&[T]
, or aTooDeeViewMut
from a&mut [T]
. insert_col()
,remove_col()
,insert_row()
, andremove_row()
implementations with good performance.
Features
copy
, included by default
The CopyOps
trait provides various operations that copy data within the same 2D array, or copy data from one array to another. Many of these
operations are named like their slice counterparts, e.g., copy_from_slice()
or copy_from_toodee()
.
translate
, included by default
The TranslateOps
trait provides common translation algorithms, including:
translate_with_wrap()
, a way to shift data around vertically and horizontally.flip_rows()
, i.e., a mirror translation of data about the center row.flip_cols()
, i.e., a mirror translation of data about the center column.
sort
, included by default
The SortOps
trait provides efficient implementations of:
sort_by_row()
operations, with stable and unstable variants.sort_by_col()
operations, with stable and unstable variants.
serde
, included by default
Serialization and deserialization of TooDee
objects.
TooDeeView
and TooDeeViewMut
can be serialized, but must be deserialized into a TooDee
structure.
Build Your Own 2D Algorithms
Traits such as SortOps
contain additional algorithms. These traits are defined by extending
the TooDeeOpsMut
trait, which has been implemented for TooDee
and TooDeeViewMut
. I recommend
taking the same approach because the algorithms you implement will then work on both structs.
Additionally, you can override the trait's default implementation if necessary.
The implementation of a new trait could look something like:
pub trait FooOps<T> : TooDeeOpsMut<T> {
fn foo(&mut self) -> Bar {
...
return bar;
}
}
The above code would provide a default foo()
implementation that could be overridden if required. Then it's
simply a matter of stating that both TooDee
and TooDeeOpsMut
implement FooOps
:
impl<T> FooOps<T> for TooDeeViewMut<'_, T> {}
impl<T> FooOps<T> for TooDee<T> {}
Once the implementations are available, just call the methods, e.g.,
let bar = my_toodee.foo();
let bar_view = my_toodee_mut_view.foo();
Happy coding 😄
TODO
- Pathfinding algorithms?
- Image/bitmap algorithms?!
Motivation
Similar libraries do exist, but they lacked either performance, flexibility, or functionality.
Here's a small feature comparison chart:
Storage order | Structs supported | Growable? | Mutable views? | Raw data access? | Iterate over row slices? | Notes | |
---|---|---|---|---|---|---|---|
toodee::TooDee | Row-major | Anything (Sized ) | Yes | Yes | Yes | Yes | |
image::ImageBuffer | Row-major | image::Pixel | No | No | Yes | No | Good for image processing - see the imageproc crate. |
image::SubImage | Row-major | image::Pixel | No | Yes | No | No | |
grid::Grid | Row-major | Clone | Yes | No | Yes | No | Similar to TooDee , but not as functionally rich. |
array2d::Array2D | Row-major | Clone | No | No | No | No | |
imgref::Img | Row-major | Anything (Sized ) | No | Yes | Yes | Yes | |
nalgebra::Matrix | Column-major | Scalar | Yes | Yes | Yes | No | Use this for vector/matrix math. |
Goals
- High performance and good flexibility, with the constraint of using a 1-D vector.
- Suitable for use in image processing, but not restricted to this problem domain.
- Provide solid implementations of non-trivial 2D operations.
Non-goals
- GPU integration
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Dependencies
~0.3–0.9MB
~21K SLoC