Implement FoldMap's folds using a SumTree

This required passing a `Context` object to `Summary` and introducing a
new `SeekDimension` trait that allows comparing two dimensions and pass
an additional context object.
This commit is contained in:
Antonio Scandurra 2021-05-06 16:13:35 +02:00
parent 7fb7a6adfc
commit 901525bf77
9 changed files with 355 additions and 152 deletions

View file

@ -30,37 +30,6 @@ where
}
}
pub fn find_insertion_index<'a, F, T, E>(slice: &'a [T], mut f: F) -> Result<usize, E>
where
F: FnMut(&'a T) -> Result<Ordering, E>,
{
use Ordering::*;
let s = slice;
let mut size = s.len();
if size == 0 {
return Ok(0);
}
let mut base = 0usize;
while size > 1 {
let half = size / 2;
let mid = base + half;
// mid is always in [0, size), that means mid is >= 0 and < size.
// mid >= 0: by definition
// mid < size: mid = size / 2 + size / 4 + size / 8 ...
let cmp = f(unsafe { s.get_unchecked(mid) })?;
base = if cmp == Greater { base } else { mid };
size -= half;
}
// base is always in [0, size) because base <= mid.
let cmp = f(unsafe { s.get_unchecked(base) })?;
if cmp == Equal {
Ok(base)
} else {
Ok(base + (cmp == Less) as usize)
}
}
pub struct RandomCharIter<T: Rng>(T);
impl<T: Rng> RandomCharIter<T> {
@ -85,14 +54,6 @@ impl<T: Rng> Iterator for RandomCharIter<T> {
mod tests {
use super::*;
#[test]
fn test_find_insertion_index() {
assert_eq!(
find_insertion_index(&[0, 4, 8], |probe| Ok::<Ordering, ()>(probe.cmp(&2))),
Ok(1)
);
}
#[test]
fn test_extend_sorted() {
let mut vec = vec![];