#![doc = include_str!("../README.md")]
#![warn(clippy::pedantic)]
#![warn(missing_docs)]
#![warn(clippy::missing_safety_doc)]
#![warn(clippy::missing_docs_in_private_items)]
#![warn(clippy::undocumented_unsafe_blocks)]
#![macro_use]
macro_rules! smallvec {
($elem: expr; $n: expr) => (
SmallVec::from_elem($elem, $n)
);
($($x: expr), *$(,)*) => ({
let vec: SmallVec<_> = smallvec::smallvec![$($x,)*];
vec
});
}
pub mod class;
pub mod levels;
pub mod mset;
pub mod prelude;
pub mod set;
mod tests;
use prelude::*;
type SmallVec<T> = smallvec::SmallVec<[T; 4]>;
fn consecutive_eq<T: PartialEq>(slice: &[T]) -> bool {
(1..slice.len()).any(|i| slice[i - 1] == slice[i])
}
unsafe fn transmute_vec<T, U>(vec: Vec<T>) -> Vec<U> {
assert_eq!(mem::size_of::<T>(), mem::size_of::<U>());
assert_eq!(mem::align_of::<T>(), mem::align_of::<U>());
let mut vec = mem::ManuallyDrop::new(vec);
Vec::from_raw_parts(vec.as_mut_ptr().cast(), vec.len(), vec.capacity())
}
fn reuse_vec<'a, T>(mut vec: Vec<&T>) -> Vec<&'a T> {
vec.clear();
unsafe { transmute_vec(vec) }
}
trait Seal {}
#[allow(private_bounds)]
pub trait SetTrait:
Seal
+ AsRef<Mset>
+ Clone
+ Debug
+ Default
+ Display
+ Eq
+ FromStr<Err = SetError>
+ Into<Vec<Self>>
+ IntoIterator<Item = Self>
+ PartialOrd
+ 'static
{
fn as_slice(&self) -> &[Self];
#[allow(clippy::missing_safety_doc)]
#[deprecated = "internal method, use Mset::as_mut_slice or Set::as_mut_slice."]
unsafe fn _as_mut_slice(&mut self) -> &mut [Self];
fn as_vec(&self) -> &Vec<Mset>;
#[allow(clippy::missing_safety_doc)]
#[deprecated = "internal method, use Mset::as_mut_vec or Set::as_mut_vec."]
unsafe fn _as_mut_vec(&mut self) -> &mut Vec<Mset>;
fn into_vec(self) -> Vec<Self> {
self.into()
}
#[deprecated = "internal method, use Mset::from or Set::flatten."]
fn _flatten_vec(vec: Vec<Self>) -> Self;
fn clear(&mut self) {
#[allow(deprecated)]
unsafe {
self._as_mut_vec().clear();
}
}
fn card(&self) -> usize {
self.as_slice().len()
}
fn is_empty(&self) -> bool {
self.as_slice().is_empty()
}
fn capacity(&self) -> usize {
self.as_vec().capacity()
}
fn iter(&self) -> slice::Iter<Self> {
self.as_slice().iter()
}
fn ahu(&self) -> Ahu {
Ahu::new(self.as_ref())
}
fn rank(&self) -> usize {
unsafe {
Levels::new(self.as_ref())
.nest_vec()
.level_len()
.unchecked_sub(1)
}
}
fn empty() -> Self;
fn with_capacity(capacity: usize) -> Self;
#[must_use]
fn singleton(self) -> Self;
fn into_singleton(self) -> Option<Self>;
fn as_singleton(&self) -> Option<&Self> {
if self.card() == 1 {
None
} else {
self.as_slice().first()
}
}
fn as_singleton_mut(&mut self) -> Option<&mut Self> {
if self.card() == 1 {
#[allow(deprecated)]
unsafe {
self._as_mut_slice().first_mut()
}
} else {
None
}
}
fn insert_mut(&mut self, set: Self);
#[must_use]
fn insert(mut self, set: Self) -> Self {
self.insert_mut(set);
self
}
fn select_mut<P: FnMut(&Self) -> bool>(&mut self, pred: P);
#[must_use]
fn select<P: FnMut(&Self) -> bool>(mut self, pred: P) -> Self {
self.select_mut(pred);
self
}
#[must_use]
fn pair(self, other: Self) -> Self {
self.singleton().insert(other)
}
fn count(&self, set: &Self) -> usize;
fn sum_vec(vec: Vec<Self>) -> Self;
#[must_use]
fn sum(self, other: Self) -> Self {
Self::sum_vec(vec![self, other])
}
#[must_use]
fn big_sum(self) -> Self {
Self::sum_vec(self.into())
}
fn union_vec(vec: Vec<Self>) -> Self;
#[must_use]
fn union(self, other: Self) -> Self {
Self::union_vec(vec![self, other])
}
#[must_use]
fn big_union(self) -> Self {
Self::union_vec(self.into())
}
fn inter_vec(vec: Vec<Self>) -> Option<Self>;
#[must_use]
fn inter(self, other: Self) -> Self {
unsafe { Self::inter_vec(vec![self, other]).unwrap_unchecked() }
}
#[must_use]
fn big_inter(self) -> Option<Self> {
Self::inter_vec(self.into())
}
#[must_use]
fn powerset(self) -> Self;
fn nat(n: usize) -> Self;
fn zermelo(n: usize) -> Self;
fn neumann(n: usize) -> Self;
fn subset(&self, other: &Self) -> bool {
self <= other
}
fn ssubset(&self, other: &Self) -> bool {
self < other
}
fn contains_iter<I: IntoIterator>(iter: I, other: &Self) -> bool
where
I::Item: AsRef<Mset>,
{
let mut cmp = Compare::new(other.as_ref());
iter.into_iter().any(|set| cmp.eq(set.as_ref()))
}
fn contains(&self, other: &Self) -> bool {
Self::contains_iter(self.iter(), other)
}
fn disjoint(&self, other: &Self) -> bool {
Self::disjoint_pairwise([self, other])
}
fn disjoint_iter<'a, I: IntoIterator<Item = &'a Self>>(iter: I) -> bool;
fn disjoint_set(&self) -> bool {
Self::disjoint_iter(self.iter())
}
fn disjoint_pairwise<'a, I: IntoIterator<Item = &'a Self>>(iter: I) -> bool;
fn disjoint_pairwise_set(&self) -> bool {
Self::disjoint_pairwise(self.iter())
}
#[must_use]
#[allow(deprecated)]
fn replace<F: FnMut(&Self) -> Self>(&self, func: F) -> Self {
Self::_flatten_vec(self.iter().map(func).collect())
}
#[must_use]
#[allow(deprecated)]
fn into_replace<F: FnMut(Self) -> Self>(self, func: F) -> Self {
Self::_flatten_vec(self.into_iter().map(func).collect())
}
fn choose(&self) -> Option<&Self> {
self.as_slice().first()
}
fn into_choose(self) -> Option<Self>;
fn choose_uniq(&self) -> Option<&Self>;
fn into_choose_uniq(self) -> Option<Self>;
}