pub struct TooDee<T> { /* private fields */ }
Expand description
Represents a two-dimensional array.
Empty arrays will always have dimensions of zero.
Implementations§
source§impl<T> TooDee<T>
impl<T> TooDee<T>
sourcepub fn new(num_cols: usize, num_rows: usize) -> TooDee<T>where
T: Default,
pub fn new(num_cols: usize, num_rows: usize) -> TooDee<T>where T: Default,
Create a new TooDee
array of the specified dimensions, and fill it with
the type’s default value.
Panics
Panics if one of the dimensions is zero but the other is non-zero. This is to enforce the rule that empty arrays have no dimensions.
Panics if num_rows * num_cols
overflows.
Examples
use toodee::{TooDee,TooDeeOps};
let toodee : TooDee<u32> = TooDee::new(10, 5);
assert_eq!(toodee.num_cols(), 10);
assert_eq!(toodee.num_rows(), 5);
assert_eq!(toodee[0][0], 0);
sourcepub fn init(num_cols: usize, num_rows: usize, init_value: T) -> TooDee<T>where
T: Clone,
pub fn init(num_cols: usize, num_rows: usize, init_value: T) -> TooDee<T>where T: Clone,
Create a new TooDee
array of the specified dimensions, and fill it with
an initial value.
Panics
Panics if one of the dimensions is zero but the other is non-zero. This is to enforce the rule that empty arrays have no dimensions.
Panics if num_rows * num_cols
overflows.
Examples
use toodee::{TooDee,TooDeeOps};
let toodee = TooDee::init(10, 5, 42u32);
assert_eq!(toodee.num_cols(), 10);
assert_eq!(toodee.num_rows(), 5);
assert_eq!(toodee[0][0], 42);
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the element capacity of the underlying Vec
.
Examples
use toodee::TooDee;
let v = vec![42u32; 10];
let toodee : TooDee<u32> = TooDee::from_vec(5, 2, v);
assert!(toodee.capacity() >= 10);
sourcepub fn with_capacity(capacity: usize) -> TooDee<T>
pub fn with_capacity(capacity: usize) -> TooDee<T>
Constructs a new, empty TooDee<T>
with the specified element capacity.
Examples
use toodee::TooDee;
let toodee : TooDee<u32> = TooDee::with_capacity(50);
assert!(toodee.capacity() >= 50);
sourcepub fn reserve_exact(&mut self, capacity: usize)
pub fn reserve_exact(&mut self, capacity: usize)
Reserves the minimum capacity for at least additional
more elements to be inserted
into the TooDee<T>
.
Examples
use toodee::TooDee;
let mut toodee : TooDee<u32> = TooDee::default();
toodee.reserve_exact(50);
assert_eq!(toodee.capacity(), 50);
sourcepub fn reserve(&mut self, capacity: usize)
pub fn reserve(&mut self, capacity: usize)
Reserves capacity for at least additional
more elements to be inserted
in the given TooDee<T>
.
Examples
use toodee::TooDee;
let mut toodee : TooDee<u32> = TooDee::default();
toodee.reserve(50);
assert!(toodee.capacity() >= 50);
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the underlying vector as much as possible.
Examples
use toodee::TooDee;
let mut toodee : TooDee<u32> = TooDee::with_capacity(50);
toodee.shrink_to_fit();
assert_eq!(toodee.capacity(), 0);
sourcepub fn from_vec(num_cols: usize, num_rows: usize, v: Vec<T>) -> TooDee<T>
pub fn from_vec(num_cols: usize, num_rows: usize, v: Vec<T>) -> TooDee<T>
Create a new TooDee
array using the provided vector. The vector’s length
must match the dimensions of the array.
Panics
Panics if one of the dimensions is zero but the other is non-zero. This is to enforce the rule that empty arrays have no dimensions.
Panics if num_cols * num_rows
overflows.
Examples
use toodee::{TooDee,TooDeeOps};
let v = vec![42u32; 10];
let mut toodee : TooDee<u32> = TooDee::from_vec(5, 2, v);
assert_eq!(toodee.num_cols(), 5);
assert_eq!(toodee.num_rows(), 2);
assert_eq!(toodee[0][0], 42);
sourcepub fn from_box(num_cols: usize, num_rows: usize, b: Box<[T]>) -> TooDee<T>
pub fn from_box(num_cols: usize, num_rows: usize, b: Box<[T]>) -> TooDee<T>
Create a new TooDee
array using the provided boxed slice. The slice’s length
must match the dimensions of the array.
Examples
use toodee::{TooDee,TooDeeOps};
let v = vec![42u32; 10];
let mut toodee : TooDee<u32> = TooDee::from_box(5, 2, v.into_boxed_slice());
assert_eq!(toodee.num_cols(), 5);
assert_eq!(toodee.num_rows(), 2);
assert_eq!(toodee[0][0], 42);
sourcepub fn data(&self) -> &[T]
pub fn data(&self) -> &[T]
Returns a reference to the raw array data
Examples
use toodee::{TooDee,TooDeeOps};
let v = vec![42u32; 10];
let mut toodee : TooDee<u32> = TooDee::from_vec(5, 2, v);
assert_eq!(toodee.data()[0], 42);
sourcepub fn data_mut(&mut self) -> &mut [T]
pub fn data_mut(&mut self) -> &mut [T]
Returns a mutable reference to the raw array data
Examples
use toodee::{TooDee,TooDeeOps};
let v = vec![42u32; 10];
let mut toodee : TooDee<u32> = TooDee::from_vec(5, 2, v);
assert_eq!(toodee.data_mut()[0], 42);
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the array, removing all values and zeroing the number of columns and rows.
Note that this method has no effect on the allocated capacity of the array.
Examples
use toodee::{TooDee,TooDeeOps};
let v = vec![42u32; 10];
let mut toodee : TooDee<u32> = TooDee::from_vec(5, 2, v);
toodee.clear();
assert_eq!(toodee.num_cols(), 0);
assert_eq!(toodee.num_rows(), 0);
assert!(toodee.capacity() >= 10);
sourcepub fn pop_row(&mut self) -> Option<DrainRow<'_, T>>
pub fn pop_row(&mut self) -> Option<DrainRow<'_, T>>
Removes the last row from the array and returns it as a Drain
, or None
if it is empty.
Examples
use toodee::{TooDee,TooDeeOps};
let v = vec![42u32; 15];
let mut toodee : TooDee<u32> = TooDee::from_vec(5, 3, v);
{
let drain = toodee.pop_row().unwrap();
assert_eq!(drain.len(), 5);
}
assert_eq!(toodee.num_cols(), 5);
assert_eq!(toodee.num_rows(), 2);
sourcepub fn push_row<I>(&mut self, data: impl IntoIterator<Item = T, IntoIter = I>)where
I: Iterator<Item = T> + ExactSizeIterator,
pub fn push_row<I>(&mut self, data: impl IntoIterator<Item = T, IntoIter = I>)where I: Iterator<Item = T> + ExactSizeIterator,
Appends a new row to the array.
Panics
Panics if the data’s length doesn’t match the length of existing rows (if any).
sourcepub fn insert_row<I>(
&mut self,
index: usize,
data: impl IntoIterator<Item = T, IntoIter = I>
)where
I: Iterator<Item = T> + ExactSizeIterator,
pub fn insert_row<I>( &mut self, index: usize, data: impl IntoIterator<Item = T, IntoIter = I> )where I: Iterator<Item = T> + ExactSizeIterator,
Inserts new data
into the array at the specified row
Panics
Panics if the data’s length doesn’t match the length of existing rows (if any).
sourcepub fn remove_row(&mut self, index: usize) -> DrainRow<'_, T>
pub fn remove_row(&mut self, index: usize) -> DrainRow<'_, T>
Removes the specified row from the array and returns it as a Drain
Panics
Panics if the specified row index is out of bounds.
Examples
use toodee::{TooDee,TooDeeOps};
let v = vec![42u32; 15];
let mut toodee : TooDee<u32> = TooDee::from_vec(5, 3, v);
{
let drain = toodee.remove_row(1);
assert_eq!(drain.len(), 5);
}
assert_eq!(toodee.num_cols(), 5);
assert_eq!(toodee.num_rows(), 2);
sourcepub fn pop_col(&mut self) -> Option<DrainCol<'_, T>>
pub fn pop_col(&mut self) -> Option<DrainCol<'_, T>>
Removes the last column from the array and returns it as a Drain
, or None
if it is empty.
Examples
use toodee::{TooDee,TooDeeOps};
let v = vec![42u32; 15];
let mut toodee : TooDee<u32> = TooDee::from_vec(5, 3, v);
{
let drain = toodee.pop_col().unwrap();
assert_eq!(drain.len(), 3);
}
assert_eq!(toodee.num_cols(), 4);
assert_eq!(toodee.num_rows(), 3);
sourcepub fn push_col<I>(&mut self, data: impl IntoIterator<Item = T, IntoIter = I>)where
I: Iterator<Item = T> + ExactSizeIterator + DoubleEndedIterator,
pub fn push_col<I>(&mut self, data: impl IntoIterator<Item = T, IntoIter = I>)where I: Iterator<Item = T> + ExactSizeIterator + DoubleEndedIterator,
Appends a new column to the array.
Panics
Panics if the data’s length doesn’t match the length of existing rows (if any).
sourcepub fn remove_col(&mut self, index: usize) -> DrainCol<'_, T> ⓘ
pub fn remove_col(&mut self, index: usize) -> DrainCol<'_, T> ⓘ
Removes the specified column from the array and returns it as a Drain
Panics
Panics if the specified column index is out of bounds.
Examples
use toodee::{TooDee,TooDeeOps};
let v = vec![42u32; 15];
let mut toodee : TooDee<u32> = TooDee::from_vec(5, 3, v);
{
let drain = toodee.remove_col(1);
assert_eq!(drain.len(), 3);
}
assert_eq!(toodee.num_cols(), 4);
assert_eq!(toodee.num_rows(), 3);
sourcepub fn insert_col<I>(
&mut self,
index: usize,
data: impl IntoIterator<Item = T, IntoIter = I>
)where
I: Iterator<Item = T> + ExactSizeIterator + DoubleEndedIterator,
pub fn insert_col<I>( &mut self, index: usize, data: impl IntoIterator<Item = T, IntoIter = I> )where I: Iterator<Item = T> + ExactSizeIterator + DoubleEndedIterator,
Inserts new data
into the array at the specified col
.
Panics
Panics if the data’s length doesn’t match the length of existing columns (if any).
sourcepub fn swap_dimensions(&mut self)
pub fn swap_dimensions(&mut self)
Switches the values for num_cols
and num_rows
without transposing the underlying data.
Trait Implementations§
source§impl<T> AsRef<Vec<T, Global>> for TooDee<T>
impl<T> AsRef<Vec<T, Global>> for TooDee<T>
We can allow immutable access to the underlying Vec
,
but not mutable access because that could lead to changes
in the Vec
’s length.
source§impl<T> CopyOps<T> for TooDee<T>
impl<T> CopyOps<T> for TooDee<T>
source§fn copy_from_slice(&mut self, src: &[T])where
T: Copy,
fn copy_from_slice(&mut self, src: &[T])where T: Copy,
source§fn clone_from_slice(&mut self, src: &[T])where
T: Clone,
fn clone_from_slice(&mut self, src: &[T])where T: Clone,
source§impl<T> Default for TooDee<T>
impl<T> Default for TooDee<T>
Custom Default
implementation because T
does not need to implement Default
.
See rust issue #26925
source§impl<'de, T> Deserialize<'de> for TooDee<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for TooDee<T>where T: Deserialize<'de>,
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,
source§impl<T> From<TooDeeView<'_, T>> for TooDee<T>where
T: Clone,
impl<T> From<TooDeeView<'_, T>> for TooDee<T>where T: Clone,
source§fn from(view: TooDeeView<'_, T>) -> Self
fn from(view: TooDeeView<'_, T>) -> Self
source§impl<T> From<TooDeeViewMut<'_, T>> for TooDee<T>where
T: Clone,
impl<T> From<TooDeeViewMut<'_, T>> for TooDee<T>where T: Clone,
source§fn from(view: TooDeeViewMut<'_, T>) -> Self
fn from(view: TooDeeViewMut<'_, T>) -> Self
source§impl<'a, T> IntoIterator for &'a TooDee<T>
impl<'a, T> IntoIterator for &'a TooDee<T>
source§impl<'a, T> IntoIterator for &'a mut TooDee<T>
impl<'a, T> IntoIterator for &'a mut TooDee<T>
source§impl<T> IntoIterator for TooDee<T>
impl<T> IntoIterator for TooDee<T>
Use Vec
’s IntoIter
for performance reasons.
TODO: return type that implements TooDeeIterator
source§impl<T: PartialEq> PartialEq<TooDee<T>> for TooDee<T>
impl<T: PartialEq> PartialEq<TooDee<T>> for TooDee<T>
source§impl<T> TooDeeOps<T> for TooDee<T>
impl<T> TooDeeOps<T> for TooDee<T>
source§fn num_cols(&self) -> usize
fn num_cols(&self) -> usize
Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let toodee : TooDee<u32> = TooDee::new(10, 5);
assert_eq!(toodee.num_cols(), 10);
source§fn num_rows(&self) -> usize
fn num_rows(&self) -> usize
Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let toodee : TooDee<u32> = TooDee::new(10, 5);
assert_eq!(toodee.num_rows(), 5);
source§fn view(&self, start: Coordinate, end: Coordinate) -> TooDeeView<'_, T>
fn view(&self, start: Coordinate, end: Coordinate) -> TooDeeView<'_, T>
Examples
use toodee::{TooDee,TooDeeOps};
let toodee : TooDee<u32> = TooDee::new(10, 5);
let view = toodee.view((1,2), (8,4));
assert_eq!(view.num_cols(), 7);
assert_eq!(view.num_rows(), 2);
source§fn rows(&self) -> Rows<'_, T> ⓘ
fn rows(&self) -> Rows<'_, T> ⓘ
Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::new(10, 5);
let mut rows = toodee.rows();
assert_eq!(rows.len(), 5);
let r0 = rows.next().unwrap();
assert_eq!(r0.len(), 10);
source§fn col(&self, col: usize) -> Col<'_, T> ⓘ
fn col(&self, col: usize) -> Col<'_, T> ⓘ
Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::new(10, 5);
let mut col = toodee.col(8);
assert_eq!(col.len(), 5);
source§unsafe fn get_unchecked_row(&self, row: usize) -> &[T]
unsafe fn get_unchecked_row(&self, row: usize) -> &[T]
Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
unsafe {
let toodee : TooDee<u32> = TooDee::new(10, 5);
let row = toodee.get_unchecked_row(3);
assert_eq!(row.len(), 10);
}
source§unsafe fn get_unchecked(&self, coord: Coordinate) -> &T
unsafe fn get_unchecked(&self, coord: Coordinate) -> &T
Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let toodee : TooDee<u32> = TooDee::new(10, 5);
unsafe {
assert_eq!(*toodee.get_unchecked((1,3)), 0);
}
source§impl<T> TooDeeOpsMut<T> for TooDee<T>
impl<T> TooDeeOpsMut<T> for TooDee<T>
source§fn view_mut(
&mut self,
start: Coordinate,
end: Coordinate
) -> TooDeeViewMut<'_, T>
fn view_mut( &mut self, start: Coordinate, end: Coordinate ) -> TooDeeViewMut<'_, T>
Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::new(10, 5);
let view = toodee.view_mut((1,2), (8,4));
assert_eq!(view.num_cols(), 7);
assert_eq!(view.num_rows(), 2);
source§fn rows_mut(&mut self) -> RowsMut<'_, T> ⓘ
fn rows_mut(&mut self) -> RowsMut<'_, T> ⓘ
Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::new(10, 5);
let mut rows = toodee.rows_mut();
assert_eq!(rows.len(), 5);
let r0 = rows.next().unwrap();
assert_eq!(r0.len(), 10);
source§fn col_mut(&mut self, col: usize) -> ColMut<'_, T> ⓘ
fn col_mut(&mut self, col: usize) -> ColMut<'_, T> ⓘ
Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::new(10, 5);
let mut col = toodee.col_mut(8);
assert_eq!(col.len(), 5);
source§fn fill(&mut self, fill: T)where
T: Clone,
fn fill(&mut self, fill: T)where T: Clone,
Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::new(10, 5);
toodee.fill(42);
assert_eq!(toodee[1][1], 42);
source§fn swap_rows(&mut self, r1: usize, r2: usize)
fn swap_rows(&mut self, r1: usize, r2: usize)
Swap/exchange the data between two rows.
Panics
Panics if either row index is out of bounds.
Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::init(10, 5, 42u32);
toodee[0].iter_mut().for_each(|v| *v = 1);
assert_eq!(toodee[(0, 2)], 42);
toodee.swap_rows(0, 2);
assert_eq!(toodee[(0, 2)], 1);
source§unsafe fn get_unchecked_row_mut(&mut self, row: usize) -> &mut [T]
unsafe fn get_unchecked_row_mut(&mut self, row: usize) -> &mut [T]
Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
unsafe {
let mut toodee : TooDee<u32> = TooDee::new(10, 5);
let row = toodee.get_unchecked_row_mut(3);
assert_eq!(row.len(), 10);
}
source§unsafe fn get_unchecked_mut(&mut self, coord: Coordinate) -> &mut T
unsafe fn get_unchecked_mut(&mut self, coord: Coordinate) -> &mut T
Examples
use toodee::{TooDee,TooDeeOps,TooDeeOpsMut};
let mut toodee : TooDee<u32> = TooDee::new(10, 5);
unsafe {
assert_eq!(*toodee.get_unchecked_mut((1,3)), 0);
}
source§fn swap(&mut self, (col1, row1): Coordinate, (col2, row2): Coordinate)
fn swap(&mut self, (col1, row1): Coordinate, (col2, row2): Coordinate)
source§fn cells_mut(&mut self) -> CellsMut<'_, T>
fn cells_mut(&mut self) -> CellsMut<'_, T>
impl<T: Eq> Eq for TooDee<T>
impl<T> StructuralEq for TooDee<T>
impl<T> StructuralPartialEq for TooDee<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for TooDee<T>where T: RefUnwindSafe,
impl<T> Send for TooDee<T>where T: Send,
impl<T> Sync for TooDee<T>where T: Sync,
impl<T> Unpin for TooDee<T>where T: Unpin,
impl<T> UnwindSafe for TooDee<T>where T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T, O> SortOps<T> for Owhere
O: TooDeeOpsMut<T>,
impl<T, O> SortOps<T> for Owhere O: TooDeeOpsMut<T>,
source§fn sort_row_ord<F>(&mut self, row: usize)where
T: Ord,
fn sort_row_ord<F>(&mut self, row: usize)where T: Ord,
source§fn sort_unstable_row_ord<F>(&mut self, row: usize)where
T: Ord,
fn sort_unstable_row_ord<F>(&mut self, row: usize)where T: Ord,
source§fn sort_by_row<F>(&mut self, row: usize, compare: F)where
F: FnMut(&T, &T) -> Ordering,
fn sort_by_row<F>(&mut self, row: usize, compare: F)where F: FnMut(&T, &T) -> Ordering,
source§fn sort_unstable_by_row<F>(&mut self, row: usize, compare: F)where
F: FnMut(&T, &T) -> Ordering,
fn sort_unstable_by_row<F>(&mut self, row: usize, compare: F)where F: FnMut(&T, &T) -> Ordering,
source§fn sort_by_row_key<B, F>(&mut self, row: usize, f: F)where
B: Ord,
F: FnMut(&T) -> B,
fn sort_by_row_key<B, F>(&mut self, row: usize, f: F)where B: Ord, F: FnMut(&T) -> B,
source§fn sort_unstable_by_row_key<B, F>(&mut self, row: usize, f: F)where
B: Ord,
F: FnMut(&T) -> B,
fn sort_unstable_by_row_key<B, F>(&mut self, row: usize, f: F)where B: Ord, F: FnMut(&T) -> B,
source§fn sort_col_ord<F>(&mut self, col: usize)where
T: Ord,
fn sort_col_ord<F>(&mut self, col: usize)where T: Ord,
source§fn sort_by_col<F>(&mut self, col: usize, compare: F)where
F: FnMut(&T, &T) -> Ordering,
fn sort_by_col<F>(&mut self, col: usize, compare: F)where F: FnMut(&T, &T) -> Ordering,
source§fn sort_unstable_by_col<F>(&mut self, col: usize, compare: F)where
F: FnMut(&T, &T) -> Ordering,
fn sort_unstable_by_col<F>(&mut self, col: usize, compare: F)where F: FnMut(&T, &T) -> Ordering,
source§impl<T, O> TranslateOps<T> for Owhere
O: TooDeeOpsMut<T>,
impl<T, O> TranslateOps<T> for Owhere O: TooDeeOpsMut<T>,
source§fn translate_with_wrap(&mut self, mid: Coordinate)
fn translate_with_wrap(&mut self, mid: Coordinate)
mid
coordinate will be moved to (0, 0), and
all other elements will be moved in the same fashion. All the original data is preserved by
wrapping at the array edges. Read more