Skip to content

Commit 9b3d6e2

Browse files
phimuemueronnodas
authored andcommitted
Introduce MaybeConstSize
1 parent 8ae44e0 commit 9b3d6e2

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/combinations.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,66 @@ pub struct CombinationsGeneric<I: Iterator, Idx> {
3939
first: bool,
4040
}
4141

42+
pub trait MaybeConstUsize {
43+
/*TODO const*/fn value(self) -> usize;
44+
}
45+
46+
pub struct ConstUsize<const N: usize>;
47+
impl<const N: usize> MaybeConstUsize for ConstUsize<N> {
48+
fn value(self) -> usize {
49+
N
50+
}
51+
}
52+
53+
impl MaybeConstUsize for usize {
54+
fn value(self) -> usize {
55+
self
56+
}
57+
}
58+
4259
/// A type holding indices of elements in a pool or buffer of items from an inner iterator
4360
/// and used to pick out different combinations in a generic way.
4461
pub trait PoolIndex<T>: BorrowMut<[usize]> {
4562
type Item;
63+
type Length: MaybeConstUsize;
4664

4765
fn extract_item<I: Iterator<Item = T>>(&self, pool: &LazyBuffer<I>) -> Self::Item
4866
where
4967
T: Clone;
5068

51-
fn len(&self) -> usize {
52-
self.borrow().len()
53-
}
69+
fn len(&self) -> Self::Length;
5470
}
5571

5672
impl<T> PoolIndex<T> for Vec<usize> {
5773
type Item = Vec<T>;
74+
type Length = usize;
5875

5976
fn extract_item<I: Iterator<Item = T>>(&self, pool: &LazyBuffer<I>) -> Vec<T>
6077
where
6178
T: Clone,
6279
{
6380
pool.get_at(self)
6481
}
82+
83+
fn len(&self) -> Self::Length {
84+
self.len()
85+
}
6586
}
6687

6788
impl<T, const K: usize> PoolIndex<T> for [usize; K] {
6889
type Item = [T; K];
90+
type Length = ConstUsize<K>;
6991

7092
fn extract_item<I: Iterator<Item = T>>(&self, pool: &LazyBuffer<I>) -> [T; K]
7193
where
7294
T: Clone,
7395
{
7496
pool.get_array(*self)
7597
}
98+
99+
fn len(&self) -> Self::Length {
100+
ConstUsize::<K>
101+
}
76102
}
77103

78104
impl<I, Idx> Clone for CombinationsGeneric<I, Idx>
@@ -105,7 +131,7 @@ impl<I: Iterator, Idx: PoolIndex<I::Item>> CombinationsGeneric<I, Idx> {
105131

106132
/// Returns the length of a combination produced by this iterator.
107133
#[inline]
108-
pub fn k(&self) -> usize {
134+
pub fn k(&self) -> Idx::Length {
109135
self.indices.len()
110136
}
111137

@@ -136,8 +162,8 @@ impl<I: Iterator, Idx: PoolIndex<I::Item>> CombinationsGeneric<I, Idx> {
136162
/// Initialises the iterator by filling a buffer with elements from the
137163
/// iterator. Returns true if there are no combinations, false otherwise.
138164
fn init(&mut self) -> bool {
139-
self.pool.prefill(self.k());
140-
let done = self.k() > self.n();
165+
self.pool.prefill(self.k().value());
166+
let done = self.k().value() > self.n();
141167
if !done {
142168
self.first = false;
143169
}

0 commit comments

Comments
 (0)