Sha256: b00812752d87fe58a5b18eee927fa67122392339b6547bbacf4b0c4a24a5b21c
Contents?: true
Size: 1.86 KB
Versions: 345
Compression:
Stored size: 1.86 KB
Contents
#[derive(Debug)] pub struct CustomSet<T> { collection: Vec<T>, } impl<T: Ord + Clone> PartialEq for CustomSet<T> { fn eq(&self, other: &Self) -> bool { self.collection.iter().all(|x| other.contains(&x)) && other.collection.iter().all(|x| self.contains(&x)) } } impl<T: Ord + Clone> CustomSet<T> { pub fn new(inputs: Vec<T>) -> CustomSet<T> { let mut s = CustomSet { collection: Vec::new() }; for input in inputs { s.add(input); } s } pub fn add(&mut self, element: T) { if !self.contains(&element) { self.collection.push(element) } } pub fn contains(&self, other: &T) -> bool { self.collection.contains(other) } pub fn is_empty(&self) -> bool { self.collection.is_empty() } pub fn is_subset(&self, other: &Self) -> bool { self.collection.iter().all(|x| other.contains(x)) } pub fn is_disjoint(&self, other: &Self) -> bool { !self.collection.iter().any(|x| other.contains(x)) } pub fn intersection(&self, other: &Self) -> CustomSet<T> { CustomSet::new(self.collection .iter() .cloned() .filter(|c| other.contains(c)) .collect()) } pub fn union(&self, other: &Self) -> CustomSet<T> { CustomSet::new(self.collection .iter() .cloned() .chain(other.collection.iter().cloned()) .collect()) } pub fn difference(&self, other: &Self) -> CustomSet<T> { CustomSet::new(self.collection .iter() .cloned() .filter(|c| !other.contains(c)) .collect()) } }
Version data entries
345 entries across 345 versions & 1 rubygems