Adjust the coordinates calculated to be zero-based
This commit is contained in:
parent
7a06b8cf39
commit
b481d5d058
|
@ -286,7 +286,7 @@ impl<T> Tree<T> {
|
|||
next_idx
|
||||
}
|
||||
|
||||
pub fn depth(&self) -> usize {
|
||||
pub fn max_depth(&self) -> usize {
|
||||
self.nodes.iter().fold(
|
||||
0,
|
||||
|max, node| if node.depth > max { node.depth } else { max },
|
||||
|
@ -295,7 +295,8 @@ impl<T> Tree<T> {
|
|||
|
||||
// indent represents the indentation that should be applied to all children in this tree. It
|
||||
// amounts to the position of the parent node.
|
||||
pub fn position(&self, indent: usize, idx: usize) -> usize {
|
||||
pub fn position(&self, indent: usize, idx: usize) -> (usize, usize) {
|
||||
println!("[{}]", idx);
|
||||
let node = &self.nodes[idx];
|
||||
match node.parent {
|
||||
Some(parent_idx) => {
|
||||
|
@ -305,11 +306,12 @@ impl<T> Tree<T> {
|
|||
.iter()
|
||||
.take_while(|n| **n != node.id)
|
||||
.fold(0, |acc, n| acc + self.width(*n));
|
||||
indent + sibling_width + 1
|
||||
println!("[{}] sibling width {}", idx, sibling_width);
|
||||
(node.depth, indent + sibling_width)
|
||||
}
|
||||
|
||||
// Root nodes won't have a parent, so just put them in column 1
|
||||
None => 1,
|
||||
// Root nodes won't have a parent, so just put them in the first column
|
||||
None => (0, 0),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -452,7 +454,7 @@ mod test {
|
|||
|
||||
let tree = Tree::from(&game_tree);
|
||||
|
||||
assert_eq!(tree.depth(), 3);
|
||||
assert_eq!(tree.max_depth(), 3);
|
||||
}
|
||||
|
||||
// A
|
||||
|
@ -487,10 +489,12 @@ mod test {
|
|||
|
||||
let tree = Tree::from(&game_tree);
|
||||
|
||||
assert_eq!(tree.position(0, 2), 1);
|
||||
assert_eq!(tree.position(0, 1), 1);
|
||||
assert_eq!(tree.position(0, 0), 1);
|
||||
assert_eq!(tree.position(0, 4), 2);
|
||||
assert_eq!(tree.position(0, 5), 3);
|
||||
assert_eq!(tree.position(0, 2), (2, 0));
|
||||
assert_eq!(tree.position(0, 1), (1, 0));
|
||||
assert_eq!(tree.position(0, 0), (0, 0));
|
||||
assert_eq!(tree.position(0, 4), (3, 1));
|
||||
assert_eq!(tree.position(0, 5), (3, 2));
|
||||
assert_eq!(tree.position(0, 6), (1, 3));
|
||||
assert_eq!(tree.position(0, 7), (1, 4));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue