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
//! The search-space interface: successor generation and the zero heuristic
//! for cost vectors.
use alloc::vec::Vec;
use search_core::{Cost, Heuristic};
/// An implicit graph with vector-valued edge weights.
///
/// Successors are generated on demand, so the graph never needs to be
/// materialized.
pub trait Graph {
/// A search state. `Ord` keys the per-state bookkeeping (a `BTreeMap`,
/// since the crate is `no_std`).
type State: Clone + Ord;
/// The cost vector of one edge, for example `[u32; 2]`.
type Weight;
/// Appends every outgoing edge of `state` to `out` as
/// `(successor, weight)`.
///
/// `out` is empty on entry; the caller clears and reuses it between
/// calls. Parallel edges and self-loops are allowed.
fn successors(&self, state: &Self::State, out: &mut Vec<(Self::State, Self::Weight)>);
}
/// A [`Graph`] that can also enumerate incoming edges, for bidirectional
/// search.
///
/// The predecessors must mirror the successors exactly: `(p, w)` is
/// reported by `predecessors(s)` once for every `(s, w)` that
/// `successors(p)` reports, parallel edges included.
pub trait ReverseGraph: Graph {
/// Appends every incoming edge of `state` to `out` as
/// `(predecessor, weight)`, where `weight` is the cost of the edge from
/// `predecessor` to `state`.
///
/// `out` is empty on entry; the caller clears and reuses it between
/// calls.
fn predecessors(&self, state: &Self::State, out: &mut Vec<(Self::State, Self::Weight)>);
}
/// A [`Graph`] whose edges have identities, for problems that must tell
/// parallel edges apart: disjoint and dissimilar paths.
///
/// An additive extension: graphs that only implement [`Graph`] keep working
/// with every other search. `edges` must report the same edges as
/// [`Graph::successors`], each with an identity unique in the graph.
pub trait EdgeGraph: Graph {
/// An edge identity, unique over the whole graph.
type Edge: Clone + Ord;
/// Appends every outgoing edge of `state` to `out` as
/// `(edge, successor, weight)`. `out` is empty on entry.
fn edges(&self, state: &Self::State, out: &mut Vec<(Self::Edge, Self::State, Self::Weight)>);
}
/// The zero heuristic for cost vectors: no estimate, the search is
/// uninformed.
///
/// Consistent for any graph with non-negative costs. `search_core::Zero`
/// covers scalar costs only, so cost vectors `[C; N]` get this local one.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ZeroHeuristic;
impl<S, C: Cost, const N: usize> Heuristic<S, [C; N]> for ZeroHeuristic {
fn estimate(&mut self, _state: &S) -> Option<[C; N]> {
Some([C::ZERO; N])
}
}