use std::fmt::Debug;
use solverforge_core::domain::PlanningSolution;
use solverforge_scoring::Director;
use super::Move;
pub struct ListChangeMove<S, V> {
source_entity_index: usize,
source_position: usize,
dest_entity_index: usize,
dest_position: usize,
list_len: fn(&S, usize) -> usize,
list_remove: fn(&mut S, usize, usize) -> Option<V>,
list_insert: fn(&mut S, usize, usize, V),
variable_name: &'static str,
descriptor_index: usize,
indices: [usize; 2],
}
impl<S, V> Clone for ListChangeMove<S, V> {
fn clone(&self) -> Self {
*self
}
}
impl<S, V> Copy for ListChangeMove<S, V> {}
impl<S, V: Debug> Debug for ListChangeMove<S, V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ListChangeMove")
.field("source_entity", &self.source_entity_index)
.field("source_position", &self.source_position)
.field("dest_entity", &self.dest_entity_index)
.field("dest_position", &self.dest_position)
.field("variable_name", &self.variable_name)
.finish()
}
}
impl<S, V> ListChangeMove<S, V> {
#[allow(clippy::too_many_arguments)]
pub fn new(
source_entity_index: usize,
source_position: usize,
dest_entity_index: usize,
dest_position: usize,
list_len: fn(&S, usize) -> usize,
list_remove: fn(&mut S, usize, usize) -> Option<V>,
list_insert: fn(&mut S, usize, usize, V),
variable_name: &'static str,
descriptor_index: usize,
) -> Self {
Self {
source_entity_index,
source_position,
dest_entity_index,
dest_position,
list_len,
list_remove,
list_insert,
variable_name,
descriptor_index,
indices: [source_entity_index, dest_entity_index],
}
}
pub fn source_entity_index(&self) -> usize {
self.source_entity_index
}
pub fn source_position(&self) -> usize {
self.source_position
}
pub fn dest_entity_index(&self) -> usize {
self.dest_entity_index
}
pub fn dest_position(&self) -> usize {
self.dest_position
}
pub fn is_intra_list(&self) -> bool {
self.source_entity_index == self.dest_entity_index
}
}
impl<S, V> Move<S> for ListChangeMove<S, V>
where
S: PlanningSolution,
V: Clone + PartialEq + Send + Sync + Debug + 'static,
{
fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
let solution = score_director.working_solution();
let source_len = (self.list_len)(solution, self.source_entity_index);
if self.source_position >= source_len {
return false;
}
let dest_len = (self.list_len)(solution, self.dest_entity_index);
let max_dest = if self.is_intra_list() {
source_len - 1 } else {
dest_len
};
if self.dest_position > max_dest {
return false;
}
if self.is_intra_list() {
if self.source_position == self.dest_position {
return false;
}
if self.dest_position == self.source_position + 1 {
return false;
}
}
true
}
fn do_move<D: Director<S>>(&self, score_director: &mut D) {
score_director.before_variable_changed(self.descriptor_index, self.source_entity_index);
if !self.is_intra_list() {
score_director.before_variable_changed(self.descriptor_index, self.dest_entity_index);
}
let value = (self.list_remove)(
score_director.working_solution_mut(),
self.source_entity_index,
self.source_position,
)
.expect("source position should be valid");
let adjusted_dest = if self.is_intra_list() && self.dest_position > self.source_position {
self.dest_position - 1
} else {
self.dest_position
};
(self.list_insert)(
score_director.working_solution_mut(),
self.dest_entity_index,
adjusted_dest,
value.clone(),
);
score_director.after_variable_changed(self.descriptor_index, self.source_entity_index);
if !self.is_intra_list() {
score_director.after_variable_changed(self.descriptor_index, self.dest_entity_index);
}
let list_remove = self.list_remove;
let list_insert = self.list_insert;
let src_entity = self.source_entity_index;
let src_pos = self.source_position;
let dest_entity = self.dest_entity_index;
score_director.register_undo(Box::new(move |s: &mut S| {
let removed = list_remove(s, dest_entity, adjusted_dest).unwrap();
list_insert(s, src_entity, src_pos, removed);
}));
}
fn descriptor_index(&self) -> usize {
self.descriptor_index
}
fn entity_indices(&self) -> &[usize] {
if self.is_intra_list() {
&self.indices[0..1]
} else {
&self.indices
}
}
fn variable_name(&self) -> &str {
self.variable_name
}
}
#[cfg(test)]
#[path = "list_change_tests.rs"]
mod tests;