Add is empty
This commit is contained in:
parent
5d8495b21f
commit
405f2fb1de
1 changed files with 38 additions and 15 deletions
53
src/lib.rs
53
src/lib.rs
|
@ -27,11 +27,7 @@ macro_rules! count {
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! grid {
|
macro_rules! grid {
|
||||||
() => {
|
() => {
|
||||||
Grid {
|
$crate::Grid::from_vec(vec![], 0)
|
||||||
rows: 0,
|
|
||||||
cols: 0,
|
|
||||||
data: vec![],
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
( [$( $x:expr ),* ]) => { {
|
( [$( $x:expr ),* ]) => { {
|
||||||
let vec = vec![$($x),*];
|
let vec = vec![$($x),*];
|
||||||
|
@ -116,21 +112,25 @@ impl<T: Clone> Grid<T> {
|
||||||
/// Grid::from_vec(vec![1,2,3,4,5], 3);
|
/// Grid::from_vec(vec![1,2,3,4,5], 3);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn from_vec(vec: Vec<T>, cols: usize) -> Grid<T> {
|
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 {
|
if cols == 0 {
|
||||||
return grid![];
|
return Grid {
|
||||||
|
data: vec![],
|
||||||
|
rows: 0,
|
||||||
|
cols: 0,
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
panic!("Vector length is zero, but cols is {:?}", cols);
|
panic!("Vector length is zero, but cols is {:?}", cols);
|
||||||
}
|
}
|
||||||
}
|
} else if rows % cols != 0 {
|
||||||
if vec.len() % cols != 0 {
|
|
||||||
panic!("Vector length must be a multiple of cols.");
|
panic!("Vector length must be a multiple of cols.");
|
||||||
}
|
} else {
|
||||||
let rows = vec.len();
|
Grid {
|
||||||
Grid {
|
data: vec,
|
||||||
data: vec,
|
rows: rows / cols,
|
||||||
rows: rows / cols,
|
cols: cols,
|
||||||
cols: cols,
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,6 +184,17 @@ impl<T: Clone> Grid<T> {
|
||||||
pub fn cols(&self) -> usize {
|
pub fn cols(&self) -> usize {
|
||||||
self.cols
|
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> {
|
impl<T: Clone> Clone for Grid<T> {
|
||||||
|
@ -234,6 +245,18 @@ impl<T: fmt::Debug> fmt::Debug for Grid<T> {
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
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]
|
#[test]
|
||||||
fn fmt_empty() {
|
fn fmt_empty() {
|
||||||
let grid: Grid<u8> = grid![];
|
let grid: Grid<u8> = grid![];
|
||||||
|
|
Loading…
Reference in a new issue