zfc

Library for sets according to ZFC.
git clone https://git.philomathiclife.com/repos/zfc
Log | Files | Refs | README

commit e81c0b442b32a4b537b541bef4fd7a997a514644
parent d0519057f07b1ae329711066f87a696be54a0a23
Author: Zack Newman <zack@philomathiclife.com>
Date:   Mon, 28 Aug 2023 16:12:38 -0600

make cardinality more useful

Diffstat:
MCargo.toml | 4++--
Msrc/lib.rs | 45++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -2,14 +2,14 @@ authors = ["Zack Newman <zack@philomathiclife.com>"] categories = ["mathematics", "no-std"] description = "Trait that represents a set according to Zermelo–Fraenkel set theory with the axiom of choice (ZFC)" -documentation = "https://docs.rs/zfc/latest/zfc/index.html" +documentation = "https://docs.rs/zfc/latest/zfc/" edition = "2021" keywords = ["math", "set", "zfc"] license = "MIT OR Apache-2.0" name = "zfc" readme = "README.md" repository = "https://git.philomathiclife.com/repos/zfc/" -version = "0.1.0" +version = "0.1.1" [lib] name = "zfc" diff --git a/src/lib.rs b/src/lib.rs @@ -23,12 +23,14 @@ clippy::blanket_clippy_restriction_lints, clippy::implicit_return, clippy::missing_trait_methods, + clippy::ref_patterns, clippy::single_char_lifetime_names )] extern crate alloc; use alloc::collections::BTreeSet; use core::borrow::Borrow; use core::cmp::Ordering; +use core::fmt::{self, Display, Formatter}; use core::ops::{Range, RangeInclusive, Sub}; use num_bigint::BigUint; /// Represents the quantity of elements in a [`Set`]. @@ -40,9 +42,50 @@ pub enum Cardinality { /// The contained `BigUint` represents the [aleph number](https://en.wikipedia.org/wiki/Aleph_number) of a transfinite `Set`. Transfinite(BigUint), } +impl Cardinality { + /// Returns a reference to the contained `BigUint`. + #[inline] + #[must_use] + pub const fn as_biguint(&self) -> &BigUint { + match *self { + Self::Finite(ref val) | Self::Transfinite(ref val) => val, + } + } +} +impl Display for Cardinality { + #[allow(clippy::min_ident_chars)] + #[inline] + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match *self { + Self::Finite(ref val) => val.fmt(f), + Self::Transfinite(ref val) => write!(f, "\u{2135}_{val}"), + } + } +} +impl fmt::Debug for Cardinality { + #[allow(clippy::min_ident_chars)] + #[inline] + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + <Self as Display>::fmt(self, f) + } +} +impl From<BigUint> for Cardinality { + #[inline] + fn from(value: BigUint) -> Self { + Self::Finite(value) + } +} +impl From<Cardinality> for BigUint { + #[inline] + fn from(value: Cardinality) -> Self { + match value { + Cardinality::Finite(val) | Cardinality::Transfinite(val) => val, + } + } +} /// Represents a set according to [Zermelo–Fraenkel set theory with the axiom of choice (ZFC)](https://en.wikipedia.org/wiki/Zermelo%E2%80%93Fraenkel_set_theory). /// Note that elements in a `Set` must not be distinguishable by order or frequency, so care must be taken in its implementation if the implementing type -/// exposes an API that distinguishes between the order or frequency of `Elem` (e.g., [`Vec`](https://doc.rust-lang.org/alloc/vec/struct.Vec.html) where `Elem` = `T`). +/// exposes an API that distinguishes between the order or frequency of `Elem` (e.g., [`Vec<T>`](https://doc.rust-lang.org/alloc/vec/struct.Vec.html) where `Elem` = `T`). pub trait Set { /// The elements that make up the set. /// Per ZFC, this must not be the same type as `Self` nor a recursive type based on `Self`.