Skip to content

Commit

Permalink
Eliminated last compile error
Browse files Browse the repository at this point in the history
  • Loading branch information
davids91 committed Apr 26, 2023
1 parent b217aec commit 5a0eba4
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ where
}

/// Creates a new `Node<T>` with the given bounds and data.
pub(crate) fn new_with_ty(bounds: Bounds, ty: NodeType<T>) -> Self {
fn new_with_ty(bounds: Bounds, ty: NodeType<T>) -> Self {
Self {
ty,
bounds,
Expand Down Expand Up @@ -280,7 +280,7 @@ where
if data.as_ref().is_none() {
data = match &child.ty {
NodeType::Leaf(d) => Some(d),
_ => panic!("Leaf Node `ty` member is not NodeType::Leaf(T)!"),
_ => panic!("Leaf Node `ty` member is not NodeType::Leaf(T) when it should be!"),
};
} else if *data.as_ref().unwrap() != leaf_data.unwrap() {
return false;
Expand All @@ -306,13 +306,16 @@ where
/// For all children of a leaf `Node`, take the most common data of all children,
/// destroy all children, and mark the `Node` as a leaf containing that data.
pub(crate) fn lod(&mut self) {
let mut all_data = [(); OCTREE_CHILDREN].map(|_| Default::default());
for (i, c) in self.children.iter_mut().enumerate().map(|(i, c)| (i, c.deref_mut())) {
let mut all_data = Vec::<T>::new();
for (_i, c) in self.children.iter_mut().enumerate().map(|(i, c)| (i, c.deref_mut())) {
if let Some(c) = c {
if c.is_leaf() {
let leaf_data = c.leaf_data();
if leaf_data.is_some() {
all_data[i] = std::mem::replace(&mut c.ty, NodeType::Simplified);
all_data.push(match std::mem::replace(&mut c.ty, NodeType::Simplified) {
NodeType::Leaf(d) => d,
_ => panic!("Leaf Node `ty` member is not NodeType::Leaf(T) when it should be!"),
});
}
} else {
c.lod();
Expand All @@ -322,16 +325,14 @@ where
}
}

let mut counts = HashMap::new();
for data in all_data.iter() {
counts.entry(*data).and_modify(|e| *e += 1).or_insert(1);
}
// Counting how many times a certain data value is present inside the children
let counts = all_data.drain(..).fold(HashMap::new(), |mut acc, v| {
acc.entry(v).and_modify(|e| *e += 1).or_insert(1);
acc
});

if !counts.is_empty() {
let mut counts = counts.iter().collect::<Vec<(&T, &i32)>>();
counts.sort_by(|a, b| b.1.cmp(a.1));

self.ty = NodeType::Leaf(*counts[0].0);
self.ty = NodeType::Leaf(counts.into_iter().max_by_key(|(_, count)| *count).unwrap().0);
}

self.children.iter_mut().for_each(|it| *it = Box::new(None));
Expand Down

0 comments on commit 5a0eba4

Please sign in to comment.