Function map

Source
pub fn map<S, F>(s: S, f: F) -> Map<S, F, ConstFalse>
Expand description

Maps each HibitTree element with UnaryFunction.

§RegularHibitTree usage

Due to current Rust limitations, type inference will not work here for lambdas. You’ll have to either specify concrete type, pass fn or implement UnaryFunction manually or with fun!.

let a: Tree<usize, _64bit<4>> = Default::default();

// This will fail to compile:
// let m = map(&a, |d| { d.clone() } );

// Lambdas with concrete input type will work.    
let m = map(&a, |d: &usize| { d.clone() } );

// Function pointers with concrete type argument will work too.
fn bypass(d: &usize) -> &usize { d }
let m = map(&a, bypass );

// As well as generic UnaryFunction implementation.
struct Cloned;
impl<T: Clone> UnaryFunction<T> for Cloned{
    type Output = T;

    fn exec(&self, arg: T) -> Self::Output {
        arg.clone()
    }
}
let m = map(&a, Cloned );

// You can use fun! macro to generate stateless UnaryFunction as well.
let m = map(&a, fun!( 
    [T: Clone] |d: T| -> T { d.clone() } 
));

§MultiHibitTree -> RegularHibitTree

You can make RegularHibitTree from MultiHibitTree with map and fun!:

 type Array = Tree<usize, _64bit<3>>;
 let mut a1: Array = Default::default();
 let mut a2: Array = Default::default();
 let mut a3: Array = Default::default();
         
 // ...
         
 let arrays = vec![a1, a2, a3];
 let intersect = multi_intersection(arrays.iter());
 fn is_multi_tree(_:&impl MultiHibitTree){}
 is_multi_tree(&intersect);

 let intersect = map(intersect, 
    fun!(['a, I: Iterator<Item=&'a usize>] |iter: I| -> usize {
        iter.sum() 
    })
 ); 
 fn is_regular_tree(_:&impl RegularHibitTree){}
 is_regular_tree(&intersect);