|
16 | 16 |
|
17 | 17 | Base.length(p::IntegerPartitions) = npartitions(p.n) |
18 | 18 |
|
19 | | -Base.start(p::IntegerPartitions) = Int[] |
20 | | -Base.done(p::IntegerPartitions, xs) = length(xs) == p.n |
21 | | -Base.next(p::IntegerPartitions, xs) = (xs = nextpartition(p.n,xs); (xs,xs)) |
| 19 | +function Base.iterate(p::IntegerPartitions, xs = Int[]) |
| 20 | + length(xs) == p.n && return |
| 21 | + xs = nextpartition(p.n,xs) |
| 22 | + (xs, xs) |
| 23 | +end |
22 | 24 |
|
23 | 25 | """ |
24 | 26 | partitions(n) |
@@ -105,13 +107,15 @@ partitions(n::Integer, m::Integer) = |
105 | 107 | FixedPartitions(n, m) : |
106 | 108 | throw(DomainError((n, m), "n and m must be positive")) |
107 | 109 |
|
108 | | -Base.start(f::FixedPartitions) = Int[] |
109 | | -function Base.done(f::FixedPartitions, s::Vector{Int}) |
110 | | - f.m <= f.n || return true |
111 | | - isempty(s) && return false |
112 | | - return f.m == 1 || s[1]-1 <= s[end] |
| 110 | +function Base.iterate(f::FixedPartitions, s::Vector{Int} = Int[]) |
| 111 | + f.m <= f.n || return |
| 112 | + if !isempty(s) |
| 113 | + (f.m == 1 || s[1]-1 <= s[end]) && return |
| 114 | + end |
| 115 | + |
| 116 | + xs = nextfixedpartition(f.n,f.m,s) |
| 117 | + (xs, xs) |
113 | 118 | end |
114 | | -Base.next(f::FixedPartitions, s::Vector{Int}) = (xs = nextfixedpartition(f.n,f.m,s); (xs,xs)) |
115 | 119 |
|
116 | 120 | function nextfixedpartition(n, m, bs) |
117 | 121 | as = copy(bs) |
@@ -177,9 +181,14 @@ number of partitions to generate can be efficiently computed using |
177 | 181 | """ |
178 | 182 | partitions(s::AbstractVector) = SetPartitions(s) |
179 | 183 |
|
180 | | -Base.start(p::SetPartitions) = (n = length(p.s); (zeros(Int32, n), ones(Int32, n-1), n, 1)) |
181 | | -Base.done(p::SetPartitions, s) = s[1][1] > 0 |
182 | | -Base.next(p::SetPartitions, s) = nextsetpartition(p.s, s...) |
| 184 | +function Base.iterate(p::SetPartitions) |
| 185 | + n = length(p.s) |
| 186 | + iterate(p, (zeros(Int32, n), ones(Int32, n-1), n, 1)) |
| 187 | +end |
| 188 | +function Base.iterate(p::SetPartitions, s) |
| 189 | + s[1][1] > 0 && return |
| 190 | + nextsetpartition(p.s, s...) |
| 191 | +end |
183 | 192 |
|
184 | 193 | function nextsetpartition(s::AbstractVector, a, b, n, m) |
185 | 194 | function makeparts(s, a, m) |
@@ -255,18 +264,22 @@ partitions(s::AbstractVector, m::Int) = |
255 | 264 | FixedSetPartitions(s, m) : |
256 | 265 | throw(DomainError((length(s), m), "length(s) and m must be positive")) |
257 | 266 |
|
258 | | -function Base.start(p::FixedSetPartitions) |
| 267 | +function Base.iterate(p::FixedSetPartitions) |
259 | 268 | n = length(p.s) |
260 | 269 | m = p.m |
261 | | - m <= n ? (vcat(ones(Int, n-m),1:m), vcat(1,n-m+2:n), n) : (Int[], Int[], n) |
| 270 | + state = m <= n ? (vcat(ones(Int, n-m),1:m), vcat(1,n-m+2:n), n) : (Int[], Int[], n) |
| 271 | + # state consists of: |
| 272 | + # vector a of length n describing to which partition every element of s belongs |
| 273 | + # vector b of length n describing the first index b[i] that belongs to partition i |
| 274 | + # integer n |
| 275 | + |
| 276 | + iterate(p, state) |
262 | 277 | end |
263 | | -# state consists of: |
264 | | -# vector a of length n describing to which partition every element of s belongs |
265 | | -# vector b of length n describing the first index b[i] that belongs to partition i |
266 | | -# integer n |
267 | 278 |
|
268 | | -Base.done(p::FixedSetPartitions, s) = isempty(s[1]) || s[1][1] > 1 |
269 | | -Base.next(p::FixedSetPartitions, s) = nextfixedsetpartition(p.s,p.m, s...) |
| 279 | +function Base.iterate(p::FixedSetPartitions, s) |
| 280 | + (isempty(s[1]) || s[1][1] > 1) && return |
| 281 | + nextfixedsetpartition(p.s,p.m, s...) |
| 282 | +end |
270 | 283 |
|
271 | 284 | function nextfixedsetpartition(s::AbstractVector, m, a, b, n) |
272 | 285 | function makeparts(s, a) |
@@ -310,7 +323,7 @@ function nextfixedsetpartition(s::AbstractVector, m, a, b, n) |
310 | 323 | end |
311 | 324 | b[k] -= 1 |
312 | 325 | b[k+1:m] = n-m+k+1:n |
313 | | - a[1:n] = 1 |
| 326 | + a[1:n] .= 1 |
314 | 327 | a[b] = 1:m |
315 | 328 | end |
316 | 329 | end |
@@ -496,4 +509,3 @@ function _ncpart!(a::Int, b::Int, nn::Int, x::Vector, partitions::Vector) |
496 | 509 | end |
497 | 510 | end |
498 | 511 | end |
499 | | - |
|
0 commit comments