Skip to content

Commit

Permalink
Add reduction operation
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKickliter committed Jan 29, 2023
1 parent 56c5fb4 commit a3eeb65
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/hex_tree_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ impl<V, C> HexTreeMap<V, C> {
}
}

/// Reduces the all children of `hex` using the user-provided
/// function.
pub fn reduce<F>(&self, hex: H3Cell, f: F) -> Option<V>
where
V: Copy,
F: FnMut(u8, &[V]) -> V,
{
let base_cell = base(hex);
match self.nodes[base_cell as usize].as_ref() {
Some(node) => {
let digits = Digits::new(hex);
node.reduce(digits, 0, f)
}
None => None,
}
}

/// Returns a reference to the value corresponding to the given
/// hex or one of its parents.
pub fn get_mut(&mut self, hex: Cell) -> Option<&mut V> {
Expand Down
39 changes: 39 additions & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,45 @@ impl<V> Node<V> {
self.coalesce(res, compactor);
}

pub(crate) fn reduce<F>(&self, mut digits: Digits, res: u8, mut f: F) -> Option<V>
where
V: Copy,
F: FnMut(u8, &[V]) -> V,
{
if let Self::Leaf(_, val) = self {
return Some(*val);
}

match (digits.next(), self) {
(Some(_digit), Self::Leaf(_, _)) => unreachable!(),
(Some(digit), Self::Parent(_, children)) => {
match &children.as_slice()[digit as usize] {
Some(node) => node.reduce(digits, res + 1, f),
None => None,
}
}
(None, node) => Some(node.apply_reduction(res, &mut f)),
}
}

pub(crate) fn apply_reduction<F>(&self, res: u8, f: &mut F) -> V
where
V: Copy,
F: FnMut(u8, &[V]) -> V,
{
match self {
Node::Leaf(_, val) => *val,
Node::Parent(_, children) => {
let nodes_to_reduce: Vec<V> = children
.iter()
.flatten()
.map(|child_node| child_node.apply_reduction(res + 1, f))
.collect();
f(res, nodes_to_reduce.as_slice())
}
}
}

pub(crate) fn coalesce<C>(&mut self, res: u8, compactor: &mut C)
where
C: Compactor<V>,
Expand Down

0 comments on commit a3eeb65

Please sign in to comment.