Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Predicate leaper [WIP] #20

Merged
merged 2 commits into from
Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ mod join;
mod map;
mod treefrog;
pub use treefrog::{
extend_anti::ExtendAnti, extend_with::ExtendWith, filter_anti::FilterAnti,
filter_with::FilterWith, Leaper, RelationLeaper,
extend_anti::ExtendAnti,
extend_with::ExtendWith,
filter_anti::FilterAnti,
filter_with::FilterWith,
filters::{PrefixFilter, ValueFilter},
Leaper, RelationLeaper,
};

/// A static, ordered list of key-value pairs.
Expand Down
84 changes: 84 additions & 0 deletions src/treefrog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,90 @@ pub trait Leaper<'a, Tuple, Val> {
fn intersect(&mut self, prefix: &Tuple, values: &mut Vec<&'a Val>);
}

pub mod filters {

use super::Leaper;

/// A treefrog leaper based on a per-prefix predicate.
pub struct PrefixFilter<Tuple, Func: Fn(&Tuple) -> bool> {
phantom: ::std::marker::PhantomData<Tuple>,
predicate: Func,
}

impl<'a, Tuple, Func> PrefixFilter<Tuple, Func>
where
Func: Fn(&Tuple) -> bool,
{
/// Creates a new filter based on the prefix
pub fn from(predicate: Func) -> Self {
PrefixFilter {
phantom: ::std::marker::PhantomData,
predicate,
}
}
}

impl<'a, Tuple, Val, Func> Leaper<'a, Tuple, Val> for PrefixFilter<Tuple, Func>
where
Func: Fn(&Tuple) -> bool,
{
/// Estimates the number of proposed values.
fn count(&mut self, prefix: &Tuple) -> usize {
if (self.predicate)(prefix) {
usize::max_value()
} else {
0
}
}
/// Populates `values` with proposed values.
fn propose(&mut self, _prefix: &Tuple, _values: &mut Vec<&'a Val>) {
panic!("PrefixFilter::propose(): variable apparently unbound");
}
/// Restricts `values` to proposed values.
fn intersect(&mut self, _prefix: &Tuple, _values: &mut Vec<&'a Val>) {
// We can only be here if we returned max_value() above.
}
}

/// A treefrog leaper based on a predicate of prefix and value.
pub struct ValueFilter<Tuple, Val, Func: Fn(&Tuple, &Val) -> bool> {
phantom: ::std::marker::PhantomData<(Tuple, Val)>,
predicate: Func,
}

impl<'a, Tuple, Val, Func> ValueFilter<Tuple, Val, Func>
where
Func: Fn(&Tuple, &Val) -> bool,
{
/// Creates a new filter based on the prefix
pub fn from(predicate: Func) -> Self {
ValueFilter {
phantom: ::std::marker::PhantomData,
predicate,
}
}
}

impl<'a, Tuple, Val, Func> Leaper<'a, Tuple, Val> for ValueFilter<Tuple, Val, Func>
where
Func: Fn(&Tuple, &Val) -> bool,
{
/// Estimates the number of proposed values.
fn count(&mut self, _prefix: &Tuple) -> usize {
usize::max_value()
}
/// Populates `values` with proposed values.
fn propose(&mut self, _prefix: &Tuple, _values: &mut Vec<&'a Val>) {
panic!("PrefixFilter::propose(): variable apparently unbound");
}
/// Restricts `values` to proposed values.
fn intersect(&mut self, prefix: &Tuple, values: &mut Vec<&'a Val>) {
values.retain(|val| (self.predicate)(prefix, val));
}
}

}

/// Extension method for relations.
pub trait RelationLeaper<Key: Ord, Val: Ord> {
/// Extend with `Val` using the elements of the relation.
Expand Down