-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathcontainers.jl
554 lines (490 loc) · 15.3 KB
/
containers.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
const MatVecType{T} = Union{Matrix{T}, Vector{T}, SRow{T}}
const ContainerTypes = Union{MatVecType, Set, Dict, Tuple, NamedTuple}
function params_all_equal(params::MatVecType{<:TypeParams})
all(map(x -> isequal(first(params), x), params))
end
function type_params(obj::S) where {T, S <:MatVecType{T}}
if isempty(obj)
return TypeParams(S, TypeParams(T, nothing))
end
params = type_params.(obj)
@req params_all_equal(params) "Not all params of Vector or Matrix entries are the same, consider using a Tuple for serialization"
return TypeParams(S, params[1])
end
function has_empty_entries(obj::T) where T
return false
end
function has_empty_entries(obj::T) where T <: ContainerTypes
isempty(obj) && return true
any(has_empty_entries, obj) && return true
return false
end
function type_params(obj::S) where {T <: ContainerTypes, S <:MatVecType{T}}
isempty(obj) && return TypeParams(S, TypeParams(T, nothing))
# empty entries can inherit params from the rest of the collection
params = type_params.(filter(!has_empty_entries, obj))
@req params_all_equal(params) "Not all params of Vector or Matrix entries are the same, consider using a Tuple for serialization"
return TypeParams(S, params[1])
end
################################################################################
# loads to handle
function load_object(s::DeserializerState, T::Type{Vector{S}},
key::Union{Symbol, Int}) where S
load_node(s, key) do _
load_object(s, T)
end
end
################################################################################
# Saving and loading vectors
@register_serialization_type Vector
function load_type_params(s::DeserializerState, T::Type{<: MatVecType})
!haskey(s, :params) && return T, nothing
subtype, params = load_node(s, :params) do _
U = decode_type(s)
subtype, params = load_type_params(s, U)
end
return T{subtype}, params
end
function save_object(s::SerializerState, x::Vector)
save_data_array(s) do
for elem in x
if serialize_with_id(typeof(elem))
ref = save_as_ref(s, elem)
save_object(s, ref)
else
save_object(s, elem)
end
end
end
end
function load_object(s::DeserializerState, T::Type{<: Vector{params}}) where params
load_node(s) do v
if serialize_with_id(params)
loaded_v::Vector{params} = load_array_node(s) do _
load_ref(s)
end
else
isempty(s.obj) && return params[]
loaded_v = load_array_node(s) do obj
load_object(s, params)
end
end
return loaded_v
end
end
function load_object(s::DeserializerState, ::Type{Vector{T}}, params::S) where {T, S}
isempty(s.obj) && return T[]
v = load_array_node(s) do _
if serialize_with_id(T)
load_ref(s)
else
load_object(s, T, params)
end
end
return v
end
function load_object(s::DeserializerState, T::Type{Vector{U}}, ::Nothing) where U
isempty(s.obj) && return U[]
return load_array_node(s) do _
load_object(s, U)
end
end
################################################################################
# Saving and loading matrices
@register_serialization_type Matrix
function save_object(s::SerializerState, mat::Matrix)
m, n = size(mat)
save_data_array(s) do
for i in 1:m
save_object(s, [mat[i, j] for j in 1:n])
end
end
end
function load_object(s::DeserializerState, T::Type{<:Matrix{S}}) where S
load_node(s) do entries
if isempty(entries)
return T(undef, 0, 0)
end
len = length(entries)
m = reduce(vcat, [
permutedims(load_object(s, Vector{S}, i)) for i in 1:len
])
return T(m)
end
end
function load_object(s::DeserializerState, T::Type{<:Matrix{S}}, params::Ring) where S
load_node(s) do entries
if isempty(entries)
return T(undef, 0, 0)
end
len = length(entries)
m = reduce(vcat, [
permutedims(load_object(s, Vector{S}, params, i)) for i in 1:len
])
return T(m)
end
end
################################################################################
# Saving and loading Tuple
@register_serialization_type Tuple
function type_params(obj::T) where T <: Tuple
return TypeParams(T, type_params.(obj))
end
function save_type_params(s::SerializerState, tp::TypeParams{T}) where T <: Tuple
save_data_dict(s) do
save_object(s, encode_type(T), :name)
save_data_array(s, :params) do
for (i, param_tp) in enumerate(params(tp))
save_type_params(s, param_tp)
end
end
end
end
function load_type_params(s::DeserializerState, T::Type{Tuple})
subtype, params = load_node(s, :params) do _
tuple_params = load_array_node(s) do _
U = decode_type(s)
load_type_params(s, U)
end
return Tuple([x[1] for x in tuple_params]), Tuple(x[2] for x in tuple_params)
end
return T{subtype...}, params
end
function save_object(s::SerializerState, obj::Tuple)
save_data_array(s) do
for entry in obj
if serialize_with_id(typeof(entry))
ref = save_as_ref(s, entry)
save_object(s, ref)
else
save_object(s, entry)
end
end
end
end
function load_object(s::DeserializerState, T::Type{<:Tuple}, params::Tuple)
entries = load_array_node(s) do (i, entry)
S = fieldtype(T, i)
if serialize_with_id(S)
return load_ref(s)
else
return load_object(s, S, params[i])
end
end
return Tuple(entries)
end
function load_object(s::DeserializerState, T::Type{<:Tuple})
entries = load_array_node(s) do (i, entry)
S = fieldtype(T, i)
load_object(s, S)
end
return T(entries)
end
################################################################################
# Saving and loading NamedTuple
@register_serialization_type NamedTuple
function type_params(obj::T) where T <: NamedTuple
return TypeParams(
T,
NamedTuple(map(x -> x.first => type_params(x.second), collect(pairs(obj))))
)
end
# Named Tuples need to preserve order so they are handled seperate from Dict
function save_type_params(s::SerializerState, tp::TypeParams{<:NamedTuple})
T = type(tp)
save_data_dict(s) do
save_object(s, encode_type(T), :name)
save_data_dict(s, :params) do
save_data_array(s, :names) do
for name in keys(params(tp))
save_object(s, name)
end
end
save_data_array(s, :tuple_params) do
for (i, param_tp) in enumerate(values(params(tp)))
save_type_params(s, param_tp)
end
end
end
end
end
function load_type_params(s::DeserializerState, T::Type{NamedTuple})
subtype, params = load_node(s, :params) do obj
tuple_params = load_array_node(s, :tuple_params) do _
U = decode_type(s)
load_type_params(s, U)
end
tuple_types, named_tuple_params = collect(zip(tuple_params...))
names = load_object(s, Vector{Symbol}, :names)
return (tuple(names...), Tuple{tuple_types...}), named_tuple_params
end
return T{subtype...}, params
end
function save_object(s::SerializerState, obj::NamedTuple)
save_object(s, values(obj))
end
function load_object(s::DeserializerState, T::Type{<: NamedTuple}, params::Tuple)
return T(load_object(s, Tuple{fieldtypes(T)...}, params))
end
################################################################################
# Saving and loading dicts
@register_serialization_type Dict
function type_params(obj::T) where {S <: Union{Symbol, Int, String},
T <: Dict{S, Any}}
return TypeParams(
T,
:key_params => TypeParams(S, nothing),
map(x -> x.first => type_params(x.second), collect(pairs(obj)))...
)
end
function type_params(obj::T) where {U, S, T <: Dict{S, U}}
if isempty(obj)
return TypeParams(
T,
:key_params => TypeParams(S, nothing),
:value_params => TypeParams(U, nothing)
)
end
key_params = Oscar.type_params.(collect(keys(obj)))
@req params_all_equal(key_params) "Not all params of keys in $obj are the same"
value_params = type_params.(collect(values(obj)))
@req params_all_equal(value_params) "Not all params of values in $obj are the same"
return TypeParams(
T,
:key_params => first(key_params),
:value_params => first(value_params)
)
end
function save_type_params(
s::SerializerState,
tp::TypeParams{Dict{S, T}, <:Tuple{Vararg{Pair}}}) where {T, S <: Union{Symbol, Int, String}}
save_data_dict(s) do
save_object(s, encode_type(Dict), :name)
save_data_dict(s, :params) do
isempty(params(tp)) && save_object(s, encode_type(T), :value_params)
for (k, param_tp) in params(tp)
save_type_params(s, param_tp, Symbol(k))
end
end
end
end
function load_type_params(s::DeserializerState, T::Type{Dict})
subtype, params = load_node(s, :params) do obj
if haskey(s, :value_params)
S, key_params = load_node(s, :key_params) do params
params isa String && return decode_type(s), nothing
load_type_params(s, decode_type(s))
end
U, value_params = load_node(s, :value_params) do _
load_type_params(s, decode_type(s))
end
isnothing(key_params) && return (S, U), value_params
isnothing(key_params) && isnothing(value_params) && return (S, U), nothing
return (S, U), Dict(:key_params => key_params, :value_params => value_params)
else
S, key_params = load_node(s, :key_params) do _
decode_type(s), nothing
end
params_dict = Dict{S, Any}()
value_types = Type[]
for (k, _) in obj
k == :key_params && continue
key = S <: Integer ? parse(Int, string(k)) : S(k)
params_dict[key] = load_node(s, k) do _
value_type = decode_type(s)
return load_type_params(s, value_type)
end
push!(value_types, params_dict[key][1])
end
params_dict = isempty(params_dict) ? nothing : params_dict
return (S, Union{value_types...}), params_dict
end
end
return Dict{subtype...}, params
end
function save_object(s::SerializerState, obj::Dict{S, T}) where {S <: Union{Symbol, String, Int}, T}
save_data_dict(s) do
for (k, v) in obj
if !Base.issingletontype(typeof(v))
save_object(s, v, Symbol(k))
end
end
end
end
function save_object(s::SerializerState, obj::Dict{S, T}) where {S, T}
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, Any}},
params::Dict{S, Any}) where {S <: Union{Symbol, String, Int}}
dict = T()
for k in keys(params)
# has no data, hence no key was generated on the data side
if Base.issingletontype(params[k][1])
dict[k] = params[k][1]()
else
dict[k] = load_object(s, params[k]..., Symbol(k))
end
end
return dict
end
function load_object(s::DeserializerState,
T::Type{<:Dict{S, U}}) where {S <: Union{Symbol, String}, U}
dict = T()
for k in keys(s.obj)
dict[S(k)] = load_object(s, U, Symbol(k))
end
return dict
end
function load_object(s::DeserializerState,
T::Type{<:Dict{S, U}}, ::Nothing) where {S <: Union{Symbol, String}, U}
dict = T()
for k in keys(s.obj)
dict[S(k)] = load_object(s, U, Symbol(k))
end
return dict
end
function load_object(s::DeserializerState,
T::Type{<:Dict{Int, Int}})
dict = T()
for k in keys(s.obj)
dict[parse(Int, string(k))] = load_object(s, Int, k)
end
return dict
end
function load_object(s::DeserializerState,
T::Type{<:Dict{Int, S}}, params::Nothing) where S
dict = T()
for k in keys(s.obj)
dict[parse(Int, string(k))] = load_object(s, S, k)
end
return dict
end
# here to handle ambiguities
function load_object(s::DeserializerState, T::Type{Dict{Int, Int}}, key::Union{Symbol, Int})
load_node(s, key) do _
load_object(s, T)
end
end
# here to handle ambiguities
function load_object(s::DeserializerState, T::Type{Dict{String, Int}}, key::Union{Symbol, Int})
load_node(s, key) do _
load_object(s, T)
end
end
# here to handle ambiguities
function load_object(s::DeserializerState, T::Type{Dict{Symbol, Int}}, key::Union{Symbol, Int})
load_node(s, key) do _
load_object(s, T)
end
end
function load_object(s::DeserializerState,
T::Type{<:Dict{S, U}},
params::Any) where {S, U}
if params isa Dict
if haskey(params, :value_params)
pairs = load_array_node(s) do _
load_object(s, Tuple{S, U}, (params[:key_params], params[:value_params]))
end
return T(k => v for (k, v) in pairs)
else
dict = Dict{S, Any}()
value_types = Type[]
for k in keys(s.obj)
key = S <: Integer ? parse(S, string(k)) : S(k)
value_type, param = params[key]
v = load_object(s, value_type, param, k)
dict[key] = load_object(s, value_type, param, k)
push!(value_types, typeof(v))
end
isempty(value_types) && return T()
value_params = type_params.(collect(values(dict)))
value_type = params_all_equal(value_params) ? typejoin(unique(value_types)...) : Any
return Dict{S, value_type}(dict)
end
else
dict = Dict{S, Any}()
value_types = Type[]
for k in keys(s.obj)
v = load_object(s, U, params, k)
key = S <: Integer ? parse(S, string(k)) : S(k)
dict[key] = v
push!(value_types, typeof(v))
end
isempty(value_types) && return T()
value_params = type_params.(collect(values(dict)))
value_type = params_all_equal(value_params) ? typejoin(unique(value_types)...) : Any
return Dict{S, value_type}(dict)
end
end
################################################################################
# Saving and loading sets
@register_serialization_type Set
function type_params(obj::T) where {S, T <: Set{S}}
if isempty(obj)
return TypeParams(T, TypeParams(S, nothing))
end
return TypeParams(T, type_params(first(obj)))
end
function load_type_params(s::DeserializerState, T::Type{<: Set})
!haskey(s, :params) && return T, nothing
subtype, params = load_node(s, :params) do _
U = decode_type(s)
subtype, params = load_type_params(s, U)
end
return T{subtype}, params
end
function save_object(s::SerializerState, x::Set)
save_data_array(s) do
for elem in x
if serialize_with_id(typeof(elem))
ref = save_as_ref(s, elem)
save_object(s, ref)
else
save_object(s, elem)
end
end
end
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 S(elems)
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 S(elems)
end
function load_object(s::DeserializerState, S::Type{<:Set{T}}) where T
elems = load_array_node(s) do _
load_object(s, T)
end
return S(elems)
end
################################################################################
# Sparse rows
@register_serialization_type SRow
function save_object(s::SerializerState, obj::SRow)
save_data_array(s) do
for (i, v) in obj
save_object(s, (i, v))
end
end
end
function load_object(s::DeserializerState, ::Type{<:SRow}, params::Ring)
pos = Int[]
entry_type = elem_type(params)
values = entry_type[]
load_array_node(s) do _
push!(pos, load_object(s, Int, 1))
push!(values, load_object(s, entry_type, params, 2))
end
return sparse_row(params, pos, values)
end