From efddb9ef4e0562832bf06b2848cfee7832452485 Mon Sep 17 00:00:00 2001 From: Sebastian Gutsche Date: Fri, 23 Nov 2018 12:00:52 +0100 Subject: [PATCH 1/6] Conversion of lists with holes --- LibGAP.jl/src/ccalls.jl | 8 ++++++++ LibGAP.jl/src/gap_to_julia.jl | 28 ++++++++++++++++++++++++++-- LibGAP.jl/test/conversion.jl | 9 +++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/LibGAP.jl/src/ccalls.jl b/LibGAP.jl/src/ccalls.jl index 9045f33ca..0138955ce 100644 --- a/LibGAP.jl/src/ccalls.jl +++ b/LibGAP.jl/src/ccalls.jl @@ -89,6 +89,14 @@ function CharWithValue(x::Cuchar) return o end +function ElmList(x::MPtr,position) + o = ccall( :GAP_ElmList, + Ptr{Cvoid}, + (MPtr,Culong), + x,Culong(position)) + return GET_FROM_GAP(o) +end + """ (func::MPtr)(args...) diff --git a/LibGAP.jl/src/gap_to_julia.jl b/LibGAP.jl/src/gap_to_julia.jl index 5f8c793b3..d969db248 100644 --- a/LibGAP.jl/src/gap_to_julia.jl +++ b/LibGAP.jl/src/gap_to_julia.jl @@ -13,6 +13,7 @@ of converted objects and should never be given by the user. gap_to_julia(::Type{GAPInputType},x::GAPInputType) = x gap_to_julia(::Type{Any}, x::GAPInputType) = gap_to_julia(x) gap_to_julia(::Type{Any}, x::Any ) = x +gap_to_julia(::Any, x::Nothing ) = nothing ## Integers gap_to_julia(::Type{Int128} ,x::Int64) = trunc(Int128 ,x) @@ -125,7 +126,7 @@ function gap_to_julia( ::Type{Array{Obj,1}}, obj :: MPtr , recursion_dict = IdDi new_array = Array{Any,1}( undef, len_list) recursion_dict[obj] = new_array for i in 1:len_list - current_obj = obj[i] + current_obj = ElmList(obj,i) if haskey(recursion_dict,current_obj) new_array[ i ] = recursion_dict[current_obj] else @@ -147,7 +148,30 @@ function gap_to_julia( ::Type{Array{T,1}}, obj :: MPtr, recursion_dict = IdDict( new_array = Array{T,1}( undef, len_list) recursion_dict[obj] = new_array for i in 1:len_list - current_obj = obj[ i ] + current_obj = ElmList(obj,i) + if haskey(recursion_dict,current_obj) + new_array[ i ] = recursion_dict[current_obj] + else + new_array[ i ] = gap_to_julia(T,current_obj,recursion_dict) + recursion_dict[ current_obj ] = new_array[ i ] + end + end + return new_array +end + +## Special case for conversion of lists with holes +function gap_to_julia( ::Type{Array{Union{Nothing,T},1}}, obj :: MPtr, recursion_dict = IdDict() ) where T + if ! Globals.IsList( obj ) + throw(ArgumentError(" is not a list")) + end + if haskey(recursion_dict,obj) + return recursion_dict[obj] + end + len_list = length(obj) + new_array = Array{Union{Nothing,T},1}( undef, len_list) + recursion_dict[obj] = new_array + for i in 1:len_list + current_obj = ElmList(obj,i) if haskey(recursion_dict,current_obj) new_array[ i ] = recursion_dict[current_obj] else diff --git a/LibGAP.jl/test/conversion.jl b/LibGAP.jl/test/conversion.jl index dd7938ad8..8c7179931 100644 --- a/LibGAP.jl/test/conversion.jl +++ b/LibGAP.jl/test/conversion.jl @@ -112,6 +112,15 @@ xx = GAP.julia_to_gap( "a" ) @test_throws MethodError GAP.gap_to_julia( Dict{Int64,Int64}, xx ) + ##Test nothing + xx = GAP.EvalString( "[1,,1]" ) + @test GAP.gap_to_julia(xx) == Any[1,nothing,1] + @test GAP.gap_to_julia(Array{Any,1},xx) == Any[1,nothing,1] + @test_throws MethodError GAP.gap_to_julia(Array{Int64,1},xx) + @test GAP.gap_to_julia(Array{Union{Nothing,Int64},1},xx) == Union{Nothing,Int64}[1,nothing,1] + @test GAP.gap_to_julia(Array{Union{Int64,Nothing},1},xx) == Union{Nothing,Int64}[1,nothing,1] + + end @testset "conversion to GAP" begin From 4625fb71ec47324aa71668cf48a6bb107dddf836 Mon Sep 17 00:00:00 2001 From: Sebastian Gutsche Date: Fri, 23 Nov 2018 19:50:43 +0100 Subject: [PATCH 2/6] Handle nothing specially in julia array conversion --- LibGAP.jl/src/julia_to_gap.jl | 3 +++ LibGAP.jl/test/conversion.jl | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/LibGAP.jl/src/julia_to_gap.jl b/LibGAP.jl/src/julia_to_gap.jl index 77da4dc0d..252a46108 100644 --- a/LibGAP.jl/src/julia_to_gap.jl +++ b/LibGAP.jl/src/julia_to_gap.jl @@ -72,6 +72,9 @@ function julia_to_gap(obj::Array{T,1}, recursive::Val{Recursive}=Val(false), rec end for i in 1:len x = obj[i] + if x == nothing + continue + end if Recursive #= # It would be much nicer to write this, but we cannot in Julia 1.x, see diff --git a/LibGAP.jl/test/conversion.jl b/LibGAP.jl/test/conversion.jl index 8c7179931..07d158bba 100644 --- a/LibGAP.jl/test/conversion.jl +++ b/LibGAP.jl/test/conversion.jl @@ -222,4 +222,8 @@ end conv = GAP.julia_to_gap(d, Val(true)); @test conv === conv.b + ##Test nothing + xx = GAP.EvalString( "[1,,1]" ) + @test GAP.julia_to_gap([1,nothing,1]) == xx + end From fa8a36b61b29f27b5a0e63349c908945b2a813b2 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sat, 24 Nov 2018 15:52:05 +0100 Subject: [PATCH 3/6] Spacing in comments Co-Authored-By: sebasguts --- LibGAP.jl/test/conversion.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LibGAP.jl/test/conversion.jl b/LibGAP.jl/test/conversion.jl index 07d158bba..c623e39a6 100644 --- a/LibGAP.jl/test/conversion.jl +++ b/LibGAP.jl/test/conversion.jl @@ -112,7 +112,7 @@ xx = GAP.julia_to_gap( "a" ) @test_throws MethodError GAP.gap_to_julia( Dict{Int64,Int64}, xx ) - ##Test nothing + ## Test nothing xx = GAP.EvalString( "[1,,1]" ) @test GAP.gap_to_julia(xx) == Any[1,nothing,1] @test GAP.gap_to_julia(Array{Any,1},xx) == Any[1,nothing,1] @@ -222,7 +222,7 @@ end conv = GAP.julia_to_gap(d, Val(true)); @test conv === conv.b - ##Test nothing + ## Test nothing xx = GAP.EvalString( "[1,,1]" ) @test GAP.julia_to_gap([1,nothing,1]) == xx From 9d1e73247a9f080c3905b94bef40aac51c6d876d Mon Sep 17 00:00:00 2001 From: Sebastian Gutsche Date: Tue, 27 Nov 2018 10:23:10 +0100 Subject: [PATCH 4/6] Changed default IsList conversion to return Array{Union{Any,Nothing},1} --- LibGAP.jl/src/gap_to_julia.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LibGAP.jl/src/gap_to_julia.jl b/LibGAP.jl/src/gap_to_julia.jl index d969db248..cc831b758 100644 --- a/LibGAP.jl/src/gap_to_julia.jl +++ b/LibGAP.jl/src/gap_to_julia.jl @@ -239,7 +239,7 @@ function gap_to_julia(x::MPtr) elseif Globals.IsString(x) return gap_to_julia(AbstractString,x) elseif Globals.IsList(x) - return gap_to_julia(Array{Any,1},x) + return gap_to_julia(Array{Union{Any,Nothing},1},x) elseif Globals.IsRecord(x) return gap_to_julia(Dict{Symbol,Any},x) end From 719ad75e5da0ce5c99a24d22038593512b917762 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 28 Nov 2018 22:56:28 +0100 Subject: [PATCH 5/6] Update comment in LibGAP.jl/src/gap_to_julia.jl Co-Authored-By: sebasguts --- LibGAP.jl/src/gap_to_julia.jl | 4 ++-- LibGAP.jl/test/conversion.jl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/LibGAP.jl/src/gap_to_julia.jl b/LibGAP.jl/src/gap_to_julia.jl index cc831b758..e9fead5d9 100644 --- a/LibGAP.jl/src/gap_to_julia.jl +++ b/LibGAP.jl/src/gap_to_julia.jl @@ -126,7 +126,7 @@ function gap_to_julia( ::Type{Array{Obj,1}}, obj :: MPtr , recursion_dict = IdDi new_array = Array{Any,1}( undef, len_list) recursion_dict[obj] = new_array for i in 1:len_list - current_obj = ElmList(obj,i) + current_obj = ElmList(obj,i) # returns 'nothing' for holes in the list if haskey(recursion_dict,current_obj) new_array[ i ] = recursion_dict[current_obj] else @@ -159,7 +159,7 @@ function gap_to_julia( ::Type{Array{T,1}}, obj :: MPtr, recursion_dict = IdDict( return new_array end -## Special case for conversion of lists with holes +## Special case for conversion of lists with holes; these are converted into 'nothing' function gap_to_julia( ::Type{Array{Union{Nothing,T},1}}, obj :: MPtr, recursion_dict = IdDict() ) where T if ! Globals.IsList( obj ) throw(ArgumentError(" is not a list")) diff --git a/LibGAP.jl/test/conversion.jl b/LibGAP.jl/test/conversion.jl index c623e39a6..215d26167 100644 --- a/LibGAP.jl/test/conversion.jl +++ b/LibGAP.jl/test/conversion.jl @@ -112,7 +112,7 @@ xx = GAP.julia_to_gap( "a" ) @test_throws MethodError GAP.gap_to_julia( Dict{Int64,Int64}, xx ) - ## Test nothing + ## Test converting GAP lists with holes in them xx = GAP.EvalString( "[1,,1]" ) @test GAP.gap_to_julia(xx) == Any[1,nothing,1] @test GAP.gap_to_julia(Array{Any,1},xx) == Any[1,nothing,1] @@ -222,7 +222,7 @@ end conv = GAP.julia_to_gap(d, Val(true)); @test conv === conv.b - ## Test nothing + ## Test converting lists with 'nothing' in them -> should be converted to a hole in the list xx = GAP.EvalString( "[1,,1]" ) @test GAP.julia_to_gap([1,nothing,1]) == xx From dc2fe11a3c1c7354e3cf75b6409085ea427a0c72 Mon Sep 17 00:00:00 2001 From: Sebastian Gutsche Date: Thu, 29 Nov 2018 09:57:41 +0100 Subject: [PATCH 6/6] Fixed nothing return in gap_to_julia --- LibGAP.jl/src/gap_to_julia.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/LibGAP.jl/src/gap_to_julia.jl b/LibGAP.jl/src/gap_to_julia.jl index e9fead5d9..e26918f7d 100644 --- a/LibGAP.jl/src/gap_to_julia.jl +++ b/LibGAP.jl/src/gap_to_julia.jl @@ -14,6 +14,7 @@ gap_to_julia(::Type{GAPInputType},x::GAPInputType) = x gap_to_julia(::Type{Any}, x::GAPInputType) = gap_to_julia(x) gap_to_julia(::Type{Any}, x::Any ) = x gap_to_julia(::Any, x::Nothing ) = nothing +gap_to_julia(::Type{Any}, x::Nothing ) = nothing ## Integers gap_to_julia(::Type{Int128} ,x::Int64) = trunc(Int128 ,x)