1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109

use core::hash::Hash;
use crate::Vec2;

const HASH_PRIME: u32 = 31;

//////////     FLOATS    ////////////
impl Hash for Vec2<f32> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        let xu = unsafe { *(&self.x as *const _ as *const u32) };
        let yu = unsafe { *(&self.y as *const _ as *const u32) };

        xu.hash(state);
        HASH_PRIME.hash(state);
        yu.hash(state);
        HASH_PRIME.hash(state);
    }
}

impl Hash for Vec2<f64> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        let xu = unsafe { *(&self.x as *const _ as *const u64) };
        let yu = unsafe { *(&self.y as *const _ as *const u64) };

        xu.hash(state);
        HASH_PRIME.hash(state);
        yu.hash(state);
        HASH_PRIME.hash(state);
    }
}
// END OF -- FLOATS    /////////////


///////////// SIGNED INTEGERS ////////////
impl Hash for Vec2<i32> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.x.hash(state);
        HASH_PRIME.hash(state);
        self.y.hash(state);
        HASH_PRIME.hash(state);
    }
}

impl Hash for Vec2<i64> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.x.hash(state);
        HASH_PRIME.hash(state);
        self.y.hash(state);
        HASH_PRIME.hash(state);
    }
}
// END OF --  SIGNED INTEGERS ////////////




/////////// UNSIGNED INTEGERS ////////////
impl Hash for Vec2<u32> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.x.hash(state);
        HASH_PRIME.hash(state);
        self.y.hash(state);
        HASH_PRIME.hash(state);
    }
}

impl Hash for Vec2<u64> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.x.hash(state);
        HASH_PRIME.hash(state);
        self.y.hash(state);
        HASH_PRIME.hash(state);
    }
}
// END OF --UNSIGNED INTEGERS ////////////


#[cfg(test)]
mod tests {
    use std::collections::{HashSet};

    use crate::Vec2;

    #[test]
    fn test_hashset() {
        let mut set = HashSet::new();
        let v = Vec2::<f32>::new(2.0, 4.0);
        set.insert(v.clone());
        assert!(set.contains(&v));

        let mut set = HashSet::new();
        let v = Vec2::<f64>::new(2.0, 4.0);
        set.insert(v.clone());
        assert!(set.contains(&v));
    }

    #[test]
    fn test_hashset_integers() {
        let mut set = HashSet::new();
        let v = Vec2::<i32>::new(2, 4);
        set.insert(v.clone());
        assert!(set.contains(&v));

        let mut set = HashSet::new();
        let v = Vec2::<i64>::new(2, 4);
        set.insert(v.clone());
        assert!(set.contains(&v));
    }
}