Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Ockajak committed Jul 9, 2024
1 parent d659985 commit e47a1f0
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 33 deletions.
81 changes: 48 additions & 33 deletions src/extensions/collectible.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![deny(missing_docs)]

use crate::extensions::iterable::Iterable;
use crate::extensions::util::unfold::unfold;
use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::hash::Hash;
use std::iter;
use std::iter::{Product, Sum};

use crate::extensions::iterable::Iterable;
use crate::extensions::util::unfold::unfold;
use std::thread::panicking;

Check failure on line 10 in src/extensions/collectible.rs

View workflow job for this annotation

GitHub Actions / Build

unused import: `std::thread::panicking`

/// Consuming collection operations.
///
Expand Down Expand Up @@ -152,10 +152,8 @@ pub trait Collectible<Item>: IntoIterator<Item = Item> {
///
/// let a = vec![1, 2, 3];
///
/// let x: Vec<Vec<i32>> = Vec::new();
/// assert_eq!(a.clone().combinations(4), x);
/// assert_eq!(a.clone().combinations(1), vec![vec![1], vec![2], vec![3]]);
/// assert_eq!(a.clone().combinations(2), vec![vec![1, 2], vec![1, 3], vec![2, 3]]);
/// assert_eq!(a.combinations(1), vec![vec![1], vec![2], vec![3]]);
/// assert_eq!(a.combinations(2), vec![vec![1, 2], vec![1, 3], vec![2, 3]]);
/// assert_eq!(a.combinations(3), vec![vec![1, 2, 3]]);
/// ```
fn combinations(&self, k: usize) -> Vec<Self>
Expand Down Expand Up @@ -827,31 +825,28 @@ pub trait Collectible<Item>: IntoIterator<Item = Item> {
(result_left, result_right)
}

// /// Creates a collection containing all subsets of the original collection.
// ///
// /// The order or subset values is preserved for ordered collections.
// ///
// /// # Example
// ///
// /// ```
// /// use cantrip::*;
// ///
// /// let a = vec![1, 2, 3];
// ///
// /// assert_eq!(a.powerset(), vec![
// /// vec![],
// /// vec![0], vec![1], vec![2],
// /// vec![0, 1], vec![0, 2], vec![1, 2],
// /// vec![1, 2, 3]]
// /// );
// /// ```
// fn powerset(self) -> Self::This<Self>
// where
// Item: Clone,
// Self: FromIterator<Item> + Sized,
// Self::This<Self>: FromIterator<Self> {
// self.combinations(2)
// }
/// Creates a collection containing all subsets of the original collection.
///
/// The order or subset values is preserved for ordered collections.
///
/// # Example
///
/// ```
/// use cantrip::*;
///
/// let a = vec![1, 2, 3];
///
/// assert_eq!(a.powerset(), vec![
/// vec![],
/// vec![1], vec![2], vec![3],
/// vec![1, 2], vec![1, 3], vec![2, 3],
/// vec![1, 2, 3]]
/// );
/// ```
fn powerset(self) -> Vec<Self>
where
Item: Clone,
Self: Sized;

/// Iterates over the entire collection, multiplying all the elements
///
Expand Down Expand Up @@ -1174,7 +1169,15 @@ where
if k == 0 {
return Vec::from_iter(iter::once(Collection::from_iter(iter::empty())));
}
let values = Vec::from_iter(iterator.cloned());
let values = Vec::from_iter(iterator);
compute_combinations(&values, k)
}

pub(crate) fn compute_combinations<'a, Item, Collection>(values: &[&Item], k: usize) -> Vec<Collection>
where
Item: Clone + 'a,
Collection: FromIterator<Item> + Sized,
{
let size = values.len();
let mut combination_indices = Vec::from_iter(0..k);
unfold(k > size, |done| {
Expand Down Expand Up @@ -1215,3 +1218,15 @@ pub(crate) fn partition_map<'a, Item: 'a, A, B, Left: Default + Extend<A>, Right
}
(result_left, result_right)
}

pub(crate) fn powerset<'a, Item, Collection>(iterator: impl Iterator<Item = &'a Item>) -> Vec<Collection>
where
Item: Clone + 'a,
Collection: FromIterator<Item> + Sized,
{
let values = Vec::from_iter(iterator);
let sizes = 1..=values.len();
iter::once(Collection::from_iter(iter::empty()))
.chain(sizes.flat_map(|size| compute_combinations::<Item, Collection>(&values, size)))
.collect()
}
9 changes: 9 additions & 0 deletions src/extensions/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ impl<Item: Ord> Collectible<Item> for BinaryHeap<Item> {
partition_map(self.iter(), function)
}

#[inline]
fn powerset(self) -> Vec<Self>
where
Item: Clone,
Self: Sized
{
powerset(self.iter())
}

#[inline]
fn scan<S, B>(&self, init: S, function: impl FnMut(&mut S, &Item) -> Option<B>) -> Self::This<B>
where
Expand Down
9 changes: 9 additions & 0 deletions src/extensions/collections/btree_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ impl<Item: Ord> Collectible<Item> for BTreeSet<Item> {
partition_map(self.iter(), function)
}

#[inline]
fn powerset(self) -> Vec<Self>
where
Item: Clone,
Self: Sized
{
powerset(self.iter())
}

#[inline]
fn scan<S, B>(&self, init: S, function: impl FnMut(&mut S, &Item) -> Option<B>) -> Self::This<B>
where
Expand Down
9 changes: 9 additions & 0 deletions src/extensions/collections/hash_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ impl<Item: Eq + Hash> Collectible<Item> for HashSet<Item> {
partition_map(self.iter(), function)
}

#[inline]
fn powerset(self) -> Vec<Self>
where
Item: Clone,
Self: Sized
{
powerset(self.iter())
}

#[inline]
fn scan<S, B>(&self, init: S, function: impl FnMut(&mut S, &Item) -> Option<B>) -> Self::This<B>
where
Expand Down
9 changes: 9 additions & 0 deletions src/extensions/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ impl<Item> Collectible<Item> for LinkedList<Item> {
partition_map(self.iter(), function)
}

#[inline]
fn powerset(self) -> Vec<Self>
where
Item: Clone,
Self: Sized
{
powerset(self.iter())
}

#[inline]
fn scan<S, B>(&self, init: S, function: impl FnMut(&mut S, &Item) -> Option<B>) -> Self::This<B>
where
Expand Down
9 changes: 9 additions & 0 deletions src/extensions/collections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ impl<Item> Collectible<Item> for Vec<Item> {
partition_map(self.iter(), function)
}

#[inline]
fn powerset(self) -> Vec<Self>
where
Item: Clone,
Self: Sized
{
powerset(self.iter())
}

#[inline]
fn scan<S, B>(&self, initial_state: S, function: impl FnMut(&mut S, &Item) -> Option<B>) -> Self::This<B>
where
Expand Down
9 changes: 9 additions & 0 deletions src/extensions/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ impl<Item> Collectible<Item> for VecDeque<Item> {
partition_map(self.iter(), function)
}

#[inline]
fn powerset(self) -> Vec<Self>
where
Item: Clone,
Self: Sized
{
powerset(self.iter())
}

#[inline]
fn scan<S, B>(&self, init: S, function: impl FnMut(&mut S, &Item) -> Option<B>) -> Self::This<B>
where
Expand Down

0 comments on commit e47a1f0

Please sign in to comment.