Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: iwburns/id-tree
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: Granola-Team/id-tree
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 2 commits
  • 1 file changed
  • 2 contributors

Commits on Aug 22, 2023

  1. Copy the full SHA
    528aff4 View commit details
  2. Merge pull request #1 from Granola-Team/fix/height-of-node

    rewrite Tree::height_of_node to not be recursive, + add inlining
    jenr24G authored Aug 22, 2023
    Copy the full SHA
    df64e00 View commit details
Showing with 15 additions and 5 deletions.
  1. +15 −5 src/tree.rs
20 changes: 15 additions & 5 deletions src/tree.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cmp::Ordering;
use std::collections::VecDeque;

use super::snowflake::ProcessUniqueId;
use super::*;
@@ -200,20 +201,29 @@ impl<T> Tree<T> {
/// assert_eq!(2, tree.height());
/// ```
///
#[inline(always)]
pub fn height(&self) -> usize {
match self.root {
Some(ref id) => self.height_of_node(id),
_ => 0,
}
}

#[inline(always)]
fn height_of_node(&self, node: &NodeId) -> usize {
let mut h = 0;
for n in self.children_ids(node).unwrap() {
h = std::cmp::max(h, self.height_of_node(n));
let mut h = 1;
let mut to_process = VecDeque::new();

to_process.push_back((h, node));
while !to_process.is_empty() {
let (next_h, id) = to_process.pop_front().unwrap();
self.children_ids(id).unwrap().for_each(|child_id| {
to_process.push_back((next_h + 1, child_id));
h = std::cmp::max(h, next_h + 1);
});
}

h + 1
h
}

/// Inserts a new `Node` into the `Tree`. The `InsertBehavior` provided will determine where