Skip to content

Commit

Permalink
fix: make Grid hashable if T is hashable
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii authored and becheran committed Dec 16, 2023
1 parent 664db9a commit 02f1dea
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ macro_rules! grid_cm {
}

/// Define the internal memory layout of the grid.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Order {
/// The data is ordered row by row.
Expand Down Expand Up @@ -1425,6 +1425,16 @@ impl<T: Clone> Clone for Grid<T> {
}
}

impl<T: std::hash::Hash> std::hash::Hash for Grid<T> {
#[inline]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.rows.hash(state);
self.cols.hash(state);
self.order.hash(state);
self.data.hash(state);
}
}

impl<T> Index<(usize, usize)> for Grid<T> {
type Output = T;

Expand Down Expand Up @@ -2511,6 +2521,15 @@ mod test {
test_grid(&clone, 2, 3, Order::RowMajor, &[1, 2, 10, 4, 5, 6]);
}

#[test]
fn hash() {
let mut set = std::collections::HashSet::new();
set.insert(grid![[1,2,3][4,5,6]]);
set.insert(grid![[1,3,3][4,5,6]]);
set.insert(grid![[1,2,3][4,5,6]]);
assert_eq!(set.len(), 2);
}

#[test]
fn macro_init() {
let grid = grid![[1, 2, 3][4, 5, 6]];
Expand Down

0 comments on commit 02f1dea

Please sign in to comment.