pub struct VecSet<'a, T> { /* private fields */ }
Expand description
Brings mathematical sets into Rust.
Implementations§
source§impl<'a, T: Copy + Ord> VecSet<'a, T>
impl<'a, T: Copy + Ord> VecSet<'a, T>
sourcepub fn new_subset<F: Fn(T) -> bool>(
parent: &'a VecSet<'_, T>,
f: F
) -> VecSet<'a, T>
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
- TheVecSet
from which the newVecSet
emerges.f
- The closure after which the newVecSet
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])
sourcepub fn union(&self, other: &VecSet<'_, T>) -> VecSet<'_, T>
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));
sourcepub fn intersection(&self, other: &VecSet<'_, T>) -> VecSet<'_, T>
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));
sourcepub fn is_disjoint_with(&self, other: &VecSet<'_, T>) -> bool
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));
sourcepub fn difference_with(&self, other: &VecSet<'_, T>) -> VecSet<'_, T>
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));
sourcepub fn cartesian_product(&self, other: &VecSet<'_, T>) -> VecSet<'_, (T, T)>
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));
sourcepub fn symmetric_difference_with(&self, other: &VecSet<'_, T>) -> VecSet<'_, T>
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));
sourcepub fn has_element(&self, elem: T) -> bool
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));
sourcepub fn insert(&mut self, elem: T)
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]);
sourcepub fn has_emerged(&self) -> bool
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());
sourcepub fn get_parent(&self) -> Option<&VecSet<'_, T>>
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());
sourcepub fn is_subset_of(&self, other: &VecSet<'_, T>) -> bool
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
- AVecSet<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));
sourcepub fn cardinality(&self) -> usize
pub fn cardinality(&self) -> usize
sourcepub fn set_elements(&mut self, vals: &[T])
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§impl<T: ToString + Copy + Ord> VecSet<'_, T>
impl<T: ToString + Copy + Ord> VecSet<'_, T>
sourcepub fn full_println(&self)
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
sourcepub fn to_full_string(&self) -> String
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());