Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove unneeded trait bound #545

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions benches/tuple_combinations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
76 changes: 75 additions & 1 deletion src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ pub trait HasCombination<I>: Sized {

/// Create a new `TupleCombinations` from a clonable iterator.
pub fn tuple_combinations<T, I>(iter: I) -> TupleCombinations<I, T>
where I: Iterator + Clone,
where I: Iterator,
I::Item: Clone,
T: HasCombination<I>,
{
Expand Down Expand Up @@ -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<T, const R: usize> {
slice: Vec<T>,
indicies: [usize; R],
}

impl<T, const R: usize> ArrayCombinations<T, R> {
pub fn new(slice: Vec<T>) -> Self {
debug_assert!(slice.len() >= R);

let mut indicies = [0; R];
for i in 0..R {
indicies[i] = i;
}

Self {
slice,
indicies,
}
}
}

impl<T: Clone, const R: usize> Iterator for ArrayCombinations<T, R> {
type Item = [T; R];

fn next(&mut self) -> Option<Self::Item> {
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::<R>();
// 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.
Expand Down
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub mod structs {
WhileSome,
Coalesce,
TupleCombinations,
ArrayCombinations,
Positions,
Update,
};
Expand Down Expand Up @@ -1442,13 +1443,20 @@ pub trait Itertools : Iterator {
/// itertools::assert_equal(it, vec![(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]);
/// ```
fn tuple_combinations<T>(self) -> TupleCombinations<Self, T>
where Self: Sized + Clone,
where Self: Sized,
Self::Item: Clone,
T: adaptors::HasCombination<Self>,
{
adaptors::tuple_combinations(self)
}

fn array_combinations<const R: usize>(self) -> ArrayCombinations<Self::Item, R>
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.
///
Expand Down
20 changes: 20 additions & 0 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,26 @@ quickcheck! {
}
}

quickcheck! {
fn equal_combinations_array(it: Iter<i16>) -> 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<i8>, pad: u8) -> bool {
correct_size_hint(it.clone().pad_using(pad as usize, |_| 0)) &&
Expand Down