Make a small code clean-up

This commit is contained in:
many 2021-04-12 11:19:25 +02:00
parent 17c8c6f945
commit 0efa011e09
No known key found for this signature in database
GPG Key ID: 2CEF23B75189EACA

View File

@ -326,30 +326,25 @@ impl<'t, 'q> QueryLevelIterator<'t, 'q> {
struct Branch<'t, 'q> { struct Branch<'t, 'q> {
query_level_iterator: QueryLevelIterator<'t, 'q>, query_level_iterator: QueryLevelIterator<'t, 'q>,
last_result: Option<(u32, u32, RoaringBitmap)>, last_result: (u32, u32, RoaringBitmap),
tree_level: TreeLevel, tree_level: TreeLevel,
branch_size: u32, branch_size: u32,
} }
impl<'t, 'q> Branch<'t, 'q> { impl<'t, 'q> Branch<'t, 'q> {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
fn compute_rank(left: u32, branch_size: u32) -> u32 { left.saturating_sub((0..branch_size).sum()) / branch_size } let compute_rank = |left: u32, branch_size: u32| left.saturating_sub((0..branch_size).sum()) / branch_size;
match (&self.last_result, &other.last_result) { let (s_left, _, _) = self.last_result;
(Some((s_left, _, _)), Some((o_left, _, _))) => { let (o_left, _, _) = other.last_result;
// we compute a rank from the left interval. // we compute a rank from the left interval.
let self_rank = compute_rank(*s_left, self.branch_size); let self_rank = compute_rank(s_left, self.branch_size);
let other_rank = compute_rank(*o_left, other.branch_size); let other_rank = compute_rank(o_left, other.branch_size);
let left_cmp = self_rank.cmp(&other_rank).reverse(); let left_cmp = self_rank.cmp(&other_rank).reverse();
// on level: higher is better, // on level: higher is better,
// we want to reduce highest levels first. // we want to reduce highest levels first.
let level_cmp = self.tree_level.cmp(&other.tree_level); let level_cmp = self.tree_level.cmp(&other.tree_level);
left_cmp.then(level_cmp) left_cmp.then(level_cmp)
},
(Some(_), None) => Ordering::Greater,
(None, Some(_)) => Ordering::Less,
(None, None) => Ordering::Equal,
}
} }
} }
@ -407,13 +402,15 @@ fn initialize_query_level_iterators<'t, 'q>(
if let Some(mut folded_query_level_iterators) = folded_query_level_iterators { if let Some(mut folded_query_level_iterators) = folded_query_level_iterators {
let (tree_level, last_result) = folded_query_level_iterators.next()?; let (tree_level, last_result) = folded_query_level_iterators.next()?;
let branch = Branch { if let Some(last_result) = last_result {
last_result, let branch = Branch {
tree_level, last_result,
query_level_iterator: folded_query_level_iterators, tree_level,
branch_size: branch.len() as u32, query_level_iterator: folded_query_level_iterators,
}; branch_size: branch.len() as u32,
positions.push(branch); };
positions.push(branch);
}
} }
} }
@ -433,28 +430,35 @@ fn set_compute_candidates<'t>(
while let Some(mut branch) = branches_heap.peek_mut() { while let Some(mut branch) = branches_heap.peek_mut() {
let is_lowest_level = branch.tree_level == lowest_level; let is_lowest_level = branch.tree_level == lowest_level;
match branch.last_result.as_mut() { let (_, _, candidates) = &mut branch.last_result;
Some((_, _, candidates)) => { candidates.intersect_with(&allowed_candidates);
candidates.intersect_with(&allowed_candidates); if candidates.is_empty() {
if candidates.len() > 0 && is_lowest_level { // we don't have candidates, get next interval.
// we have candidates, but we can't dig deeper, return candidates. match branch.query_level_iterator.next()? {
final_candidates = Some(std::mem::take(candidates)); (_, Some(last_result)) => {
break; branch.last_result = last_result;
} else if candidates.len() > 0 { },
// we have candidates, lets dig deeper in levels. // TODO clean up this
let mut query_level_iterator = branch.query_level_iterator.dig(ctx)?; (_, None) => { std::collections::binary_heap::PeekMut::<'_, Branch<'_, '_>>::pop(branch); },
let (tree_level, last_result) = query_level_iterator.next()?; }
}
else if is_lowest_level {
// we have candidates, but we can't dig deeper, return candidates.
final_candidates = Some(take(candidates));
break;
} else {
// we have candidates, lets dig deeper in levels.
let mut query_level_iterator = branch.query_level_iterator.dig(ctx)?;
match query_level_iterator.next()? {
(tree_level, Some(last_result)) => {
branch.query_level_iterator = query_level_iterator; branch.query_level_iterator = query_level_iterator;
branch.tree_level = tree_level; branch.tree_level = tree_level;
branch.last_result = last_result; branch.last_result = last_result;
} else { },
// we don't have candidates, get next interval. // TODO clean up this
let (_, last_result) = branch.query_level_iterator.next()?; (_, None) => { std::collections::binary_heap::PeekMut::<'_, Branch<'_, '_>>::pop(branch); },
branch.last_result = last_result; }
}
},
// None = no candidates to find.
None => break,
} }
} }