Skip to content

Commit 8832da0

Browse files
committed
refactored counting functions to use std::iter::successors
1 parent 62ad5ce commit 8832da0

File tree

3 files changed

+12
-31
lines changed

3 files changed

+12
-31
lines changed

src/diff/dijkstra.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -156,33 +156,20 @@ fn edge_between<'s, 'v>(before: &Vertex<'s, 'v>, after: &Vertex<'s, 'v>) -> Edge
156156

157157
/// What is the total number of AST nodes?
158158
fn node_count(root: Option<&Syntax>) -> u32 {
159-
let mut node = root;
160-
let mut count = 0;
161-
while let Some(current_node) = node {
162-
let current_count = match current_node {
163-
Syntax::List {
164-
num_descendants, ..
165-
} => *num_descendants,
166-
Syntax::Atom { .. } => 1,
167-
};
168-
count += current_count;
169-
170-
node = current_node.next_sibling();
171-
}
172-
173-
count
159+
let iter = std::iter::successors(root, |node| node.next_sibling());
160+
161+
iter.map(|node| match node {
162+
Syntax::List {
163+
num_descendants, ..
164+
} => *num_descendants,
165+
Syntax::Atom { .. } => 1,
166+
})
167+
.sum::<u32>()
174168
}
175169

176170
/// How many top-level AST nodes do we have?
177171
fn tree_count(root: Option<&Syntax>) -> u32 {
178-
let mut node = root;
179-
let mut count = 0;
180-
while let Some(current_node) = node {
181-
count += 1;
182-
node = current_node.next_sibling();
183-
}
184-
185-
count
172+
std::iter::successors(root, |node| node.next_sibling()).count() as _
186173
}
187174

188175
pub(crate) fn mark_syntax<'a>(

src/diff/stack.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,7 @@ impl<'b, T> Stack<'b, T> {
3939

4040
// O(n)
4141
pub(crate) fn size(&self) -> usize {
42-
let mut count = 0;
43-
let mut node = &self.head;
44-
while let Some(next) = node {
45-
count += 1;
46-
node = &next.next;
47-
}
48-
count
42+
std::iter::successors(self.head, |&n| n.next).count()
4943
}
5044

5145
pub(crate) fn is_empty(&self) -> bool {

src/parse/guess_language.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ fn from_emacs_mode_header(src: &str) -> Option<Language> {
569569
"yaml" => Yaml,
570570
"zig" => Zig,
571571
_ => continue,
572-
})
572+
});
573573
}
574574

575575
None

0 commit comments

Comments
 (0)