Compare commits
2 Commits
3e477945ea
...
04e8c554cc
| Author | SHA1 | Date | |
|---|---|---|---|
| 04e8c554cc | |||
| bb965413a8 |
+73
-14
@@ -14,12 +14,14 @@ enum Assignment<'a, V, K> {
|
||||
Clear(K)
|
||||
}
|
||||
|
||||
|
||||
/// Collection of references to values owned by a domain.
|
||||
pub type DomainValues<'a, V> = Vec<&'a V>;
|
||||
/// The domain of values that can be assigned to variables
|
||||
|
||||
/// The domain of values that can be assigned to a variable.
|
||||
/// The values are owned by the instance.
|
||||
#[derive(Clone)]
|
||||
pub struct Domain<V> {
|
||||
pub values: Vec<V>
|
||||
values: Vec<V>
|
||||
}
|
||||
|
||||
impl<V> Domain<V> {
|
||||
@@ -27,11 +29,21 @@ impl<V> Domain<V> {
|
||||
Domain { values }
|
||||
}
|
||||
|
||||
/// Returns all values of a Domain instance
|
||||
/// Returns references to all values of this instance
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate planner;
|
||||
/// # use planner::solver::Domain;
|
||||
/// let domain = Domain::new(vec!["a", "b", "c"]);
|
||||
/// assert_eq!(domain.all(), vec![&"a", &"b", &"c"]);
|
||||
/// ```
|
||||
pub fn all(&self) -> DomainValues<V> {
|
||||
self.values.iter().collect()
|
||||
}
|
||||
/// Returns a Filter filter applied to inner values
|
||||
/// Returns a collection of references to a filtered
|
||||
/// subset of this domain.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
@@ -45,13 +57,39 @@ impl<V> Domain<V> {
|
||||
/// assert_eq!(domain.filter(even), vec![&2]);
|
||||
/// assert_eq!(domain.filter(|i: &i32| i % 2 == 1), vec![&1,&3]);
|
||||
/// ```
|
||||
pub fn filter(&self, filter_func: fn(&V) -> bool) -> DomainValues<V> {
|
||||
pub fn filter<F>(&self, filter_func: F) -> DomainValues<V>
|
||||
where F: Fn(&V) -> bool
|
||||
{
|
||||
self.values
|
||||
.iter()
|
||||
.filter(|v: &&V| filter_func(*v))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Wrapper for `find`, returns a optionnal reference
|
||||
/// to the first found value of this domain.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # extern crate planner;
|
||||
/// # use planner::solver::Domain;
|
||||
/// let domain = Domain::new(vec![1,2,3]);
|
||||
/// fn even(i: &i32) -> bool {
|
||||
/// *i == 2
|
||||
/// };
|
||||
/// assert_eq!(domain.find(even), Some(&2));
|
||||
/// assert_eq!(domain.find(|i: &i32| i % 2 == 1), Some(&1));
|
||||
/// assert_eq!(domain.find(|i| *i == 4), None);
|
||||
/// ```
|
||||
pub fn find<F>(&self, getter_func: F) -> Option<&V>
|
||||
where F: Fn(&V) -> bool
|
||||
{
|
||||
self.values
|
||||
.iter()
|
||||
.find(|v: &&V| getter_func(*v))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl<V: fmt::Debug> fmt::Debug for Domain<V> {
|
||||
@@ -63,6 +101,12 @@ impl<V: fmt::Debug> fmt::Debug for Domain<V> {
|
||||
|
||||
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> {
|
||||
/// The initial assignements map
|
||||
variables: Variables<'a, V, K>,
|
||||
@@ -72,7 +116,7 @@ pub struct Problem<'a, V, K> {
|
||||
constraints: Vec<Constraint<'a,V,K>>,
|
||||
}
|
||||
|
||||
impl<'a,V, K: Eq + Hash + Clone> Problem<'a, V, K> {
|
||||
impl<'a, V, K: Eq + Hash + Clone> Problem<'a, V, K> {
|
||||
|
||||
pub fn build() -> ProblemBuilder<'a,V, K> {
|
||||
ProblemBuilder::new()
|
||||
@@ -88,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.
|
||||
fn _push_updates(&self) -> Option<Vec<Assignment<'a,V, K>>> {
|
||||
// TODO: should be able to inject a choosing strategy
|
||||
if let Some((key,_)) = self.variables.iter().find(|(_, val)| val.is_none()) {
|
||||
let domain_values = self.domains.get(key).expect("No domain for variable !");
|
||||
if let Some(key) = self._next_assign() {
|
||||
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.
|
||||
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)
|
||||
for value in domain_values.into_iter() {
|
||||
updates.push(Assignment::Update(key.clone(), *value));
|
||||
}
|
||||
domain_values.iter().for_each(|value| {
|
||||
updates.push(
|
||||
Assignment::Update(
|
||||
key.clone(),
|
||||
*value
|
||||
)
|
||||
);
|
||||
});
|
||||
Some(updates)
|
||||
} else { // End of assignements
|
||||
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
|
||||
fn _is_valid(&self) -> bool {
|
||||
for validator in self.constraints.iter() {
|
||||
|
||||
+3
-2
@@ -151,7 +151,8 @@ mod api {
|
||||
}
|
||||
});
|
||||
let ini = if let Some(id) = initial_id {
|
||||
let new_ini = domain.values.iter().find(|r| r.id == id);
|
||||
// Not working because we're capturing `id`
|
||||
let new_ini = domain.find(|r| {r.id == id});
|
||||
println!("Overrided {:?}", new_ini);
|
||||
new_ini
|
||||
} else {
|
||||
@@ -184,7 +185,7 @@ fn main() -> Result<(), Error> {
|
||||
let (allowed_origins, failed_origins) = AllowedOrigins::some(&["http://localhost:8080"]);
|
||||
assert!(failed_origins.is_empty());
|
||||
|
||||
// You can also deserialize this
|
||||
// You can also deserialize this
|
||||
let cors = rocket_cors::CorsOptions {
|
||||
allowed_origins: allowed_origins,
|
||||
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
|
||||
|
||||
Reference in New Issue
Block a user