Skip to content

Commit d4ecad7

Browse files
mendelsshopphimuemue
authored andcommitted
Added const generic verions of combinations_with_replacement
(array_combinations_with_replacement)
1 parent 9bba778 commit d4ecad7

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/combinations_with_replacement.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ where
2424

2525
/// Iterator for `Box<[I]>` valued combinations_with_replacement returned by [`.combinations_with_replacement()`](crate::Itertools::combinations_with_replacement)
2626
pub type CombinationsWithReplacement<I> = CombinationsWithReplacementGeneric<I, Box<[usize]>>;
27+
/// Iterator for const generic combinations_with_replacement returned by [`.array_combinations_with_replacement()`](crate::Itertools::array_combinations_with_replacement)
28+
pub type ArrayCombinationsWithReplacement<I, const K: usize> =
29+
CombinationsWithReplacementGeneric<I, [usize; K]>;
30+
2731
impl<I, Idx> fmt::Debug for CombinationsWithReplacementGeneric<I, Idx>
2832
where
2933
I: Iterator + fmt::Debug,
@@ -33,6 +37,15 @@ where
3337
debug_fmt_fields!(CombinationsWithReplacementGeneric, indices, pool, first);
3438
}
3539

40+
/// Create a new `ArrayCombinationsWithReplacement`` from a clonable iterator.
41+
pub fn array_combinations_with_replacement<I: Iterator, const K: usize>(
42+
iter: I,
43+
) -> ArrayCombinationsWithReplacement<I, K>
44+
where
45+
I::Item: Clone,
46+
{
47+
ArrayCombinationsWithReplacement::new(iter, [0; K])
48+
}
3649
/// Create a new `CombinationsWithReplacement` from a clonable iterator.
3750
pub fn combinations_with_replacement<I>(iter: I, k: usize) -> CombinationsWithReplacement<I>
3851
where

src/lib.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ pub mod traits {
153153
pub use crate::tuple_impl::HomogeneousTuple;
154154
}
155155

156+
#[cfg(feature = "use_alloc")]
157+
use crate::combinations_with_replacement::ArrayCombinationsWithReplacement;
156158
pub use crate::concat_impl::concat;
157159
pub use crate::cons_tuples_impl::cons_tuples;
158160
pub use crate::diff::diff_with;
@@ -1804,7 +1806,35 @@ pub trait Itertools: Iterator {
18041806
{
18051807
combinations_with_replacement::combinations_with_replacement(self, k)
18061808
}
1807-
1809+
/// Return an iterator that iterates over the `k`-length combinations of
1810+
/// the elements from an iterator, with replacement.
1811+
///
1812+
/// Iterator element type is [Self::Item; K]. The iterator produces a new
1813+
/// array per iteration, and clones the iterator elements.
1814+
///
1815+
/// ```
1816+
/// use itertools::Itertools;
1817+
///
1818+
/// let it = (1..4).array_combinations_with_replacement::<2>();
1819+
/// itertools::assert_equal(it, vec![
1820+
/// [1, 1],
1821+
/// [1, 2],
1822+
/// [1, 3],
1823+
/// [2, 2],
1824+
/// [2, 3],
1825+
/// [3, 3],
1826+
/// ]);
1827+
/// ```
1828+
#[cfg(feature = "use_alloc")]
1829+
fn array_combinations_with_replacement<const K: usize>(
1830+
self,
1831+
) -> ArrayCombinationsWithReplacement<Self, K>
1832+
where
1833+
Self: Sized,
1834+
Self::Item: Clone,
1835+
{
1836+
combinations_with_replacement::array_combinations_with_replacement(self)
1837+
}
18081838
/// Return an iterator adaptor that iterates over all k-permutations of the
18091839
/// elements from an iterator.
18101840
///

0 commit comments

Comments
 (0)