HashSet
only implements the normal set. HashSet
can not represent its complementary set when the complementary set is an infinite set.
FlagSet
implemented as a tuple of a HashSet
and a bool
value. When the bool
value is true, FlagSet
represents the HashSet
. When the bool
value is true, FlagSet
represents the the complementary set of HashSet
.
As with the HashSet
type, a FlagSet
requires that the elements implement the Eq
and Hash
traits. In addition to operations of FlagSet
, the elements also implement the Clone
trait.
FlagSet
also defines five kinds of operations of sets based on Binary Operations
A ∪B -> A + B
A ∩ B -> A & B
A - B -> A - B
A Xor B -> A ^ B
CuA -> !A
```rust use std::collections::HashSet; use flag_set::FlagSet;
let a: HashSet<_> = vec![1, 2, 3].intoiter().collect(); let b: HashSet<_> = vec![2, 3, 4].intoiter().collect();
let flagap = FlagSet(a.clone(), true); let flagan = FlagSet(a.clone(), false); let flagbp = FlagSet(b.clone(), true); let flagbn = FlagSet(b.clone(), false);
// 用new方法创建实例 // use new method create an instance asserteq!(flagbn, FlagSet::new(vec![2, 3, 4], false));
// 测试并 // test union asserteq!(FlagSet::new(vec![1, 2, 3, 4], true), flagap.clone() + flagbp.clone()); asserteq!(FlagSet::new(vec![4], false), flagap.clone() + flagbn.clone()); asserteq!(FlagSet::new(vec![1], false), flagan.clone() + flagbp.clone()); asserteq!(FlagSet::new(vec![2, 3], false), flagan.clone() + flagbn.clone());
// 测试交 // test intersection asserteq!(FlagSet::new(vec![2, 3], true), flagap.clone() & flagbp.clone()); asserteq!(FlagSet::new(vec![1], true), flagap.clone() & flagbn.clone()); asserteq!(FlagSet::new(vec![4], true), flagan.clone() & flagbp.clone()); asserteq!(FlagSet::new(vec![1, 2, 3, 4], false), flagan.clone() & flagbn.clone());
// 测试减 // test substraction asserteq!(FlagSet::new(vec![1], true), flagap.clone() - flagbp.clone()); asserteq!(FlagSet::new(vec![2, 3], true), flagap.clone() - flagbn.clone()); asserteq!(FlagSet::new(vec![1, 2, 3, 4], false), flagan.clone() - flagbp.clone()); asserteq!(FlagSet::new(vec![4], true), flagan.clone() - flagbn.clone());
// 测试否 // test not asserteq!(FlagSet(a.clone(), true), !flagan.clone());
// 测试对称差 // test symmetric difference asserteq!(FlagSet::new(vec![1, 4], true), flagap.clone() ^ flag_bp.clone()); ```