rustworkx_core/
distancemap.rs1use std::hash::Hash;
20
21use petgraph::graph::IndexType;
22
23use crate::dictmap::*;
24use hashbrown::HashMap;
25
26pub trait DistanceMap<K, V> {
28 fn build(num_elements: usize) -> Self;
30
31 fn get_item(&self, pos: K) -> Option<&V>;
34
35 fn put_item(&mut self, pos: K, val: V);
37}
38
39impl<K: IndexType, V: Clone> DistanceMap<K, V> for Vec<Option<V>> {
40 #[inline]
41 fn build(num_elements: usize) -> Self {
42 vec![None; num_elements]
43 }
44
45 #[inline]
46 fn get_item(&self, pos: K) -> Option<&V> {
47 self[pos.index()].as_ref()
48 }
49
50 #[inline]
51 fn put_item(&mut self, pos: K, val: V) {
52 self[pos.index()] = Some(val);
53 }
54}
55
56impl<K: Eq + Hash, V: Clone> DistanceMap<K, V> for DictMap<K, V> {
57 #[inline]
58 fn build(_num_elements: usize) -> Self {
59 DictMap::<K, V>::default()
60 }
61
62 #[inline]
63 fn get_item(&self, pos: K) -> Option<&V> {
64 self.get(&pos)
65 }
66
67 #[inline]
68 fn put_item(&mut self, pos: K, val: V) {
69 self.insert(pos, val);
70 }
71}
72
73impl<K: Eq + Hash, V: Clone> DistanceMap<K, V> for HashMap<K, V> {
74 #[inline]
75 fn build(_num_elements: usize) -> Self {
76 HashMap::<K, V>::default()
77 }
78
79 #[inline]
80 fn get_item(&self, pos: K) -> Option<&V> {
81 self.get(&pos)
82 }
83
84 #[inline]
85 fn put_item(&mut self, pos: K, val: V) {
86 self.insert(pos, val);
87 }
88}