first step towards weekly planning

This commit is contained in:
artus
2018-12-24 15:44:19 +01:00
parent f0bc06d9ed
commit 8236b8ac9f
7 changed files with 99 additions and 17 deletions

View File

@@ -2,7 +2,7 @@ use std::fmt;
use std::clone::Clone;
use std::collections::HashMap;
type Variables<'a, V> = HashMap<String, Option<&'a V>>;
pub type Variables<'a, V> = HashMap<String, Option<&'a V>>;
enum Assignment<'a, V> {
Update(String, &'a V),
@@ -11,12 +11,12 @@ enum Assignment<'a, V> {
#[derive(Clone)]
struct Domain<V> {
pub struct Domain<V> {
values: Vec<V>
}
impl<V> Domain<V> {
fn new(values: Vec<V>) -> Domain<V> {
pub fn new(values: Vec<V>) -> Domain<V> {
Domain { values }
}
}
@@ -35,6 +35,7 @@ fn assign_next<'a,'b, V>(assign: &'b Variables<'a, V>, domain: &'a Domain<V>)
where V: fmt::Debug
{
// Panics on empty domain
// If domain values are filtered, then the branch is a dead end
if domain.values.is_empty() { panic!("No values in domain : {:?}", domain); };
// TODO: should be able to inject a choosing strategy
@@ -51,8 +52,12 @@ fn assign_next<'a,'b, V>(assign: &'b Variables<'a, V>, domain: &'a Domain<V>)
}
/// 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)
-> Vec<Variables<'a, V>>
pub fn solve_all<'a, V>(
mut assign: Variables<'a, V>,
domain: &'a Domain<V>,
is_valid: fn(&Variables<'a,V>) -> bool
) -> Vec<Variables<'a, V>>
where V: Clone + fmt::Debug
{
let mut solutions: Vec<Variables<V>> = vec![];
let mut stack: Vec<Assignment<'a, V>> = vec![];