orx_tree/common_traits/display.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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
use crate::{
pinned_storage::PinnedStorage, traversal::OverDepthSiblingIdxNode, Dfs, MemoryPolicy, Node,
NodeMut, NodeRef, Traversal, Traverser, Tree, TreeVariant,
};
use alloc::string::ToString;
use alloc::vec::Vec;
use core::fmt::Display;
impl<V, M, P> Display for Node<'_, V, M, P>
where
V: TreeVariant,
M: MemoryPolicy,
P: PinnedStorage,
V::Item: Display,
{
/// Creates a convenient-to-read string representation of the tree or a subtree rooted at a node.
///
/// # Examples
///
/// ```
/// use orx_tree::*;
///
/// // 1
/// // ╱ ╲
/// // ╱ ╲
/// // 2 3
/// // ╱ ╲ ╱ ╲
/// // 4 5 6 7
/// // | | ╱ ╲
/// // 8 9 10 11
///
/// let mut tree = DynTree::new(1);
///
/// let mut root = tree.root_mut();
/// let [id2, id3] = root.push_children([2, 3]);
/// let [id4, _] = tree.node_mut(&id2).push_children([4, 5]);
/// tree.node_mut(&id4).push_child(8);
/// let [id6, id7] = tree.node_mut(&id3).push_children([6, 7]);
/// tree.node_mut(&id6).push_child(9);
/// tree.node_mut(&id7).push_children([10, 11]);
///
/// let expected_str = r#"1
/// ├──2
/// │ ├──4
/// │ │ └──8
/// │ └──5
/// └──3
/// ├──6
/// │ └──9
/// └──7
/// ├──10
/// └──11
/// "#;
/// assert_eq!(tree.to_string(), expected_str);
///
/// let expected_str = r#"3
/// ├──6
/// │ └──9
/// └──7
/// ├──10
/// └──11
/// "#;
/// println!("{}", tree.node(&id3).to_string());
/// assert_eq!(tree.node(&id3).to_string(), expected_str);
/// ```
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut t = Traversal.dfs().over_nodes().with_depth().with_sibling_idx();
display_node(f, self, &mut t)
}
}
impl<V, M, P> Display for NodeMut<'_, V, M, P>
where
V: TreeVariant,
M: MemoryPolicy,
P: PinnedStorage,
V::Item: Display,
{
/// Creates a convenient-to-read string representation of the tree or a subtree rooted at a node.
///
/// # Examples
///
/// ```
/// use orx_tree::*;
///
/// // 1
/// // ╱ ╲
/// // ╱ ╲
/// // 2 3
/// // ╱ ╲ ╱ ╲
/// // 4 5 6 7
/// // | | ╱ ╲
/// // 8 9 10 11
///
/// let mut tree = DynTree::new(1);
///
/// let mut root = tree.root_mut();
/// let [id2, id3] = root.push_children([2, 3]);
/// let [id4, _] = tree.node_mut(&id2).push_children([4, 5]);
/// tree.node_mut(&id4).push_child(8);
/// let [id6, id7] = tree.node_mut(&id3).push_children([6, 7]);
/// tree.node_mut(&id6).push_child(9);
/// tree.node_mut(&id7).push_children([10, 11]);
///
/// let expected_str = r#"1
/// ├──2
/// │ ├──4
/// │ │ └──8
/// │ └──5
/// └──3
/// ├──6
/// │ └──9
/// └──7
/// ├──10
/// └──11
/// "#;
/// assert_eq!(tree.to_string(), expected_str);
///
/// let expected_str = r#"3
/// ├──6
/// │ └──9
/// └──7
/// ├──10
/// └──11
/// "#;
/// println!("{}", tree.node(&id3).to_string());
/// assert_eq!(tree.node(&id3).to_string(), expected_str);
/// ```
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut t = Traversal.dfs().over_nodes().with_depth().with_sibling_idx();
display_node(f, self, &mut t)
}
}
impl<V, M, P> Display for Tree<V, M, P>
where
V: TreeVariant,
M: MemoryPolicy,
P: PinnedStorage,
V::Item: Display,
{
/// Creates a convenient-to-read string representation of the tree or a subtree rooted at a node.
///
/// # Examples
///
/// ```
/// use orx_tree::*;
///
/// // 1
/// // ╱ ╲
/// // ╱ ╲
/// // 2 3
/// // ╱ ╲ ╱ ╲
/// // 4 5 6 7
/// // | | ╱ ╲
/// // 8 9 10 11
///
/// let mut tree = DynTree::new(1);
///
/// let mut root = tree.root_mut();
/// let [id2, id3] = root.push_children([2, 3]);
/// let [id4, _] = tree.node_mut(&id2).push_children([4, 5]);
/// tree.node_mut(&id4).push_child(8);
/// let [id6, id7] = tree.node_mut(&id3).push_children([6, 7]);
/// tree.node_mut(&id6).push_child(9);
/// tree.node_mut(&id7).push_children([10, 11]);
///
/// let expected_str = r#"1
/// ├──2
/// │ ├──4
/// │ │ └──8
/// │ └──5
/// └──3
/// ├──6
/// │ └──9
/// └──7
/// ├──10
/// └──11
/// "#;
/// assert_eq!(tree.to_string(), expected_str);
///
/// let expected_str = r#"3
/// ├──6
/// │ └──9
/// └──7
/// ├──10
/// └──11
/// "#;
/// println!("{}", tree.node(&id3).to_string());
/// assert_eq!(tree.node(&id3).to_string(), expected_str);
/// ```
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self.get_root() {
Some(root) => {
let mut t = Traversal.dfs().over_nodes().with_depth().with_sibling_idx();
display_node(f, &root, &mut t)
}
None => Ok(()),
}
}
}
fn display_node<'a, V, M, P>(
f: &mut core::fmt::Formatter<'_>,
node: &'a impl NodeRef<'a, V, M, P>,
t: &'a mut Dfs<OverDepthSiblingIdxNode>,
) -> core::fmt::Result
where
V: TreeVariant + 'a,
M: MemoryPolicy,
P: PinnedStorage,
V::Item: Display,
{
fn set_depth(depths: &mut Vec<bool>, depth: usize, continues: bool) {
match depth < depths.len() {
true => depths[depth] = continues,
false => depths.push(continues),
}
}
let mut depths: Vec<bool> = Default::default();
let mut iter = node.walk_with(t);
if let Some((_, _, root)) = iter.next() {
writeln!(f, "{}", root.data().to_string().as_str())?;
set_depth(&mut depths, 0, true);
for (depth, sibling_idx, node) in iter {
let is_last_sibling = node.num_siblings() == sibling_idx + 1;
set_depth(&mut depths, depth, true);
set_depth(&mut depths, depth - 1, !is_last_sibling);
for d in depths.iter().take(depth - 1) {
match d {
true => write!(f, "│")?,
false => write!(f, " ")?,
}
write!(f, " ")?;
}
let first_depth_char = match is_last_sibling {
true => 'â””',
false => '├',
};
write!(f, "{}", first_depth_char)?;
write!(f, "──")?;
writeln!(f, "{}", node.data().to_string().as_str())?;
}
}
Ok(())
}