11export combinations,
2- CoolLexCombinations,
3- multiset_combinations,
4- with_replacement_combinations,
5- powerset
2+ CoolLexCombinations, multiset_combinations, with_replacement_combinations, powerset
63
74# The Combinations iterator
85struct Combinations
96 n:: Int
107 t:: Int
118end
129
13- @inline function Base. iterate (c:: Combinations , s = [min (c. t - 1 , i) for i in 1 : c. t])
10+ @inline function Base. iterate (c:: Combinations , s= [min (c. t - 1 , i) for i in 1 : c. t])
1411 if c. t == 0 # special case to generate 1 result for t==0
1512 isempty (s) && return (s, [1 ])
16- return
13+ return nothing
1714 end
1815 for i in c. t: - 1 : 1
1916 s[i] += 1
2017 if s[i] > (c. n - (c. t - i))
2118 continue
2219 end
23- for j in i+ 1 : c. t
20+ for j in ( i+ 1 ) : c. t
2421 s[j] = s[j- 1 ] + 1
2522 end
2623 break
2724 end
28- s[1 ] > c. n - c. t + 1 && return
25+ s[1 ] > c. n - c. t + 1 && return nothing
2926 (s, s)
3027end
3128
@@ -49,14 +46,13 @@ function combinations(a, t::Integer)
4946 (reorder (c) for c in Combinations (length (a), t))
5047end
5148
52-
5349"""
5450 combinations(a)
5551
5652Generate combinations of the elements of `a` of all orders. Chaining of order iterators
5753is eager, but the sequence at each order is lazy.
5854"""
59- combinations (a) = Iterators. flatten ([combinations (a, k) for k = 0 : length (a)])
55+ combinations (a) = Iterators. flatten ([combinations (a, k) for k in 0 : length (a)])
6056
6157# cool-lex combinations iterator
6258
@@ -102,7 +98,7 @@ function Base.iterate(C::CoolLexCombinations)
10298end
10399
104100function Base. iterate (C:: CoolLexCombinations , S:: CoolLexIterState )
105- (S. R3 & S. R2 != 0 ) && return
101+ (S. R3 & S. R2 != 0 ) && return nothing
106102
107103 R0 = S. R0
108104 R1 = S. R1
134130
135131Base. length (C:: CoolLexCombinations ) = max (0 , binomial (C. n, C. t))
136132
137-
138133struct MultiSetCombinations{T}
139134 m:: T
140135 f:: Vector{Int}
@@ -158,7 +153,7 @@ function Base.length(c::MultiSetCombinations)
158153 end
159154 else
160155 for j in t: - 1 : 1
161- p[j+ 1 ] = sum (p[max (1 ,j+ 1 - f): (j+ 1 )])
156+ p[j+ 1 ] = sum (p[max (1 , j+ 1 - f): (j+ 1 )])
162157 end
163158 end
164159 end
167162
168163function multiset_combinations (m, f:: Vector{<:Integer} , t:: Integer )
169164 length (m) == length (f) || error (" Lengths of m and f are not the same." )
170- ref = length (f) > 0 ? vcat ([[i for j in 1 : f[i] ] for i in 1 : length (f)]. .. ) : Int[]
165+ ref = length (f) > 0 ? vcat ([[i for j in 1 : f[i]] for i in 1 : length (f)]. .. ) : Int[]
171166 if t < 0
172167 t = length (ref) + 1
173168 end
@@ -185,8 +180,9 @@ function multiset_combinations(a, t::Integer)
185180 multiset_combinations (m, f, t)
186181end
187182
188- function Base. iterate (c:: MultiSetCombinations , s = c. ref)
189- ((! isempty (s) && max (s[1 ], c. t) > length (c. ref)) || (isempty (s) && c. t > 0 )) && return
183+ function Base. iterate (c:: MultiSetCombinations , s= c. ref)
184+ ((! isempty (s) && max (s[1 ], c. t) > length (c. ref)) || (isempty (s) && c. t > 0 )) &&
185+ return nothing
190186
191187 ref = c. ref
192188 n = length (ref)
@@ -196,7 +192,7 @@ function Base.iterate(c::MultiSetCombinations, s = c.ref)
196192 if t > 0
197193 s = copy (s)
198194 for i in t: - 1 : 1
199- if s[i] < ref[i + (n - t)]
195+ if s[i] < ref[i+ (n - t)]
200196 j = 1
201197 while ref[j] <= s[i]
202198 j += 1
@@ -232,8 +228,8 @@ Generate all combinations with replacement of size `t` from an array `a`.
232228"""
233229with_replacement_combinations (a, t:: Integer ) = WithReplacementCombinations (a, t)
234230
235- function Base. iterate (c:: WithReplacementCombinations , s = [1 for i in 1 : c. t])
236- (! isempty (s) && s[1 ] > length (c. a) || c. t < 0 ) && return
231+ function Base. iterate (c:: WithReplacementCombinations , s= [1 for i in 1 : c. t])
232+ (! isempty (s) && s[1 ] > length (c. a) || c. t < 0 ) && return nothing
237233
238234 n = length (c. a)
239235 t = c. t
@@ -269,7 +265,7 @@ returns an iterator object. Use `collect(powerset(a, min, max))` to get an array
269265subsets.
270266"""
271267function powerset (a, min:: Integer = 0 , max:: Integer = length (a))
272- itrs = [combinations (a, k) for k = min: max]
268+ itrs = [combinations (a, k) for k in min: max]
273269 min < 1 && append! (itrs, eltype (a)[])
274270 Iterators. flatten (itrs)
275271end
0 commit comments