File tree 1 file changed +40
-0
lines changed
1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 46
46
}
47
47
}
48
48
49
+ impl < I > CombinationsWithReplacement < I >
50
+ where
51
+ I : Iterator ,
52
+ I :: Item : Clone ,
53
+ {
54
+ /// Increments indices representing the combination to advance to the next
55
+ /// (in lexicographic order by increasing sequence) combination.
56
+ ///
57
+ /// Returns true if we've run out of combinations, false otherwise.
58
+ fn increment_indices ( & mut self ) -> bool {
59
+ // Check if we need to consume more from the iterator
60
+ // This will run while we increment our first index digit
61
+ self . pool . get_next ( ) ;
62
+
63
+ // Work out where we need to update our indices
64
+ let mut increment = None ;
65
+ for ( i, indices_int) in self . indices . iter ( ) . enumerate ( ) . rev ( ) {
66
+ if * indices_int < self . pool . len ( ) - 1 {
67
+ increment = Some ( ( i, indices_int + 1 ) ) ;
68
+ break ;
69
+ }
70
+ }
71
+ match increment {
72
+ // If we can update the indices further
73
+ Some ( ( increment_from, increment_value) ) => {
74
+ // We need to update the rightmost non-max value
75
+ // and all those to the right
76
+ for i in & mut self . indices [ increment_from..] {
77
+ * i = increment_value;
78
+ }
79
+ // TODO: once MSRV >= 1.50, use `fill` instead:
80
+ // self.indices[increment_from..].fill(increment_value);
81
+ false
82
+ }
83
+ // Otherwise, we're done
84
+ None => true ,
85
+ }
86
+ }
87
+ }
88
+
49
89
impl < I > Iterator for CombinationsWithReplacement < I >
50
90
where
51
91
I : Iterator ,
You can’t perform that action at this time.
0 commit comments