Rust crate key_set
KeySet representing concepts of All, None, Some(list), and AllExceptSome(list), with basic set calculations (intersection, difference, inverse).
Other versions:
- TypeScript: https://github.com/eturino/ts-key-set
- Ruby: https://github.com/eturino/ruby_key_set
Usage
We have an enum with:
KeySet::Allrepresents the entirety of possible keys (𝕌)KeySet::Nonerepresents an empty set (∅)KeySet::Some(vec)represents a concrete set (A ⊂ 𝕌)KeySet::AllExceptSome(vec)represents the complementary set of a set, all the elements except the given ones (A' = {x ∈ 𝕌 | x ∉ A}) _(see Complement in Wikipedia)*
We can have a KeySet of T where T: Ord + Debug + Clone
KeySet implements cmp::Ord, cmp::PartialOrd, cmp::Eq, cmp::PartialEq, std::fmt::Debug, and std::fmt::Display
Creation: KeySet::for_some(&list), KeySet::for_all_except_some(&list)
Build your KeySets using the factory functions, giving
- To get a KeySet that represents the given list:
KeySet::for_some(&list)- if the list is empty, we'll get
None - otherwise, we'll get
Some
- if the list is empty, we'll get
- To get a KeySet that represents the complementary set of the given list:
KeySet::for_all_except_some(&list)- if the list is empty, we'll get
All - otherwise, we'll get
AllExceptSome
- if the list is empty, we'll get
contains(&element)
Returns a boolean defining if the KeySet includes the given element.
invert()
All KeySet has an invert() method that returns an instance of the opposite class, which represents the complementary KeySet. _(see Complement in Wikipedia)*
All⟷NoneSome⟷AllExceptSome
remove(&other)
Returns a new KeySet with the difference between ThisSet - OtherSet (A - B)
intersect(&other)
Returns a new KeySet with the intersection of both Sets (A ∩ B), representing the elements present in both sets
clone()
All KeySet has a clone() method, which will return a new instance of the same class that represents the same KeySet.
If the KeySet is KeySetSome or KeySetAllExceptSome, they will have a vector with the same keys.