How can I use a HashMap with f64 as key in Rust?

前端 未结 3 402
刺人心
刺人心 2020-12-06 17:08

I want to use a HashMap, for saving the distances of a point with known x and key y to another point. f64 as value shouldn\'t matte

3条回答
  •  星月不相逢
    2020-12-06 17:56

    Presented with no comment beyond read all the other comments and answers to understand why you probably don't want to do this:

    use std::hash;
    use std::collections::HashMap;
    
    #[derive(Debug, Copy, Clone)]
    struct DontUseThisUnlessYouUnderstandTheDangers(f64);
    
    impl DontUseThisUnlessYouUnderstandTheDangers {
        fn key(&self) -> u64 {
            unsafe { mem::transmute(self.0) }
        }
    }
    
    impl hash::Hash for DontUseThisUnlessYouUnderstandTheDangers {
        fn hash(&self, state: &mut H)
            where H: hash::Hasher
        {
            self.key().hash(state)
        }
    }
    
    impl PartialEq for DontUseThisUnlessYouUnderstandTheDangers {
        fn eq(&self, other: &DontUseThisUnlessYouUnderstandTheDangers) -> bool {
            self.key() == other.key()
        }
    }
    
    impl Eq for DontUseThisUnlessYouUnderstandTheDangers {}
    
    fn main() {
        let a = DontUseThisUnlessYouUnderstandTheDangers(0.1);
        let b = DontUseThisUnlessYouUnderstandTheDangers(0.2);
        let c = DontUseThisUnlessYouUnderstandTheDangers(0.3);
    
        let mut map = HashMap::new();
        map.insert(a, 1);
        map.insert(b, 2);
    
        println!("{:?}", map.get(&a));
        println!("{:?}", map.get(&b));
        println!("{:?}", map.get(&c));
    }
    

    Basically, if you want to treat a f64 as a set of bits that have no meaning, well, we can treat them as an equivalently sized bag of bits that know how to be hashed and bitwise-compared.

    Don't be surprised when one of the 16 million NaN values doesn't equal another one.

提交回复
热议问题