pub struct VecSet<'a, T> { /* private fields */ }
Expand description

Brings mathematical sets into Rust.

Implementations§

source§

impl<'a, T: Copy + Ord> VecSet<'a, T>

source

pub fn new(values: &[T]) -> VecSet<'a, T>

Creates a new VecSet.

Arguments
  • values - The values for the VecSet.
Returns

A new VecSet.

Examples
use lib_rapid::math::sets::vec_sets::VecSet;
 
let set = VecSet::new(&vec![0, 1, 2, 3, 4, 5]);
assert_eq!(set.elements(), &vec![0, 1, 2, 3, 4, 5]);
source

pub fn empty_set() -> VecSet<'a, T>

Get the empty set, ∅.

Returns

A VecSet<T> with no elements.

Examples
use lib_rapid::math::sets::vec_sets::{VecSet, set};
let v: Vec<u8> = Vec::new();
 
assert_eq!(v, VecSet::empty_set().elements());
source

pub fn new_subset<F: Fn(T) -> bool>( parent: &'a VecSet<'_, T>, f: F ) -> VecSet<'a, T>

Creates a new VecSet using a parent-VecSet to which it applies a closure (A rule which is applied to each element and checks if this rule holds true).

Arguments
  • parent - The VecSet from which the new VecSet emerges.
  • f - The closure after which the new VecSet is created.
Returns

A child VecSet.

Examples
use lib_rapid::math::sets::vec_sets::VecSet;
let test1:       VecSet<u8> = VecSet::new(&vec![0,1,2,3,4]);
let from_parent: VecSet<u8> = VecSet::<u8>::new_subset(&test1, |x| x % 2 == 0);
assert_eq!(from_parent, VecSet::new(&vec![0,2,4]));
assert_eq!(test1.elements(), &vec![0,1,2,3,4])
source

pub fn union(&self, other: &VecSet<'_, T>) -> VecSet<'_, T>

Does a mathematical union on two VecSets.
In other words, it creates a new set of all values that are either in self or other or both. self ∪ other.

Arguments
  • self - The first set.
  • other - The second set.
Returns

A new VecSet<T>: self ∪ other.

Examples
use lib_rapid::math::sets::vec_sets::VecSet;
use lib_rapid::math::sets::vec_sets::set;
use lib_rapid::compsci::general::BinaryInsert;
let s:  VecSet<i32> = VecSet::new(&vec![0,1,2,3,4,5,6,7,8,9,10]);
let s1: VecSet<i32> = VecSet::new(&vec![11,12,13,13,11,0,0,0]);
 
let c:  VecSet<i32> = s.union(&s1);
assert_eq!(c, set!(0,1,2,3,4,5,6,7,8,9,10,11,12,13));
source

pub fn intersection(&self, other: &VecSet<'_, T>) -> VecSet<'_, T>

Does a mathematical intersection on two sets.
In other words: It creates a new set of all values that are present in both sets.

Arguments
  • self - The first set.
  • other - The second set.
Returns

A new VecSet<T>: self ∩ other.

Examples
use lib_rapid::math::sets::vec_sets::VecSet;
use lib_rapid::math::sets::vec_sets::set;
use lib_rapid::compsci::general::BinaryInsert; // Used for "set!"
 
let s:  VecSet<i32> = VecSet::new(&vec![0,1,2,3,4,5,6,7,8,9,10,11]);
let s2: VecSet<i32> = VecSet::new(&vec![0,1,2,3,11,0,0,0]);
 
let c:  VecSet<i32> = s.intersection(&s2);
assert_eq!(c, set!(0, 1, 2, 3, 11));
source

pub fn is_disjoint_with(&self, other: &VecSet<'_, T>) -> bool

Checks for disjointness between self and other.
Two sets are disjoint if and only if self has no elements of other. The two sets A := {1, 2, 3} and B := {1, 5, 6} are not disjoint, because they share the common element 1.

Arguments
  • other - The other set to be checked for disjointness.
Returns

A bool

Examples
 use lib_rapid::math::sets::vec_sets::VecSet;
 use lib_rapid::math::sets::vec_sets::set;
 
 let s1 = set!(0, 1, 2, 3, 4, 5, 6, 7, 8);
 let s2 = set!(0, 1, 2, 3, 4, 5, 6);
 let s3 = set!(8, 9, 10, 11, 12, 13, 14);
 let s4 = set!(9, 10, 11, 12, 13, 14, 15);
 
 assert_eq!(false, s1.is_disjoint_with(&s2));
 assert_eq!(false, s1.is_disjoint_with(&s3));
 assert_eq!(true, s1.is_disjoint_with(&s4));
source

pub fn difference_with(&self, other: &VecSet<'_, T>) -> VecSet<'_, T>

Checks for mathematical difference between two sets - A \ B.
The difference between two sets is the set of all elements that are present in a set A, but not in the set B. For example, the difference between A := {1, 5, 6} and B := {2, 3, 4} is {5, 6}, because 5 and 6 are the elements not present in B.

Arguments
  • other - The other set to be checked for.
Returns

A VecSet<T>.

Examples
 use lib_rapid::math::sets::vec_sets::VecSet;
 use lib_rapid::math::sets::vec_sets::set;
 
 let s1 = set!(1, 2, 3, 4, 5);
 let s2 = set!(3, 4, 5);
 
 assert_eq!(set!(1, 2), s1.difference_with(&s2));
source

pub fn cartesian_product(&self, other: &VecSet<'_, T>) -> VecSet<'_, (T, T)>

Gets the cartesian product of two sets in O(n·m).
The cartesian product of two sets is the set of all tuples of the form (a, b), such that every unique permutation is captured. Let A := {1, 2} and B := {3, 4}. Then, the cartesian product C is C := {(1, 3), (1, 4), (2, 3), (2, 4)}.

Arguments
  • other - &VecSet<T>
Returns

A VecSet<(T, T)>.

 use lib_rapid::math::sets::vec_sets::VecSet;
 use lib_rapid::math::sets::vec_sets::set;
 
 let s1 = set!(1, 2, 3);
 let s2 = set!(1, 2);
 
 assert_eq!(set!((1, 1), (1, 2), (2, 1), (2, 2), (3, 1), (3, 2)), s1.cartesian_product(&s2));
source

pub fn symmetric_difference_with(&self, other: &VecSet<'_, T>) -> VecSet<'_, T>

Gets the symmetric difference.
The symmetric difference is the set of all elements that are either in self or other, but not in both.

Arguments
  • other - A &VecSet<T>.
Examples
 use lib_rapid::math::sets::vec_sets::VecSet;
 use lib_rapid::math::sets::vec_sets::set;
 
 let s1 = set!(1, 2, 3, 4, 5);
 let s2 = set!(3, 4);
 
 assert_eq!(set!(1, 2, 5), s1.symmetric_difference_with(&s2));
source

pub fn has_element(&self, elem: T) -> bool

Lets you check for an element in a set.

Arguments
  • elem - The element to check for.
Returns

A boolean value which determines if elem ∈ self.

Examples
use lib_rapid::math::sets::vec_sets::VecSet;
use lib_rapid::math::sets::vec_sets::set;
 
let set = set!(0, 1, 2, 3, 4, 5, 6);
 
assert_eq!(false, set.has_element(7));
assert_eq!(false, set.has_element(-1));
assert_eq!(true, set.has_element(1));
source

pub fn insert(&mut self, elem: T)

Lets you insert an element into a set. Does not insert already present values.
Mathematically speaking, it takes the set A and a set B, where the set B is defined as B := {b}, where b is the new value. A is then redefined as A = A_{pre} ∪ B.

Arguments
  • elem - The element to insert.
Returns

Nothing.

Examples
use lib_rapid::math::sets::vec_sets::VecSet;
use lib_rapid::math::sets::vec_sets::set;
let mut s: VecSet<i32> = VecSet::new(&vec![0,1,2,3,4,5,6,7,8,9,10]);
 
s.insert(5);
assert_eq!(s.elements(), &vec![0,1,2,3,4,5,6,7,8,9,10]);
source

pub fn has_emerged(&self) -> bool

Lets you check wether a set has a parent (emerged from another set) or not.

Returns

A boolean value which determines if the set is a subset of any other set.

Examples
use lib_rapid::math::sets::vec_sets::VecSet;
use lib_rapid::math::sets::vec_sets::set;
 
let set = set!(0, 1, 2, 3, 4, 5, 6);
let subset = VecSet::new_subset(&set, |x| x % 2 == 0);
 
assert_eq!(true, subset.has_emerged());
assert_eq!(false, set.has_emerged());
source

pub fn get_parent(&self) -> Option<&VecSet<'_, T>>

Gets you the optional superset from which the Set emerged.

Returns

A Option<&VecSet<T>>.

Examples
use lib_rapid::math::sets::vec_sets::VecSet;
use lib_rapid::math::sets::vec_sets::set;
 
let set = set!(0, 1, 2, 3, 4, 5, 6);
let subset = VecSet::new_subset(&set, |x| x % 2 == 0);
 
assert_eq!(&set, subset.get_parent().unwrap());
source

pub fn is_subset_of(&self, other: &VecSet<'_, T>) -> bool

Determines whether self is a subset of other, unconditional from whether self emerged from other.

Arguments
  • other - A VecSet<T>.
Returns

A bool

Examples
use lib_rapid::math::sets::vec_sets::VecSet;
use lib_rapid::math::sets::vec_sets::set;
 
let set  = set!(0, 1, 2, 3, 4, 5, 6);
let set2 = set!(0, 1, 2, 3, 4);
 
assert_eq!(false, set.is_subset_of(&set2));
assert_eq!(true, set2.is_subset_of(&set));
source

pub fn cardinality(&self) -> usize

Gets the cardinality of a set.
The cardinality of a set is the count of all elements.

Returns

A usize: |self|.

Examples
use lib_rapid::math::sets::vec_sets::VecSet;
use lib_rapid::math::sets::vec_sets::set;
 
let set = set!(0, 1, 2, 3, 4, 5, 6);
 
assert_eq!(7, set.cardinality());
source

pub fn set_elements(&mut self, vals: &[T])

Lets you set the elements of a set.

Arguments
  • vals - The Vec to change the values to.
Returns

Nothing.

Examples
use lib_rapid::math::sets::vec_sets::VecSet;
use lib_rapid::math::sets::vec_sets::set;
 
let mut set = set!(0, 1, 2, 3, 4, 5, 6);
set.set_elements(&vec![0, 2, 4, 6]);
 
assert_eq!(&vec![0, 2, 4, 6], set.elements());
source

pub fn elements(&self) -> &[T]

Lets you get the elements of a set.

Arguments
  • none
Returns

A &[T] containing all elements.

Examples
use lib_rapid::math::sets::vec_sets::VecSet;
use lib_rapid::math::sets::vec_sets::set;
 
let mut set = set!(0, 1, 2, 3, 4, 5, 6);
 
assert_eq!(&vec![0, 1, 2, 3, 4, 5, 6], set.elements());
source§

impl<T: ToString + Copy + Ord> VecSet<'_, T>

source

pub fn full_println(&self)

Lets you print a set with all its parents recursively.

Returns

Nothing.

Examples
use lib_rapid::math::sets::vec_sets::VecSet;
let s:  VecSet<i32> = VecSet::new(&vec![0,1,2,3,4,5,6,7,8,9,10]);
let s1: VecSet<i32> = VecSet::new_subset(&s, |x| x % 2 == 0);
let s2: VecSet<i32> = VecSet::new_subset(&s1, |x| x == 4);
 
s2.full_println(); // Prints this set and the superset, see to_full_string.
println!("{}", s2); // Only prints this set
source

pub fn to_full_string(&self) -> String

Converts a set with all subsets to a string.

Returns

A String containing the result.

Examples
use lib_rapid::math::sets::vec_sets::VecSet;
let s:  VecSet<i32> = VecSet::new(&vec![0,1,2,3,4,5,6,7,8,9,10]);
let s1: VecSet<i32> = VecSet::new_subset(&s, |x| x % 2 == 0);
let s2: VecSet<i32> = VecSet::new_subset(&s1, |x| x == 4);
assert_eq!(s2.to_full_string(), "{ 4 } ⊆ { 0; 2; 4; 6; 8; 10 } ⊆ { 0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 }".to_string());

Trait Implementations§

source§

impl<'a, T: Clone> Clone for VecSet<'a, T>

source§

fn clone(&self) -> VecSet<'a, T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a, T: Debug> Debug for VecSet<'a, T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: ToString + Copy + Ord> Display for VecSet<'_, T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> Index<usize> for VecSet<'_, T>

§

type Output = T

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<T: Copy + Clone + Ord> IntoIterator for &VecSet<'_, T>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = IntoIter<<&VecSet<'_, T> as IntoIterator>::Item>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T> IntoIterator for VecSet<'_, T>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = IntoIter<<VecSet<'_, T> as IntoIterator>::Item>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T: PartialEq> PartialEq for VecSet<'_, T>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<'a, T> RefUnwindSafe for VecSet<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for VecSet<'a, T>
where T: Send + Sync,

§

impl<'a, T> Sync for VecSet<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for VecSet<'a, T>
where T: Unpin,

§

impl<'a, T> UnwindSafe for VecSet<'a, T>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.