Add is empty
This commit is contained in:
parent
5d8495b21f
commit
405f2fb1de
1 changed files with 38 additions and 15 deletions
45
src/lib.rs
45
src/lib.rs
|
@ -27,11 +27,7 @@ macro_rules! count {
|
|||
#[macro_export]
|
||||
macro_rules! grid {
|
||||
() => {
|
||||
Grid {
|
||||
rows: 0,
|
||||
cols: 0,
|
||||
data: vec![],
|
||||
}
|
||||
$crate::Grid::from_vec(vec![], 0)
|
||||
};
|
||||
( [$( $x:expr ),* ]) => { {
|
||||
let vec = vec![$($x),*];
|
||||
|
@ -116,23 +112,27 @@ impl<T: Clone> Grid<T> {
|
|||
/// Grid::from_vec(vec![1,2,3,4,5], 3);
|
||||
/// ```
|
||||
pub fn from_vec(vec: Vec<T>, cols: usize) -> Grid<T> {
|
||||
if vec.len() == 0 {
|
||||
let rows = vec.len();
|
||||
if rows == 0 {
|
||||
if cols == 0 {
|
||||
return grid![];
|
||||
return Grid {
|
||||
data: vec![],
|
||||
rows: 0,
|
||||
cols: 0,
|
||||
};
|
||||
} else {
|
||||
panic!("Vector length is zero, but cols is {:?}", cols);
|
||||
}
|
||||
}
|
||||
if vec.len() % cols != 0 {
|
||||
} else if rows % cols != 0 {
|
||||
panic!("Vector length must be a multiple of cols.");
|
||||
}
|
||||
let rows = vec.len();
|
||||
} else {
|
||||
Grid {
|
||||
data: vec,
|
||||
rows: rows / cols,
|
||||
cols: cols,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a reference to an element, without performing bound checks.
|
||||
/// Generally not recommended, use with caution!
|
||||
|
@ -184,6 +184,17 @@ impl<T: Clone> Grid<T> {
|
|||
pub fn cols(&self) -> usize {
|
||||
self.cols
|
||||
}
|
||||
|
||||
/// Returns true if the grid contains no elements.
|
||||
/// For example:
|
||||
/// ```
|
||||
/// use grid::*;
|
||||
/// let grid : Grid<u8> = grid![];
|
||||
/// assert!(grid.is_empty());
|
||||
/// ```
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.cols == 0 && self.rows == 0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone> Clone for Grid<T> {
|
||||
|
@ -234,6 +245,18 @@ impl<T: fmt::Debug> fmt::Debug for Grid<T> {
|
|||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn is_empty_false() {
|
||||
let grid: Grid<u8> = grid![[1, 2, 3]];
|
||||
assert!(!grid.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_empty_true() {
|
||||
let grid: Grid<u8> = grid![];
|
||||
assert!(grid.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fmt_empty() {
|
||||
let grid: Grid<u8> = grid![];
|
||||
|
|
Loading…
Reference in a new issue