use alloc::vec::Vec;
use crate::graph::Vertex;
use crate::path::Time;
pub(super) type JointState = Vec<(Vertex, bool)>;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(super) struct Key {
pub(super) time: Time,
pub(super) base: JointState,
pub(super) moved: JointState,
}
impl Key {
pub(super) fn standard(time: Time, base: JointState) -> Self {
Self {
time,
base,
moved: Vec::new(),
}
}
pub(super) fn is_standard(&self) -> bool {
self.moved.is_empty()
}
pub(super) fn is_goal(&self) -> bool {
self.is_standard() && self.base.iter().all(|&(_, finished)| finished)
}
pub(super) fn positions(&self) -> impl Iterator<Item = (Vertex, bool)> + '_ {
self.moved
.iter()
.chain(&self.base[self.moved.len()..])
.copied()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct Choice {
pub(super) next: Vertex,
pub(super) finished: bool,
pub(super) cost: u32,
}
impl Choice {
pub(super) fn finish(vertex: Vertex) -> Self {
Self {
next: vertex,
finished: true,
cost: 0,
}
}
pub(super) fn move_to(next: Vertex) -> Self {
Self {
next,
finished: false,
cost: 1,
}
}
pub(super) fn between(from: (Vertex, bool), to: (Vertex, bool)) -> Self {
debug_assert!(!from.1 || to == from, "finished agents stay finished");
if to.1 {
Self::finish(to.0)
} else {
Self::move_to(to.0)
}
}
pub(super) fn position(self) -> (Vertex, bool) {
(self.next, self.finished)
}
}
pub(super) fn collide(a: (Vertex, Vertex), b: (Vertex, Vertex)) -> bool {
let same = a.1 == b.1;
let swap = a.0 != a.1 && a.0 == b.1 && a.1 == b.0;
same || swap
}