cleanups
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||
use std::fmt;
|
|
||||||
|
|
||||||
mod solver;
|
mod solver;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -2,6 +2,14 @@ use std::fmt;
|
|||||||
use std::clone::Clone;
|
use std::clone::Clone;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
type Variables<'a, V> = HashMap<String, Option<&'a V>>;
|
||||||
|
|
||||||
|
enum Assignment<'a, V> {
|
||||||
|
Update(String, &'a V),
|
||||||
|
Clear(String)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Domain<V> {
|
struct Domain<V> {
|
||||||
values: Vec<V>
|
values: Vec<V>
|
||||||
@@ -19,7 +27,6 @@ impl<V: fmt::Debug> fmt::Debug for Domain<V> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Variables<'a, V> = HashMap<String, Option<&'a V>>;
|
|
||||||
|
|
||||||
/// Returns all possible Updates for next assignements, prepended with
|
/// Returns all possible Updates for next assignements, prepended with
|
||||||
/// 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.
|
||||||
@@ -43,17 +50,6 @@ fn assign_next<'a,'b, V>(assign: &'b Variables<'a, V>, domain: &'a Domain<V>)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if any constraint is violated
|
|
||||||
fn check_consistency<'a, V>(assign: &'a Variables<'a, V>, constraint: fn(&Variables<'a,V>) -> bool) -> bool {
|
|
||||||
constraint(assign)
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Assignment<'a, V> {
|
|
||||||
Update(String, &'a V),
|
|
||||||
Clear(String)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Visit all possible solutions, using a stack.
|
/// Visit all possible solutions, using a stack.
|
||||||
fn solve_all<'a, V: Clone + fmt::Debug>(mut assign: Variables<'a, V>, domain: &'a Domain<V>, is_valid: fn(&Variables<'a,V>) -> bool)
|
fn solve_all<'a, V: Clone + fmt::Debug>(mut assign: Variables<'a, V>, domain: &'a Domain<V>, is_valid: fn(&Variables<'a,V>) -> bool)
|
||||||
-> Vec<Variables<'a, V>>
|
-> Vec<Variables<'a, V>>
|
||||||
@@ -110,5 +106,24 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(solve_all(assign, &domain, constraint), solutions);
|
assert_eq!(solve_all(assign, &domain, constraint), solutions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_solver_find_pairs_with_initial() {
|
||||||
|
use super::*;
|
||||||
|
// Find all pairs of two differents
|
||||||
|
let assign: Variables<i32> = [
|
||||||
|
("Left".to_string(), None),
|
||||||
|
("Right".to_string(), Some(&2)),
|
||||||
|
].iter().cloned().collect();
|
||||||
|
let domain = Domain::new(vec![1,2,3]);
|
||||||
|
let constraint = |assign: &Variables<i32>| {
|
||||||
|
assign.get("Left").unwrap() == assign.get("Right").unwrap()
|
||||||
|
};
|
||||||
|
let solutions: Vec<Variables<i32>> = vec![
|
||||||
|
[("Left".to_string(), Some(&2)), ("Right".to_string(), Some(&2)),].iter().cloned().collect(),
|
||||||
|
];
|
||||||
|
|
||||||
|
assert_eq!(solve_all(assign, &domain, constraint), solutions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user