Implement `Hash` for all `Matrix<N, R, C>` where `N: Hash`.

Implements #508.
This commit is contained in:
Jack Wrenn 2018-12-27 12:05:25 -05:00 committed by Sébastien Crozet
parent 8b8f127f8d
commit 53632cd1b4
1 changed files with 23 additions and 1 deletions

View File

@ -7,6 +7,7 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq};
use std::any::TypeId; use std::any::TypeId;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fmt; use std::fmt;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData; use std::marker::PhantomData;
use std::mem; use std::mem;
@ -72,7 +73,7 @@ pub type MatrixCross<N, R1, C1, R2, C2> =
/// dynamically-sized column vector should be represented as a `Matrix<N, Dynamic, U1, S>` (given /// dynamically-sized column vector should be represented as a `Matrix<N, Dynamic, U1, S>` (given
/// some concrete types for `N` and a compatible data storage type `S`). /// some concrete types for `N` and a compatible data storage type `S`).
#[repr(C)] #[repr(C)]
#[derive(Hash, Clone, Copy)] #[derive(Clone, Copy)]
pub struct Matrix<N: Scalar, R: Dim, C: Dim, S> { pub struct Matrix<N: Scalar, R: Dim, C: Dim, S> {
/// The data storage that contains all the matrix components and informations about its number /// The data storage that contains all the matrix components and informations about its number
/// of rows and column (if needed). /// of rows and column (if needed).
@ -1482,3 +1483,24 @@ where
self.as_ref().ulps_eq(other.as_ref(), epsilon, max_ulps) self.as_ref().ulps_eq(other.as_ref(), epsilon, max_ulps)
} }
} }
impl<N, R, C, S> Hash for Matrix<N, R, C, S>
where
N: Scalar + Hash,
R: Dim,
C: Dim,
S: Storage<N, R, C>,
{
fn hash<H: Hasher>(&self, state: &mut H) {
let (nrows, ncols) = self.shape();
(nrows, ncols).hash(state);
for j in 0..ncols {
for i in 0..nrows {
unsafe {
self.get_unchecked(i, j).hash(state);
}
}
}
}
}