Crossbeam skipmap vs hashmap

The following code works as expected and prints the value after the get().

    fn test_skipmap() {
        let mut map: HashMap<Arc<Vector>, String> = HashMap::new();
        let v = Arc::new(Vector::randomized(3));
        let v2 = Arc::new(Vector::randomized(3));
        let v3 = Arc::new(Vector::randomized(3));

        map.insert(Arc::clone(&v), "Harry".into());
        map.insert(Arc::clone(&v2), "Neville".into());
        map.insert(Arc::clone(&v3), "Ron".into());

        println!("map len {}", map.len());

        let val = map.get(Arc::clone(&v).as_ref());
        if let Some(val) = val {
            println!("val is {}", val);
        }
    }

Vector is some struct that implements all the requisite traits to be used as a key in a HashMap. However if I change the HashMap to be Crossbeam SkipMap my get() always fails. What am I missing?

Thanks in advance.

Crossbeam’s SkipMap is an ordered map. This requires the keys to implement Ord, which is a requirement different from the Eq + Hash that hashmaps require for their keys.

But maybe your issue is unrelated to this. In any case, you should better explain what exactly you mean with “my get() always fails”. “Fails” as in compilation error or panic, or does it return None? If it’s the latter, you could try making the Ord implementation explicit (in case it isn’t already – also, rust analyzer can help with that whilst keeping the default behavior of the derived one) and add some println debugging to it to see if it gets called properly and behaves as expected; if it’s a compilation error or panic, you should share the error.

1 Like

What happens if you replace HashMap with BTreeMap?

How exactly did you implement the traits? My first guess would be that the Ord implementation is inconsistent.

2 Likes

Sorry. get() returns a None and yes it works fine when I use a BTreeMap in place of a HashMap.

Yes you were correct. The Ord was inconsistent. But surprisingly it worked with BTreeMap and never with SkipMap. Thanks for the pointer.

1 Like