File tree 4 files changed +20
-20
lines changed 4 files changed +20
-20
lines changed Original file line number Diff line number Diff line change @@ -51,13 +51,11 @@ impl<I> Iterator for Combinations<I>
51
51
type Item = Vec < I :: Item > ;
52
52
fn next ( & mut self ) -> Option < Self :: Item > {
53
53
let mut pool_len = self . pool . len ( ) ;
54
- if self . pool . is_done ( ) {
55
- if pool_len == 0 || self . k > pool_len {
56
- return None ;
57
- }
58
- }
59
54
60
55
if self . first {
56
+ if self . pool . is_done ( ) {
57
+ return None ;
58
+ }
61
59
self . first = false ;
62
60
} else if self . k == 0 {
63
61
return None ;
Original file line number Diff line number Diff line change 66
66
// If this is the first iteration, return early
67
67
if self . first {
68
68
// In empty edge cases, stop iterating immediately
69
- return if self . k == 0 || self . pool . is_done ( ) {
69
+ return if self . k != 0 && ! self . pool . get_next ( ) {
70
70
None
71
71
// Otherwise, yield the initial state
72
72
} else {
77
77
78
78
// Check if we need to consume more from the iterator
79
79
// This will run while we increment our first index digit
80
- if !self . pool . is_done ( ) {
81
- self . pool . get_next ( ) ;
80
+ if self . pool . get_next ( ) {
82
81
self . max_index = self . pool . len ( ) - 1 ;
83
82
}
84
83
Original file line number Diff line number Diff line change @@ -12,19 +12,10 @@ where
12
12
I : Iterator ,
13
13
{
14
14
pub fn new ( it : I ) -> LazyBuffer < I > {
15
- let mut it = it;
16
- let mut buffer = Vec :: new ( ) ;
17
- let done;
18
- if let Some ( first) = it. next ( ) {
19
- buffer. push ( first) ;
20
- done = false ;
21
- } else {
22
- done = true ;
23
- }
24
15
LazyBuffer {
25
16
it : it,
26
- done : done ,
27
- buffer : buffer ,
17
+ done : false ,
18
+ buffer : Vec :: new ( ) ,
28
19
}
29
20
}
30
21
Original file line number Diff line number Diff line change @@ -599,6 +599,13 @@ fn combinations_of_too_short() {
599
599
#[ test]
600
600
fn combinations_zero ( ) {
601
601
it:: assert_equal ( ( 1 ..3 ) . combinations ( 0 ) , vec ! [ vec![ ] ] ) ;
602
+ it:: assert_equal ( ( 0 ..0 ) . combinations ( 0 ) , vec ! [ vec![ ] ] ) ;
603
+ }
604
+
605
+ #[ test]
606
+ fn permutations_zero ( ) {
607
+ it:: assert_equal ( ( 1 ..3 ) . permutations ( 0 ) , vec ! [ vec![ ] ] ) ;
608
+ it:: assert_equal ( ( 0 ..0 ) . permutations ( 0 ) , vec ! [ vec![ ] ] ) ;
602
609
}
603
610
604
611
#[ test]
@@ -620,7 +627,12 @@ fn combinations_with_replacement() {
620
627
// Zero size
621
628
it:: assert_equal (
622
629
( 0 ..3 ) . combinations_with_replacement ( 0 ) ,
623
- <Vec < Vec < _ > > >:: new ( ) ,
630
+ vec ! [ vec![ ] ] ,
631
+ ) ;
632
+ // Zero size on empty pool
633
+ it:: assert_equal (
634
+ ( 0 ..0 ) . combinations_with_replacement ( 0 ) ,
635
+ vec ! [ vec![ ] ] ,
624
636
) ;
625
637
// Empty pool
626
638
it:: assert_equal (
You can’t perform that action at this time.
0 commit comments