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,6 +402,7 @@ 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()?;
if let Some(last_result) = last_result {
let branch = Branch { let branch = Branch {
last_result, last_result,
tree_level, tree_level,
@ -416,6 +412,7 @@ fn initialize_query_level_iterators<'t, 'q>(
positions.push(branch); positions.push(branch);
} }
} }
}
Ok(positions) Ok(positions)
} }
@ -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.len() > 0 && is_lowest_level { if candidates.is_empty() {
// we don't have candidates, get next interval.
match branch.query_level_iterator.next()? {
(_, Some(last_result)) => {
branch.last_result = last_result;
},
// TODO clean up this
(_, None) => { std::collections::binary_heap::PeekMut::<'_, Branch<'_, '_>>::pop(branch); },
}
}
else if is_lowest_level {
// we have candidates, but we can't dig deeper, return candidates. // we have candidates, but we can't dig deeper, return candidates.
final_candidates = Some(std::mem::take(candidates)); final_candidates = Some(take(candidates));
break; break;
} else if candidates.len() > 0 { } else {
// we have candidates, lets dig deeper in levels. // we have candidates, lets dig deeper in levels.
let mut query_level_iterator = branch.query_level_iterator.dig(ctx)?; let mut query_level_iterator = branch.query_level_iterator.dig(ctx)?;
let (tree_level, last_result) = query_level_iterator.next()?; 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.
let (_, last_result) = branch.query_level_iterator.next()?;
branch.last_result = last_result;
}
}, },
// None = no candidates to find. // TODO clean up this
None => break, (_, None) => { std::collections::binary_heap::PeekMut::<'_, Branch<'_, '_>>::pop(branch); },
}
} }
} }