use std::collections::hash_map::RandomState;
use std::collections::hash_set::{self, HashSet};
use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct Set {
pub data: HashSet<Vec<u8>>
}
impl Set {
pub fn new() -> Set {
Set { data: HashSet::new() }
}
pub fn with_capacity(size: usize) -> Set {
Set { data: HashSet::with_capacity(size) }
}
pub fn fill(data: HashSet<Vec<u8>>) -> Set {
Set { data: data }
}
pub fn insert(&mut self, element: Vec<u8>) -> bool {
self.data.insert(element)
}
pub fn remove(&mut self, element: &[u8]) -> bool {
self.data.remove(element)
}
pub fn contains(&self, element: &[u8]) -> bool {
self.data.contains(element)
}
pub fn len(&self) -> usize {
self.data.len()
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
pub fn union<'a>(&'a self, other: &'a Set) -> Union<'a> {
Union {
iter: self.data.union(&other.data),
}
}
pub fn intersection<'a>(&'a self, other: &'a Set) -> Intersection<'a> {
Intersection {
iter: self.data.intersection(&other.data),
}
}
pub fn difference<'a>(&'a self, other: &'a Set) -> Difference<'a> {
Difference {
iter: self.data.difference(&other.data),
}
}
pub fn symmetric_difference<'a>(&'a self, other: &'a Set) -> SymmetricDifference<'a> {
SymmetricDifference {
iter: self.data.symmetric_difference(&other.data),
}
}
pub fn is_subset(&self, other: &Set) -> bool {
self.data.is_subset(&other.data)
}
pub fn is_superset(&self, other: &Set) -> bool {
self.data.is_superset(&other.data)
}
}
#[derive(Clone)]
pub struct Union<'a> {
iter: hash_set::Union<'a, Vec<u8>, RandomState>,
}
impl<'a> fmt::Debug for Union<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.iter.fmt(f)
}
}
impl<'a> Iterator for Union<'a> {
type Item = &'a Vec<u8>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
#[derive(Clone)]
pub struct Intersection<'a> {
iter: hash_set::Intersection<'a, Vec<u8>, RandomState>,
}
impl<'a> fmt::Debug for Intersection<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.iter.fmt(f)
}
}
impl<'a> Iterator for Intersection<'a> {
type Item = &'a Vec<u8>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
#[derive(Clone)]
pub struct Difference<'a> {
iter: hash_set::Difference<'a, Vec<u8>, RandomState>,
}
impl<'a> fmt::Debug for Difference<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.iter.fmt(f)
}
}
impl<'a> Iterator for Difference<'a> {
type Item = &'a Vec<u8>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
#[derive(Clone)]
pub struct SymmetricDifference<'a> {
iter: hash_set::SymmetricDifference<'a, Vec<u8>, RandomState>,
}
impl<'a> fmt::Debug for SymmetricDifference<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.iter.fmt(f)
}
}
impl<'a> Iterator for SymmetricDifference<'a> {
type Item = &'a Vec<u8>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}