Path: blob/main/crates/polars-arrow/src/bitmap/utils/chunk_iterator/merge.rs
6940 views
use super::BitChunk;12/// Merges 2 [`BitChunk`]s into a single [`BitChunk`] so that the new items represents3/// the bitmap where bits from `next` are placed in `current` according to `offset`.4/// # Panic5/// The caller must ensure that `0 < offset < size_of::<T>() * 8`6/// # Example7/// ```rust,ignore8/// let current = 0b01011001;9/// let next = 0b01011011;10/// let result = merge_reversed(current, next, 1);11/// assert_eq!(result, 0b10101100);12/// ```13#[inline]14pub fn merge_reversed<T>(mut current: T, mut next: T, offset: usize) -> T15where16T: BitChunk,17{18// 8 _bits_:19// current = [c0, c1, c2, c3, c4, c5, c6, c7]20// next = [n0, n1, n2, n3, n4, n5, n6, n7]21// offset = 322// expected = [n5, n6, n7, c0, c1, c2, c3, c4]2324// 1. unset most significants of `next` up to `offset`25let inverse_offset = size_of::<T>() * 8 - offset;26next <<= inverse_offset;27// next = [n5, n6, n7, 0 , 0 , 0 , 0 , 0 ]2829// 2. unset least significants of `current` up to `offset`30current >>= offset;31// current = [0 , 0 , 0 , c0, c1, c2, c3, c4]3233current | next34}3536#[cfg(test)]37mod tests {38use super::*;3940#[test]41fn test_merge_reversed() {42let current = 0b00000000;43let next = 0b00000001;44let result = merge_reversed::<u8>(current, next, 1);45assert_eq!(result, 0b10000000);4647let current = 0b01011001;48let next = 0b01011011;49let result = merge_reversed::<u8>(current, next, 1);50assert_eq!(result, 0b10101100);51}5253#[test]54fn test_merge_reversed_offset2() {55let current = 0b00000000;56let next = 0b00000001;57let result = merge_reversed::<u8>(current, next, 3);58assert_eq!(result, 0b00100000);59}60}616263