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

Version Path
trackler-2.2.1.128 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.127 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.126 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.125 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.124 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.123 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.122 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.121 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.120 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.119 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.118 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.117 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.116 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.115 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.114 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.113 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.111 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.110 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.109 tracks/rust/exercises/custom-set/example.rs
trackler-2.2.1.108 tracks/rust/exercises/custom-set/example.rs