@@ -39,40 +39,66 @@ pub struct CombinationsGeneric<I: Iterator, Idx> {
39
39
first : bool ,
40
40
}
41
41
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
+
42
59
/// A type holding indices of elements in a pool or buffer of items from an inner iterator
43
60
/// and used to pick out different combinations in a generic way.
44
61
pub trait PoolIndex < T > : BorrowMut < [ usize ] > {
45
62
type Item ;
63
+ type Length : MaybeConstUsize ;
46
64
47
65
fn extract_item < I : Iterator < Item = T > > ( & self , pool : & LazyBuffer < I > ) -> Self :: Item
48
66
where
49
67
T : Clone ;
50
68
51
- fn len ( & self ) -> usize {
52
- self . borrow ( ) . len ( )
53
- }
69
+ fn len ( & self ) -> Self :: Length ;
54
70
}
55
71
56
72
impl < T > PoolIndex < T > for Vec < usize > {
57
73
type Item = Vec < T > ;
74
+ type Length = usize ;
58
75
59
76
fn extract_item < I : Iterator < Item = T > > ( & self , pool : & LazyBuffer < I > ) -> Vec < T >
60
77
where
61
78
T : Clone ,
62
79
{
63
80
pool. get_at ( self )
64
81
}
82
+
83
+ fn len ( & self ) -> Self :: Length {
84
+ self . len ( )
85
+ }
65
86
}
66
87
67
88
impl < T , const K : usize > PoolIndex < T > for [ usize ; K ] {
68
89
type Item = [ T ; K ] ;
90
+ type Length = ConstUsize < K > ;
69
91
70
92
fn extract_item < I : Iterator < Item = T > > ( & self , pool : & LazyBuffer < I > ) -> [ T ; K ]
71
93
where
72
94
T : Clone ,
73
95
{
74
96
pool. get_array ( * self )
75
97
}
98
+
99
+ fn len ( & self ) -> Self :: Length {
100
+ ConstUsize :: < K >
101
+ }
76
102
}
77
103
78
104
impl < I , Idx > Clone for CombinationsGeneric < I , Idx >
@@ -105,7 +131,7 @@ impl<I: Iterator, Idx: PoolIndex<I::Item>> CombinationsGeneric<I, Idx> {
105
131
106
132
/// Returns the length of a combination produced by this iterator.
107
133
#[ inline]
108
- pub fn k ( & self ) -> usize {
134
+ pub fn k ( & self ) -> Idx :: Length {
109
135
self . indices . len ( )
110
136
}
111
137
@@ -136,8 +162,8 @@ impl<I: Iterator, Idx: PoolIndex<I::Item>> CombinationsGeneric<I, Idx> {
136
162
/// Initialises the iterator by filling a buffer with elements from the
137
163
/// iterator. Returns true if there are no combinations, false otherwise.
138
164
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 ( ) ;
141
167
if !done {
142
168
self . first = false ;
143
169
}
0 commit comments