Skip to main content

dijkstra_suite/
path.rs

1//! Standardized path types and behaviours
2//!
3//! ## The`Path` type
4//!
5//! The `Path` type is the standardized way to represent a path across `dijkstra_suite`
6//! and the rest of the world
7//!
8//! A `Path` is represented by two properties, `weight` and `steps`
9//!
10//! The `weight` props holds the total weight (or, cost) of the path
11//!
12//! The `steps` prop holds an ordered list of node ids that represent the actual path
13//!
14//! ```rust
15//! use dijkstra_suite::path::Path;
16//!
17//! let mut path: Path<(i32, i32), i32> = Path::default();
18//! println!("path: {:?}", path);
19//!
20//! path.steps.push((4, 2));
21//! println!("path.weight: {:?}", path.weight);
22//! println!("path.steps: {:?}", path.steps);
23//!
24//! let expected_steps = vec![(4, 2)];
25//! assert_eq!(path.steps, expected_steps);
26//! ```
27
28use crate::node::{NodeId, NodeWeight};
29
30#[derive(Debug, Default)]
31pub struct Path<I: NodeId, W: NodeWeight> {
32    pub weight: W,
33    pub steps: Vec<I>,
34}
35
36impl<I: NodeId, W: NodeWeight> From<(W, Vec<I>)> for Path<I, W> {
37    fn from(value: (W, Vec<I>)) -> Self {
38        let (weight, steps) = value;
39
40        Path { weight, steps }
41    }
42}
43
44impl<I: NodeId, W: NodeWeight> PartialEq for Path<I, W> {
45    fn eq(&self, other: &Self) -> bool {
46        self.weight == other.weight && self.steps == other.steps
47    }
48}
49
50#[cfg(test)]
51mod test {
52    use std::collections::HashMap;
53
54    use crate::path::Path;
55
56    #[test]
57    fn test_path_struct() {
58        let mut path: Path<(i32, i32), i32> = Path::default();
59        println!("path: {:?}", path);
60
61        path.steps.push((4, 2));
62
63        let expected_steps = vec![(4, 2)];
64        println!("path.steps: {:?}", path.steps);
65        assert_eq!(path.steps, expected_steps);
66
67        // path.steps.push(NodeId(4, 2));
68
69        for step in path.steps {
70            println!("step: {:?}", step);
71        }
72
73        // assert_eq!(1, 2)
74    }
75}