polars_core/chunked_array/
flags.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::sync::atomic::{AtomicU32, Ordering};

use crate::series::IsSorted;

/// An interior mutable version of [`StatisticsFlags`]
pub struct StatisticsFlagsIM {
    inner: AtomicU32,
}

bitflags::bitflags! {
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
    pub struct StatisticsFlags: u32 {
        const IS_SORTED_ANY = 0x03;

        const IS_SORTED_ASC = 0x01;
        const IS_SORTED_DSC = 0x02;
        const CAN_FAST_EXPLODE_LIST = 0x04;
    }
}

impl std::fmt::Debug for StatisticsFlagsIM {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("ChunkedArrayFlagsIM")
            .field(&self.get())
            .finish()
    }
}

impl Clone for StatisticsFlagsIM {
    fn clone(&self) -> Self {
        Self::new(self.get())
    }
}

impl PartialEq for StatisticsFlagsIM {
    fn eq(&self, other: &Self) -> bool {
        self.get() == other.get()
    }
}
impl Eq for StatisticsFlagsIM {}

impl From<StatisticsFlags> for StatisticsFlagsIM {
    fn from(value: StatisticsFlags) -> Self {
        Self {
            inner: AtomicU32::new(value.bits()),
        }
    }
}

impl StatisticsFlagsIM {
    pub fn new(value: StatisticsFlags) -> Self {
        Self {
            inner: AtomicU32::new(value.bits()),
        }
    }

    pub fn empty() -> Self {
        Self::new(StatisticsFlags::empty())
    }

    pub fn get_mut(&mut self) -> StatisticsFlags {
        StatisticsFlags::from_bits(*self.inner.get_mut()).unwrap()
    }
    pub fn set_mut(&mut self, value: StatisticsFlags) {
        *self.inner.get_mut() = value.bits();
    }

    pub fn get(&self) -> StatisticsFlags {
        StatisticsFlags::from_bits(self.inner.load(Ordering::Relaxed)).unwrap()
    }
    pub fn set(&self, value: StatisticsFlags) {
        self.inner.store(value.bits(), Ordering::Relaxed);
    }
}

impl StatisticsFlags {
    pub fn is_sorted(&self) -> IsSorted {
        let is_sorted_asc = self.contains(Self::IS_SORTED_ASC);
        let is_sorted_dsc = self.contains(Self::IS_SORTED_DSC);

        assert!(!is_sorted_asc || !is_sorted_dsc);

        if is_sorted_asc {
            IsSorted::Ascending
        } else if is_sorted_dsc {
            IsSorted::Descending
        } else {
            IsSorted::Not
        }
    }

    pub fn set_sorted(&mut self, is_sorted: IsSorted) {
        let is_sorted = match is_sorted {
            IsSorted::Not => Self::empty(),
            IsSorted::Ascending => Self::IS_SORTED_ASC,
            IsSorted::Descending => Self::IS_SORTED_DSC,
        };
        self.remove(Self::IS_SORTED_ASC | Self::IS_SORTED_DSC);
        self.insert(is_sorted);
    }

    pub fn is_sorted_any(&self) -> bool {
        self.contains(Self::IS_SORTED_ASC) | self.contains(Self::IS_SORTED_DSC)
    }
    pub fn is_sorted_ascending(&self) -> bool {
        self.contains(Self::IS_SORTED_ASC)
    }
    pub fn is_sorted_descending(&self) -> bool {
        self.contains(Self::IS_SORTED_DSC)
    }

    pub fn can_fast_explode_list(&self) -> bool {
        self.contains(Self::CAN_FAST_EXPLODE_LIST)
    }
}