-
Notifications
You must be signed in to change notification settings - Fork 990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PMMR Backend Support for append_pruned_root (Continued) #3659
Changes from 14 commits
daa688f
8d283bc
2428c6b
aa9659f
78a7049
2d6eae3
c9edb5b
b7bac19
1f3b0c6
a2eaa49
764f3ea
7c839f0
649d264
3c7d7a4
f794e58
04511d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::{marker, ops::Range, u64}; | ||
use std::{cmp::max, marker, ops::Range, u64}; | ||
|
||
use croaring::Bitmap; | ||
|
||
|
@@ -568,6 +568,9 @@ pub fn bintree_postorder_height(num: u64) -> u64 { | |
/// of any size (somewhat unintuitively but this is how the PMMR is "append | ||
/// only"). | ||
pub fn is_leaf(pos: u64) -> bool { | ||
if pos == 0 { | ||
return false; | ||
} | ||
bintree_postorder_height(pos) == 0 | ||
} | ||
|
||
|
@@ -665,14 +668,27 @@ pub fn family_branch(pos: u64, last_pos: u64) -> Vec<(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) | ||
pub fn bintree_rightmost(pos: u64) -> u64 { | ||
pos - bintree_postorder_height(pos) | ||
} | ||
|
||
/// 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) | ||
pub fn bintree_leftmost(pos: u64) -> u64 { | ||
let height = bintree_postorder_height(pos); | ||
pos + 2 - (2 << height) | ||
} | ||
|
||
/// Iterator over all leaf pos beneath the provided subtree root (including the root itself). | ||
pub fn bintree_leaf_pos_iter(pos: u64) -> impl Iterator<Item = u64> { | ||
let leaf_start = max(1, bintree_leftmost(pos as u64)); | ||
let leaf_end = bintree_rightmost(pos as u64); | ||
(leaf_start..=leaf_end).filter(|x| is_leaf(*x)) | ||
} | ||
|
||
/// Iterator over all pos beneath the provided subtree root (including the root itself). | ||
pub fn bintree_pos_iter(pos: u64) -> impl Iterator<Item = u64> { | ||
let leaf_start = max(1, bintree_leftmost(pos as u64)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the max(1, part is redundant. bintree_leftmost, like bintree_postorder_height uses 1-based positions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's redundant in theory however the tests are constructed with the assumption that |
||
(leaf_start..=pos).into_iter() | ||
} | ||
|
||
/// All pos in the subtree beneath the provided root, including root itself. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be more efficient to iterate over leaf_indices, and map those into node_indices with the very cheap
map( \leaf_index -> 2 * leaf_index - count_ones(leaf_index) )
probably should add that as a method leaf_to_node_index()...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to iterate over the leaf indices of a subtree without reading them from an already-stored bitmap, (which is implementation-specific and not available in these helpers which I assume are supposed to be 'pure' MMR operations)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't see what a bitmap has to do with this routine which is only about indices.
i'm suggesting to convert both leaf_start and leaf_end into leaf indices (which is as expensive as an is_leaf test), using a node_to_leaf_index method, then construct a range from those, and map each element back to a node index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, that makes it clearer, will implement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the max(1, part is also redundant here.
PS: I just made a PR rewriting that MMR doc to stick with 0-based positions.