Function rayon::iter::walk_tree

source ·
pub fn walk_tree<S, B, I>(root: S, children_of: B) -> WalkTree<S, B>
where S: Send, B: Fn(&S) -> I + Send + Sync, I: IntoIterator<Item = S>, I::IntoIter: DoubleEndedIterator,
Expand description

Create a tree like parallel iterator from an initial root node. The children_of function should take a node and iterate on all of its child nodes. The best parallelization is obtained when the tree is balanced but we should also be able to handle harder cases.

§Ordering

This function does not guarantee any ordering but will use whatever algorithm is thought to achieve the fastest traversal. See also walk_tree_prefix which guarantees a prefix order and walk_tree_postfix which guarantees a postfix order.

§Example

     4
    / \
   /   \
  2     3
       / \
      1   2
use rayon::iter::walk_tree;
use rayon::prelude::*;

let par_iter = walk_tree(4, |&e| {
    if e <= 2 {
        Vec::new()
    } else {
        vec![e / 2, e / 2 + 1]
    }
});
assert_eq!(par_iter.sum::<u32>(), 12);