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

Added array_permutations (attempt 2) #1014

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions src/combinations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::array;
use std::fmt;
use std::iter::FusedIterator;

use super::lazy_buffer::{LazyBuffer, PoolIndex};
use super::lazy_buffer::{LazyBuffer, ArrayOrVecHelper};
use alloc::vec::Vec;

use crate::adaptors::checked_binomial;
Expand Down Expand Up @@ -57,7 +57,7 @@ where
debug_fmt_fields!(Combinations, indices, pool, first);
}

impl<I: Iterator, Idx: PoolIndex> CombinationsGeneric<I, Idx> {
impl<I: Iterator, Idx: ArrayOrVecHelper> CombinationsGeneric<I, Idx> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Idx still a good name if the trait is ArrayOrVecHelper?

/// Constructor with arguments the inner iterator and the initial state for the indices.
fn new(iter: I, indices: Idx) -> Self {
Self {
Expand Down Expand Up @@ -174,7 +174,7 @@ impl<I, Idx> Iterator for CombinationsGeneric<I, Idx>
where
I: Iterator,
I::Item: Clone,
Idx: PoolIndex,
Idx: ArrayOrVecHelper,
{
type Item = Idx::Item<I::Item>;
fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -212,7 +212,7 @@ impl<I, Idx> FusedIterator for CombinationsGeneric<I, Idx>
where
I: Iterator,
I::Item: Clone,
Idx: PoolIndex,
Idx: ArrayOrVecHelper,
{
}

Expand Down
6 changes: 3 additions & 3 deletions src/lazy_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl MaybeConstUsize for usize {

/// A type holding indices of elements in a pool or buffer of items from an inner iterator
/// and used to pick out different combinations in a generic way.
pub trait PoolIndex: BorrowMut<[usize]> {
pub trait ArrayOrVecHelper: BorrowMut<[usize]> {
type Item<T>;
type Length: MaybeConstUsize;

Expand All @@ -113,7 +113,7 @@ pub trait PoolIndex: BorrowMut<[usize]> {
fn len(&self) -> Self::Length;
}

impl PoolIndex for Vec<usize> {
impl ArrayOrVecHelper for Vec<usize> {
type Item<T> = Vec<T>;
type Length = usize;

Expand All @@ -133,7 +133,7 @@ impl PoolIndex for Vec<usize> {
}
}

impl<const K: usize> PoolIndex for [usize; K] {
impl<const K: usize> ArrayOrVecHelper for [usize; K] {
type Item<T> = [T; K];
type Length = ConstUsize<K>;

Expand Down
16 changes: 8 additions & 8 deletions src/permutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use alloc::vec::Vec;
use std::fmt;
use std::iter::FusedIterator;

use super::lazy_buffer::{LazyBuffer, MaybeConstUsize as _, PoolIndex};
use super::lazy_buffer::{ArrayOrVecHelper, LazyBuffer, MaybeConstUsize as _};
use crate::size_hint::{self, SizeHint};

#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct PermutationsGeneric<I: Iterator, Idx: PoolIndex> {
pub struct PermutationsGeneric<I: Iterator, Idx: ArrayOrVecHelper> {
vals: LazyBuffer<I>,
state: PermutationState<Idx>,
}
Expand All @@ -23,13 +23,13 @@ impl<I, Idx> Clone for PermutationsGeneric<I, Idx>
where
I: Clone + Iterator,
I::Item: Clone,
Idx: Clone + PoolIndex,
Idx: Clone + ArrayOrVecHelper,
{
clone_fields!(vals, state);
}

#[derive(Clone, Debug)]
enum PermutationState<Idx: PoolIndex> {
enum PermutationState<Idx: ArrayOrVecHelper> {
/// No permutation generated yet.
Start { k: Idx::Length },
/// Values from the iterator are not fully loaded yet so `n` is still unknown.
Expand All @@ -44,7 +44,7 @@ enum PermutationState<Idx: PoolIndex> {
End,
}

impl<I, Idx: PoolIndex> fmt::Debug for PermutationsGeneric<I, Idx>
impl<I, Idx: ArrayOrVecHelper> fmt::Debug for PermutationsGeneric<I, Idx>
where
I: Iterator + fmt::Debug,
I::Item: fmt::Debug,
Expand All @@ -60,7 +60,7 @@ pub fn permutations<I: Iterator>(iter: I, k: usize) -> Permutations<I> {
}
}

impl<I, Idx: PoolIndex> Iterator for PermutationsGeneric<I, Idx>
impl<I, Idx: ArrayOrVecHelper> Iterator for PermutationsGeneric<I, Idx>
where
I: Iterator,
I::Item: Clone,
Expand Down Expand Up @@ -140,7 +140,7 @@ where
}
}

impl<I, Idx: PoolIndex> FusedIterator for PermutationsGeneric<I, Idx>
impl<I, Idx: ArrayOrVecHelper> FusedIterator for PermutationsGeneric<I, Idx>
where
I: Iterator,
I::Item: Clone,
Expand All @@ -165,7 +165,7 @@ fn advance(indices: &mut [usize], cycles: &mut [usize]) -> bool {
true
}

impl<Idx: PoolIndex> PermutationState<Idx> {
impl<Idx: ArrayOrVecHelper> PermutationState<Idx> {
fn size_hint_for(&self, n: usize) -> SizeHint {
// At the beginning, there are `n!/(n-k)!` items to come.
let at_start = |n, k: Idx::Length| {
Expand Down