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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::{bitmap::Bitmap, datatypes::DataType};
use super::{display_fmt, Array};
mod ffi;
mod from;
mod iterator;
mod mutable;
pub use iterator::*;
pub use mutable::*;
#[derive(Debug, Clone)]
pub struct BooleanArray {
data_type: DataType,
values: Bitmap,
validity: Option<Bitmap>,
offset: usize,
}
impl BooleanArray {
#[inline]
pub fn new_empty() -> Self {
Self::from_data(Bitmap::new(), None)
}
#[inline]
pub fn new_null(length: usize) -> Self {
let bitmap = Bitmap::new_zeroed(length);
Self::from_data(bitmap.clone(), Some(bitmap))
}
#[inline]
pub fn from_data(values: Bitmap, validity: Option<Bitmap>) -> Self {
if let Some(ref validity) = validity {
assert_eq!(values.len(), validity.len());
}
Self {
data_type: DataType::Boolean,
values,
validity,
offset: 0,
}
}
#[inline]
pub fn slice(&self, offset: usize, length: usize) -> Self {
let validity = self.validity.clone().map(|x| x.slice(offset, length));
Self {
data_type: self.data_type.clone(),
values: self.values.clone().slice(offset, length),
validity,
offset: self.offset + offset,
}
}
#[inline]
pub fn value(&self, i: usize) -> bool {
self.values.get_bit(i)
}
#[inline]
pub unsafe fn value_unchecked(&self, i: usize) -> bool {
self.values.get_bit_unchecked(i)
}
#[inline]
pub fn values(&self) -> &Bitmap {
&self.values
}
}
impl Array for BooleanArray {
#[inline]
fn as_any(&self) -> &dyn std::any::Any {
self
}
#[inline]
fn len(&self) -> usize {
self.values.len()
}
#[inline]
fn data_type(&self) -> &DataType {
&self.data_type
}
#[inline]
fn validity(&self) -> &Option<Bitmap> {
&self.validity
}
#[inline]
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice(offset, length))
}
}
impl std::fmt::Display for BooleanArray {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
display_fmt(self.iter(), "BooleanArray", f, false)
}
}
impl<P: AsRef<[Option<bool>]>> From<P> for BooleanArray {
#[inline]
fn from(slice: P) -> Self {
Self::from_trusted_len_iter(slice.as_ref().iter().map(|x| x.as_ref()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn basics() {
let data = vec![Some(true), None, Some(false)];
let array: BooleanArray = data.into_iter().collect();
assert_eq!(array.value(0), true);
assert_eq!(array.value(1), false);
assert_eq!(array.value(2), false);
assert_eq!(array.values(), &Bitmap::from_u8_slice(&[0b00000001], 3));
assert_eq!(
array.validity(),
&Some(Bitmap::from_u8_slice(&[0b00000101], 3))
);
assert_eq!(array.is_valid(0), true);
assert_eq!(array.is_valid(1), false);
assert_eq!(array.is_valid(2), true);
let array2 = BooleanArray::from_data(array.values().clone(), array.validity().clone());
assert_eq!(array, array2);
let array = array.slice(1, 2);
assert_eq!(array.value(0), false);
assert_eq!(array.value(1), false);
}
#[test]
fn empty() {
let array = BooleanArray::new_empty();
assert_eq!(array.values().len(), 0);
assert_eq!(array.validity(), &None);
}
}