MCPcopy Index your code
hub / github.com/douchuan/algorithm / build

Function build

src/tree/binary/builder/level.rs:26–62  ·  view source on GitHub ↗

?? why K: std::str::FromStr

(vec: &[&str])

Source from the content-addressed store, hash-verified

24
25// ?? why K: std::str::FromStr
26fn build<K: std::str::FromStr, V>(vec: &[&str]) -> Tree<K, V> {
27 let tokens = expand_sharp(vec);
28 let mut tree = Tree::default();
29 let mut tree_size = 0;
30 // Binary Tree in array
31 let mut aux: Vec<Option<NonNull<Node<K, V>>>> = Vec::new();
32 for (i, &token) in tokens.iter().enumerate() {
33 let node = token.parse().map(Node::new_key).ok();
34 aux.push(node);
35
36 if node.is_some() {
37 tree_size += 1;
38 }
39
40 if i == 0 {
41 tree.root = node;
42 continue;
43 }
44
45 if let Some(mut node) = node {
46 unsafe {
47 let parent = binary_tree::parent(i);
48 let mut parent_node = aux[parent].unwrap();
49 node.as_mut().parent = Some(parent_node);
50 if binary_tree::left(parent) == i {
51 parent_node.as_mut().left = Some(node);
52 } else {
53 parent_node.as_mut().right = Some(node);
54 }
55 }
56 }
57 }
58
59 tree.set_size(tree_size);
60
61 tree
62}
63
64// "#" symbol as empty child
65fn expand_sharp<'a>(vec: &[&'a str]) -> Vec<&'a str> {

Callers 1

build_in_levelMethod · 0.85

Calls 8

expand_sharpFunction · 0.85
parentFunction · 0.85
leftFunction · 0.85
parseMethod · 0.80
pushMethod · 0.80
is_someMethod · 0.80
set_sizeMethod · 0.80
iterMethod · 0.45

Tested by

no test coverage detected