works on Domain docs and api

This commit is contained in:
2019-02-14 21:11:52 +01:00
parent 3e477945ea
commit bb965413a8
2 changed files with 47 additions and 8 deletions

View File

@@ -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> {

View File

@@ -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 {