Skip to content

Commit

Permalink
Use ArrayVec in KdNodeIter
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Oom committed Aug 17, 2024
1 parent 2d0814e commit aff4eeb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
3 changes: 1 addition & 2 deletions kdtree-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ fn main() {
let kdtree = build_kdtree(&geometries, &cost);
let duration = Instant::now().duration_since(start_time);
let duration = Duration::new(duration.as_secs() as i64, duration.as_nanos() as i32);
eprintln!("Done in {duration:.3}...");

let cost = tree_cost(
&geometries,
Expand All @@ -208,9 +209,7 @@ fn main() {
args.empty_factor,
);
let stats = statistics(&geometries, &kdtree);
eprintln!("Done...");
eprintln!("Tree statistics:");
eprintln!(" Build time: {duration:.3}");
eprintln!(" SAH cost: {cost:.3}");
eprintln!(" Geometries: {}", stats.geometries);
eprintln!(" Node count: {}", stats.node_count);
Expand Down
24 changes: 17 additions & 7 deletions kdtree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ impl KdNode {
}

#[inline]
pub fn iter_nodes(&self) -> KdTreeNodeIter {
KdTreeNodeIter {
stack: vec![(1, self)],
}
pub fn iter_nodes(&self) -> KdNodeIter {
KdNodeIter::new(self)
}

#[inline]
Expand Down Expand Up @@ -152,13 +150,25 @@ impl Display for KdNode {
}
}

pub struct KdTreeNodeIter<'a> {
pub(crate) stack: Vec<(usize, &'a KdNode)>,
pub struct KdNodeIter<'a> {
pub(crate) stack: ArrayVec<(usize, &'a KdNode), MAX_DEPTH>,
}

impl<'a> Iterator for KdTreeNodeIter<'a> {
impl<'a> KdNodeIter<'a> {
#[inline]
fn new(node: &'a KdNode) -> Self {
let mut stack = ArrayVec::<(usize, &'a KdNode), MAX_DEPTH>::new();
unsafe {
stack.push_unchecked((1, node));
}
Self { stack }
}
}

impl<'a> Iterator for KdNodeIter<'a> {
type Item = (usize, &'a KdNode);

#[inline]
fn next(&mut self) -> Option<Self::Item> {
if let Some((depth, node)) = self.stack.pop() {
match node {
Expand Down

0 comments on commit aff4eeb

Please sign in to comment.