Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Ockajak committed Jul 18, 2024
1 parent 9d23ca3 commit e3a60a9
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/extensions/collectible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ pub trait Collectible<Item>: IntoIterator<Item = Item> {
/// assert_eq!(a.add_multi(vec![3, 4]), vec![1, 2, 3, 3, 4]);
/// ```
#[inline]
fn add_multi(self, iterable: impl IntoIterator<Item = Item>) -> Self
fn add_multi(self, elements: impl IntoIterator<Item = Item>) -> Self
where
Self: IntoIterator<Item = Item> + Sized + FromIterator<Item>,
{
self.into_iter().chain(iterable).collect()
self.into_iter().chain(elements).collect()
}

/// Creates a new collection containing combinations of specified size from the elements
Expand Down
20 changes: 20 additions & 0 deletions src/extensions/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,26 @@ impl<Item> Traversable<Item> for BinaryHeap<Item> {
impl<Item: Ord> Collectible<Item> for BinaryHeap<Item> {
type This<I> = BinaryHeap<I>;

#[inline]
fn add(mut self, value: Item) -> Self
where
Self: IntoIterator<Item = Item> + FromIterator<Item>
{
self.push(value);
self
}

#[inline]
fn add_multi(mut self, elements: impl IntoIterator<Item = Item>) -> Self
where
Self: IntoIterator<Item = Item> + Sized + FromIterator<Item>,
{
elements.into_iter().for_each(|x| {
self.push(x);
});
self
}

#[inline]
fn combinations(&self, k: usize) -> Vec<Self>
where
Expand Down
20 changes: 20 additions & 0 deletions src/extensions/collections/btree_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ use std::hash::Hash;
impl<Key: Ord, Value> Map<Key, Value> for BTreeMap<Key, Value> {
type This<X, V> = BTreeMap<X, V>;

#[inline]
fn add(mut self, key: Key, value: Value) -> Self
where
Self: IntoIterator<Item = (Key, Value)> + FromIterator<(Key, Value)>,
{
let _unused = self.insert(key, value);
self
}

#[inline]
fn add_multi(mut self, entries: impl IntoIterator<Item = (Key, Value)>) -> Self
where
Self: IntoIterator<Item = (Key, Value)> + 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)
Expand Down
20 changes: 20 additions & 0 deletions src/extensions/collections/btree_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,26 @@ impl<Item> Traversable<Item> for BTreeSet<Item> {
impl<Item: Ord> Collectible<Item> for BTreeSet<Item> {
type This<I> = BTreeSet<I>;

#[inline]
fn add(mut self, value: Item) -> Self
where
Self: IntoIterator<Item = Item> + FromIterator<Item>
{
let _ = self.insert(value);
self
}

#[inline]
fn add_multi(mut self, elements: impl IntoIterator<Item = Item>) -> Self
where
Self: IntoIterator<Item = Item> + Sized + FromIterator<Item>,
{
for x in elements {
let _unused = self.insert(x);
};
self
}

#[inline]
fn combinations(&self, k: usize) -> Vec<Self>
where
Expand Down
22 changes: 21 additions & 1 deletion src/extensions/collections/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,29 @@ use std::cmp::Ordering;
use std::collections::HashMap;
use std::hash::Hash;

impl<Key, Value> Map<Key, Value> for HashMap<Key, Value> {
impl<Key: Eq + Hash, Value> Map<Key, Value> for HashMap<Key, Value> {
type This<X, V> = HashMap<X, V>;

#[inline]
fn add(mut self, key: Key, value: Value) -> Self
where
Self: IntoIterator<Item = (Key, Value)> + FromIterator<(Key, Value)>,
{
let _unused = self.insert(key, value);
self
}

#[inline]
fn add_multi(mut self, entries: impl IntoIterator<Item = (Key, Value)>) -> Self
where
Self: IntoIterator<Item = (Key, Value)> + 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)
Expand Down
20 changes: 20 additions & 0 deletions src/extensions/collections/hash_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,26 @@ impl<Item> Traversable<Item> for HashSet<Item> {
impl<Item: Eq + Hash> Collectible<Item> for HashSet<Item> {
type This<I> = HashSet<I>;

#[inline]
fn add(mut self, value: Item) -> Self
where
Self: IntoIterator<Item = Item> + FromIterator<Item>
{
let _ = self.insert(value);
self
}

#[inline]
fn add_multi(mut self, elements: impl IntoIterator<Item = Item>) -> Self
where
Self: IntoIterator<Item = Item> + Sized + FromIterator<Item>,
{
for x in elements {
let _unused = self.insert(x);
};
self
}

#[inline]
fn combinations(&self, k: usize) -> Vec<Self>
where
Expand Down
20 changes: 20 additions & 0 deletions src/extensions/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ impl<Item> Traversable<Item> for LinkedList<Item> {
impl<Item> Collectible<Item> for LinkedList<Item> {
type This<I> = LinkedList<I>;

#[inline]
fn add(mut self, value: Item) -> Self
where
Self: IntoIterator<Item = Item> + FromIterator<Item>
{
self.push_back(value);
self
}

#[inline]
fn add_multi(mut self, elements: impl IntoIterator<Item = Item>) -> Self
where
Self: IntoIterator<Item = Item> + Sized + FromIterator<Item>,
{
elements.into_iter().for_each(|x| {
self.push_back(x);
});
self
}

#[inline]
fn combinations(&self, k: usize) -> Vec<Self>
where
Expand Down
20 changes: 20 additions & 0 deletions src/extensions/collections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ impl<Item> Traversable<Item> for Vec<Item> {
impl<Item> Collectible<Item> for Vec<Item> {
type This<I> = Vec<I>;

#[inline]
fn add(mut self, value: Item) -> Self
where
Self: IntoIterator<Item = Item> + FromIterator<Item>
{
self.push(value);
self
}

#[inline]
fn add_multi(mut self, elements: impl IntoIterator<Item = Item>) -> Self
where
Self: IntoIterator<Item = Item> + Sized + FromIterator<Item>,
{
elements.into_iter().for_each(|x| {
self.push(x);
});
self
}

#[inline]
fn combinations(&self, k: usize) -> Vec<Self>
where
Expand Down
20 changes: 20 additions & 0 deletions src/extensions/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ impl<Item> Traversable<Item> for VecDeque<Item> {
impl<Item> Collectible<Item> for VecDeque<Item> {
type This<I> = VecDeque<I>;

#[inline]
fn add(mut self, value: Item) -> Self
where
Self: IntoIterator<Item = Item> + FromIterator<Item>
{
self.push_back(value);
self
}

#[inline]
fn add_multi(mut self, elements: impl IntoIterator<Item = Item>) -> Self
where
Self: IntoIterator<Item = Item> + Sized + FromIterator<Item>,
{
elements.into_iter().for_each(|x| {
self.push_back(x);
});
self
}

#[inline]
fn combinations(&self, k: usize) -> Vec<Self>
where
Expand Down
22 changes: 11 additions & 11 deletions src/extensions/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ pub trait Map<Key, Value> {
/// ]));
/// ```
#[inline]
fn add_multi(self, iterable: impl IntoIterator<Item = (Key, Value)>) -> Self
fn add_multi(self, entries: impl IntoIterator<Item = (Key, Value)>) -> Self
where
Self: IntoIterator<Item = (Key, Value)> + 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.
Expand Down Expand Up @@ -274,12 +274,12 @@ pub trait Map<Key, Value> {
/// assert_eq!(e.delete_multi(&vec![1]), HashMap::new());
/// ```
#[inline]
fn delete_multi<'a>(self, iterable: &'a impl Iterable<Item<'a> = &'a Key>) -> Self
fn delete_multi<'a>(self, keys: &'a impl Iterable<Item<'a> = &'a Key>) -> Self
where
Key: Eq + Hash + 'a,
Self: IntoIterator<Item = (Key, Value)> + 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()
}

Expand All @@ -304,7 +304,7 @@ pub trait Map<Key, Value> {
///
/// assert!(!a.disjoint(&vec![3, 4]));
/// ```
fn disjoint<'a>(&'a self, elements: &'a impl Iterable<Item<'a> = &'a Key>) -> bool
fn disjoint<'a>(&'a self, keys: &'a impl Iterable<Item<'a> = &'a Key>) -> bool
where
Key: Eq + Hash + 'a;

Expand Down Expand Up @@ -961,13 +961,13 @@ pub trait Map<Key, Value> {
/// }
/// ```
#[inline]
fn intersect<'a>(self, iterable: &'a impl Iterable<Item<'a> = &'a (Key, Value)>) -> Self
fn intersect<'a>(self, entries: &'a impl Iterable<Item<'a> = &'a (Key, Value)>) -> Self
where
Key: Eq + Hash + 'a,
Value: Eq + Hash + 'a,
Self: IntoIterator<Item = (Key, Value)> + 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()
}

Expand Down Expand Up @@ -1761,7 +1761,7 @@ pub trait Map<Key, Value> {
/// assert!(!a.subset(&vec![1, 2]));
/// assert!(!a.subset(&vec![]));
/// ```
fn subset<'a>(&'a self, elements: &'a impl Iterable<Item<'a> = &'a Key>) -> bool
fn subset<'a>(&'a self, keys: &'a impl Iterable<Item<'a> = &'a Key>) -> bool
where
Key: Eq + Hash + 'a;

Expand Down Expand Up @@ -1822,13 +1822,13 @@ pub trait Map<Key, Value> {
/// ```
#[inline]
fn substitute_multi<'a>(
self, elements: &'a impl Iterable<Item<'a> = &'a Key>, replacement: impl IntoIterator<Item = (Key, Value)>,
self, keys: &'a impl Iterable<Item<'a> = &'a Key>, replacement: impl IntoIterator<Item = (Key, Value)>,
) -> Self
where
Key: Eq + Hash + 'a,
Self: IntoIterator<Item = (Key, Value)> + 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()
}
Expand Down Expand Up @@ -1858,7 +1858,7 @@ pub trait Map<Key, Value> {
/// assert!(!a.superset(&vec![3, 4]));
/// assert!(!e.superset(&vec![1]));
/// ```
fn superset<'a>(&'a self, elements: &'a impl Iterable<Item<'a> = &'a Key>) -> bool
fn superset<'a>(&'a self, keys: &'a impl Iterable<Item<'a> = &'a Key>) -> bool
where
Key: Eq + Hash + 'a;

Expand Down

0 comments on commit e3a60a9

Please sign in to comment.