vortex_array/stats/
mod.rs1use arrow_buffer::BooleanBufferBuilder;
9use arrow_buffer::MutableBuffer;
10use arrow_buffer::bit_iterator::BitIterator;
11use enum_iterator::last;
12pub use stats_set::*;
13
14mod array;
15mod bound;
16pub mod flatbuffers;
17pub mod majority;
18mod precision;
19mod provider;
20mod stat;
21mod stat_bound;
22mod stats_set;
23
24pub use array::*;
25pub use bound::*;
26pub use precision::*;
27pub use provider::*;
28pub use stat::*;
29pub use stat_bound::*;
30use vortex_error::VortexExpect;
31
32pub fn as_stat_bitset_bytes(stats: &[Stat]) -> Vec<u8> {
33 let max_stat = u8::from(last::<Stat>().vortex_expect("last stat")) as usize + 1;
34 let mut stat_bitset = BooleanBufferBuilder::new_from_buffer(
36 MutableBuffer::from_len_zeroed(max_stat.div_ceil(8)),
37 max_stat,
38 );
39 for stat in stats {
40 stat_bitset.set_bit(u8::from(*stat) as usize, true);
41 }
42
43 stat_bitset
44 .finish()
45 .into_inner()
46 .into_vec()
47 .unwrap_or_else(|b| b.to_vec())
48}
49
50pub fn stats_from_bitset_bytes(bytes: &[u8]) -> Vec<Stat> {
51 BitIterator::new(bytes, 0, bytes.len() * 8)
52 .enumerate()
53 .filter_map(|(i, b)| b.then_some(i))
54 .filter_map(|i| {
56 let Ok(stat) = u8::try_from(i) else {
57 tracing::debug!("invalid stat encountered: {i}");
58 return None;
59 };
60 Stat::try_from(stat).ok()
61 })
62 .collect::<Vec<_>>()
63}