diff --git a/CHANGELOG.md b/CHANGELOG.md index 1459650..2f9544e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,6 @@ - Add `dijkstra::mssp`. - Add `dijkstra::sssp`. -- Implement `CountAllVertices` for slices. -- Implement `EdgeWeight` for slices. - Implement `Indegree` for slices. - Implement `IsEdge` for slices. - Implement `IterEdges` for slices. @@ -28,6 +26,7 @@ - Implement `CountAllEdges` for `[HashSet]`. - Implement `CountAllEdges` for `[HashMap]`. - Implement `CountAllVertices` for `[T]`. +- Implement `EdgeWeight` for `[HashMap]`. ## [0.6.3] - 2024-04-06 diff --git a/src/op/edge_weight.rs b/src/op/edge_weight.rs index c2ba413..4115b26 100644 --- a/src/op/edge_weight.rs +++ b/src/op/edge_weight.rs @@ -97,6 +97,17 @@ where } } +// Slice + +impl EdgeWeight for [HashMap] +where + H: BuildHasher, +{ + fn edge_weight(&self, s: usize, t: usize) -> Option<&W> { + self.get(s).and_then(|m| m.get(&t)) + } +} + // Arr impl EdgeWeight for [HashMap; V] @@ -138,6 +149,21 @@ mod tests { assert_eq!(graph.edge_weight(2, 1), Some(&8)); } + #[test] + fn slice() { + let graph: &[HashMap] = &[ + HashMap::from([(1, 2), (2, 3)]), + HashMap::from([(0, 4)]), + HashMap::from([(0, 7), (1, 8)]), + ]; + + assert_eq!(graph.edge_weight(0, 1), Some(&2)); + assert_eq!(graph.edge_weight(0, 2), Some(&3)); + assert_eq!(graph.edge_weight(1, 0), Some(&4)); + assert_eq!(graph.edge_weight(2, 0), Some(&7)); + assert_eq!(graph.edge_weight(2, 1), Some(&8)); + } + #[test] fn arr() { let graph = [