Path: blob/main/crates/polars-core/src/chunked_array/flags.rs
6940 views
use polars_utils::relaxed_cell::RelaxedCell;12use crate::series::IsSorted;34/// An interior mutable version of [`StatisticsFlags`]5#[derive(Clone)]6pub struct StatisticsFlagsIM {7inner: RelaxedCell<u32>,8}910bitflags::bitflags! {11#[derive(Clone, Copy, Debug, PartialEq, Eq)]12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]13pub struct StatisticsFlags: u32 {14const IS_SORTED_ANY = 0x03;1516const IS_SORTED_ASC = 0x01;17const IS_SORTED_DSC = 0x02;18const CAN_FAST_EXPLODE_LIST = 0x04;1920/// Recursive version of `CAN_FAST_EXPLODE_LIST`.21///22/// This can also apply to other nested chunked arrays and signals that there all lists23/// have been compacted recursively.24const HAS_TRIMMED_LISTS_TO_NORMALIZED_OFFSETS = 0x08;25/// All masked out values have their nulls propagated.26const HAS_PROPAGATED_NULLS = 0x10;27}28}2930impl std::fmt::Debug for StatisticsFlagsIM {31fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {32f.debug_tuple("ChunkedArrayFlagsIM")33.field(&self.get())34.finish()35}36}3738impl PartialEq for StatisticsFlagsIM {39fn eq(&self, other: &Self) -> bool {40self.get() == other.get()41}42}4344impl Eq for StatisticsFlagsIM {}4546impl From<StatisticsFlags> for StatisticsFlagsIM {47fn from(value: StatisticsFlags) -> Self {48Self {49inner: RelaxedCell::from(value.bits()),50}51}52}5354impl StatisticsFlagsIM {55pub fn new(value: StatisticsFlags) -> Self {56Self {57inner: RelaxedCell::from(value.bits()),58}59}6061pub fn empty() -> Self {62Self::new(StatisticsFlags::empty())63}6465pub fn get_mut(&mut self) -> StatisticsFlags {66StatisticsFlags::from_bits(*self.inner.get_mut()).unwrap()67}68pub fn set_mut(&mut self, value: StatisticsFlags) {69*self.inner.get_mut() = value.bits();70}7172pub fn get(&self) -> StatisticsFlags {73StatisticsFlags::from_bits(self.inner.load()).unwrap()74}75pub fn set(&self, value: StatisticsFlags) {76self.inner.store(value.bits());77}78}7980impl StatisticsFlags {81pub fn is_sorted(&self) -> IsSorted {82let is_sorted_asc = self.contains(Self::IS_SORTED_ASC);83let is_sorted_dsc = self.contains(Self::IS_SORTED_DSC);8485assert!(!is_sorted_asc || !is_sorted_dsc);8687if is_sorted_asc {88IsSorted::Ascending89} else if is_sorted_dsc {90IsSorted::Descending91} else {92IsSorted::Not93}94}9596pub fn set_sorted(&mut self, is_sorted: IsSorted) {97let is_sorted = match is_sorted {98IsSorted::Not => Self::empty(),99IsSorted::Ascending => Self::IS_SORTED_ASC,100IsSorted::Descending => Self::IS_SORTED_DSC,101};102self.remove(Self::IS_SORTED_ASC | Self::IS_SORTED_DSC);103self.insert(is_sorted);104}105106pub fn is_sorted_any(&self) -> bool {107self.contains(Self::IS_SORTED_ASC) | self.contains(Self::IS_SORTED_DSC)108}109pub fn is_sorted_ascending(&self) -> bool {110self.contains(Self::IS_SORTED_ASC)111}112pub fn is_sorted_descending(&self) -> bool {113self.contains(Self::IS_SORTED_DSC)114}115116pub fn can_fast_explode_list(&self) -> bool {117self.contains(Self::CAN_FAST_EXPLODE_LIST)118}119120pub fn has_propagated_nulls(&self) -> bool {121self.contains(Self::HAS_PROPAGATED_NULLS)122}123124pub fn has_trimmed_lists_to_normalized_offsets(&self) -> bool {125self.contains(Self::HAS_TRIMMED_LISTS_TO_NORMALIZED_OFFSETS)126}127}128129130