mod merge;
mod vec;
use coalesce::Coalesce;
-use filter_map::FilterMap;
+use filter_map::FilterMapValue;
use merge::Merger;
pub use vec::SeekableVec;
fn next(&mut self) -> Option<(Self::Key, Self::Value)>;
fn seek(&mut self, key: &Self::Key);
- fn filter_map<F, V>(self, func: F) -> FilterMap<Self, F, V>
+ fn filter_mapv<F, V>(self, func: F) -> FilterMapValue<Self, F, V>
where
F: FnMut(&Self::Key, Self::Value, &mut Self) -> Option<V>,
{
- FilterMap { next: self, func }
+ FilterMapValue { next: self, func }
}
fn merge<L>(iter: L) -> Merger<Self>
use crate::seekable::{Iter, Seekable};
-pub struct FilterMap<S, F, V>
+pub struct FilterMapValue<S, F, V>
where
S: Seekable,
F: FnMut(&S::Key, S::Value, &mut S) -> Option<V>,
pub(crate) func: F,
}
-impl<S, F, V> IntoIterator for FilterMap<S, F, V>
+impl<S, F, V> IntoIterator for FilterMapValue<S, F, V>
where
S: Seekable,
F: FnMut(&S::Key, S::Value, &mut S) -> Option<V>,
}
}
-impl<S, F, V> Seekable for FilterMap<S, F, V>
+impl<S, F, V> Seekable for FilterMapValue<S, F, V>
where
S: Seekable,
F: FnMut(&S::Key, S::Value, &mut S) -> Option<V>,
}
}
+pub struct FilterMapKey<S, F, FS, K>
+where
+ S: Seekable,
+ F: FnMut(&S::Key, &S::Value, &mut S) -> Option<K>,
+ FS: FnMut(&K, &mut S),
+{
+ pub(crate) next: S,
+ pub(crate) func: F,
+ pub(crate) seekfunc: FS,
+}
+
+impl<S, F, FS, K> IntoIterator for FilterMapKey<S, F, FS, K>
+where
+ S: Seekable,
+ F: FnMut(&S::Key, &S::Value, &mut S) -> Option<K>,
+ FS: FnMut(&K, &mut S),
+{
+ type Item = (K, S::Value);
+ type IntoIter = Iter<Self>;
+ fn into_iter(self) -> Self::IntoIter {
+ Iter(self)
+ }
+}
+
+impl<S, F, FS, K> Seekable for FilterMapKey<S, F, FS, K>
+where
+ S: Seekable,
+ F: FnMut(&S::Key, &S::Value, &mut S) -> Option<K>,
+ 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;
let sv = SeekableVec::from(v.clone().into_iter().collect::<Vec<i32>>());
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
.into_iter()
.map(|i| (i, "foo"))
.collect::<Vec<(i32, &str)>>();
- 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;
}