Skip to content

Commit

Permalink
task: implement EdgeWeight for [HashMap<usize, W>]
Browse files Browse the repository at this point in the history
  • Loading branch information
basdirks-purple committed Apr 7, 2024
1 parent 6226c03 commit b7a680d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -28,6 +26,7 @@
- Implement `CountAllEdges` for `[HashSet<T>]`.
- Implement `CountAllEdges` for `[HashMap<K, W>]`.
- Implement `CountAllVertices` for `[T]`.
- Implement `EdgeWeight` for `[HashMap<usize, W>]`.

## [0.6.3] - 2024-04-06

Expand Down
26 changes: 26 additions & 0 deletions src/op/edge_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ where
}
}

// Slice

impl<W, H> EdgeWeight<W> for [HashMap<usize, W, H>]
where
H: BuildHasher,
{
fn edge_weight(&self, s: usize, t: usize) -> Option<&W> {
self.get(s).and_then(|m| m.get(&t))
}
}

// Arr

impl<const V: usize, W, H> EdgeWeight<W> for [HashMap<usize, W, H>; V]
Expand Down Expand Up @@ -138,6 +149,21 @@ mod tests {
assert_eq!(graph.edge_weight(2, 1), Some(&8));
}

#[test]
fn slice() {
let graph: &[HashMap<usize, i32>] = &[
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 = [
Expand Down

0 comments on commit b7a680d

Please sign in to comment.