cache get_shift() and get_leaf_shift() in prune_list (#1495)
* cache shift and leaf_shift values in prune_list for fast lookup later * rustfmt * fixup core tests
This commit is contained in:
+11
-2
@@ -594,6 +594,9 @@ pub fn peak_map_height(mut pos: u64) -> (u64, u64) {
|
||||
/// are built.
|
||||
|
||||
pub fn bintree_postorder_height(num: u64) -> u64 {
|
||||
if num == 0 {
|
||||
return 0;
|
||||
}
|
||||
peak_map_height(num - 1).1
|
||||
}
|
||||
|
||||
@@ -669,7 +672,13 @@ pub fn family_branch(pos: u64, last_pos: u64) -> Vec<(u64, u64)> {
|
||||
branch
|
||||
}
|
||||
|
||||
/// Gets the position of the rightmost node (i.e. leaf) relative to the current
|
||||
fn bintree_rightmost(num: u64) -> u64 {
|
||||
/// Gets the position of the rightmost node (i.e. leaf) beneath the provided subtree root.
|
||||
pub fn bintree_rightmost(num: u64) -> u64 {
|
||||
num - bintree_postorder_height(num)
|
||||
}
|
||||
|
||||
/// Gets the position of the rightmost node (i.e. leaf) beneath the provided subtree root.
|
||||
pub fn bintree_leftmost(num: u64) -> u64 {
|
||||
let height = bintree_postorder_height(num);
|
||||
num + 2 - (2 << height)
|
||||
}
|
||||
|
||||
@@ -56,6 +56,32 @@ fn first_100_mmr_heights() {
|
||||
}
|
||||
}
|
||||
|
||||
// The pos of the rightmost leaf for the provided MMR size (last leaf in subtree).
|
||||
#[test]
|
||||
fn test_bintree_rightmost() {
|
||||
assert_eq!(pmmr::bintree_rightmost(0), 0);
|
||||
assert_eq!(pmmr::bintree_rightmost(1), 1);
|
||||
assert_eq!(pmmr::bintree_rightmost(2), 2);
|
||||
assert_eq!(pmmr::bintree_rightmost(3), 2);
|
||||
assert_eq!(pmmr::bintree_rightmost(4), 4);
|
||||
assert_eq!(pmmr::bintree_rightmost(5), 5);
|
||||
assert_eq!(pmmr::bintree_rightmost(6), 5);
|
||||
assert_eq!(pmmr::bintree_rightmost(7), 5);
|
||||
}
|
||||
|
||||
// The pos of the leftmost leaf for the provided MMR size (first leaf in subtree).
|
||||
#[test]
|
||||
fn test_bintree_leftmost() {
|
||||
assert_eq!(pmmr::bintree_leftmost(0), 0);
|
||||
assert_eq!(pmmr::bintree_leftmost(1), 1);
|
||||
assert_eq!(pmmr::bintree_leftmost(2), 2);
|
||||
assert_eq!(pmmr::bintree_leftmost(3), 1);
|
||||
assert_eq!(pmmr::bintree_leftmost(4), 4);
|
||||
assert_eq!(pmmr::bintree_leftmost(5), 5);
|
||||
assert_eq!(pmmr::bintree_leftmost(6), 4);
|
||||
assert_eq!(pmmr::bintree_leftmost(7), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_n_leaves() {
|
||||
// make sure we handle an empty MMR correctly
|
||||
|
||||
Reference in New Issue
Block a user