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}
21
22impl std::fmt::Debug for StatisticsFlagsIM {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        f.debug_tuple("ChunkedArrayFlagsIM")
25            .field(&self.get())
26            .finish()
27    }
28}
29
30impl Clone for StatisticsFlagsIM {
31    fn clone(&self) -> Self {
32        Self::new(self.get())
33    }
34}
35
36impl PartialEq for StatisticsFlagsIM {
37    fn eq(&self, other: &Self) -> bool {
38        self.get() == other.get()
39    }
40}
41impl Eq for StatisticsFlagsIM {}
42
43impl From<StatisticsFlags> for StatisticsFlagsIM {
44    fn from(value: StatisticsFlags) -> Self {
45        Self {
46            inner: AtomicU32::new(value.bits()),
47        }
48    }
49}
50
51impl StatisticsFlagsIM {
52    pub fn new(value: StatisticsFlags) -> Self {
53        Self {
54            inner: AtomicU32::new(value.bits()),
55        }
56    }
57
58    pub fn empty() -> Self {
59        Self::new(StatisticsFlags::empty())
60    }
61
62    pub fn get_mut(&mut self) -> StatisticsFlags {
63        StatisticsFlags::from_bits(*self.inner.get_mut()).unwrap()
64    }
65    pub fn set_mut(&mut self, value: StatisticsFlags) {
66        *self.inner.get_mut() = value.bits();
67    }
68
69    pub fn get(&self) -> StatisticsFlags {
70        StatisticsFlags::from_bits(self.inner.load(Ordering::Relaxed)).unwrap()
71    }
72    pub fn set(&self, value: StatisticsFlags) {
73        self.inner.store(value.bits(), Ordering::Relaxed);
74    }
75}
76
77impl StatisticsFlags {
78    pub fn is_sorted(&self) -> IsSorted {
79        let is_sorted_asc = self.contains(Self::IS_SORTED_ASC);
80        let is_sorted_dsc = self.contains(Self::IS_SORTED_DSC);
81
82        assert!(!is_sorted_asc || !is_sorted_dsc);
83
84        if is_sorted_asc {
85            IsSorted::Ascending
86        } else if is_sorted_dsc {
87            IsSorted::Descending
88        } else {
89            IsSorted::Not
90        }
91    }
92
93    pub fn set_sorted(&mut self, is_sorted: IsSorted) {
94        let is_sorted = match is_sorted {
95            IsSorted::Not => Self::empty(),
96            IsSorted::Ascending => Self::IS_SORTED_ASC,
97            IsSorted::Descending => Self::IS_SORTED_DSC,
98        };
99        self.remove(Self::IS_SORTED_ASC | Self::IS_SORTED_DSC);
100        self.insert(is_sorted);
101    }
102
103    pub fn is_sorted_any(&self) -> bool {
104        self.contains(Self::IS_SORTED_ASC) | self.contains(Self::IS_SORTED_DSC)
105    }
106    pub fn is_sorted_ascending(&self) -> bool {
107        self.contains(Self::IS_SORTED_ASC)
108    }
109    pub fn is_sorted_descending(&self) -> bool {
110        self.contains(Self::IS_SORTED_DSC)
111    }
112
113    pub fn can_fast_explode_list(&self) -> bool {
114        self.contains(Self::CAN_FAST_EXPLODE_LIST)
115    }
116}