Skip to main content

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        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 PartialEq for StatisticsFlagsIM {
39    fn eq(&self, other: &Self) -> bool {
40        self.get() == other.get()
41    }
42}
43
44impl Eq for StatisticsFlagsIM {}
45
46impl From<StatisticsFlags> for StatisticsFlagsIM {
47    fn from(value: StatisticsFlags) -> Self {
48        Self {
49            inner: RelaxedCell::from(value.bits()),
50        }
51    }
52}
53
54impl StatisticsFlagsIM {
55    #[inline]
56    pub fn new(value: StatisticsFlags) -> Self {
57        Self {
58            inner: RelaxedCell::from(value.bits()),
59        }
60    }
61
62    #[inline]
63    pub fn empty() -> Self {
64        Self::new(StatisticsFlags::empty())
65    }
66
67    #[inline]
68    pub fn get_mut(&mut self) -> StatisticsFlags {
69        StatisticsFlags::from_bits(*self.inner.get_mut()).unwrap()
70    }
71
72    #[inline]
73    pub fn set_mut(&mut self, value: StatisticsFlags) {
74        *self.inner.get_mut() = value.bits();
75    }
76
77    #[inline]
78    pub fn get(&self) -> StatisticsFlags {
79        StatisticsFlags::from_bits(self.inner.load()).unwrap()
80    }
81
82    #[inline]
83    pub fn set(&self, value: StatisticsFlags) {
84        self.inner.store(value.bits());
85    }
86}
87
88impl StatisticsFlags {
89    pub fn is_sorted(&self) -> IsSorted {
90        let is_sorted_asc = self.contains(Self::IS_SORTED_ASC);
91        let is_sorted_dsc = self.contains(Self::IS_SORTED_DSC);
92
93        assert!(!is_sorted_asc || !is_sorted_dsc);
94
95        if is_sorted_asc {
96            IsSorted::Ascending
97        } else if is_sorted_dsc {
98            IsSorted::Descending
99        } else {
100            IsSorted::Not
101        }
102    }
103
104    pub fn set_sorted(&mut self, is_sorted: IsSorted) {
105        let is_sorted = match is_sorted {
106            IsSorted::Not => Self::empty(),
107            IsSorted::Ascending => Self::IS_SORTED_ASC,
108            IsSorted::Descending => Self::IS_SORTED_DSC,
109        };
110        self.remove(Self::IS_SORTED_ASC | Self::IS_SORTED_DSC);
111        self.insert(is_sorted);
112    }
113
114    #[inline]
115    pub fn is_sorted_any(&self) -> bool {
116        self.contains(Self::IS_SORTED_ASC) | self.contains(Self::IS_SORTED_DSC)
117    }
118    pub fn is_sorted_ascending(&self) -> bool {
119        self.contains(Self::IS_SORTED_ASC)
120    }
121    pub fn is_sorted_descending(&self) -> bool {
122        self.contains(Self::IS_SORTED_DSC)
123    }
124
125    pub fn can_fast_explode_list(&self) -> bool {
126        self.contains(Self::CAN_FAST_EXPLODE_LIST)
127    }
128
129    pub fn has_propagated_nulls(&self) -> bool {
130        self.contains(Self::HAS_PROPAGATED_NULLS)
131    }
132
133    pub fn has_trimmed_lists_to_normalized_offsets(&self) -> bool {
134        self.contains(Self::HAS_TRIMMED_LISTS_TO_NORMALIZED_OFFSETS)
135    }
136}