rshyper_algo/astar/
priority_node.rs

1/*
2    appellation: priority_node <module>
3    authors: @FL03
4*/
5use core::cmp::Ordering;
6use rshyper::idx::{RawIndex, VertexId};
7
8/// Priority queue node for A* algorithm
9#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
10#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
11pub struct PriorityNode<P = i64, Idx = usize>
12where
13    Idx: RawIndex,
14{
15    pub(crate) vertex: VertexId<Idx>,
16    pub(crate) priority: P, // Negative f_score for min-heap behavior
17}
18
19impl<P, Idx> PriorityNode<P, Idx>
20where
21    Idx: RawIndex,
22{
23    /// Create a new priority node with the given vertex and priority
24    pub fn new(vertex: VertexId<Idx>, priority: P) -> Self {
25        Self { vertex, priority }
26    }
27    /// returns an immutable reference to the priority of the node
28    pub const fn priority(&self) -> &P {
29        &self.priority
30    }
31    /// returns an immutable reference to the vertex of the node
32    pub const fn vertex(&self) -> &VertexId<Idx> {
33        &self.vertex
34    }
35}
36
37impl<P, Idx> PartialEq<P> for PriorityNode<P, Idx>
38where
39    P: PartialEq,
40    Idx: RawIndex + PartialEq,
41{
42    fn eq(&self, other: &P) -> bool {
43        self.priority() == other
44    }
45}
46
47impl<P, Idx> Ord for PriorityNode<P, Idx>
48where
49    P: Ord,
50    Idx: RawIndex + Ord,
51{
52    fn cmp(&self, other: &Self) -> Ordering {
53        // Reverse ordering to create a min-heap (lowest f_score has highest priority)
54        other.priority().cmp(&self.priority)
55    }
56}
57
58impl<P, Idx> PartialOrd for PriorityNode<P, Idx>
59where
60    P: PartialOrd,
61    Idx: RawIndex + PartialOrd,
62{
63    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
64        // Reverse ordering to create a min-heap (lowest f_score has highest priority)
65        other.priority().partial_cmp(&self.priority)
66    }
67}
68
69impl<P, Idx> PartialOrd<P> for PriorityNode<P, Idx>
70where
71    P: PartialOrd,
72    Idx: RawIndex + PartialOrd,
73{
74    fn partial_cmp(&self, other: &P) -> Option<Ordering> {
75        // Reverse ordering to create a min-heap (lowest f_score has highest priority)
76        other.partial_cmp(self.priority())
77    }
78}