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 e3a60a9 commit 522d2e0
Show file tree
Hide file tree
Showing 12 changed files with 374 additions and 167 deletions.
41 changes: 15 additions & 26 deletions src/extensions/collectible.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::extensions::iterable::Iterable;
use crate::extensions::util::unfold::unfold;
use crate::extensions::{collect_by_index, frequencies};
use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashMap, HashSet, LinkedList};
use std::collections::{BinaryHeap, HashMap, LinkedList};
use std::hash::Hash;
use std::iter;
use std::iter::{Product, Sum};

use crate::extensions::{collect_by_index, frequencies};
use crate::extensions::iterable::Iterable;
use crate::extensions::util::unfold::unfold;

/// Consuming collection operations.
///
/// Methods have the following properties:
Expand All @@ -30,11 +31,11 @@ pub trait Collectible<Item>: IntoIterator<Item = Item> {
/// assert_eq!(a.add(3), vec![1, 2, 3, 3]);
/// ```
#[inline]
fn add(self, value: Item) -> Self
fn add(self, element: Item) -> Self
where
Self: IntoIterator<Item = Item> + FromIterator<Item>,
{
self.into_iter().chain(iter::once(value)).collect()
self.into_iter().chain(iter::once(element)).collect()
}

/// Creates a new collection by appending all elements of another collection to
Expand Down Expand Up @@ -109,7 +110,7 @@ pub trait Collectible<Item>: IntoIterator<Item = Item> {
/// assert_eq!(e.delete(&2), vec![]);
/// ```
#[inline]
fn delete(self, value: &Item) -> Self
fn delete(self, element: &Item) -> Self
where
Item: PartialEq,
Self: IntoIterator<Item = Item> + Sized + FromIterator<Item>,
Expand All @@ -118,7 +119,7 @@ pub trait Collectible<Item>: IntoIterator<Item = Item> {
self
.into_iter()
.filter(|x| {
if !removed && value == x {
if !removed && element == x {
removed = true;
false
} else {
Expand Down Expand Up @@ -180,12 +181,12 @@ pub trait Collectible<Item>: IntoIterator<Item = Item> {
/// assert_eq!(Vec::fill_with(|| 1, 0), vec![]);
/// ```
#[inline]
fn fill_with(mut value: impl FnMut() -> Item, size: usize) -> Self
fn fill_with(mut element: impl FnMut() -> Item, size: usize) -> Self
where
Item: Clone,
Self: FromIterator<Item>,
{
iter::repeat(value()).take(size).collect()
iter::repeat(element()).take(size).collect()
}

/// Creates a new collection by filtering this collection using a
Expand Down Expand Up @@ -1211,13 +1212,13 @@ pub trait Collectible<Item>: IntoIterator<Item = Item> {
/// assert_eq!(e.substitute(&1, 2), vec![]);
/// ```
#[inline]
fn substitute(self, value: &Item, replacement: Item) -> Self
fn substitute(self, element: &Item, replacement: Item) -> Self
where
Item: PartialEq,
Self: IntoIterator<Item = Item> + FromIterator<Item>,
{
let mut replaced = Some(replacement);
self.into_iter().map(|item| if &item == value { replaced.take().unwrap_or(item) } else { item }).collect()
self.into_iter().map(|item| if &item == element { replaced.take().unwrap_or(item) } else { item }).collect()
}

/// Creates a new collection from this collection by replacing the
Expand Down Expand Up @@ -1314,11 +1315,11 @@ pub trait Collectible<Item>: IntoIterator<Item = Item> {
///
/// assert_eq!(unit, vec![1]);
#[inline]
fn unit(value: Item) -> Self
fn unit(element: Item) -> Self
where
Self: FromIterator<Item> + Sized,
{
iter::once(value).collect()
iter::once(element).collect()
}
}

Expand Down Expand Up @@ -1380,15 +1381,3 @@ pub(crate) fn powerset<'a, Item: Clone + 'a, Collection: FromIterator<Item> + Si
.chain(sizes.flat_map(|size| compute_combinations::<Item, Collection>(&values, size)))
.collect()
}

pub(crate) fn substitute_multi<'a, Item, Collection>(
collection: Collection, elements: &'a impl Iterable<Item<'a> = &'a Item>, replacement: impl IntoIterator<Item = Item>,
) -> Collection
where
Item: Eq + Hash + 'a,
Collection: IntoIterator<Item = Item> + FromIterator<Item>,
{
let iterator = elements.iterator();
let removed: HashSet<&Item> = HashSet::from_iter(iterator);
collection.into_iter().filter(|x| !removed.contains(x)).chain(replacement).collect()
}
4 changes: 2 additions & 2 deletions src/extensions/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ impl<Item: Ord> Collectible<Item> for BinaryHeap<Item> {
type This<I> = BinaryHeap<I>;

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

Expand Down
56 changes: 49 additions & 7 deletions src/extensions/collections/btree_set.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::extensions::*;
use std::cmp::Ordering;
use std::collections::{BTreeSet, HashMap};
use std::collections::{BTreeSet, HashMap, LinkedList};
use std::hash::Hash;

impl<Item> Traversable<Item> for BTreeSet<Item> {
Expand Down Expand Up @@ -134,11 +134,11 @@ impl<Item: Ord> Collectible<Item> for BTreeSet<Item> {
type This<I> = BTreeSet<I>;

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

Expand All @@ -149,7 +149,7 @@ impl<Item: Ord> Collectible<Item> for BTreeSet<Item> {
{
for x in elements {
let _unused = self.insert(x);
};
}
self
}

Expand All @@ -161,6 +161,28 @@ impl<Item: Ord> Collectible<Item> for BTreeSet<Item> {
combinations(self.iter(), k)
}

#[inline]
fn delete(mut self, element: &Item) -> Self
where
Item: PartialEq,
Self: IntoIterator<Item = Item> + Sized + FromIterator<Item>,
{
let _unused = self.remove(element);
self
}

#[inline]
fn delete_multi<'a>(mut self, elements: &'a impl Iterable<Item<'a> = &'a Item>) -> Self
where
Item: Eq + Hash + 'a,
Self: FromIterator<Item>,
{
for element in elements.iterator() {
let _unused = self.remove(element);
}
self
}

#[inline]
fn filter_map<B>(&self, function: impl FnMut(&Item) -> Option<B>) -> Self::This<B>
where
Expand Down Expand Up @@ -205,13 +227,33 @@ impl<Item: Ord> Collectible<Item> for BTreeSet<Item> {
}

#[inline]
fn substitute(mut self, element: &Item, replacement: Item) -> Self
where
Item: PartialEq,
Self: IntoIterator<Item = Item> + FromIterator<Item>,
{
if self.remove(element) {
let _unused = self.insert(replacement);
}
self
}

fn substitute_multi<'a>(
self, elements: &'a impl Iterable<Item<'a> = &'a Item>, replacement: impl IntoIterator<Item = Item>,
mut self, elements: &'a impl Iterable<Item<'a> = &'a Item>, replacements: impl IntoIterator<Item = Item>,
) -> Self
where
Item: Eq + Hash + 'a,
Self: IntoIterator<Item = Item> + FromIterator<Item>,
{
substitute_multi(self, elements, replacement)
let mut replacement_values = LinkedList::<Item>::new();
for (element, replacement) in elements.iterator().zip(replacements) {
if self.remove(element) {
replacement_values.push_back(replacement);
}
}
for replacement in replacement_values {
let _unused = self.insert(replacement);
}
self
}
}
22 changes: 22 additions & 0 deletions src/extensions/collections/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ impl<Key: Eq + Hash, Value> Map<Key, Value> for HashMap<Key, Value> {
count_unique(self.values())
}

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

#[inline]
fn delete_multi<'a>(mut self, keys: &'a impl Iterable<Item<'a> = &'a Key>) -> Self
where
Key: Eq + Hash + 'a,
Self: IntoIterator<Item = (Key, Value)> + FromIterator<(Key, Value)>
{
for key in keys.iterator() {
let _unused = self.remove(key);
}
self
}

#[inline]
fn disjoint<'a>(&'a self, elements: &'a impl Iterable<Item<'a> = &'a Key>) -> bool
where
Expand Down
58 changes: 50 additions & 8 deletions src/extensions/collections/hash_set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::collections::{HashMap, HashSet, LinkedList};
use std::hash::Hash;

use crate::extensions::*;
Expand Down Expand Up @@ -135,11 +135,11 @@ impl<Item: Eq + Hash> Collectible<Item> for HashSet<Item> {
type This<I> = HashSet<I>;

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

Expand All @@ -150,7 +150,7 @@ impl<Item: Eq + Hash> Collectible<Item> for HashSet<Item> {
{
for x in elements {
let _unused = self.insert(x);
};
}
self
}

Expand All @@ -162,6 +162,28 @@ impl<Item: Eq + Hash> Collectible<Item> for HashSet<Item> {
combinations(self.iter(), k)
}

#[inline]
fn delete(mut self, element: &Item) -> Self
where
Item: PartialEq,
Self: IntoIterator<Item = Item> + Sized + FromIterator<Item>,
{
let _unused = self.remove(element);
self
}

#[inline]
fn delete_multi<'a>(mut self, elements: &'a impl Iterable<Item<'a> = &'a Item>) -> Self
where
Item: Eq + Hash + 'a,
Self: FromIterator<Item>,
{
for element in elements.iterator() {
let _unused = self.remove(element);
}
self
}

#[inline]
fn filter_map<B>(&self, function: impl FnMut(&Item) -> Option<B>) -> Self::This<B>
where
Expand Down Expand Up @@ -206,13 +228,33 @@ impl<Item: Eq + Hash> Collectible<Item> for HashSet<Item> {
}

#[inline]
fn substitute(mut self, element: &Item, replacement: Item) -> Self
where
Item: PartialEq,
Self: IntoIterator<Item = Item> + FromIterator<Item>,
{
if self.remove(element) {
let _unused = self.insert(replacement);
}
self
}

fn substitute_multi<'a>(
self, elements: &'a impl Iterable<Item<'a> = &'a Item>, replacement: impl IntoIterator<Item = Item>,
mut self, elements: &'a impl Iterable<Item<'a> = &'a Item>, replacements: impl IntoIterator<Item = Item>,
) -> Self
where
Item: 'a,
Item: Eq + Hash + 'a,
Self: IntoIterator<Item = Item> + FromIterator<Item>,
{
substitute_multi(self, elements, replacement)
let mut replacement_values = LinkedList::<Item>::new();
for (element, replacement) in elements.iterator().zip(replacements) {
if self.remove(element) {
replacement_values.push_back(replacement);
}
}
for replacement in replacement_values {
let _unused = self.insert(replacement);
}
self
}
}
10 changes: 4 additions & 6 deletions src/extensions/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ impl<Item> Collectible<Item> for LinkedList<Item> {
type This<I> = LinkedList<I>;

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

Expand Down Expand Up @@ -345,21 +345,19 @@ impl<Item> Sequence<Item> for LinkedList<Item> {
}

#[inline]
fn windowed(&self, size: usize, step: usize) -> Self::This<Self>
fn windowed(&self, size: usize, step: usize) -> Vec<Self>
where
Item: Clone,
Self: IntoIterator<Item = Item> + FromIterator<Item>,
Self::This<Self>: FromIterator<Self>,
{
windowed(self.iter(), size, step)
}

#[inline]
fn windowed_circular(&self, size: usize, step: usize) -> Self::This<Self>
fn windowed_circular(&self, size: usize, step: usize) -> Vec<Self>
where
Item: Clone,
Self: IntoIterator<Item = Item> + FromIterator<Item>,
Self::This<Self>: FromIterator<Self>,
{
windowed_circular(self.iter(), size, step)
}
Expand Down
Loading

0 comments on commit 522d2e0

Please sign in to comment.