use std::collections::HashSet;
use polars::prelude::PlSmallStr;
use crate::{core::Domain, error::Fallible};
#[cfg(feature = "ffi")]
mod ffi;
#[derive(Debug, Clone, PartialEq, Default)]
pub struct CategoricalDomain {
categories: Option<Vec<PlSmallStr>>,
}
impl CategoricalDomain {
pub fn new_with_categories(categories: Vec<PlSmallStr>) -> Fallible<Self> {
if categories.len() != HashSet::<_>::from_iter(categories.iter()).len() {
return fallible!(MakeDomain, "categories must be distinct");
}
Ok(CategoricalDomain {
categories: Some(categories),
})
}
pub fn categories(&self) -> Option<&Vec<PlSmallStr>> {
self.categories.as_ref()
}
}
impl Domain for CategoricalDomain {
type Carrier = PlSmallStr;
fn member(&self, value: &Self::Carrier) -> Fallible<bool> {
Ok(self
.categories
.as_ref()
.map(|e| e.contains(value))
.unwrap_or(true))
}
}