Skip to content
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

Simplifies the count_ones definition. #1742

Merged
merged 1 commit into from
Dec 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions fastfield_codecs/src/null_index/dense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ const BLOCK_BITVEC_SIZE: usize = 8;
const BLOCK_OFFSET_SIZE: usize = 4;
const SERIALIZED_BLOCK_SIZE: usize = BLOCK_BITVEC_SIZE + BLOCK_OFFSET_SIZE;

/// Interpreting the bitvec as a list of 64 bits from the low weight to the
/// high weight.
///
/// This function returns the number of bits set to 1 within
/// `[0..pos_in_vec)`.
#[inline]
fn count_ones(bitvec: u64, pos_in_bitvec: u32) -> u32 {
if pos_in_bitvec == 63 {
bitvec.count_ones()
} else {
let mask = (1u64 << (pos_in_bitvec + 1)) - 1;
let masked_bitvec = bitvec & mask;
masked_bitvec.count_ones()
}
let mask = (1u64 << pos_in_bitvec) - 1;
let masked_bitvec = bitvec & mask;
masked_bitvec.count_ones()
}

#[derive(Clone, Copy)]
Expand All @@ -66,9 +67,7 @@ impl DenseCodec {
pub fn exists(&self, idx: u32) -> bool {
let block_pos = idx / ELEMENTS_PER_BLOCK;
let bitvec = self.dense_index_block(block_pos).bitvec;

let pos_in_bitvec = idx % ELEMENTS_PER_BLOCK;

get_bit_at(bitvec, pos_in_bitvec)
}
#[inline]
Expand All @@ -90,8 +89,7 @@ impl DenseCodec {
let pos_in_block_bit_vec = idx % ELEMENTS_PER_BLOCK;
let ones_in_block = count_ones(index_block.bitvec, pos_in_block_bit_vec);
if get_bit_at(index_block.bitvec, pos_in_block_bit_vec) {
// -1 is ok, since idx does exist, so there's at least one
Some(index_block.offset + ones_in_block - 1)
Some(index_block.offset + ones_in_block)
} else {
None
}
Expand Down Expand Up @@ -319,9 +317,10 @@ mod tests {
set_bit_at(&mut block, 0);
set_bit_at(&mut block, 2);

assert_eq!(count_ones(block, 0), 1);
assert_eq!(count_ones(block, 0), 0);
assert_eq!(count_ones(block, 1), 1);
assert_eq!(count_ones(block, 2), 2);
assert_eq!(count_ones(block, 2), 1);
assert_eq!(count_ones(block, 3), 2);
}
}

Expand Down