use alloc::vec::Vec;
use core::fmt;
use core::marker::PhantomData;
use crate::best_first::GraphResult;
use crate::problem::Successors;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum Direction {
Forward = 0,
Backward = 1,
}
impl Direction {
pub(crate) const fn opposite(self) -> Self {
match self {
Self::Forward => Self::Backward,
Self::Backward => Self::Forward,
}
}
pub(crate) const fn index(self) -> usize {
self as usize
}
}
pub trait MeetRelation<S, N = S> {
fn insert_forward(&mut self, id: usize, state: &S);
fn insert_backward(&mut self, id: usize, node: &N);
fn meets_forward(&self, state: &S, emit: impl FnMut(usize));
fn meets_backward(&self, node: &N, emit: impl FnMut(usize));
}
impl<S, N, M: MeetRelation<S, N> + ?Sized> MeetRelation<S, N> for &mut M {
fn insert_forward(&mut self, id: usize, state: &S) {
(**self).insert_forward(id, state);
}
fn insert_backward(&mut self, id: usize, node: &N) {
(**self).insert_backward(id, node);
}
fn meets_forward(&self, state: &S, emit: impl FnMut(usize)) {
(**self).meets_forward(state, emit);
}
fn meets_backward(&self, node: &N, emit: impl FnMut(usize)) {
(**self).meets_backward(node, emit);
}
}
#[derive(Debug, Clone)]
pub struct MeetFn<F, S, N> {
relation: F,
forward: Vec<(usize, S)>,
backward: Vec<(usize, N)>,
}
pub fn meet_fn<S, N, F: Fn(&S, &N) -> bool>(relation: F) -> MeetFn<F, S, N> {
MeetFn {
relation,
forward: Vec::new(),
backward: Vec::new(),
}
}
impl<S: Clone, N: Clone, F: Fn(&S, &N) -> bool> MeetRelation<S, N> for MeetFn<F, S, N> {
fn insert_forward(&mut self, id: usize, state: &S) {
self.forward.push((id, state.clone()));
}
fn insert_backward(&mut self, id: usize, node: &N) {
self.backward.push((id, node.clone()));
}
fn meets_forward(&self, state: &S, mut emit: impl FnMut(usize)) {
for (id, node) in &self.backward {
if (self.relation)(state, node) {
emit(*id);
}
}
}
fn meets_backward(&self, node: &N, mut emit: impl FnMut(usize)) {
for (id, state) in &self.forward {
if (self.relation)(state, node) {
emit(*id);
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplayError<S, A> {
pub state: S,
pub action: A,
}
impl<S, A> fmt::Display for ReplayError<S, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a backward action has no matching successor edge from the meeting path")
}
}
impl<S: fmt::Debug, A: fmt::Debug> core::error::Error for ReplayError<S, A> {}
pub type MeetResult<G> =
Result<GraphResult<G>, ReplayError<<G as Successors>::State, <G as Successors>::Action>>;
pub(crate) trait Meets<T> {
fn insert(&mut self, direction: Direction, id: usize, node: &T);
fn meets(&self, direction: Direction, node: &T, emit: impl FnMut(usize));
}
pub(crate) struct Equality;
impl<S> Meets<S> for Equality {
fn insert(&mut self, _: Direction, _: usize, _: &S) {}
fn meets(&self, _: Direction, _: &S, _: impl FnMut(usize)) {}
}
pub(crate) type ReplayFn<G> = fn(
&G,
&<G as Successors>::State,
&<G as Successors>::Action,
) -> Option<(<G as Successors>::State, <G as Successors>::Cost)>;
pub(crate) struct Meeting<G: Successors, M> {
pub(crate) relation: M,
pub(crate) replay: ReplayFn<G>,
graph: PhantomData<fn(&G)>,
}
impl<G: Successors, M> Meeting<G, M> {
pub(crate) fn new(relation: M) -> Self
where
G::Action: PartialEq,
G::State: Clone,
{
Self {
relation,
replay: replay_step::<G>,
graph: PhantomData,
}
}
}
fn replay_step<G>(graph: &G, state: &G::State, action: &G::Action) -> Option<(G::State, G::Cost)>
where
G: Successors,
G::Action: PartialEq,
G::State: Clone,
{
let mut found = None;
graph.successors(state, |label, next, cost| {
if found.is_none() && label == *action {
found = Some((next, cost));
}
});
found
}