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