polars_arrow/legacy/kernels/take_agg/
boolean.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use super::*;

/// Take kernel for single chunk and an iterator as index.
/// # Safety
/// caller must ensure iterators indexes are in bounds
#[inline]
pub unsafe fn take_min_bool_iter_unchecked_nulls<I: IntoIterator<Item = usize>>(
    arr: &BooleanArray,
    indices: I,
    len: IdxSize,
) -> Option<bool> {
    let mut null_count = 0 as IdxSize;
    let validity = arr.validity().unwrap();

    for idx in indices {
        if validity.get_bit_unchecked(idx) {
            if !arr.value_unchecked(idx) {
                return Some(false);
            }
        } else {
            null_count += 1;
        }
    }
    if null_count == len {
        None
    } else {
        Some(true)
    }
}

/// Take kernel for single chunk and an iterator as index.
/// # Safety
/// caller must ensure iterators indexes are in bounds
#[inline]
pub unsafe fn take_min_bool_iter_unchecked_no_nulls<I: IntoIterator<Item = usize>>(
    arr: &BooleanArray,
    indices: I,
) -> Option<bool> {
    if arr.is_empty() {
        return None;
    }

    for idx in indices {
        if !arr.value_unchecked(idx) {
            return Some(false);
        }
    }
    Some(true)
}

/// Take kernel for single chunk and an iterator as index.
/// # Safety
/// caller must ensure iterators indexes are in bounds
#[inline]
pub unsafe fn take_max_bool_iter_unchecked_nulls<I: IntoIterator<Item = usize>>(
    arr: &BooleanArray,
    indices: I,
    len: IdxSize,
) -> Option<bool> {
    let mut null_count = 0 as IdxSize;
    let validity = arr.validity().unwrap();

    for idx in indices {
        if validity.get_bit_unchecked(idx) {
            if arr.value_unchecked(idx) {
                return Some(true);
            }
        } else {
            null_count += 1;
        }
    }
    if null_count == len {
        None
    } else {
        Some(false)
    }
}

/// Take kernel for single chunk and an iterator as index.
/// # Safety
/// caller must ensure iterators indexes are in bounds
#[inline]
pub unsafe fn take_max_bool_iter_unchecked_no_nulls<I: IntoIterator<Item = usize>>(
    arr: &BooleanArray,
    indices: I,
) -> Option<bool> {
    if arr.is_empty() {
        return None;
    }

    for idx in indices {
        if arr.value_unchecked(idx) {
            return Some(true);
        }
    }
    Some(false)
}