polars_core/chunked_array/
flags.rs

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