构建锦标赛树, from bottom to top a中不能包含T::minimal()这个特殊值,pop需要用到T::minimal()做临界值
(data: &[K])
| 93 | /// 构建锦标赛树, from bottom to top |
| 94 | /// a中不能包含T::minimal()这个特殊值,pop需要用到T::minimal()做临界值 |
| 95 | fn do_build<K, V>(data: &[K]) -> Tree<K, V> |
| 96 | where |
| 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 |
| 118 | unsafe fn branch<K, V>( |