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

Implement iteration over GAP iterators #967

Merged
merged 1 commit into from
Sep 30, 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
5 changes: 5 additions & 0 deletions src/adapter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ function Base.iterate(obj::GapObj)
# we can still allow iteration util some large bound
iterate(obj, (1, typemax(Int)))
end
elseif Wrappers.IsIterator(obj)
iterate(obj, Wrappers.ShallowCopy(obj))
elseif Wrappers.IsCollection(obj)
iterate(obj, Wrappers.Iterator(obj)::GapObj)
else
Expand All @@ -506,6 +508,9 @@ function Base.iterate(obj::GapObj, iter::GapObj)
end
end

Base.IteratorEltype(::Type{GapObj}) = Base.EltypeUnknown()
Base.IteratorSize(::Type{GapObj}) = Base.SizeUnknown()

# copy and deepcopy:
# The following is just a preliminary solution,
# in order to avoid Julia crashes when one calls `deepcopy` for a `GapObj`.
Expand Down
2 changes: 1 addition & 1 deletion src/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ function (::Type{T})(obj::GapObj) where {T<:UnitRange}
result = obj[1]:obj[len]
end

return convert(T, result)
return convert(T, result)::T
end


Expand Down
1 change: 1 addition & 0 deletions src/wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import GAP: @wrap
@wrap IsCollection(x::Any)::Bool
@wrap IsDoneIterator(x::Any)::Bool
@wrap IsEmpty(x::Any)::Bool
@wrap IsIterator(x::Any)::Bool
@wrap IsList(x::Any)::Bool
@wrap IsMatrixObj(x::Any)::Bool
@wrap IsPackageLoaded(x::GapObj)::Bool
Expand Down
14 changes: 13 additions & 1 deletion test/adapter.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
@testset "iteration" begin
# iterating over a GAP plain list, which uses index based iteration
l = GAP.evalstr("[1, 2, 3]")
lj = collect(l)
@test lj isa Vector{Any}
@test lj isa Vector{Int}
@test lj == [1, 2, 3]
lj = collect(Int, l)
@test lj isa Vector{Int}
@test lj == [1, 2, 3]

# iterating over a GAP object (here, a group) which creates a GAP Iterator object
s = GAP.Globals.SymmetricGroup(3)
xs = []
for x in s
push!(xs, x)
end
@test length(xs) == 6

# iterating over a GAP iterator
gap_iter = GAP.Globals.IteratorOfCombinations(GAP.Obj([1,1,1]))
# iterate twice to verify we don't "eat up" the GAP iterator
# and don't otherwise change its state
for i in 1:2
collect(gap_iter)
vs = Vector{Int}.(gap_iter)
@test vs == [[], [1], [1, 1], [1, 1, 1]]
end
end

@testset "deepcopy" begin
Expand Down
Loading