use alloc::vec::Vec;
use crate::graph::Vertex;
pub type Time = u32;
pub type Path = Vec<Vertex>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Objective {
SumOfCosts,
Makespan,
}
#[must_use]
pub fn position_at(path: &[Vertex], time: Time) -> Vertex {
let index = usize::try_from(time).unwrap_or(usize::MAX);
path.get(index)
.or(path.last())
.copied()
.expect("a path has at least one position")
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Reservation {
Visit {
vertex: Vertex,
time: Time,
},
Move {
from: Vertex,
to: Vertex,
time: Time,
},
Park {
vertex: Vertex,
time: Time,
},
}
pub(crate) fn reservations(path: &[Vertex]) -> impl Iterator<Item = Reservation> + '_ {
let time = |index: usize| Time::try_from(index).expect("path lengths fit in u32 time steps");
let last = path.len() - 1;
let visits = path[..last]
.iter()
.enumerate()
.map(move |(index, &vertex)| Reservation::Visit {
vertex,
time: time(index),
});
let moves = path
.windows(2)
.enumerate()
.filter(|(_, step)| step[0] != step[1])
.map(move |(index, step)| Reservation::Move {
from: step[0],
to: step[1],
time: time(index),
});
let park = Reservation::Park {
vertex: path[last],
time: time(last),
};
visits.chain(moves).chain(core::iter::once(park))
}
pub(crate) fn trim_trailing_waits(path: &mut Path) {
while path.len() > 1 && path[path.len() - 2] == path[path.len() - 1] {
path.pop();
}
}
#[must_use]
pub fn path_cost(path: &[Vertex]) -> u32 {
let Some(&last) = path.last() else {
return 0;
};
let trailing = path.iter().rev().take_while(|&&v| v == last).count();
let arrival = path.len() - trailing;
u32::try_from(arrival).expect("path lengths fit in u32 time steps")
}
#[must_use]
pub fn sum_of_costs<P: AsRef<[Vertex]>>(paths: &[P]) -> u32 {
paths.iter().map(|path| path_cost(path.as_ref())).sum()
}
#[must_use]
pub fn makespan<P: AsRef<[Vertex]>>(paths: &[P]) -> u32 {
paths
.iter()
.map(|path| path_cost(path.as_ref()))
.max()
.unwrap_or(0)
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
#[test]
fn cost_counts_until_final_arrival() {
assert_eq!(path_cost(&[3]), 0);
assert_eq!(path_cost(&[0, 1, 2]), 2);
assert_eq!(path_cost(&[0, 1, 1, 2, 2, 2]), 3);
assert_eq!(path_cost(&[2, 1, 2]), 2);
assert_eq!(sum_of_costs(&[vec![0, 1], vec![5, 5, 4]]), 3);
assert_eq!(makespan(&[vec![0, 1], vec![5, 5, 4]]), 2);
}
#[test]
fn reserves_visits_moves_and_park() {
let reserved: Vec<Reservation> = reservations(&[3, 3, 4]).collect();
assert_eq!(
reserved,
[
Reservation::Visit { vertex: 3, time: 0 },
Reservation::Visit { vertex: 3, time: 1 },
Reservation::Move {
from: 3,
to: 4,
time: 1
},
Reservation::Park { vertex: 4, time: 2 },
]
);
}
#[test]
fn trims_only_trailing_waits() {
let mut path = vec![1, 1, 2, 2, 2];
trim_trailing_waits(&mut path);
assert_eq!(path, [1, 1, 2]);
let mut single = vec![4, 4];
trim_trailing_waits(&mut single);
assert_eq!(single, [4]);
}
#[test]
fn position_stays_at_goal() {
assert_eq!(position_at(&[0, 1, 2], 1), 1);
assert_eq!(position_at(&[0, 1, 2], 10), 2);
}
}