use alloc::collections::{BTreeSet, BinaryHeap};
use alloc::vec::Vec;
use core::cmp::Reverse;
use search_core::{Cost, Heuristic};
use crate::cost::add;
use crate::graph::Graph;
use crate::output::{Solution, Stats};
pub(crate) fn lex_first<G, H, C, P>(
graph: &G,
heuristic: &mut H,
start: &G::State,
is_goal: &P,
first: usize,
stats: &mut Stats,
) -> Option<Solution<G::State, [C; 2]>>
where
G: Graph<Weight = [C; 2]>,
H: Heuristic<G::State, [C; 2]>,
C: Cost,
P: Fn(&G::State) -> bool,
{
let key = |f: [C; 2]| [f[first], f[1 - first]];
let mut nodes: Vec<(G::State, [C; 2], Option<usize>)> = Vec::new();
let mut open = BinaryHeap::new();
let mut closed = BTreeSet::new();
let mut successors = Vec::new();
let h = heuristic.estimate(start)?;
nodes.push((start.clone(), [C::ZERO; 2], None));
open.push(Reverse((key(h), 0)));
stats.generated += 1;
while let Some(Reverse((_, index))) = open.pop() {
let (here, g, _) = nodes[index].clone();
if !closed.insert(here.clone()) {
continue;
}
if is_goal(&here) {
return Some(rebuild(&nodes, index));
}
stats.expanded += 1;
graph.successors(&here, &mut successors);
for (next, weight) in successors.drain(..) {
if closed.contains(&next) {
continue;
}
let Some(h) = heuristic.estimate(&next) else {
continue;
};
let g_next = add(g, weight);
nodes.push((next, g_next, Some(index)));
open.push(Reverse((key(add(g_next, h)), nodes.len() - 1)));
stats.generated += 1;
}
}
None
}
fn rebuild<S: Clone, C: Copy>(
nodes: &[(S, [C; 2], Option<usize>)],
index: usize,
) -> Solution<S, [C; 2]> {
let mut path = Vec::new();
let mut current = Some(index);
while let Some(i) = current {
path.push(nodes[i].0.clone());
current = nodes[i].2;
}
path.reverse();
Solution {
cost: nodes[index].1,
path,
}
}