code cleanup

This commit is contained in:
2019-02-14 21:34:31 +01:00
parent bb965413a8
commit 04e8c554cc

View File

@@ -101,6 +101,12 @@ impl<V: fmt::Debug> fmt::Debug for Domain<V> {
pub type Constraint<'a,V, K> = fn(&Variables<'a,V, K>) -> bool; pub type Constraint<'a,V, K> = fn(&Variables<'a,V, K>) -> bool;
/// Could be more efficient to just use fixed array of options as variables,
/// using a helper to bind Keys to Index in this array.
/// Domains could be a similar array of DomainValues.
/// It makes sense with an array where indexing is O(1)
pub struct Problem<'a, V, K> { pub struct Problem<'a, V, K> {
/// The initial assignements map /// The initial assignements map
variables: Variables<'a, V, K>, variables: Variables<'a, V, K>,
@@ -126,22 +132,37 @@ impl<'a,V, K: Eq + Hash + Clone> Problem<'a, V, K> {
/// a Clear to ensure the variable is unset before when leaving the branch. /// a Clear to ensure the variable is unset before when leaving the branch.
fn _push_updates(&self) -> Option<Vec<Assignment<'a,V, K>>> { fn _push_updates(&self) -> Option<Vec<Assignment<'a,V, K>>> {
// TODO: should be able to inject a choosing strategy // TODO: should be able to inject a choosing strategy
if let Some((key,_)) = self.variables.iter().find(|(_, val)| val.is_none()) { if let Some(key) = self._next_assign() {
let domain_values = self.domains.get(key).expect("No domain for variable !"); let domain_values = self.domains
.get(key)
.expect("No domain for variable !");
assert!(!domain_values.is_empty());
// Push a clear assignment first, just before going up the stack. // Push a clear assignment first, just before going up the stack.
let mut updates = vec![Assignment::Clear(key.clone())]; let mut updates = vec![Assignment::Clear(key.clone())];
if domain_values.is_empty() { panic!("No value in domain !"); }
// TODO: should be able to filter domain values (inference, pertinence) // TODO: should be able to filter domain values (inference, pertinence)
for value in domain_values.into_iter() { domain_values.iter().for_each(|value| {
updates.push(Assignment::Update(key.clone(), *value)); updates.push(
} Assignment::Update(
key.clone(),
*value
)
);
});
Some(updates) Some(updates)
} else { // End of assignements } else { // End of assignements
None None
} }
} }
fn _next_assign(&self) -> Option<&K> {
self.variables
.iter()
.find_map(|(key, val)| {
if val.is_none() { Some(key) }
else { None }
})
}
/// Checks that the current assignments doesn't violate any constraint /// Checks that the current assignments doesn't violate any constraint
fn _is_valid(&self) -> bool { fn _is_valid(&self) -> bool {
for validator in self.constraints.iter() { for validator in self.constraints.iter() {