MCPcopy Create free account
hub / github.com/douchuan/algorithm / do_build

Function do_build

src/tree/binary/builder/tournament.rs:95–115  ·  view source on GitHub ↗

构建锦标赛树, from bottom to top a中不能包含T::minimal()这个特殊值,pop需要用到T::minimal()做临界值

(data: &[K])

Source from the content-addressed store, hash-verified

93/// 构建锦标赛树, from bottom to top
94/// a中不能包含T::minimal()这个特殊值,pop需要用到T::minimal()做临界值
95fn do_build<K, V>(data: &[K]) -> Tree<K, V>
96where
97 K: Copy + std::cmp::Ord,
98{
99 //build leaf
100 let mut nodes: Vec<NonNull<Node<K, V>>> = data.iter().map(|v| Node::new_key(*v)).collect();
101 while nodes.len() > 1 {
102 nodes = nodes
103 .chunks(2)
104 .map(|chunk| match *chunk {
105 [t1, t2] => unsafe { branch(t1, t2) },
106 [t] => t,
107 _ => unreachable!(),
108 })
109 .collect();
110 }
111
112 let mut tree = Tree::default();
113 tree.root = nodes.first().cloned();
114 tree
115}
116
117/// 创建分支节点,取t1, t2较大者的value构造parent
118unsafe fn branch<K, V>(

Callers 3

build_tournament_treeMethod · 0.85
t_build_treeFunction · 0.85
t_popFunction · 0.85

Calls 4

branchFunction · 0.85
firstMethod · 0.80
iterMethod · 0.45
lenMethod · 0.45

Tested by 2

t_build_treeFunction · 0.68
t_popFunction · 0.68