Derive Hash for many data types

This commit is contained in:
tali 2022-12-14 15:03:43 -05:00
parent 096189373c
commit 8291a07ee4
3 changed files with 16 additions and 8 deletions

View File

@ -8,7 +8,7 @@ use crate::piece::{Piece, Rot, Shape};
/// ///
/// DAS inputs are not included, since functionally DAS is equivalent to repeated taps, /// DAS inputs are not included, since functionally DAS is equivalent to repeated taps,
/// and a bot can effectively hypertap infinitely fast during its analysis. /// and a bot can effectively hypertap infinitely fast during its analysis.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
pub enum Movement { pub enum Movement {
/// "Sonic" drop. /// "Sonic" drop.
Drop, Drop,
@ -37,7 +37,7 @@ impl Movement {
} }
/// Represents a direction of horizontal movement. /// Represents a direction of horizontal movement.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
#[repr(i16)] #[repr(i16)]
pub enum Horizontal { pub enum Horizontal {
/// Left. /// Left.
@ -56,7 +56,7 @@ pub enum Horizontal {
/// # use mino::{piece::Rot, input::Spin}; /// # use mino::{piece::Rot, input::Spin};
/// assert_eq!(Rot::N + Spin::Cw, Rot::E); /// assert_eq!(Rot::N + Spin::Cw, Rot::E);
/// ``` /// ```
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
#[repr(i8)] #[repr(i8)]
pub enum Spin { pub enum Spin {
/// Clockwise. /// Clockwise.

View File

@ -5,7 +5,7 @@ use crate::matrix::Mat;
use core::ops::Range; use core::ops::Range;
/// Represents a location for a piece, including its orientation. /// Represents a location for a piece, including its orientation.
#[derive(Copy, Clone, Eq, PartialEq, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Loc { pub struct Loc {
/// Horizontal coordinate. /// Horizontal coordinate.
pub x: i16, pub x: i16,
@ -31,7 +31,7 @@ impl From<(i16, i16)> for Loc {
/// Represents a rotation state for a piece. The initial state is "north" (`N`), and there /// Represents a rotation state for a piece. The initial state is "north" (`N`), and there
/// are 4 total orientations to represent each 90 degree turn possible. /// are 4 total orientations to represent each 90 degree turn possible.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Default)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Default, Hash)]
#[repr(i8)] #[repr(i8)]
pub enum Rot { pub enum Rot {
/// North; the initial state. /// North; the initial state.
@ -78,6 +78,7 @@ pub struct Cells<'c> {
} }
impl PartialEq for Cells<'_> { impl PartialEq for Cells<'_> {
#[inline]
fn eq(&self, rhs: &Self) -> bool { fn eq(&self, rhs: &Self) -> bool {
// XXX(iitalics): identical 'data' should imply identical width; width is only // XXX(iitalics): identical 'data' should imply identical width; width is only
// stored to make bounds tests faster but could theoretically be derived from the // stored to make bounds tests faster but could theoretically be derived from the
@ -87,6 +88,13 @@ impl PartialEq for Cells<'_> {
} }
} }
impl core::hash::Hash for Cells<'_> {
#[inline]
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
(self.x, self.y, self.data()).hash(state)
}
}
impl<'c> Cells<'c> { impl<'c> Cells<'c> {
/// Constructs cells from the initial extents and rowwise bit representations of the /// Constructs cells from the initial extents and rowwise bit representations of the
/// occupied cells. /// occupied cells.
@ -183,7 +191,7 @@ impl core::fmt::Debug for Cells<'_> {
} }
/// Represents the current state of a piece. /// Represents the current state of a piece.
#[derive(Clone, Eq, PartialEq, Debug)] #[derive(Clone, Eq, PartialEq, Debug, Hash)]
pub struct Piece<S> { pub struct Piece<S> {
/// The shape of the piece. /// The shape of the piece.
pub shape: S, pub shape: S,
@ -238,7 +246,7 @@ pub mod test {
// .X. // .X.
// .XX origin at (1,1) // .XX origin at (1,1)
// ... // ...
#[derive(Copy, Clone, Eq, PartialEq, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Tri; pub struct Tri;
impl Shape<'static> for Tri { impl Shape<'static> for Tri {

View File

@ -7,7 +7,7 @@ mod code_gen {
include!(concat!(env!("OUT_DIR"), "/srs.rs")); include!(concat!(env!("OUT_DIR"), "/srs.rs"));
} }
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
#[repr(u8)] #[repr(u8)]
pub enum Shape { pub enum Shape {
I = code_gen::shape_indices::I as u8, I = code_gen::shape_indices::I as u8,