diff --git a/src/extensions/collectible.rs b/src/extensions/collectible.rs index 0dc3d8e..a2050d4 100644 --- a/src/extensions/collectible.rs +++ b/src/extensions/collectible.rs @@ -50,11 +50,11 @@ pub trait Collectible: IntoIterator { /// assert_eq!(a.add_multi(vec![3, 4]), vec![1, 2, 3, 3, 4]); /// ``` #[inline] - fn add_multi(self, iterable: impl IntoIterator) -> Self + fn add_multi(self, elements: impl IntoIterator) -> Self where Self: IntoIterator + Sized + FromIterator, { - self.into_iter().chain(iterable).collect() + self.into_iter().chain(elements).collect() } /// Creates a new collection containing combinations of specified size from the elements diff --git a/src/extensions/collections/binary_heap.rs b/src/extensions/collections/binary_heap.rs index fb4557d..8812061 100644 --- a/src/extensions/collections/binary_heap.rs +++ b/src/extensions/collections/binary_heap.rs @@ -133,6 +133,26 @@ impl Traversable for BinaryHeap { impl Collectible for BinaryHeap { type This = BinaryHeap; + #[inline] + fn add(mut self, value: Item) -> Self + where + Self: IntoIterator + FromIterator + { + self.push(value); + self + } + + #[inline] + fn add_multi(mut self, elements: impl IntoIterator) -> Self + where + Self: IntoIterator + Sized + FromIterator, + { + elements.into_iter().for_each(|x| { + self.push(x); + }); + self + } + #[inline] fn combinations(&self, k: usize) -> Vec where diff --git a/src/extensions/collections/btree_map.rs b/src/extensions/collections/btree_map.rs index e270aeb..60e439b 100644 --- a/src/extensions/collections/btree_map.rs +++ b/src/extensions/collections/btree_map.rs @@ -6,6 +6,26 @@ use std::hash::Hash; impl Map for BTreeMap { type This = BTreeMap; + #[inline] + fn add(mut self, key: Key, value: Value) -> Self + where + Self: IntoIterator + FromIterator<(Key, Value)>, + { + let _unused = self.insert(key, value); + self + } + + #[inline] + fn add_multi(mut self, entries: impl IntoIterator) -> Self + where + Self: IntoIterator + FromIterator<(Key, Value)>, + { + for (k, v) in entries { + let _unused = self.insert(k, v); + } + self + } + #[inline] fn all(&self, predicate: impl FnMut((&Key, &Value)) -> bool) -> bool { self.iter().all(predicate) diff --git a/src/extensions/collections/btree_set.rs b/src/extensions/collections/btree_set.rs index 7eb282c..ea043b2 100644 --- a/src/extensions/collections/btree_set.rs +++ b/src/extensions/collections/btree_set.rs @@ -133,6 +133,26 @@ impl Traversable for BTreeSet { impl Collectible for BTreeSet { type This = BTreeSet; + #[inline] + fn add(mut self, value: Item) -> Self + where + Self: IntoIterator + FromIterator + { + let _ = self.insert(value); + self + } + + #[inline] + fn add_multi(mut self, elements: impl IntoIterator) -> Self + where + Self: IntoIterator + Sized + FromIterator, + { + for x in elements { + let _unused = self.insert(x); + }; + self + } + #[inline] fn combinations(&self, k: usize) -> Vec where diff --git a/src/extensions/collections/hash_map.rs b/src/extensions/collections/hash_map.rs index 55f2c72..be680e6 100644 --- a/src/extensions/collections/hash_map.rs +++ b/src/extensions/collections/hash_map.rs @@ -3,9 +3,29 @@ use std::cmp::Ordering; use std::collections::HashMap; use std::hash::Hash; -impl Map for HashMap { +impl Map for HashMap { type This = HashMap; + #[inline] + fn add(mut self, key: Key, value: Value) -> Self + where + Self: IntoIterator + FromIterator<(Key, Value)>, + { + let _unused = self.insert(key, value); + self + } + + #[inline] + fn add_multi(mut self, entries: impl IntoIterator) -> Self + where + Self: IntoIterator + FromIterator<(Key, Value)>, + { + for (k, v) in entries { + let _unused = self.insert(k, v); + } + self + } + #[inline] fn all(&self, predicate: impl FnMut((&Key, &Value)) -> bool) -> bool { self.iter().all(predicate) diff --git a/src/extensions/collections/hash_set.rs b/src/extensions/collections/hash_set.rs index d7dd495..89632df 100644 --- a/src/extensions/collections/hash_set.rs +++ b/src/extensions/collections/hash_set.rs @@ -134,6 +134,26 @@ impl Traversable for HashSet { impl Collectible for HashSet { type This = HashSet; + #[inline] + fn add(mut self, value: Item) -> Self + where + Self: IntoIterator + FromIterator + { + let _ = self.insert(value); + self + } + + #[inline] + fn add_multi(mut self, elements: impl IntoIterator) -> Self + where + Self: IntoIterator + Sized + FromIterator, + { + for x in elements { + let _unused = self.insert(x); + }; + self + } + #[inline] fn combinations(&self, k: usize) -> Vec where diff --git a/src/extensions/collections/linked_list.rs b/src/extensions/collections/linked_list.rs index 04b9881..2ca000b 100644 --- a/src/extensions/collections/linked_list.rs +++ b/src/extensions/collections/linked_list.rs @@ -135,6 +135,26 @@ impl Traversable for LinkedList { impl Collectible for LinkedList { type This = LinkedList; + #[inline] + fn add(mut self, value: Item) -> Self + where + Self: IntoIterator + FromIterator + { + self.push_back(value); + self + } + + #[inline] + fn add_multi(mut self, elements: impl IntoIterator) -> Self + where + Self: IntoIterator + Sized + FromIterator, + { + elements.into_iter().for_each(|x| { + self.push_back(x); + }); + self + } + #[inline] fn combinations(&self, k: usize) -> Vec where diff --git a/src/extensions/collections/vec.rs b/src/extensions/collections/vec.rs index d55f99a..c59ee60 100644 --- a/src/extensions/collections/vec.rs +++ b/src/extensions/collections/vec.rs @@ -135,6 +135,26 @@ impl Traversable for Vec { impl Collectible for Vec { type This = Vec; + #[inline] + fn add(mut self, value: Item) -> Self + where + Self: IntoIterator + FromIterator + { + self.push(value); + self + } + + #[inline] + fn add_multi(mut self, elements: impl IntoIterator) -> Self + where + Self: IntoIterator + Sized + FromIterator, + { + elements.into_iter().for_each(|x| { + self.push(x); + }); + self + } + #[inline] fn combinations(&self, k: usize) -> Vec where diff --git a/src/extensions/collections/vec_deque.rs b/src/extensions/collections/vec_deque.rs index b3e327c..2e4be6b 100644 --- a/src/extensions/collections/vec_deque.rs +++ b/src/extensions/collections/vec_deque.rs @@ -135,6 +135,26 @@ impl Traversable for VecDeque { impl Collectible for VecDeque { type This = VecDeque; + #[inline] + fn add(mut self, value: Item) -> Self + where + Self: IntoIterator + FromIterator + { + self.push_back(value); + self + } + + #[inline] + fn add_multi(mut self, elements: impl IntoIterator) -> Self + where + Self: IntoIterator + Sized + FromIterator, + { + elements.into_iter().for_each(|x| { + self.push_back(x); + }); + self + } + #[inline] fn combinations(&self, k: usize) -> Vec where diff --git a/src/extensions/map.rs b/src/extensions/map.rs index 78d7b1a..9b09e19 100644 --- a/src/extensions/map.rs +++ b/src/extensions/map.rs @@ -92,11 +92,11 @@ pub trait Map { /// ])); /// ``` #[inline] - fn add_multi(self, iterable: impl IntoIterator) -> Self + fn add_multi(self, entries: impl IntoIterator) -> Self where Self: IntoIterator + FromIterator<(Key, Value)>, { - self.into_iter().chain(iterable).collect() + self.into_iter().chain(entries).collect() } /// Tests if every entry of the map matches a predicate. @@ -274,12 +274,12 @@ pub trait Map { /// assert_eq!(e.delete_multi(&vec![1]), HashMap::new()); /// ``` #[inline] - fn delete_multi<'a>(self, iterable: &'a impl Iterable = &'a Key>) -> Self + fn delete_multi<'a>(self, keys: &'a impl Iterable = &'a Key>) -> Self where Key: Eq + Hash + 'a, Self: IntoIterator + FromIterator<(Key, Value)>, { - let removed: HashSet<&Key> = HashSet::from_iter(iterable.iterator()); + let removed: HashSet<&Key> = HashSet::from_iter(keys.iterator()); self.into_iter().filter(|(k, _)| !removed.contains(k)).collect() } @@ -304,7 +304,7 @@ pub trait Map { /// /// assert!(!a.disjoint(&vec![3, 4])); /// ``` - fn disjoint<'a>(&'a self, elements: &'a impl Iterable = &'a Key>) -> bool + fn disjoint<'a>(&'a self, keys: &'a impl Iterable = &'a Key>) -> bool where Key: Eq + Hash + 'a; @@ -961,13 +961,13 @@ pub trait Map { /// } /// ``` #[inline] - fn intersect<'a>(self, iterable: &'a impl Iterable = &'a (Key, Value)>) -> Self + fn intersect<'a>(self, entries: &'a impl Iterable = &'a (Key, Value)>) -> Self where Key: Eq + Hash + 'a, Value: Eq + Hash + 'a, Self: IntoIterator + FromIterator<(Key, Value)>, { - let retained: HashSet<(&Key, &Value)> = HashSet::from_iter(iterable.iterator().map(|(k, v)| (k, v))); + let retained: HashSet<(&Key, &Value)> = HashSet::from_iter(entries.iterator().map(|(k, v)| (k, v))); self.into_iter().filter(|(k, v)| retained.contains(&(k, v))).collect() } @@ -1761,7 +1761,7 @@ pub trait Map { /// assert!(!a.subset(&vec![1, 2])); /// assert!(!a.subset(&vec![])); /// ``` - fn subset<'a>(&'a self, elements: &'a impl Iterable = &'a Key>) -> bool + fn subset<'a>(&'a self, keys: &'a impl Iterable = &'a Key>) -> bool where Key: Eq + Hash + 'a; @@ -1822,13 +1822,13 @@ pub trait Map { /// ``` #[inline] fn substitute_multi<'a>( - self, elements: &'a impl Iterable = &'a Key>, replacement: impl IntoIterator, + self, keys: &'a impl Iterable = &'a Key>, replacement: impl IntoIterator, ) -> Self where Key: Eq + Hash + 'a, Self: IntoIterator + FromIterator<(Key, Value)>, { - let iterator = elements.iterator(); + let iterator = keys.iterator(); let removed: HashSet<&Key> = HashSet::from_iter(iterator); self.into_iter().filter(|x| !removed.contains(&x.0)).chain(replacement).collect() } @@ -1858,7 +1858,7 @@ pub trait Map { /// assert!(!a.superset(&vec![3, 4])); /// assert!(!e.superset(&vec![1])); /// ``` - fn superset<'a>(&'a self, elements: &'a impl Iterable = &'a Key>) -> bool + fn superset<'a>(&'a self, keys: &'a impl Iterable = &'a Key>) -> bool where Key: Eq + Hash + 'a;