1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//! The lazy open list of best-first search ([`OpenList`]): a binary min-heap with lazy
//! deletion.
use alloc::collections::BinaryHeap;
use core::cmp::Ordering;
use crate::space::NodeId;
/// One open-list entry.
///
/// Ordered by smallest [`Entry::key`], then largest [`Entry::g`] (deeper nodes first, which
/// usually reaches the goal sooner among equal keys), then smallest [`Entry::seq`] (first
/// in, first out), so the expansion order is deterministic.
struct Entry<K, C> {
/// The priority, such as `f = g + h` for A*.
key: K,
/// The path cost of the node when this entry was pushed. An entry whose `g` no longer
/// matches the node's best `g` is outdated and skipped.
g: C,
/// Push counter; breaks the remaining ties.
seq: u64,
/// The node this entry stands for.
node: NodeId,
}
impl<K: Ord, C: Ord> Entry<K, C> {
/// Ordering where the entry to pop first is the *greatest*, as [`BinaryHeap`] wants.
fn priority_cmp(&self, other: &Self) -> Ordering {
other
.key
.cmp(&self.key)
.then_with(|| self.g.cmp(&other.g))
.then_with(|| other.seq.cmp(&self.seq))
}
}
impl<K: Ord, C: Ord> PartialEq for Entry<K, C> {
fn eq(&self, other: &Self) -> bool {
self.priority_cmp(other).is_eq()
}
}
impl<K: Ord, C: Ord> Eq for Entry<K, C> {}
impl<K: Ord, C: Ord> PartialOrd for Entry<K, C> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<K: Ord, C: Ord> Ord for Entry<K, C> {
fn cmp(&self, other: &Self) -> Ordering {
self.priority_cmp(other)
}
}
/// A min-heap of nodes keyed by `K`, with path costs of type `C` for liveness checks.
///
/// Decrease-key is done by pushing a new entry; the outdated one stays in the heap and is
/// recognised on pop because its `g` differs from the node's current `g`. Callers that
/// need stricter liveness (for example "not closed yet") pass it in the `live` predicate.
pub(crate) struct OpenList<K, C = K> {
/// The entries.
heap: BinaryHeap<Entry<K, C>>,
/// Number of pushes so far; the next entry's [`Entry::seq`].
pushes: u64,
}
impl<K: Ord + Copy, C: Ord + Copy> OpenList<K, C> {
/// Creates an empty open list.
pub(crate) fn new() -> Self {
Self {
heap: BinaryHeap::new(),
pushes: 0,
}
}
/// Opens `node` with priority `key` and current path cost `g`.
pub(crate) fn push(&mut self, node: NodeId, key: K, g: C) {
self.heap.push(Entry {
key,
g,
seq: self.pushes,
node,
});
self.pushes += 1;
}
/// Pops the entry with the smallest key whose `g` still equals `current_g(node)`.
///
/// Outdated entries are dropped on the way. Returns `None` once the heap is empty.
pub(crate) fn pop_live(&mut self, current_g: impl Fn(NodeId) -> C) -> Option<NodeId> {
self.pop_where(|node, g| g.cmp(¤t_g(node)).is_eq())
.map(|(node, _)| node)
}
/// Pops the smallest entry for which `live(node, g)` holds, dropping the others on the
/// way, and returns its node and key.
pub(crate) fn pop_where(&mut self, live: impl Fn(NodeId, C) -> bool) -> Option<(NodeId, K)> {
self.drop_dead(live);
self.heap.pop().map(|entry| (entry.node, entry.key))
}
/// Returns the node and key of the smallest entry for which `live(node, g)` holds,
/// without removing it. Dead entries above it are dropped.
pub(crate) fn peek_where(&mut self, live: impl Fn(NodeId, C) -> bool) -> Option<(NodeId, K)> {
self.drop_dead(live);
self.heap.peek().map(|entry| (entry.node, entry.key))
}
/// Removes every entry.
pub(crate) fn clear(&mut self) {
self.heap.clear();
}
/// Pops entries off the top until the top entry is live or the heap is empty.
fn drop_dead(&mut self, live: impl Fn(NodeId, C) -> bool) {
while self
.heap
.peek()
.is_some_and(|entry| !live(entry.node, entry.g))
{
self.heap.pop();
}
}
}