Skip to content

Commit

Permalink
containers completed, need some upgrades though
Browse files Browse the repository at this point in the history
  • Loading branch information
antonydellavecchia committed Nov 19, 2024
1 parent f61ecd1 commit 66a5f2e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 111 deletions.
46 changes: 23 additions & 23 deletions src/Serialization/Rings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ function load_object(s::DeserializerState, ::Type{ZZModRing})
end

#elements
@register_serialization_type zzModRingElem uses_params
@register_serialization_type ZZModRingElem uses_params
@register_serialization_type zzModRingElem
@register_serialization_type ZZModRingElem

function save_object(s::SerializerState, x::ModRingElemUnion)
save_data_basic(s, string(x))
Expand All @@ -86,11 +86,11 @@ end
################################################################################
# Polynomial Rings

@register_serialization_type PolyRing uses_id uses_params
@register_serialization_type MPolyRing uses_id uses_params
@register_serialization_type UniversalPolyRing uses_id uses_params
@register_serialization_type MPolyDecRing uses_id uses_params
@register_serialization_type AbstractAlgebra.Generic.LaurentMPolyWrapRing uses_id uses_params
@register_serialization_type PolyRing uses_id
@register_serialization_type MPolyRing uses_id
@register_serialization_type UniversalPolyRing uses_id
@register_serialization_type MPolyDecRing uses_id
@register_serialization_type AbstractAlgebra.Generic.LaurentMPolyWrapRing uses_id

function save_object(s::SerializerState, R::PolyRingUnionType)
base = base_ring(R)
Expand Down Expand Up @@ -135,10 +135,10 @@ end

################################################################################
# Polynomial Ring Elem Types
@register_serialization_type MPolyRingElem uses_params
@register_serialization_type MPolyDecRingElem uses_params
@register_serialization_type UniversalPolyRingElem uses_params
@register_serialization_type AbstractAlgebra.Generic.LaurentMPolyWrap uses_params
@register_serialization_type MPolyRingElem
@register_serialization_type MPolyDecRingElem
@register_serialization_type UniversalPolyRingElem
@register_serialization_type AbstractAlgebra.Generic.LaurentMPolyWrap

# elements
function save_object(s::SerializerState, p::Union{UniversalPolyRingElem, MPolyRingElem})
Expand Down Expand Up @@ -171,7 +171,7 @@ end
################################################################################
# Univariate Polynomials

@register_serialization_type PolyRingElem uses_params
@register_serialization_type PolyRingElem

function save_object(s::SerializerState, p::PolyRingElem)
coeffs = coefficients(p)
Expand Down Expand Up @@ -251,8 +251,8 @@ end
################################################################################
# Polynomial Ideals

@register_serialization_type MPolyIdeal uses_params
@register_serialization_type LaurentMPolyIdeal uses_params
@register_serialization_type MPolyIdeal
@register_serialization_type LaurentMPolyIdeal

function save_object(s::SerializerState, I::T) where T <: IdealOrdUnionType
save_object(s, gens(I))
Expand All @@ -271,7 +271,7 @@ end

# this will need adjustments to cover the NCRing case

@register_serialization_type IdealGens uses_params
@register_serialization_type IdealGens

function save_object(s::SerializerState, obj::IdealGens)
save_data_dict(s) do
Expand All @@ -298,9 +298,9 @@ end
################################################################################
# Matrices
@register_serialization_type MatSpace uses_id
@register_serialization_type MatElem uses_params
@register_serialization_type MatElem
@register_serialization_type SMatSpace uses_id
@register_serialization_type SMat uses_params
@register_serialization_type SMat

function save_object(s::SerializerState, obj::MatSpace)
save_data_dict(s) do
Expand Down Expand Up @@ -420,8 +420,8 @@ function load_object(s::DeserializerState, ::Type{<: SeriesRing}, base_ring::Rin
end

# elements
@register_serialization_type RelPowerSeriesRingElem uses_params
@register_serialization_type AbsPowerSeriesRingElem uses_params
@register_serialization_type RelPowerSeriesRingElem
@register_serialization_type AbsPowerSeriesRingElem

function save_object(s::SerializerState, r::RelPowerSeriesRingElem)
v = valuation(r)
Expand Down Expand Up @@ -528,9 +528,9 @@ function load_object(s::DeserializerState, ::Type{<: LaurentUnionType}, base_rin
end

# elements
@register_serialization_type Generic.LaurentSeriesFieldElem "LaurentSeriesFieldElem" uses_params
@register_serialization_type Generic.LaurentSeriesRingElem "LaurentSeriesRingElem" uses_params
@register_serialization_type ZZLaurentSeriesRingElem uses_params
@register_serialization_type Generic.LaurentSeriesFieldElem "LaurentSeriesFieldElem"
@register_serialization_type Generic.LaurentSeriesRingElem "LaurentSeriesRingElem"
@register_serialization_type ZZLaurentSeriesRingElem

function save_object(s::SerializerState, r:: Union{Generic.LaurentSeriesElem, ZZLaurentSeriesRingElem})
v = valuation(r)
Expand Down Expand Up @@ -612,7 +612,7 @@ function load_object(s::DeserializerState, ::Type{MPolyQuoRing})
end

### Serialization of Monomial orderings
@register_serialization_type MonomialOrdering uses_params
@register_serialization_type MonomialOrdering

function save_object(s::SerializerState, o::MonomialOrdering)
save_data_dict(s) do
Expand Down
9 changes: 7 additions & 2 deletions src/Serialization/basic_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ function save_object(s::SerializerState, x::T) where T <: Union{BasicTypeUnion,
end

load_object(s::DeserializerState, T::Type, ::Nothing) = load_object(s, T)

################################################################################
# Bool
@register_serialization_type Bool
Expand All @@ -23,7 +24,9 @@ end
# ZZRingElem
@register_serialization_type ZZRingElem

function load_object(s::DeserializerState, ::Type{ZZRingElem}, ::ZZRing)
load_object(s::DeserializerState, T::Type{ZZRingElem}, ::ZZRing) = load_object(s, T)

function load_object(s::DeserializerState, ::Type{ZZRingElem})
load_node(s) do str
return ZZRingElem(str)
end
Expand All @@ -33,7 +36,9 @@ end
# QQFieldElem
@register_serialization_type QQFieldElem

function load_object(s::DeserializerState, ::Type{QQFieldElem}, ::QQField)
load_object(s::DeserializerState, T::Type{QQFieldElem}, ::QQField) = load_object(s, T)

function load_object(s::DeserializerState, ::Type{QQFieldElem})
# TODO: simplify the code below once https://github.com/Nemocas/Nemo.jl/pull/1375
# is merged and in a Nemo release
load_node(s) do q
Expand Down
109 changes: 29 additions & 80 deletions src/Serialization/containers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ end

################################################################################
# Saving and loading matrices
@register_serialization_type Matrix uses_params
@register_serialization_type Matrix

function save_object(s::SerializerState, mat::Matrix)
m, n = size(mat)
Expand All @@ -81,30 +81,30 @@ function save_object(s::SerializerState, mat::Matrix)
end
end

function load_object(s::DeserializerState, ::Type{<:Matrix}, params::Type)
function load_object(s::DeserializerState, T::Type{<:Matrix{S}}) where S
load_node(s) do entries
if isempty(entries)
return Matrix{params}(undef, 0, 0)
return T(undef, 0, 0)
end
len = length(entries)
m = reduce(vcat, [
permutedims(load_object(s, Vector, params, i)) for i in 1:len
permutedims(load_object(s, Vector{S}, i)) for i in 1:len
])
return Matrix{params}(m)
return T(m)
end
end

function load_object(s::DeserializerState, ::Type{<:Matrix}, params::Tuple)
function load_object(s::DeserializerState, T::Type{<:Matrix{S}}, params::Ring) where S
load_node(s) do entries
if isempty(entries)
return Matrix{params[1]}(undef, 0, 0)
return T(undef, 0, 0)
end

len = length(entries)
m = reduce(vcat, [
permutedims(load_object(s, Vector, params, i)) for i in 1:len
permutedims(load_object(s, Vector{S}, params, i)) for i in 1:len
])
return Matrix{params[1]}(m)
return Matrix{elem_type(params)}(m)
end
end

Expand Down Expand Up @@ -177,21 +177,13 @@ function save_object(s::SerializerState, obj::Dict{S, T}) where {S <: Union{Symb
end
end

function save_object(s::SerializerState, obj::Dict)
save_data_array(s) do
for (k, v) in obj
save_object(s, (k, v))
end
end
end

function load_object(s::DeserializerState, ::Type{Dict{String, Int}})
return Dict{String, Int}(string(k) => parse(Int, v) for (k,v) in s.obj)
end

function load_object(s::DeserializerState, ::Type{Dict{Int, Int}})
return Dict{Int, Int}(parse(Int,string(k)) => parse(Int, v) for (k,v) in s.obj)
end
#function save_object(s::SerializerState, obj::Dict)
# save_data_array(s) do
# for (k, v) in obj
# save_object(s, (k, v))
# end
# end
#end

function load_object(s::DeserializerState,
T::Type{<:Dict{S, U}},
Expand All @@ -205,26 +197,14 @@ end

################################################################################
# Saving and loading sets
@register_serialization_type Set uses_params
@register_serialization_type Set

function save_type_params(s::SerializerState, obj::Set{T}) where T
save_data_dict(s) do
save_object(s, encode_type(Set), :name)
if serialize_with_params(T) && !isempty(obj)
save_type_params(s, first(obj), :params)
else
save_object(s, encode_type(T), :params)
end
function type_params(obj::T) where T <: Set
if isempty(obj)
return T, nothing
end
end

function load_type_params(s::DeserializerState, ::Type{<:Set})
T = decode_type(s)
if serialize_with_params(T) && haskey(s, :params)
params = load_params_node(s)
return (T, params)
end
return T
return T, type_params(first(obj))
end

function save_object(s::SerializerState, x::Set)
Expand All @@ -240,49 +220,18 @@ function save_object(s::SerializerState, x::Set)
end
end

function load_object(s::DeserializerState, ::Type{<: Set}, params::Any)
load_node(s) do v
if serialize_with_id(params)
loaded_v = params[load_ref(s, x) for x in v]
else
loaded_v = params[]
for (i, entry) in enumerate(v)
push!(loaded_v, load_object(s, params, i))
end
end
return Set(loaded_v)
end
end

# handles nested
function load_object(s::DeserializerState, ::Type{<: Set}, params::Tuple)
T = params[1]
load_node(s) do v
if isempty(v)
return Set{T}()
else
loaded_v = Set{T}()
len = length(v)
for i in 1:len
load_node(s, i) do _
push!(loaded_v, load_object(s, T, params[2]))
end
end
return Set{typeof(first(loaded_v))}(loaded_v)
end
function load_object(s::DeserializerState, S::Type{<:Set{T}}, params::Any) where T
elems = load_array_node(s) do _
load_object(s, T, params)
end
return Set(elems)
end

function load_object(s::DeserializerState, ::Type{<: Set}, params::Ring)
T = elem_type(params)
loaded_entries = load_array_node(s) do _
if serialize_with_params(T)
return load_object(s, T, params)
else
return load_object(s, T)
end
function load_object(s::DeserializerState, S::Type{<:Set{T}}, ::Nothing) where T
elems = load_array_node(s) do _
load_object(s, T, nothing)
end
return Set{T}(loaded_entries)
return Set(elems)
end

################################################################################
Expand Down
32 changes: 30 additions & 2 deletions src/Serialization/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,34 @@ function save_type_params(s::SerializerState, T::Type{<:Dict{S, U}}, params::Vec
end
end

function save_type_params(s::SerializerState, T::Type,
params::Vector{<:Pair{Symbol, S}}) where S
save_data_dict(s) do
save_object(s, encode_type(T), :name)
save_data_dict(s, :params) do
for param in params
save_type_params(s, param.second..., Symbol(param.first))
end
end
end
end

#function save_type_params(s::SerializerState, T::Type{<:Dict{S, U}}, params::Vector{<:Pair}) where {S, U}
# save_data_dict(s) do
# save_object(s, encode_type(T), :name)
# save_data_dict(s, :params) do
# save_object(s, encode_type(S), :key_type)
# isempty(params) && save_object(s, encode_type(U), :value_type)
# save_data_array(s, :param_pairs) do
# for param in params
# println(param.first)
# println(param.second)
# end
# end
# end
# end
#end

function load_type_params(s::DeserializerState, key::Symbol)
load_node(s, key) do _
load_type_params(s)
Expand Down Expand Up @@ -311,7 +339,7 @@ function load_type_params(s::DeserializerState)
load_type_params(s)
end
end
if T <: MatVecType
if T <: Union{MatVecType, Set}
return T{subtype}, params
elseif T <: Union{Tuple, NamedTuple}
return T{subtype...}, params
Expand Down Expand Up @@ -766,7 +794,7 @@ function load(io::IO; params::Any = nothing, type::Any = nothing,
jsondict_str = JSON3.write(jsondict)
s = deserializer_open(IOBuffer(jsondict_str),
serializer,
with_attrs)
with_attrs)
end

try
Expand Down
4 changes: 2 additions & 2 deletions test/Serialization/PolynomialsSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ cases = [
end
end

@test_skip @testset "Graded Ring" begin
@testset "Graded Ring" begin
R, (x, y) = QQ[:x, :y]
A = [1 3; 2 1]
M, (m1, m2) = grade(R, A)
Expand Down Expand Up @@ -91,7 +91,7 @@ cases = [
end

if R isa MPolyRing{T} where T <: Union{QQFieldElem, ZZRingElem, zzModRingElem}
@test_skip @testset "MPoly Ideals over $(case[4])" begin
@testset "MPoly Ideals over $(case[4])" begin
q = z
i = Oscar.ideal(R, [p, q])
test_save_load_roundtrip(path, i) do loaded_i
Expand Down
Loading

0 comments on commit 66a5f2e

Please sign in to comment.