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

Add CartesianIterator #68

Merged
merged 1 commit into from
May 2, 2024
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
45 changes: 45 additions & 0 deletions src/Iteration/CartesianIterator.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# iterator over the set of vectors `v` of length n containing positive integers
# such that `v[i] ≤ ref[i]` for all `i`
#
# NOTE: The iterator can be instructed to not create copies of the vectors,
# which are however modified during iteration.
struct CartesianIterator{V<:AbstractVector{Int}}
ref::V
copy::Bool

function CartesianIterator(ref::V, copy::Bool=true) where {V}
@assert length(ref) > 0 "need at least one entry"
@assert all(>(0), ref) "all entries must be positive"
return new{V}(ref, copy)
end
end

function Base.length(it::CartesianIterator)
return prod(it.ref)
end

Base.eltype(::Type{<:CartesianIterator{V}}) where {V} = V

function Base.iterate(it::CartesianIterator)
state = ones(Int, length(it.ref))
return (state, state)
end

function Base.iterate(it::CartesianIterator, state)
if it.copy
state = copy(state)
end
i = length(it.ref) + 1
@inbounds while true
i -= 1
if i == 0
return nothing
end
if state[i] < it.ref[i]
state[i] += 1
break
end
state[i] = 1
end
return (state, state)
end
4 changes: 3 additions & 1 deletion src/Iteration/Iteration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ module Iteration
using SparseArrays

export EmptyIterator, SingletonIterator, VectorIterator,
StrictlyIncreasingIndices, NondecreasingIndices, BitvectorIterator
StrictlyIncreasingIndices, NondecreasingIndices, BitvectorIterator,
CartesianIterator

include("EmptyIterator.jl")
include("SingletonIterator.jl")
include("VectorIterator.jl")
include("StrictlyIncreasingIndices.jl")
include("NondecreasingIndices.jl")
include("BitvectorIterator.jl")
include("CartesianIterator.jl")

end # module
13 changes: 13 additions & 0 deletions test/Iteration/CartesianIterator.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using ReachabilityBase.Iteration

@test_throws AssertionError CartesianIterator(Int[])
@test_throws AssertionError CartesianIterator([-1])

ci = CartesianIterator([2, 3], false)
ci2 = CartesianIterator([2, 3], true)
@test length(ci) == length(ci2) == 6
@test [copy(v) for v in ci] == collect(ci2) == [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3]]

ci = CartesianIterator([1, 1, 1], true)
@test length(ci) == 1
@test collect(ci) == [[1, 1, 1]]
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ end
@testset "StrictlyIncreasingIndices" begin
include("Iteration/StrictlyIncreasingIndices.jl")
end
@testset "CartesianIterator" begin
include("Iteration/CartesianIterator.jl")
end
end
@testset "Timing" begin
include("Timing/timing.jl")
Expand Down
Loading