Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-arrow/src/bitmap/utils/chunk_iterator/merge.rs
6940 views
1
use super::BitChunk;
2
3
/// Merges 2 [`BitChunk`]s into a single [`BitChunk`] so that the new items represents
4
/// the bitmap where bits from `next` are placed in `current` according to `offset`.
5
/// # Panic
6
/// The caller must ensure that `0 < offset < size_of::<T>() * 8`
7
/// # Example
8
/// ```rust,ignore
9
/// let current = 0b01011001;
10
/// let next = 0b01011011;
11
/// let result = merge_reversed(current, next, 1);
12
/// assert_eq!(result, 0b10101100);
13
/// ```
14
#[inline]
15
pub fn merge_reversed<T>(mut current: T, mut next: T, offset: usize) -> T
16
where
17
T: BitChunk,
18
{
19
// 8 _bits_:
20
// current = [c0, c1, c2, c3, c4, c5, c6, c7]
21
// next = [n0, n1, n2, n3, n4, n5, n6, n7]
22
// offset = 3
23
// expected = [n5, n6, n7, c0, c1, c2, c3, c4]
24
25
// 1. unset most significants of `next` up to `offset`
26
let inverse_offset = size_of::<T>() * 8 - offset;
27
next <<= inverse_offset;
28
// next = [n5, n6, n7, 0 , 0 , 0 , 0 , 0 ]
29
30
// 2. unset least significants of `current` up to `offset`
31
current >>= offset;
32
// current = [0 , 0 , 0 , c0, c1, c2, c3, c4]
33
34
current | next
35
}
36
37
#[cfg(test)]
38
mod tests {
39
use super::*;
40
41
#[test]
42
fn test_merge_reversed() {
43
let current = 0b00000000;
44
let next = 0b00000001;
45
let result = merge_reversed::<u8>(current, next, 1);
46
assert_eq!(result, 0b10000000);
47
48
let current = 0b01011001;
49
let next = 0b01011011;
50
let result = merge_reversed::<u8>(current, next, 1);
51
assert_eq!(result, 0b10101100);
52
}
53
54
#[test]
55
fn test_merge_reversed_offset2() {
56
let current = 0b00000000;
57
let next = 0b00000001;
58
let result = merge_reversed::<u8>(current, next, 3);
59
assert_eq!(result, 0b00100000);
60
}
61
}
62
63