Skip to content

Commit

Permalink
Rename filter items.
Browse files Browse the repository at this point in the history
  • Loading branch information
olson-sean-k committed Oct 27, 2023
1 parent e8e7092 commit f8768a6
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 136 deletions.
168 changes: 84 additions & 84 deletions src/walk/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ pub type Filtrate<T> = State<FiltrateKind, T>;
pub type Residue<T> = State<ResidueKind, T>;

pub type IsomerSubstituent<'a, F, R> = <F as Isomer<R>>::Substituent<'a>;
pub type SeparationFiltrate<S> = <S as Separation>::Filtrate;
pub type SeparationResidue<S> = <S as Separation>::Residue;
pub type SeparationFiltrate<S> = <S as Feed>::Filtrate;
pub type SeparationResidue<S> = <S as Feed>::Residue;
pub type SeparationSubstituent<'a, S> =
IsomerSubstituent<'a, SeparationFiltrate<S>, SeparationResidue<S>>;

Expand Down Expand Up @@ -132,18 +132,18 @@ impl<T> AsRef<T> for Residue<TreeResidue<T>> {
}
}

pub trait Separation {
pub trait Feed {
type Filtrate;
type Residue;
}

impl<T, U> Separation for (T, U) {
impl<T, U> Feed for (T, U) {
type Filtrate = T;
type Residue = U;
}

// TODO: Remove this?
pub trait Isomorphic: Separation
pub trait Isomorphic: Feed
where
SeparationFiltrate<Self>: Isomer<SeparationResidue<Self>>,
for<'a> SeparationResidue<Self>:
Expand All @@ -155,7 +155,7 @@ impl<F, R, S> Isomorphic for S
where
F: Isomer<R>,
for<'a> R: 'a + Isomer<F, Substituent<'a> = <F as Isomer<R>>::Substituent<'a>>,
S: Separation<Filtrate = F, Residue = R>,
S: Feed<Filtrate = F, Residue = R>,
{
}

Expand All @@ -167,62 +167,59 @@ pub trait Isomer<T>: Sized {
fn substituent(&self) -> Self::Substituent<'_>;
}

// TODO: Consider renaming `Filtered` to `Separation`. Of course, this requires renaming the
// `Separation` trait as well. Perhaps `Separation` can be renamed to `Feed` and `Feed` can
// be renamed to `SeparatingFilter` or some such.
#[derive(Clone, Copy, Debug)]
pub enum Filtered<S>
pub enum Separation<S>
where
S: Separation,
S: Feed,
{
Filtrate(Filtrate<S::Filtrate>),
Residue(Residue<S::Residue>),
}

impl<S> Filtered<S>
impl<S> Separation<S>
where
S: Separation,
S: Feed,
{
// TODO: Provide a trait-based and intentionally very limited way to implement a base
// `Feed` that constructs a `Filtrate<T>` from unfiltered `Iterator` items of `T`.
pub(in crate::walk) fn from_inner_filtrate(filtrate: S::Filtrate) -> Self {
Filtered::Filtrate(Filtrate::new(filtrate))
Separation::Filtrate(Filtrate::new(filtrate))
}

fn from_inner_residue(residue: S::Residue) -> Self {
Filtered::Residue(Residue::new(residue))
Separation::Residue(Residue::new(residue))
}

pub fn map_filtrate<U, F>(self, f: F) -> Filtered<(U, S::Residue)>
pub fn map_filtrate<U, F>(self, f: F) -> Separation<(U, S::Residue)>
where
F: FnOnce(S::Filtrate) -> U,
{
match self {
Filtered::Filtrate(filtrate) => filtrate.map(f).into(),
Filtered::Residue(residue) => residue.into(),
Separation::Filtrate(filtrate) => filtrate.map(f).into(),
Separation::Residue(residue) => residue.into(),
}
}

pub fn filtrate(self) -> Option<Filtrate<S::Filtrate>> {
match self {
Filtered::Filtrate(filtrate) => Some(filtrate),
Separation::Filtrate(filtrate) => Some(filtrate),
_ => None,
}
}

pub fn as_filtrate(&self) -> Option<&Filtrate<S::Filtrate>> {
match self {
Filtered::Filtrate(ref filtrate) => Some(filtrate),
Separation::Filtrate(ref filtrate) => Some(filtrate),
_ => None,
}
}

pub fn as_ref(&self) -> Filtered<(&S::Filtrate, &S::Residue)> {
pub fn as_ref(&self) -> Separation<(&S::Filtrate, &S::Residue)> {
match self {
Filtered::Filtrate(ref filtrate) => {
Filtered::Filtrate(Filtrate::new(filtrate.as_ref()))
Separation::Filtrate(ref filtrate) => {
Separation::Filtrate(Filtrate::new(filtrate.as_ref()))
},
Filtered::Residue(ref residue) => Filtered::Residue(Residue::new(residue.as_ref())),
Separation::Residue(ref residue) => Separation::Residue(Residue::new(residue.as_ref())),
}
}

Expand All @@ -235,27 +232,27 @@ where
>,
{
match self {
Filtered::Filtrate(ref filtrate) => filtrate.as_ref().substituent(),
Filtered::Residue(ref residue) => residue.as_ref().substituent(),
Separation::Filtrate(ref filtrate) => filtrate.as_ref().substituent(),
Separation::Residue(ref residue) => residue.as_ref().substituent(),
}
}
}

// TODO: Base this `impl` on concrete types (i.e., `(T, TreeResidue<R>)`) instead of trait bounds
// on `S`.
impl<S, U> Filtered<S>
impl<S, U> Separation<S>
where
S: Separation<Residue = TreeResidue<U>>,
S: Feed<Residue = TreeResidue<U>>,
{
pub fn filter_map_node<F>(self, f: F) -> Self
where
F: FnOnce(S::Filtrate) -> U,
{
match self {
Filtered::Filtrate(filtrate) => {
Filtered::from_inner_residue(TreeResidue::Node(f(filtrate.into_inner())))
Separation::Filtrate(filtrate) => {
Separation::from_inner_residue(TreeResidue::Node(f(filtrate.into_inner())))
},
filtered => filtered,
separation => separation,
}
}

Expand All @@ -265,16 +262,16 @@ where
F: FnOnce(S::Filtrate) -> U,
{
match self {
Filtered::Filtrate(filtrate) => {
Separation::Filtrate(filtrate) => {
cancellation.skip_tree();
Filtered::from_inner_residue(TreeResidue::Node(f(filtrate.into_inner())))
Separation::from_inner_residue(TreeResidue::Node(f(filtrate.into_inner())))
},
Filtered::Residue(residue) => match residue.into_inner() {
Separation::Residue(residue) => match residue.into_inner() {
TreeResidue::Node(residue) => {
cancellation.skip_tree();
Filtered::from_inner_residue(TreeResidue::Tree(residue))
Separation::from_inner_residue(TreeResidue::Tree(residue))
},
residue => Filtered::from_inner_residue(residue),
residue => Separation::from_inner_residue(residue),
},
}
}
Expand All @@ -288,7 +285,9 @@ where
Substituent<'a> = <S::Filtrate as Isomer<TreeResidue<U>>>::Substituent<'a>,
>,
I: SkipTree,
F: FnOnce(<S::Filtrate as Isomer<TreeResidue<U>>>::Substituent<'_>) -> Option<TreeResidue<()>>,
F: FnOnce(
<S::Filtrate as Isomer<TreeResidue<U>>>::Substituent<'_>,
) -> Option<TreeResidue<()>>,
{
match f(self.substituent()) {
Some(TreeResidue::Tree(())) => self.filter_map_tree(cancellation, From::from),
Expand All @@ -298,60 +297,61 @@ where
}
}

impl<T, R> Filtered<(Option<T>, R)> {
pub fn transpose_filtrate(self) -> Option<Filtered<(T, R)>> {
impl<T, R> Separation<(Option<T>, R)> {
pub fn transpose_filtrate(self) -> Option<Separation<(T, R)>> {
match self {
Filtered::Filtrate(filtrate) => match filtrate.into_inner() {
Separation::Filtrate(filtrate) => match filtrate.into_inner() {

Check failure on line 303 in src/walk/filter.rs

View workflow job for this annotation

GitHub Actions / Lint

manual implementation of `Option::map`
Some(filtrate) => Some(Filtrate::new(filtrate).into()),
_ => None,
},
Filtered::Residue(residue) => Some(residue.into()),
Separation::Residue(residue) => Some(residue.into()),
}
}
}

impl<T, E, R> Filtered<(Result<T, E>, R)> {
pub fn transpose_filtrate(self) -> Result<Filtered<(T, R)>, Filtrate<E>> {
impl<T, E, R> Separation<(Result<T, E>, R)> {
pub fn transpose_filtrate(self) -> Result<Separation<(T, R)>, Filtrate<E>> {
match self {
Filtered::Filtrate(filtrate) => match filtrate.into_inner() {
Separation::Filtrate(filtrate) => match filtrate.into_inner() {
Ok(filtrate) => Ok(Filtrate::new(filtrate).into()),
Err(error) => Err(Filtrate::new(error)),
},
Filtered::Residue(residue) => Ok(residue.into()),
Separation::Residue(residue) => Ok(residue.into()),
}
}
}

impl<S> From<Filtrate<S::Filtrate>> for Filtered<S>
impl<S> From<Filtrate<S::Filtrate>> for Separation<S>
where
S: Separation,
S: Feed,
{
fn from(filtrate: Filtrate<S::Filtrate>) -> Self {
Filtered::Filtrate(filtrate)
Separation::Filtrate(filtrate)
}
}

impl<S> From<Residue<S::Residue>> for Filtered<S>
impl<S> From<Residue<S::Residue>> for Separation<S>
where
S: Separation,
S: Feed,
{
fn from(residue: Residue<S::Residue>) -> Self {
Filtered::Residue(residue)
Separation::Residue(residue)
}
}

impl<S> Separation for Filtered<S>
// TODO: Remove this?
impl<S> Feed for Separation<S>
where
S: Separation,
S: Feed,
{
type Filtrate = S::Filtrate;
type Residue = S::Residue;
}

pub trait Feed {
type Separation: Separation;
pub trait SeparatingFilter {
type Feed: Feed;

fn feed(&mut self) -> Option<Filtered<Self::Separation>>;
fn feed(&mut self) -> Option<Separation<Self::Feed>>;
}

pub trait SkipTree {
Expand Down Expand Up @@ -401,7 +401,7 @@ impl<T> AsRef<T> for TreeResidue<T> {
//}

pub trait TreeIterator:
Feed + Iterator<Item = <Self::Separation as Separation>::Filtrate> + SkipTree
Iterator<Item = <Self::Feed as Feed>::Filtrate> + SeparatingFilter + SkipTree
{
//fn filter_tree_substituent<F>(self, f: F) -> FilterTreeSubstituent<Self, F>
//where
Expand All @@ -416,14 +416,14 @@ pub trait TreeIterator:
fn filter_map_tree<S, F>(self, f: F) -> FilterMapTree<Self, S, F>
where
Self: Sized,
S: Separation,
F: FnMut(WalkCancellation<Self>, Filtered<Self::Separation>) -> Filtered<S>;
S: Feed,
F: FnMut(WalkCancellation<Self>, Separation<Self::Feed>) -> Separation<S>;
}

impl<U, I> TreeIterator for I
where
I: Feed + Iterator<Item = <Self::Separation as Separation>::Filtrate> + SkipTree,
I::Separation: Separation<Residue = TreeResidue<U>>,
I: Iterator<Item = <Self::Feed as Feed>::Filtrate> + SeparatingFilter + SkipTree,
I::Feed: Feed<Residue = TreeResidue<U>>,
{
//fn filter_tree_substituent<F>(self, f: F) -> FilterTreeSubstituent<Self, F>
//where
Expand All @@ -441,8 +441,8 @@ where
fn filter_map_tree<S, F>(self, f: F) -> FilterMapTree<Self, S, F>
where
Self: Sized,
S: Separation,
F: FnMut(WalkCancellation<Self>, Filtered<I::Separation>) -> Filtered<S>,
S: Feed,
F: FnMut(WalkCancellation<Self>, Separation<I::Feed>) -> Separation<S>,
{
FilterMapTree {
input: self,
Expand Down Expand Up @@ -516,32 +516,32 @@ pub struct FilterMapTree<I, S, F> {
_phantom: PhantomData<fn() -> S>,
}

impl<U, I, S, F> Feed for FilterMapTree<I, S, F>
impl<U, I, S, F> Iterator for FilterMapTree<I, S, F>
where
I: Feed,
I::Separation: Separation<Residue = TreeResidue<U>>,
S: Separation,
F: FnMut(WalkCancellation<I>, Filtered<I::Separation>) -> Filtered<S>,
I: SeparatingFilter,
I::Feed: Feed<Residue = TreeResidue<U>>,
S: Feed,
F: FnMut(WalkCancellation<I>, Separation<I::Feed>) -> Separation<S>,
{
type Separation = S;
type Item = <<Self as SeparatingFilter>::Feed as Feed>::Filtrate;

fn feed(&mut self) -> Option<Filtered<Self::Separation>> {
let filtered = self.input.feed();
filtered.map(|filtered| (self.f)(WalkCancellation(&mut self.input), filtered))
fn next(&mut self) -> Option<Self::Item> {
filtrate(self)
}
}

impl<U, I, S, F> Iterator for FilterMapTree<I, S, F>
impl<U, I, S, F> SeparatingFilter for FilterMapTree<I, S, F>
where
I: Feed,
I::Separation: Separation<Residue = TreeResidue<U>>,
S: Separation,
F: FnMut(WalkCancellation<I>, Filtered<I::Separation>) -> Filtered<S>,
I: SeparatingFilter,
I::Feed: Feed<Residue = TreeResidue<U>>,
S: Feed,
F: FnMut(WalkCancellation<I>, Separation<I::Feed>) -> Separation<S>,
{
type Item = <<Self as Feed>::Separation as Separation>::Filtrate;
type Feed = S;

fn next(&mut self) -> Option<Self::Item> {
filtrate(self)
fn feed(&mut self) -> Option<Separation<Self::Feed>> {
let separation = self.input.feed();
separation.map(|separation| (self.f)(WalkCancellation(&mut self.input), separation))
}
}

Expand All @@ -554,13 +554,13 @@ where
}
}

pub fn filtrate<I>(filter: &mut I) -> Option<<I::Separation as Separation>::Filtrate>
pub fn filtrate<I>(filter: &mut I) -> Option<<I::Feed as Feed>::Filtrate>
where
I: Feed,
I: SeparatingFilter,
{
loop {
if let Some(filtered) = filter.feed() {
return match filtered.filtrate() {
if let Some(separation) = filter.feed() {
return match separation.filtrate() {
None => {
continue;
},
Expand Down
Loading

0 comments on commit f8768a6

Please sign in to comment.