Skip to content

Commit

Permalink
Merge pull request #77 from iwburns/make-rustfmt-happy
Browse files Browse the repository at this point in the history
Make rustfmt happy
  • Loading branch information
iwburns authored Feb 9, 2019
2 parents 90be4ed + 482c00a commit 9739f5e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
52 changes: 22 additions & 30 deletions src/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,15 @@ impl<'a, T> Iterator for PreOrderTraversalIds<'a, T> {
type Item = NodeId;

fn next(&mut self) -> Option<NodeId> {
self.data
.pop_front()
.and_then(|node_id| {
self.tree.get(&node_id).ok()
.and_then(|node_ref| {
// prepend child_ids
for child_id in node_ref.children().iter().rev() {
self.data.push_front(child_id.clone());
}

Some(node_id)
})
self.data.pop_front().and_then(|node_id| {
self.tree.get(&node_id).ok().and_then(|node_ref| {
// prepend child_ids
for child_id in node_ref.children().iter().rev() {
self.data.push_front(child_id.clone());
}

Some(node_id)
})
})
}
}
Expand Down Expand Up @@ -271,25 +268,23 @@ impl<'a, T> Iterator for PostOrderTraversal<'a, T> {
/// Iterates over all of the `NodeId`s in the sub-tree of a given `NodeId` in the `Tree`. Each call to
/// `next` will return the next `NodeId` in Post-Order Traversal order.
///
pub struct PostOrderTraversalIds<'a, T: 'a> {
tree: &'a Tree<T>,
pub struct PostOrderTraversalIds {
ids: IntoIter<NodeId>,
}

impl<'a, T> PostOrderTraversalIds<'a, T> {
pub(crate) fn new(tree: &'a Tree<T>, node_id: NodeId) -> PostOrderTraversalIds<T> {
impl PostOrderTraversalIds {
pub(crate) fn new<T>(tree: &Tree<T>, node_id: NodeId) -> PostOrderTraversalIds {
// over allocating, but all at once instead of re-sizing and re-allocating as we go
let mut ids = Vec::with_capacity(tree.capacity());

PostOrderTraversalIds::process_nodes(node_id, tree, &mut ids);

PostOrderTraversalIds {
tree: tree,
ids: ids.into_iter(),
}
}

fn process_nodes(starting_id: NodeId, tree: &Tree<T>, ids: &mut Vec<NodeId>) {
fn process_nodes<T>(starting_id: NodeId, tree: &Tree<T>, ids: &mut Vec<NodeId>) {
let node = tree.get(&starting_id).unwrap();

for child_id in node.children() {
Expand All @@ -300,7 +295,7 @@ impl<'a, T> PostOrderTraversalIds<'a, T> {
}
}

impl<'a, T> Iterator for PostOrderTraversalIds<'a, T> {
impl Iterator for PostOrderTraversalIds {
type Item = NodeId;

fn next(&mut self) -> Option<NodeId> {
Expand Down Expand Up @@ -379,17 +374,14 @@ impl<'a, T> Iterator for LevelOrderTraversalIds<'a, T> {
type Item = NodeId;

fn next(&mut self) -> Option<NodeId> {
self.data
.pop_front()
.and_then(|node_id| {
self.tree.get(&node_id).ok()
.and_then(|node_ref| {
for child_id in node_ref.children() {
self.data.push_back(child_id.clone());
}

Some(node_id)
})
self.data.pop_front().and_then(|node_id| {
self.tree.get(&node_id).ok().and_then(|node_ref| {
for child_id in node_ref.children() {
self.data.push_back(child_id.clone());
}

Some(node_id)
})
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ impl<T> Tree<T> {
pub fn traverse_post_order_ids(
&self,
node_id: &NodeId,
) -> Result<PostOrderTraversalIds<T>, NodeIdError> {
) -> Result<PostOrderTraversalIds, NodeIdError> {
let (is_valid, error) = self.is_valid_node_id(node_id);
if !is_valid {
return Err(error.expect(
Expand Down

0 comments on commit 9739f5e

Please sign in to comment.