1#[cfg(feature = "arrow")]
10mod arrow;
11mod buf;
12mod buf_mut;
13mod count_ones;
14mod index_iterator;
15mod macros;
16mod meta;
17mod ops;
18mod pack;
19mod select;
20mod take;
21mod view;
22
23pub use arrow_buffer::bit_chunk_iterator::BitChunkIterator;
24pub use arrow_buffer::bit_chunk_iterator::BitChunks;
25pub use arrow_buffer::bit_chunk_iterator::UnalignedBitChunk;
26pub use arrow_buffer::bit_chunk_iterator::UnalignedBitChunkIterator;
27pub use arrow_buffer::bit_iterator::BitIterator;
28pub use arrow_buffer::bit_iterator::BitSliceIterator;
29pub use buf::*;
30pub use buf_mut::*;
31pub use index_iterator::BitIndexIterator;
32pub use meta::*;
33pub use pack::*;
34pub use take::*;
35pub use view::*;
36
37#[inline]
43pub fn collect_bool_word<F>(len: usize, f: F) -> u64
44where
45 F: FnMut(usize) -> bool,
46{
47 assert!(len <= 64, "cannot pack {len} bits into a u64 word");
48
49 let mut word = [0u64; 1];
50 collect_bool_words_inline(&mut word, len, f);
51 word[0]
52}
53
54#[inline]
72pub fn collect_bool_words<F>(words: &mut [u64], len: usize, f: F)
73where
74 F: FnMut(usize) -> bool,
75{
76 let num_words = len.div_ceil(64);
77 assert!(
78 words.len() >= num_words,
79 "words slice has {} entries, need at least {num_words}",
80 words.len(),
81 );
82
83 collect_bool_words_inline(words, len, f)
84}
85
86#[inline]
92pub fn read_u64_le(bytes: &[u8]) -> u64 {
93 debug_assert!(bytes.len() <= 8);
94 let mut buf = [0u8; 8];
95 buf[..bytes.len()].copy_from_slice(bytes);
96 u64::from_le_bytes(buf)
97}
98
99#[inline]
111pub fn splice_word_at_bit(words: &mut [u64], bit_offset: usize, word: u64) {
112 let dest_word = bit_offset / 64;
113 let bit_in_word = bit_offset % 64;
114 words[dest_word] |= word << bit_in_word;
115 if bit_in_word != 0 {
116 let high = word >> (64 - bit_in_word);
117 if high != 0 {
118 words[dest_word + 1] = high;
119 }
120 }
121}
122
123#[inline]
132pub fn pack_bools_into_words<F>(words: &mut [u64], bit_offset: usize, len: usize, mut f: F)
133where
134 F: FnMut(usize) -> bool,
135{
136 if len == 0 {
137 return;
138 }
139 let num_words = (bit_offset + len).div_ceil(64);
140 assert!(
141 words.len() >= num_words,
142 "words slice has {} entries, need at least {num_words}",
143 words.len(),
144 );
145
146 let mut done = 0;
147 while len - done >= 64 {
148 let word = collect_bool_word(64, |bit| f(done + bit));
149 splice_word_at_bit(words, bit_offset + done, word);
150 done += 64;
151 }
152 let tail = len - done;
153 if tail > 0 {
154 let word = collect_bool_word(tail, |bit| f(done + bit));
155 splice_word_at_bit(words, bit_offset + done, word);
156 }
157}
158
159#[allow(clippy::inline_always)]
165#[inline(always)]
166pub fn get_bit(buf: &[u8], index: usize) -> bool {
167 buf[index / 8] & (1 << (index % 8)) != 0
168}
169
170#[allow(clippy::inline_always)]
176#[inline(always)]
177pub unsafe fn get_bit_unchecked(buf: *const u8, index: usize) -> bool {
178 (unsafe { *buf.add(index / 8) } & (1 << (index % 8))) != 0
179}
180
181#[allow(clippy::inline_always)]
187#[inline(always)]
188pub unsafe fn set_bit_unchecked(buf: *mut u8, index: usize) {
189 unsafe { *buf.add(index / 8) |= 1 << (index % 8) };
190}
191
192#[allow(clippy::inline_always)]
198#[inline(always)]
199pub unsafe fn unset_bit_unchecked(buf: *mut u8, index: usize) {
200 unsafe { *buf.add(index / 8) &= !(1 << (index % 8)) };
201}
202
203#[cfg(test)]
204mod tests {
205 use super::collect_bool_word;
206 use super::pack_bools_into_words;
207 use super::read_u64_le;
208
209 #[test]
210 fn collect_bool_word_packs_lsb_first() {
211 let word = collect_bool_word(5, |idx| idx.is_multiple_of(2));
212 assert_eq!(word, 0b10101);
213 }
214
215 #[test]
216 fn collect_bool_word_empty() {
217 assert_eq!(collect_bool_word(0, |_| true), 0);
218 }
219
220 #[test]
221 fn read_u64_le_zero_pads_tail() {
222 assert_eq!(read_u64_le(&[0x34, 0x12]), 0x1234);
223 assert_eq!(read_u64_le(&[0xff; 8]), u64::MAX);
224 }
225
226 #[test]
227 #[should_panic(expected = "cannot pack 65 bits into a u64 word")]
228 fn collect_bool_word_rejects_too_many_bits() {
229 let _ = collect_bool_word(65, |_| true);
230 }
231
232 fn pack(bit_offset: usize, len: usize, f: impl Fn(usize) -> bool) -> Vec<bool> {
233 let num_words = (bit_offset + len).div_ceil(64);
234 let mut words = vec![0u64; num_words];
235 pack_bools_into_words(&mut words, bit_offset, len, &f);
236 (0..bit_offset + len)
237 .map(|i| (words[i / 64] >> (i % 64)) & 1 == 1)
238 .collect()
239 }
240
241 #[test]
242 fn pack_bools_aligned_multi_word_with_tail() {
243 let bits = pack(0, 130, |i| i.is_multiple_of(3));
244 for i in 0..130 {
245 assert_eq!(bits[i], i.is_multiple_of(3), "bit {i}");
246 }
247 }
248
249 #[test]
250 fn pack_bools_unaligned_crossing_words() {
251 let bits = pack(40, 200, |i| i.is_multiple_of(7));
252 assert!(bits[..40].iter().all(|&b| !b));
253 for i in 0..200 {
254 assert_eq!(bits[40 + i], i.is_multiple_of(7), "bit {}", 40 + i);
255 }
256 }
257
258 #[test]
259 fn pack_bools_preserves_low_bits_of_leading_word() {
260 let mut words = vec![0u64; 2];
261 words[0] = 0b11111;
262 pack_bools_into_words(&mut words, 5, 70, |_| true);
263 for i in 0..5 {
264 assert_eq!((words[0] >> i) & 1, 1, "preserved bit {i}");
265 }
266 for i in 5..75 {
267 assert_eq!((words[i / 64] >> (i % 64)) & 1, 1, "extended bit {i}");
268 }
269 }
270}