adds solve_one
This commit is contained in:
@@ -144,6 +144,39 @@ impl<'a,V, K: Eq + Hash + Clone> Problem<'a, V, K> {
|
||||
};
|
||||
solutions
|
||||
}
|
||||
|
||||
pub fn solve_one(&mut self) -> Option<Variables<'a,V,K>>
|
||||
where V: Clone + fmt::Debug,
|
||||
K: Clone + fmt::Debug,
|
||||
{
|
||||
let mut stack: Vec<Assignment<'a, V, K>> = vec![];
|
||||
stack.append(&mut self._push_updates().unwrap());
|
||||
loop {
|
||||
let node = stack.pop();
|
||||
if node.is_none() {
|
||||
return None;
|
||||
};
|
||||
match node.unwrap() {
|
||||
Assignment::Update(key, val) => {
|
||||
// Assign the variable and open new branches, if any.
|
||||
*self.variables.get_mut(&key).unwrap() = Some(val);
|
||||
// TODO: handle case of empty domain.values
|
||||
if let Some(mut nodes) = self._push_updates() {
|
||||
stack.append(&mut nodes);
|
||||
} else {
|
||||
// Assignements are completed
|
||||
if self._is_valid() {
|
||||
return Some(self.variables.clone());
|
||||
};
|
||||
};
|
||||
},
|
||||
Assignment::Clear(key) => {
|
||||
// We are closing this branch, unset the variable
|
||||
*self.variables.get_mut(&key).unwrap() = None;
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ProblemBuilder<'a, V, K>(Problem<'a, V, K>);
|
||||
|
||||
@@ -103,19 +103,20 @@ mod api {
|
||||
let mut problem = problem
|
||||
.add_constraint(|_| true)
|
||||
.finish();
|
||||
let results = problem.solve_all();
|
||||
let one_result = results.first().unwrap();
|
||||
|
||||
Json(TemplateObject {
|
||||
items: one_result
|
||||
.into_iter()
|
||||
.map(|(k,v)| {
|
||||
TemplateItems {
|
||||
key: (format!("{}", k.0), format!("{:?}", k.1)),
|
||||
value: v.map(|r| RecipeObject::from(&conn, r.clone())),
|
||||
}})
|
||||
.collect(),
|
||||
})
|
||||
if let Some(one_result) = problem.solve_one() {
|
||||
Json(TemplateObject {
|
||||
items: one_result
|
||||
.into_iter()
|
||||
.map(|(k,v)| {
|
||||
TemplateItems {
|
||||
key: (format!("{}", k.0), format!("{:?}", k.1)),
|
||||
value: v.map(|r| RecipeObject::from(&conn, r.clone())),
|
||||
}})
|
||||
.collect(),
|
||||
})
|
||||
} else {
|
||||
panic!("No solution at all !");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user