polars_core/chunked_array/
flags.rs

1use std::sync::atomic::{AtomicU32, Ordering};
2
3use crate::series::IsSorted;
4
5/// An interior mutable version of [`StatisticsFlags`]
6pub struct StatisticsFlagsIM {
7    inner: AtomicU32,
8}
9
10bitflags::bitflags! {
11    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
12    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13    pub struct StatisticsFlags: u32 {
14        const IS_SORTED_ANY = 0x03;
15
16        const IS_SORTED_ASC = 0x01;
17        const IS_SORTED_DSC = 0x02;
18        const CAN_FAST_EXPLODE_LIST = 0x04;
19
20        /// Recursive version of `CAN_FAST_EXPLODE_LIST`.
21        ///
22        /// This can also apply to other nested chunked arrays and signals that there all lists
23        /// have been compacted recursively.
24        const HAS_TRIMMED_LISTS_TO_NORMALIZED_OFFSETS = 0x08;
25        /// All masked out values have their nulls propagated.
26        const HAS_PROPAGATED_NULLS = 0x10;
27    }
28}
29
30impl std::fmt::Debug for StatisticsFlagsIM {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        f.debug_tuple("ChunkedArrayFlagsIM")
33            .field(&self.get())
34            .finish()
35    }
36}
37
38impl Clone for StatisticsFlagsIM {
39    fn clone(&self) -> Self {
40        Self::new(self.get())
41    }
42}
43
44impl PartialEq for StatisticsFlagsIM {
45    fn eq(&self, other: &Self) -> bool {
46        self.get() == other.get()
47    }
48}
49impl Eq for StatisticsFlagsIM {}
50
51impl From<StatisticsFlags> for StatisticsFlagsIM {
52    fn from(value: StatisticsFlags) -> Self {
53        Self {
54            inner: AtomicU32::new(value.bits()),
55        }
56    }
57}
58
59impl StatisticsFlagsIM {
60    pub fn new(value: StatisticsFlags) -> Self {
61        Self {
62            inner: AtomicU32::new(value.bits()),
63        }
64    }
65
66    pub fn empty() -> Self {
67        Self::new(StatisticsFlags::empty())
68    }
69
70    pub fn get_mut(&mut self) -> StatisticsFlags {
71        StatisticsFlags::from_bits(*self.inner.get_mut()).unwrap()
72    }
73    pub fn set_mut(&mut self, value: StatisticsFlags) {
74        *self.inner.get_mut() = value.bits();
75    }
76
77    pub fn get(&self) -> StatisticsFlags {
78        StatisticsFlags::from_bits(self.inner.load(Ordering::Relaxed)).unwrap()
79    }
80    pub fn set(&self, value: StatisticsFlags) {
81        self.inner.store(value.bits(), Ordering::Relaxed);
82    }
83}
84
85impl StatisticsFlags {
86    pub fn is_sorted(&self) -> IsSorted {
87        let is_sorted_asc = self.contains(Self::IS_SORTED_ASC);
88        let is_sorted_dsc = self.contains(Self::IS_SORTED_DSC);
89
90        assert!(!is_sorted_asc || !is_sorted_dsc);
91
92        if is_sorted_asc {
93            IsSorted::Ascending
94        } else if is_sorted_dsc {
95            IsSorted::Descending
96        } else {
97            IsSorted::Not
98        }
99    }
100
101    pub fn set_sorted(&mut self, is_sorted: IsSorted) {
102        let is_sorted = match is_sorted {
103            IsSorted::Not => Self::empty(),
104            IsSorted::Ascending => Self::IS_SORTED_ASC,
105            IsSorted::Descending => Self::IS_SORTED_DSC,
106        };
107        self.remove(Self::IS_SORTED_ASC | Self::IS_SORTED_DSC);
108        self.insert(is_sorted);
109    }
110
111    pub fn is_sorted_any(&self) -> bool {
112        self.contains(Self::IS_SORTED_ASC) | self.contains(Self::IS_SORTED_DSC)
113    }
114    pub fn is_sorted_ascending(&self) -> bool {
115        self.contains(Self::IS_SORTED_ASC)
116    }
117    pub fn is_sorted_descending(&self) -> bool {
118        self.contains(Self::IS_SORTED_DSC)
119    }
120
121    pub fn can_fast_explode_list(&self) -> bool {
122        self.contains(Self::CAN_FAST_EXPLODE_LIST)
123    }
124
125    pub fn has_propagated_nulls(&self) -> bool {
126        self.contains(Self::HAS_PROPAGATED_NULLS)
127    }
128
129    pub fn has_trimmed_lists_to_normalized_offsets(&self) -> bool {
130        self.contains(Self::HAS_TRIMMED_LISTS_TO_NORMALIZED_OFFSETS)
131    }
132}