Skip to main content

a_star/
path.rs

1use std::fmt::Debug;
2use std::hash::Hash;
3
4/// A node path.
5#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
6pub struct Path<Node, Cost>
7where
8    Cost: Copy + Default,
9{
10    nodes: Vec<Node>,
11    cost: Cost,
12}
13
14impl<Node, Cost> Path<Node, Cost>
15where
16    Cost: Copy + Default,
17{
18    //! Construction
19
20    /// Creates a new path with the given initial `capacity`.
21    pub fn new(capacity: usize) -> Self {
22        Self {
23            nodes: Vec::with_capacity(capacity),
24            cost: Cost::default(),
25        }
26    }
27}
28
29impl<Node, Cost> Path<Node, Cost>
30where
31    Cost: Copy + Default,
32{
33    //! Properties
34
35    /// Gets the nodes.
36    pub fn nodes(&self) -> &[Node] {
37        self.nodes.as_slice()
38    }
39
40    /// Gets the path cost.
41    pub fn cost(&self) -> Cost {
42        self.cost
43    }
44}
45
46impl<Node, Cost> Path<Node, Cost>
47where
48    Cost: Copy + Default,
49{
50    //! Mutations
51
52    /// Clears the path.
53    pub fn clear(&mut self) {
54        self.nodes.clear();
55        self.cost = Cost::default();
56    }
57
58    /// Sets the cost.
59    pub fn set_cost(&mut self, cost: Cost) {
60        self.cost = cost;
61    }
62
63    /// Adds the node.
64    pub fn push(&mut self, node: Node) {
65        self.nodes.push(node);
66    }
67
68    /// Reverses the nodes.
69    pub fn reverse(&mut self) {
70        self.nodes.reverse();
71    }
72}