use std::fmt::Debug;
use solverforge_core::domain::PlanningSolution;
use solverforge_scoring::Director;
use super::Move;
pub struct SwapMove<S, V> {
left_entity_index: usize,
right_entity_index: usize,
getter: fn(&S, usize) -> Option<V>,
setter: fn(&mut S, usize, Option<V>),
variable_name: &'static str,
descriptor_index: usize,
indices: [usize; 2],
}
impl<S, V> Clone for SwapMove<S, V> {
fn clone(&self) -> Self {
*self
}
}
impl<S, V> Copy for SwapMove<S, V> {}
impl<S, V: Debug> Debug for SwapMove<S, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SwapMove")
.field("left_entity_index", &self.left_entity_index)
.field("right_entity_index", &self.right_entity_index)
.field("descriptor_index", &self.descriptor_index)
.field("variable_name", &self.variable_name)
.finish()
}
}
impl<S, V> SwapMove<S, V> {
pub fn new(
left_entity_index: usize,
right_entity_index: usize,
getter: fn(&S, usize) -> Option<V>,
setter: fn(&mut S, usize, Option<V>),
variable_name: &'static str,
descriptor_index: usize,
) -> Self {
Self {
left_entity_index,
right_entity_index,
getter,
setter,
variable_name,
descriptor_index,
indices: [left_entity_index, right_entity_index],
}
}
pub fn left_entity_index(&self) -> usize {
self.left_entity_index
}
pub fn right_entity_index(&self) -> usize {
self.right_entity_index
}
}
impl<S, V> Move<S> for SwapMove<S, V>
where
S: PlanningSolution,
V: Clone + PartialEq + Send + Sync + Debug + 'static,
{
fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
if self.left_entity_index == self.right_entity_index {
return false;
}
let left_val = (self.getter)(score_director.working_solution(), self.left_entity_index);
let right_val = (self.getter)(score_director.working_solution(), self.right_entity_index);
left_val != right_val
}
fn do_move<D: Director<S>>(&self, score_director: &mut D) {
let left_value = (self.getter)(score_director.working_solution(), self.left_entity_index);
let right_value = (self.getter)(score_director.working_solution(), self.right_entity_index);
score_director.before_variable_changed(self.descriptor_index, self.left_entity_index);
score_director.before_variable_changed(self.descriptor_index, self.right_entity_index);
(self.setter)(
score_director.working_solution_mut(),
self.left_entity_index,
right_value.clone(),
);
(self.setter)(
score_director.working_solution_mut(),
self.right_entity_index,
left_value.clone(),
);
score_director.after_variable_changed(self.descriptor_index, self.left_entity_index);
score_director.after_variable_changed(self.descriptor_index, self.right_entity_index);
let setter = self.setter;
let left_idx = self.left_entity_index;
let right_idx = self.right_entity_index;
score_director.register_undo(Box::new(move |s: &mut S| {
setter(s, left_idx, left_value);
setter(s, right_idx, right_value);
}));
}
fn descriptor_index(&self) -> usize {
self.descriptor_index
}
fn entity_indices(&self) -> &[usize] {
&self.indices
}
fn variable_name(&self) -> &str {
self.variable_name
}
}