From 7d0a1f058c392a4395f78775ae41c8a2238cf1cd Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Mon, 17 May 2021 16:05:51 +0100 Subject: [PATCH 1/2] feat: remove unneeded trait bound tuple_combinations has an unnecessary trait bound. --- src/adaptors/mod.rs | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index dfc68978f..97aab74da 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -690,7 +690,7 @@ pub trait HasCombination: Sized { /// Create a new `TupleCombinations` from a clonable iterator. pub fn tuple_combinations(iter: I) -> TupleCombinations - where I: Iterator + Clone, + where I: Iterator, I::Item: Clone, T: HasCombination, { diff --git a/src/lib.rs b/src/lib.rs index 1a6632e20..43b65325f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1442,7 +1442,7 @@ pub trait Itertools : Iterator { /// itertools::assert_equal(it, vec![(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]); /// ``` fn tuple_combinations(self) -> TupleCombinations - where Self: Sized + Clone, + where Self: Sized, Self::Item: Clone, T: adaptors::HasCombination, { From 783bca10d8d1a8dddc33d12f745b999b85cdcea7 Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Sun, 23 May 2021 13:20:17 +0100 Subject: [PATCH 2/2] feat: ArrayCombinations using const generics --- benches/tuple_combinations.rs | 50 +++++++++++++++++++++++ src/adaptors/mod.rs | 74 +++++++++++++++++++++++++++++++++++ src/lib.rs | 8 ++++ tests/quick.rs | 20 ++++++++++ 4 files changed, 152 insertions(+) diff --git a/benches/tuple_combinations.rs b/benches/tuple_combinations.rs index 4e26b282e..a9255bf83 100644 --- a/benches/tuple_combinations.rs +++ b/benches/tuple_combinations.rs @@ -99,6 +99,51 @@ fn tuple_comb_c4(c: &mut Criterion) { }); } + +fn array_comb_c1(c: &mut Criterion) { + c.bench_function("array comb c1", move |b| { + b.iter(|| { + for [i] in (0..N1).array_combinations() { + black_box(i); + } + }) + }); +} + + +fn array_comb_c2(c: &mut Criterion) { + c.bench_function("array comb c2", move |b| { + b.iter(|| { + for [i, j] in (0..N2).array_combinations() { + black_box(i + j); + } + }) + }); +} + + +fn array_comb_c3(c: &mut Criterion) { + c.bench_function("array comb c3", move |b| { + b.iter(|| { + for [i, j, k] in (0..N3).array_combinations() { + black_box(i + j + k); + } + }) + }); +} + + +fn array_comb_c4(c: &mut Criterion) { + c.bench_function("array comb c4", move |b| { + b.iter(|| { + for [i, j, k, l] in (0..N4).array_combinations() { + black_box(i + j + k + l); + } + }) + }); +} + + criterion_group!( benches, tuple_comb_for1, @@ -109,5 +154,10 @@ criterion_group!( tuple_comb_c2, tuple_comb_c3, tuple_comb_c4, + array_comb_c1, + array_comb_c2, + array_comb_c3, + array_comb_c4, ); + criterion_main!(benches); diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index 97aab74da..eecf4df6d 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -812,6 +812,80 @@ impl_tuple_combination!(Tuple10Combination Tuple9Combination; a b c d e f g h i) impl_tuple_combination!(Tuple11Combination Tuple10Combination; a b c d e f g h i j); impl_tuple_combination!(Tuple12Combination Tuple11Combination; a b c d e f g h i j k); +#[derive(Debug, Clone)] +pub struct ArrayCombinations { + slice: Vec, + indicies: [usize; R], +} + +impl ArrayCombinations { + pub fn new(slice: Vec) -> Self { + debug_assert!(slice.len() >= R); + + let mut indicies = [0; R]; + for i in 0..R { + indicies[i] = i; + } + + Self { + slice, + indicies, + } + } +} + +impl Iterator for ArrayCombinations { + type Item = [T; R]; + + fn next(&mut self) -> Option { + if self.indicies[R-1] == 0 { + return None; + } + + // SAFETY: uninitialized data is never read + let output = unsafe { + let mut output: [T; R] = std::mem::uninitialized(); + for i in 0..R { + output[i] = self.slice[self.indicies[i]].clone(); + } + output + }; + + // // The below uses currently unstable rust. Once they are stable + // // we can replace the deprecated uninitialized + + // let mut output = std::mem::MaybeUninit::uninit_array::(); + // for i in 0..R { + // // SAFETY: only writing so no UB + // unsafe { + // *output[i].as_mut_ptr() = self.slice[self.indicies[i]].clone(); + // } + // } + // // SAFETY: initialised above + // let output = unsafe { std::mem::MaybeUninit::array_assume_init(output) }; + + let mut x = R; + for i in (0..R).rev() { + self.indicies[i] += 1; + if self.indicies[i] == self.slice.len() + i - R + 1 { + x = i; + } else { + break + } + } + + if x == 0 { + self.indicies[R-1] = 0; + } else { + for i in x..R { + self.indicies[i] = self.indicies[i-1] + 1; + } + } + + Some(output) + } +} + /// An iterator adapter to filter values within a nested `Result::Ok`. /// /// See [`.filter_ok()`](crate::Itertools::filter_ok) for more information. diff --git a/src/lib.rs b/src/lib.rs index 43b65325f..2b3d8b50c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,6 +105,7 @@ pub mod structs { WhileSome, Coalesce, TupleCombinations, + ArrayCombinations, Positions, Update, }; @@ -1449,6 +1450,13 @@ pub trait Itertools : Iterator { adaptors::tuple_combinations(self) } + fn array_combinations(self) -> ArrayCombinations + where Self: Sized, + Self::Item: Clone + std::fmt::Debug, + { + ArrayCombinations::new(self.collect()) + } + /// Return an iterator adaptor that iterates over the `k`-length combinations of /// the elements from an iterator. /// diff --git a/tests/quick.rs b/tests/quick.rs index 7769cb432..4d7d6a890 100644 --- a/tests/quick.rs +++ b/tests/quick.rs @@ -895,6 +895,26 @@ quickcheck! { } } +quickcheck! { + fn equal_combinations_array(it: Iter) -> bool { + let values = it.clone().collect_vec(); + if values.len() < 2 { + return true; + } + + let mut cmb = it.array_combinations(); + for i in 0..values.len() { + for j in i+1..values.len() { + let pair = [values[i], values[j]]; + if pair != cmb.next().unwrap() { + return false; + } + } + } + cmb.next() == None + } +} + quickcheck! { fn size_pad_tail(it: Iter, pad: u8) -> bool { correct_size_hint(it.clone().pad_using(pad as usize, |_| 0)) &&