From: Chris Mikkelson Date: Mon, 17 Jun 2024 02:08:38 +0000 (-0500) Subject: map values only; generic keys too complex X-Git-Url: https://git.mikk.net/?a=commitdiff_plain;h=58a1b5e2dd4ff022177421186eb655244a6cfa65;p=mtbl-rs map values only; generic keys too complex --- diff --git a/src/seekable.rs b/src/seekable.rs index 94aa316..ffa8161 100644 --- a/src/seekable.rs +++ b/src/seekable.rs @@ -3,7 +3,7 @@ mod filter_map; mod merge; mod vec; use coalesce::Coalesce; -use filter_map::FilterMap; +use filter_map::FilterMapValue; use merge::Merger; pub use vec::SeekableVec; @@ -14,11 +14,11 @@ pub trait Seekable: Sized { fn next(&mut self) -> Option<(Self::Key, Self::Value)>; fn seek(&mut self, key: &Self::Key); - fn filter_map(self, func: F) -> FilterMap + fn filter_mapv(self, func: F) -> FilterMapValue where F: FnMut(&Self::Key, Self::Value, &mut Self) -> Option, { - FilterMap { next: self, func } + FilterMapValue { next: self, func } } fn merge(iter: L) -> Merger diff --git a/src/seekable/filter_map.rs b/src/seekable/filter_map.rs index 2246f6b..586868e 100644 --- a/src/seekable/filter_map.rs +++ b/src/seekable/filter_map.rs @@ -1,6 +1,6 @@ use crate::seekable::{Iter, Seekable}; -pub struct FilterMap +pub struct FilterMapValue where S: Seekable, F: FnMut(&S::Key, S::Value, &mut S) -> Option, @@ -9,7 +9,7 @@ where pub(crate) func: F, } -impl IntoIterator for FilterMap +impl IntoIterator for FilterMapValue where S: Seekable, F: FnMut(&S::Key, S::Value, &mut S) -> Option, @@ -21,7 +21,7 @@ where } } -impl Seekable for FilterMap +impl Seekable for FilterMapValue where S: Seekable, F: FnMut(&S::Key, S::Value, &mut S) -> Option, @@ -47,6 +47,56 @@ where } } +pub struct FilterMapKey +where + S: Seekable, + F: FnMut(&S::Key, &S::Value, &mut S) -> Option, + FS: FnMut(&K, &mut S), +{ + pub(crate) next: S, + pub(crate) func: F, + pub(crate) seekfunc: FS, +} + +impl IntoIterator for FilterMapKey +where + S: Seekable, + F: FnMut(&S::Key, &S::Value, &mut S) -> Option, + FS: FnMut(&K, &mut S), +{ + type Item = (K, S::Value); + type IntoIter = Iter; + fn into_iter(self) -> Self::IntoIter { + Iter(self) + } +} + +impl Seekable for FilterMapKey +where + S: Seekable, + F: FnMut(&S::Key, &S::Value, &mut S) -> Option, + FS: FnMut(&K, &mut S), +{ + type Key = K; + type Value = S::Value; + + fn next(&mut self) -> Option<(Self::Key, Self::Value)> { + loop { + match self.next.next() { + None => return None, + Some((k, v)) => { + if let Some(newk) = (self.func)(&k, &v, &mut self.next) { + return Some((newk, v)); + } + } + } + } + } + fn seek(&mut self, key: &Self::Key) { + (self.seekfunc)(key, &mut self.next); + } +} + #[cfg(test)] mod test { use crate::seekable::SeekableVec; @@ -59,7 +109,7 @@ mod test { let sv = SeekableVec::from(v.clone().into_iter().collect::>()); v.sort(); assert_eq!( - sv.filter_map(|_k, _v, _s| Some("hello")) + sv.filter_mapv(|_k, _v, _s| Some("hello")) .into_iter() .cmp(v.into_iter().map(|i| (i, "hello"))), Ordering::Equal @@ -72,7 +122,7 @@ mod test { .into_iter() .map(|i| (i, "foo")) .collect::>(); - let mut sv = SeekableVec::from(v.clone()).filter_map(|k, _v, s| { + let mut sv = SeekableVec::from(v.clone()).filter_mapv(|k, _v, s| { if k % 5 == 0 { return None; }