@@ -7,7 +7,6 @@ use super::lazy_buffer::LazyBuffer;
7
7
/// See [`.combinations()`](../trait.Itertools.html#method.combinations) for more information.
8
8
#[ must_use = "iterator adaptors are lazy and do nothing unless consumed" ]
9
9
pub struct Combinations < I : Iterator > {
10
- k : usize ,
11
10
indices : Vec < usize > ,
12
11
pool : LazyBuffer < I > ,
13
12
first : bool ,
@@ -17,24 +16,20 @@ impl<I> Clone for Combinations<I>
17
16
where I : Clone + Iterator ,
18
17
I :: Item : Clone ,
19
18
{
20
- clone_fields ! ( k , indices, pool, first) ;
19
+ clone_fields ! ( indices, pool, first) ;
21
20
}
22
21
23
22
impl < I > fmt:: Debug for Combinations < I >
24
23
where I : Iterator + fmt:: Debug ,
25
24
I :: Item : fmt:: Debug ,
26
25
{
27
- debug_fmt_fields ! ( Combinations , k , indices, pool, first) ;
26
+ debug_fmt_fields ! ( Combinations , indices, pool, first) ;
28
27
}
29
28
30
29
/// Create a new `Combinations` from a clonable iterator.
31
30
pub fn combinations < I > ( iter : I , k : usize ) -> Combinations < I >
32
31
where I : Iterator
33
32
{
34
- let mut indices: Vec < usize > = Vec :: with_capacity ( k) ;
35
- for i in 0 ..k {
36
- indices. push ( i) ;
37
- }
38
33
let mut pool: LazyBuffer < I > = LazyBuffer :: new ( iter) ;
39
34
40
35
for _ in 0 ..k {
@@ -44,8 +39,7 @@ pub fn combinations<I>(iter: I, k: usize) -> Combinations<I>
44
39
}
45
40
46
41
Combinations {
47
- k : k,
48
- indices : indices,
42
+ indices : ( 0 ..k) . collect ( ) ,
49
43
pool : pool,
50
44
first : true ,
51
45
}
@@ -64,11 +58,11 @@ impl<I> Iterator for Combinations<I>
64
58
return None ;
65
59
}
66
60
self . first = false ;
67
- } else if self . k == 0 {
61
+ } else if self . indices . len ( ) == 0 {
68
62
return None ;
69
63
} else {
70
64
// Scan from the end, looking for an index to increment
71
- let mut i: usize = self . k - 1 ;
65
+ let mut i: usize = self . indices . len ( ) - 1 ;
72
66
73
67
// Check if we need to consume more from the iterator
74
68
if self . indices [ i] == pool_len - 1 && !self . pool . is_done ( ) {
@@ -77,7 +71,7 @@ impl<I> Iterator for Combinations<I>
77
71
}
78
72
}
79
73
80
- while self . indices [ i] == i + pool_len - self . k {
74
+ while self . indices [ i] == i + pool_len - self . indices . len ( ) {
81
75
if i > 0 {
82
76
i -= 1 ;
83
77
} else {
@@ -88,18 +82,12 @@ impl<I> Iterator for Combinations<I>
88
82
89
83
// Increment index, and reset the ones to its right
90
84
self . indices [ i] += 1 ;
91
- let mut j = i + 1 ;
92
- while j < self . k {
85
+ for j in i+1 ..self . indices . len ( ) {
93
86
self . indices [ j] = self . indices [ j - 1 ] + 1 ;
94
- j += 1 ;
95
87
}
96
88
}
97
89
98
90
// Create result vector based on the indices
99
- let mut result = Vec :: with_capacity ( self . k ) ;
100
- for i in self . indices . iter ( ) {
101
- result. push ( self . pool [ * i] . clone ( ) ) ;
102
- }
103
- Some ( result)
91
+ Some ( self . indices . iter ( ) . map ( |i| self . pool [ * i] . clone ( ) ) . collect ( ) )
104
92
}
105
93
}
0 commit comments