Skip to content

Commit c33c82f

Browse files
committed
better NamedTuple code
1 parent deca298 commit c33c82f

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

src/FileFormats/MOF/write.jl

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,21 @@ function write_nlpblock(
9595
objective = MOI.objective_expr(nlp_block.evaluator)
9696
objective = _lift_variable_indices(objective)
9797
sense = MOI.get(model, MOI.ObjectiveSense())
98-
object["objective"] = NamedTuple{(:sense, :function)}((
99-
moi_to_object(sense),
100-
moi_to_object(Nonlinear(objective), name_map),
101-
))
98+
object["objective"] = (;
99+
:sense => moi_to_object(sense),
100+
:function => moi_to_object(Nonlinear(objective), name_map),
101+
)
102102
end
103103
for (row, bounds) in enumerate(nlp_block.constraint_bounds)
104104
constraint = MOI.constraint_expr(nlp_block.evaluator, row)
105105
(func, set) = extract_function_and_set(constraint)
106106
func = _lift_variable_indices(func)
107107
push!(
108108
object["constraints"],
109-
NamedTuple{(:function, :set)}((
110-
moi_to_object(Nonlinear(func), name_map),
111-
moi_to_object(set, name_map),
112-
)),
109+
(;
110+
:function => moi_to_object(Nonlinear(func), name_map),
111+
:set => moi_to_object(set, name_map),
112+
),
113113
)
114114
end
115115
return
@@ -124,15 +124,14 @@ function write_objective(
124124
return # Objective must have been written from NLPBlock.
125125
end
126126
sense = MOI.get(model, MOI.ObjectiveSense())
127-
keys = Symbol[:sense]
128-
values = Any[moi_to_object(sense)]
129127
if sense != MOI.FEASIBILITY_SENSE
130128
F = MOI.get(model, MOI.ObjectiveFunctionType())
131129
objective_function = MOI.get(model, MOI.ObjectiveFunction{F}())
132-
push!(keys, :function)
133-
push!(values, moi_to_object(objective_function, name_map))
130+
object["objective"] = (;
131+
:sense => moi_to_object(sense),
132+
:function => moi_to_object(objective_function, name_map),
133+
)
134134
end
135-
object["objective"] = NamedTuple{Tuple(keys)}(values)
136135
return
137136
end
138137

@@ -177,28 +176,22 @@ function moi_to_object(
177176
set = MOI.get(model, MOI.ConstraintSet(), index)
178177
dual_start = MOI.get(model, MOI.ConstraintDualStart(), index)
179178
primal_start = MOI.get(model, MOI.ConstraintPrimalStart(), index)
180-
keys = Symbol[]
181-
values = Any[]
179+
pairs = Pair{Symbol,Any}[]
182180
if F != MOI.VariableIndex
183181
name = MOI.get(model, MOI.ConstraintName(), index)
184182
if name != ""
185-
push!(keys, :name)
186-
push!(values, name)
183+
push!(pairs, :name => name)
187184
end
188185
end
189-
push!(keys, :function)
190-
push!(values, moi_to_object(func, name_map))
191-
push!(keys, :set)
192-
push!(values, moi_to_object(set, name_map))
186+
push!(pairs, :function => moi_to_object(func, name_map))
187+
push!(pairs, :set => moi_to_object(set, name_map))
193188
if !isnothing(dual_start)
194-
push!(keys, :dual_start)
195-
push!(values, dual_start)
189+
push!(pairs, :dual_start => dual_start)
196190
end
197191
if !isnothing(primal_start)
198-
push!(keys, :primal_start)
199-
push!(values, primal_start)
192+
push!(pairs, :primal_start => primal_start)
200193
end
201-
return NamedTuple{Tuple(keys)}(values)
194+
return NamedTuple(pairs)
202195
end
203196

204197
function moi_to_object(sense::MOI.OptimizationSense)
@@ -443,13 +436,11 @@ function moi_to_object(
443436
set::SetType,
444437
::Dict{MOI.VariableIndex,String},
445438
) where {SetType}
446-
keys = Symbol[:type]
447-
values = Any[head_name(SetType)]
439+
pairs = Pair{Symbol,Any}[:type=>head_name(SetType)]
448440
for key in fieldnames(SetType)
449-
push!(keys, Symbol(string(key)))
450-
push!(values, getfield(set, key))
441+
push!(pairs, Symbol(string(key)) => getfield(set, key))
451442
end
452-
return NamedTuple{Tuple(keys)}(values)
443+
return NamedTuple(pairs)
453444
end
454445

455446
# ========== Non-typed scalar sets ==========

0 commit comments

Comments
 (0)