|
1 | 1 | use core::array;
|
2 |
| -use core::borrow::BorrowMut; |
3 | 2 | use std::fmt;
|
4 | 3 | use std::iter::FusedIterator;
|
5 | 4 |
|
6 |
| -use super::lazy_buffer::LazyBuffer; |
| 5 | +use super::lazy_buffer::{LazyBuffer, PoolIndex}; |
7 | 6 | use alloc::vec::Vec;
|
8 | 7 |
|
9 | 8 | use crate::adaptors::checked_binomial;
|
| 9 | +use crate::lazy_buffer::MaybeConstUsize as _; |
10 | 10 |
|
11 | 11 | /// Iterator for `Vec` valued combinations returned by [`.combinations()`](crate::Itertools::combinations)
|
12 | 12 | pub type Combinations<I> = CombinationsGeneric<I, Vec<usize>>;
|
@@ -39,79 +39,6 @@ pub struct CombinationsGeneric<I: Iterator, Idx> {
|
39 | 39 | first: bool,
|
40 | 40 | }
|
41 | 41 |
|
42 |
| -pub trait MaybeConstUsize : Clone + Copy + std::fmt::Debug { |
43 |
| - /*TODO const*/fn value(self) -> usize; |
44 |
| -} |
45 |
| - |
46 |
| -#[derive(Clone, Copy, Debug)] |
47 |
| -pub struct ConstUsize<const N: usize>; |
48 |
| -impl<const N: usize> MaybeConstUsize for ConstUsize<N> { |
49 |
| - fn value(self) -> usize { |
50 |
| - N |
51 |
| - } |
52 |
| -} |
53 |
| - |
54 |
| -impl MaybeConstUsize for usize { |
55 |
| - fn value(self) -> usize { |
56 |
| - self |
57 |
| - } |
58 |
| -} |
59 |
| - |
60 |
| -/// A type holding indices of elements in a pool or buffer of items from an inner iterator |
61 |
| -/// and used to pick out different combinations in a generic way. |
62 |
| -pub trait PoolIndex: BorrowMut<[usize]> { |
63 |
| - type Item<T>; |
64 |
| - type Length: MaybeConstUsize; |
65 |
| - |
66 |
| - fn extract_item<I: Iterator>(&self, pool: &LazyBuffer<I>) -> Self::Item<I::Item> |
67 |
| - where |
68 |
| - I::Item: Clone; |
69 |
| - |
70 |
| - fn from_fn<T, F: Fn(usize)->T>(k: Self::Length, f: F) -> Self::Item<T>; |
71 |
| - |
72 |
| - fn len(&self) -> Self::Length; |
73 |
| -} |
74 |
| - |
75 |
| -impl PoolIndex for Vec<usize> { |
76 |
| - type Item<T> = Vec<T>; |
77 |
| - type Length = usize; |
78 |
| - |
79 |
| - fn extract_item<I: Iterator>(&self, pool: &LazyBuffer<I>) -> Self::Item<I::Item> |
80 |
| - where |
81 |
| - I::Item: Clone |
82 |
| - { |
83 |
| - pool.get_at(self) |
84 |
| - } |
85 |
| - |
86 |
| - fn from_fn<T, F: Fn(usize)->T>(k: Self::Length, f: F) -> Self::Item<T> { |
87 |
| - (0..k).map(f).collect() |
88 |
| - } |
89 |
| - |
90 |
| - fn len(&self) -> Self::Length { |
91 |
| - self.len() |
92 |
| - } |
93 |
| -} |
94 |
| - |
95 |
| -impl<const K: usize> PoolIndex for [usize; K] { |
96 |
| - type Item<T> = [T; K]; |
97 |
| - type Length = ConstUsize<K>; |
98 |
| - |
99 |
| - fn extract_item<I: Iterator>(&self, pool: &LazyBuffer<I>) -> Self::Item<I::Item> |
100 |
| - where |
101 |
| - I::Item: Clone |
102 |
| - { |
103 |
| - pool.get_array(*self) |
104 |
| - } |
105 |
| - |
106 |
| - fn from_fn<T, F: Fn(usize)->T>(_k: Self::Length, f: F) -> Self::Item<T> { |
107 |
| - std::array::from_fn(f) |
108 |
| - } |
109 |
| - |
110 |
| - fn len(&self) -> Self::Length { |
111 |
| - ConstUsize::<K> |
112 |
| - } |
113 |
| -} |
114 |
| - |
115 | 42 | impl<I, Idx> Clone for CombinationsGeneric<I, Idx>
|
116 | 43 | where
|
117 | 44 | I: Iterator + Clone,
|
|
0 commit comments