use crate::expander::Command;
#[derive(Debug, Clone)]
pub struct AgentCommand {
pub agent_id: u32,
pub command: Command,
}
#[derive(Debug, Clone)]
pub struct TimelineStep {
pub step: usize,
pub agent_commands: Vec<AgentCommand>,
}
pub struct Scheduler;
impl Scheduler {
pub fn schedule(agents: &[(u32, Vec<Command>)]) -> Vec<TimelineStep> {
if agents.is_empty() {
return Vec::new();
}
let max_len = agents.iter().map(|(_, cmds)| cmds.len()).max().unwrap_or(0);
let mut timeline = Vec::with_capacity(max_len);
for step in 0..max_len {
let mut agent_commands = Vec::new();
for (agent_id, commands) in agents {
if let Some(cmd) = commands.get(step) {
agent_commands.push(AgentCommand {
agent_id: *agent_id,
command: *cmd,
});
}
}
timeline.push(TimelineStep {
step,
agent_commands,
});
}
timeline
}
pub fn max_steps(agents: &[(u32, Vec<Command>)]) -> usize {
agents.iter().map(|(_, cmds)| cmds.len()).max().unwrap_or(0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_single_agent() {
let agents = vec![(0, vec![Command::Straight, Command::Right, Command::Left])];
let timeline = Scheduler::schedule(&agents);
assert_eq!(timeline.len(), 3);
assert_eq!(timeline[0].agent_commands.len(), 1);
assert_eq!(timeline[0].agent_commands[0].agent_id, 0);
assert_eq!(timeline[0].agent_commands[0].command, Command::Straight);
}
#[test]
fn test_multiple_agents_same_length() {
let agents = vec![
(0, vec![Command::Straight, Command::Right]),
(1, vec![Command::Left, Command::Straight]),
];
let timeline = Scheduler::schedule(&agents);
assert_eq!(timeline.len(), 2);
assert_eq!(timeline[0].agent_commands.len(), 2);
assert_eq!(timeline[1].agent_commands.len(), 2);
}
#[test]
fn test_multiple_agents_different_length() {
let agents = vec![
(0, vec![Command::Straight, Command::Right, Command::Left]),
(1, vec![Command::Left]),
];
let timeline = Scheduler::schedule(&agents);
assert_eq!(timeline.len(), 3);
assert_eq!(timeline[0].agent_commands.len(), 2);
assert_eq!(timeline[1].agent_commands.len(), 1); assert_eq!(timeline[2].agent_commands.len(), 1); }
#[test]
fn test_empty_agents() {
let agents: Vec<(u32, Vec<Command>)> = vec![];
let timeline = Scheduler::schedule(&agents);
assert!(timeline.is_empty());
}
#[test]
fn test_max_steps() {
let agents = vec![
(0, vec![Command::Straight, Command::Right, Command::Left]),
(1, vec![Command::Left]),
];
assert_eq!(Scheduler::max_steps(&agents), 3);
}
}