Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NondecreasingIndices #28

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Iteration/NondecreasingIndices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ julia> for v in NondecreasingIndices(4, 2)
struct NondecreasingIndices
n::Int
m::Int

function NondecreasingIndices(n::Int, m::Int)
@assert n > 0 && m > 0 "require n > 0 and m > 0"
new(n, m)
end
end

Base.eltype(::Type{NondecreasingIndices}) = Vector{Int}
Expand All @@ -51,7 +56,8 @@ Base.length(ndi::NondecreasingIndices) = binomial(ndi.n + ndi.m - 1, ndi.m)
# initialization
function Base.iterate(ndi::NondecreasingIndices)
v = ones(Int, ndi.m)
return (v, v)
next = ndi.n == 1 ? nothing : v
return (v, next)
end

# normal iteration
Expand Down
6 changes: 2 additions & 4 deletions src/Iteration/StrictlyIncreasingIndices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,15 @@ Base.length(sii::StrictlyIncreasingIndices) = binomial(sii.n, sii.m)
# initialization
function Base.iterate(sii::StrictlyIncreasingIndices)
v = [1:sii.m;]
return (v, v)
next = sii.n == sii.m ? nothing : v
return (v, next)
end

# normal iteration
function Base.iterate(sii::StrictlyIncreasingIndices, state::AbstractVector{Int})
v = state
i = sii.m
diff = sii.n
if i == diff
return nothing
end
while v[i] == diff
i -= 1
diff -= 1
Expand Down
12 changes: 11 additions & 1 deletion test/Iteration/NondecreasingIndices.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
using ReachabilityBase.Iteration

vectors = Vector{AbstractVector{Int}}()
for v in NondecreasingIndices(2, 3)
ndi = NondecreasingIndices(2, 3)
@test length(ndi) == 4
for v in ndi
push!(vectors, copy(v))
end
@test vectors == [[1, 1, 1], [1, 1, 2], [1, 2, 2], [2, 2, 2]]

vectors = Vector{AbstractVector{Int}}()
ndi = NondecreasingIndices(1, 3)
@test length(ndi) == 1
for v in ndi
push!(vectors, copy(v))
end
@test vectors == [[1, 1, 1]]
12 changes: 11 additions & 1 deletion test/Iteration/StrictlyIncreasingIndices.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
using ReachabilityBase.Iteration

vectors = Vector{AbstractVector{Int}}()
for v in StrictlyIncreasingIndices(5, 4)
sii = StrictlyIncreasingIndices(5, 4)
@test length(sii) == 5
for v in sii
push!(vectors, copy(v))
end
@test vectors == [[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5]]

vectors = Vector{AbstractVector{Int}}()
sii = StrictlyIncreasingIndices(2, 2)
@test length(sii) == 1
for v in sii
push!(vectors, copy(v))
end
@test vectors == [[1, 2]]