-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlib.rs
97 lines (92 loc) · 2.35 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::fmt;
/// Positioned Tree; a Tree with position (x, y)
#[derive(Debug, Clone, PartialEq)]
pub enum PositionedTree<T: fmt::Display> {
Node {
value: T,
left: Box<PositionedTree<T>>,
right: Box<PositionedTree<T>>,
x: u32,
y: u32,
},
End,
}
impl<T: fmt::Display> PositionedTree<T> {
pub fn end() -> Self {
PositionedTree::End
}
pub fn leaf(v: T, x: u32, y: u32) -> Self {
PositionedTree::node(v, PositionedTree::End, PositionedTree::End, x, y)
}
pub fn node(v: T, left: PositionedTree<T>, right: PositionedTree<T>, x: u32, y: u32) -> Self {
PositionedTree::Node {
value: v,
left: Box::new(left),
right: Box::new(right),
x: x,
y: y,
}
}
pub fn get_x(&self) -> Option<u32> {
match self {
PositionedTree::Node {
value: _,
left: _,
right: _,
x,
y: _,
} => Some(*x),
PositionedTree::End => None,
}
}
pub fn get_y(&self) -> Option<u32> {
match self {
PositionedTree::Node {
value: _,
left: _,
right: _,
x: _,
y,
} => Some(*y),
PositionedTree::End => None,
}
}
}
impl<T: fmt::Display> fmt::Display for PositionedTree<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self {
PositionedTree::Node {
value,
left,
right,
x,
y,
} => write!(f, "T[{},{}]({} {} {})", x, y, value, left, right),
PositionedTree::End => write!(f, "."),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_positioned_node() {
let tree = PositionedTree::node(
'a',
PositionedTree::node(
'b',
PositionedTree::end(),
PositionedTree::leaf('c', 2, 3),
1,
2,
),
PositionedTree::leaf('d', 4, 2),
3,
1,
);
assert_eq!(
tree.to_string(),
"T[3,1](a T[1,2](b . T[2,3](c . .)) T[4,2](d . .))"
)
}
}