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
//! Tree sorting operations.
use crate::tree::Tree;
impl Tree {
/// Sorts children at each level using the given comparison function.
///
/// This recursively sorts all children throughout the tree.
///
/// # Examples
///
/// ```
/// use treelog::Tree;
/// use std::cmp::Ordering;
///
/// let mut tree = Tree::Node("root".to_string(), vec![
/// Tree::Leaf(vec!["z".to_string()]),
/// Tree::Leaf(vec!["a".to_string()]),
/// ]);
/// let mut compare = |a: &Tree, b: &Tree| {
/// match (a, b) {
/// (Tree::Leaf(lines_a), Tree::Leaf(lines_b)) => {
/// lines_a[0].cmp(&lines_b[0])
/// }
/// _ => Ordering::Equal,
/// }
/// };
/// tree.sort_children(&mut compare);
/// ```
pub fn sort_children<F>(&mut self, compare: &mut F)
where
F: FnMut(&Tree, &Tree) -> std::cmp::Ordering,
{
if let Tree::Node(_, children) = self {
// Sort children at this level
children.sort_by(&mut *compare);
// Recursively sort children's children
for child in children.iter_mut() {
child.sort_children(compare);
}
}
}
/// Sorts children alphabetically by label (for nodes) or first line (for leaves).
///
/// This recursively sorts all children throughout the tree.
///
/// # Examples
///
/// ```
/// use treelog::Tree;
///
/// let mut tree = Tree::Node("root".to_string(), vec![
/// Tree::Node("z".to_string(), vec![]),
/// Tree::Node("a".to_string(), vec![]),
/// ]);
/// tree.sort_by_label();
/// ```
pub fn sort_by_label(&mut self) {
let mut compare = |a: &Tree, b: &Tree| {
let label_a = match a {
Tree::Node(label, _) => label.as_str(),
Tree::Leaf(lines) => lines.first().map(|s| s.as_str()).unwrap_or(""),
};
let label_b = match b {
Tree::Node(label, _) => label.as_str(),
Tree::Leaf(lines) => lines.first().map(|s| s.as_str()).unwrap_or(""),
};
label_a.cmp(label_b)
};
self.sort_children(&mut compare);
}
/// Sorts children by depth, with the deepest first or last.
///
/// This recursively sorts all children throughout the tree.
///
/// # Examples
///
/// ```
/// use treelog::Tree;
///
/// let mut tree = Tree::Node("root".to_string(), vec![
/// Tree::Leaf(vec!["shallow".to_string()]),
/// Tree::Node("deep".to_string(), vec![
/// Tree::Leaf(vec!["deep_leaf".to_string()])
/// ]),
/// ]);
/// tree.sort_by_depth(true); // deepest first
/// ```
///
/// Note: This method requires the `stats` feature to be enabled.
pub fn sort_by_depth(&mut self, deepest_first: bool) {
let mut compare = |a: &Tree, b: &Tree| {
let depth_a = a.depth();
let depth_b = b.depth();
if deepest_first {
depth_b.cmp(&depth_a)
} else {
depth_a.cmp(&depth_b)
}
};
self.sort_children(&mut compare);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sort_children() {
use std::cmp::Ordering;
let mut tree = Tree::Node(
"root".to_string(),
vec![
Tree::Leaf(vec!["z".to_string()]),
Tree::Leaf(vec!["a".to_string()]),
],
);
let mut compare = |a: &Tree, b: &Tree| match (a, b) {
(Tree::Leaf(lines_a), Tree::Leaf(lines_b)) => lines_a[0].cmp(&lines_b[0]),
_ => Ordering::Equal,
};
tree.sort_children(&mut compare);
if let Tree::Node(_, children) = &tree
&& let Tree::Leaf(lines) = &children[0]
{
assert_eq!(lines[0], "a");
}
}
#[test]
fn test_sort_by_label() {
let mut tree = Tree::Node(
"root".to_string(),
vec![
Tree::Node("z".to_string(), vec![]),
Tree::Node("a".to_string(), vec![]),
],
);
tree.sort_by_label();
if let Tree::Node(_, children) = &tree {
assert_eq!(children[0].label(), Some("a"));
assert_eq!(children[1].label(), Some("z"));
}
}
#[cfg(feature = "stats")]
#[test]
fn test_sort_by_depth() {
let mut tree = Tree::Node(
"root".to_string(),
vec![
Tree::Leaf(vec!["shallow".to_string()]),
Tree::Node(
"deep".to_string(),
vec![Tree::Leaf(vec!["deep_leaf".to_string()])],
),
],
);
tree.sort_by_depth(true); // deepest first
if let Tree::Node(_, children) = &tree {
assert!(children[0].is_node());
assert!(children[1].is_leaf());
}
}
}