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 c60120a commit 9ae767b
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 129 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ data.group_by(|x| x % 2); // HashMap::from([(0, vec![2]), (1, vec![1, 3]
| *all_equal* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| *all_unique* | :heavy_check_mark: | :heavy_check_mark: | | |
| *any* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
| *cartesian_product* | :heavy_check_mark: | | :heavy_check_mark: | |
| *cartesian_product* | :heavy_check_mark: | | | |
| *combinations* | :heavy_check_mark: | | :heavy_check_mark: | :heavy_check_mark: |
| *combinations_repetitive* | :heavy_check_mark: | | :heavy_check_mark: | :heavy_check_mark: |
| *combinations_repetitive* | :heavy_check_mark: | | | :heavy_check_mark: |
| *chunked* | :heavy_check_mark: | | | |
| *chunked_by* | :heavy_check_mark: | | | |
| *chunked_exact* | :heavy_check_mark: | | | |
Expand Down
70 changes: 1 addition & 69 deletions src/extensions/collectible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use std::iter::{Product, Sum};
/// - May create a new collection
///
pub trait Collectible<Item>: IntoIterator<Item = Item> {
// FIXME - implement these methods
// cartesian_product

/// Original collection type
type This<I>;
Expand Down Expand Up @@ -133,7 +131,7 @@ pub trait Collectible<Item>: IntoIterator<Item = Item> {
.collect()
}

/// Creates a collection containing all combinations of specified size from the elements
/// Creates a collection containing combinations of specified size from the elements
/// of the original collection.
///
/// The order or combined values is preserved for ordered collections.
Expand All @@ -154,35 +152,6 @@ pub trait Collectible<Item>: IntoIterator<Item = Item> {
Item: Clone,
Self: Sized;

/// Creates a collection containing all combinations with repetition of specified size
/// from the elements of the original collection.
///
/// The order or combined values is preserved for ordered collections.
///
/// # Example
///
/// ```
/// use cantrip::*;
///
/// let a = vec![1, 2, 3];
///
/// assert_eq!(a.combinations_repetitive(1), vec![vec![1], vec![2], vec![3]]);
/// assert_eq!(
/// a.combinations_repetitive(2),
/// vec![vec![1, 1], vec![1, 2], vec![1, 3], vec![2, 2], vec![2, 3], vec![3, 3]]
/// );
/// assert_eq!(
/// a.combinations_repetitive(3), vec![
/// vec![1, 1, 1], vec![1, 1, 2], vec![1, 1, 3], vec![1, 2, 2], vec![1, 2, 3],
/// vec![1, 3, 3], vec![2, 2, 2], vec![2, 2, 3], vec![2, 3, 3], vec![3, 3, 3],
/// ]
/// );
/// ```
fn combinations_repetitive(&self, k: usize) -> Vec<Self>
where
Item: Clone,
Self: Sized;

/// Creates a collection containing an element
/// specified number of times.
///
Expand Down Expand Up @@ -1375,43 +1344,6 @@ where
compute_combinations(&values, k)
}

pub(crate) fn combinations_repetitive<'a, Item, Collection>(
iterator: impl Iterator<Item = &'a Item>, k: usize,
) -> Vec<Collection>
where
Item: Clone + 'a,
Collection: FromIterator<Item> + Sized,
{
if k == 0 {
return Vec::from_iter(iter::once(Collection::from_iter(iter::empty())));
}
let values = Vec::from_iter(iterator);
let size = values.len();
let mut combination = Vec::fill(0, k);
unfold(k > size, |done| {
if *done {
return None;
}
let result = Some(Collection::from_iter(combination.iter().map(|index| values[*index].clone())));
let mut current_slot = k - 1;
while combination[current_slot] >= size - 1 {
if current_slot > 0 {
current_slot -= 1;
} else {
*done = true;
return result;
}
}
let current_index = combination[current_slot] + 1;
#[allow(clippy::needless_range_loop)]
for slot in current_slot..k {
combination[slot] = current_index;
}
result
})
.collect()
}

pub(crate) fn compute_combinations<'a, Item, Collection>(values: &[&Item], k: usize) -> Vec<Collection>
where
Item: Clone + 'a,
Expand Down
9 changes: 0 additions & 9 deletions src/extensions/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,6 @@ impl<Item: Ord> Collectible<Item> for BinaryHeap<Item> {
combinations(self.iter(), k)
}

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

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

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

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

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

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

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

#[inline]
fn filter_map<B>(&self, function: impl FnMut(&Item) -> Option<B>) -> Self::This<B>
where
Expand Down Expand Up @@ -173,6 +164,24 @@ impl<Item> Sequence<Item> for LinkedList<Item> {
all_unique(self.iter())
}

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

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

#[inline]
fn map_while<B>(&self, predicate: impl FnMut(&Item) -> Option<B>) -> Self::This<B> {
self.iter().map_while(predicate).collect()
Expand Down
27 changes: 18 additions & 9 deletions src/extensions/collections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,6 @@ impl<Item> Collectible<Item> for Vec<Item> {
combinations(self.iter(), k)
}

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

#[inline]
fn filter_map<B>(&self, function: impl FnMut(&Item) -> Option<B>) -> Self::This<B>
where
Expand Down Expand Up @@ -186,6 +177,24 @@ impl<Item> Sequence<Item> for Vec<Item> {
all_unique(self.iter())
}

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

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

#[inline]
fn map_while<B>(&self, predicate: impl FnMut(&Item) -> Option<B>) -> Self::This<B> {
self.iter().map_while(predicate).collect()
Expand Down
27 changes: 18 additions & 9 deletions src/extensions/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,6 @@ impl<Item> Collectible<Item> for VecDeque<Item> {
combinations(self.iter(), k)
}

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

#[inline]
fn filter_map<B>(&self, function: impl FnMut(&Item) -> Option<B>) -> Self::This<B>
where
Expand Down Expand Up @@ -173,6 +164,24 @@ impl<Item> Sequence<Item> for VecDeque<Item> {
all_unique(self.iter())
}

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

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

#[inline]
fn map_while<B>(&self, predicate: impl FnMut(&Item) -> Option<B>) -> Self::This<B> {
self.iter().map_while(predicate).collect()
Expand Down
Loading

0 comments on commit 9ae767b

Please sign in to comment.