Skip to content

Commit b9668c6

Browse files
committed
Other fixes
1 parent eeb889c commit b9668c6

File tree

2 files changed

+40
-44
lines changed

2 files changed

+40
-44
lines changed

src/Utilities/DoubleDicts.jl

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ abstract type AbstractDoubleDict{V} <: AbstractDict{MOI.ConstraintIndex,V} end
99
abstract type AbstractDoubleDictInner{F,S,V} <:
1010
AbstractDict{MOI.ConstraintIndex{F,S},V} end
1111

12-
_inner(d::AbstractDoubleDictInner) = d.inner
12+
"""
13+
typed_value(dict::AbstractDoubleDictInner{F,S,V}, value) where {F,S,V}
14+
15+
Convert the `value` stored inside `dict` to the equivalent on the outer
16+
`DoubleDict`. This is useful when the value type `V` of the inner dict is
17+
different to the outer dict. (See, e.g., [`IndexDoubleDict`](@ref).)
18+
"""
19+
function typed_value(::AbstractDoubleDictInner{F,S,V}, value::V) where {F,S,V}
20+
return value
21+
end
1322

1423
"""
1524
DoubleDict{V}
@@ -83,32 +92,10 @@ mutable struct IndexDoubleDictInner{F,S} <:
8392
end
8493
end
8594

86-
# _typed_value
87-
88-
@inline function _typed_value(::DoubleDictInner{F,S,V}, v::V) where {F,S,V}
89-
return v
90-
end
91-
92-
@inline function _typed_value(::IndexDoubleDictInner{F,S}, v::Int64) where {F,S}
95+
function typed_value(::IndexDoubleDictInner{F,S}, v::Int64) where {F,S}
9396
return MOI.ConstraintIndex{F,S}(v)
9497
end
9598

96-
# _reverse_dict
97-
98-
# reversing IndexDoubleDict is ok because they map CI to CI
99-
function MOI.Utilities._reverse_dict(
100-
dest::IndexDoubleDict,
101-
src::IndexDoubleDict,
102-
)
103-
for (key, value) in src.dict
104-
dest.dict[key] = MOI.Utilities._reverse_dict(value)
105-
end
106-
return
107-
end
108-
# reversing other double dict types is not ok because the map CI fo K
109-
# so it wont be a double dict anymore, double dict keys are always CIs.
110-
# We keep the default fallback
111-
11299
# Base.sizehint!
113100

114101
function Base.sizehint!(::AbstractDoubleDict, ::Integer)
@@ -122,7 +109,7 @@ function Base.sizehint!(::AbstractDoubleDict, ::Integer)
122109
end
123110

124111
function Base.sizehint!(d::AbstractDoubleDictInner, n::Integer)
125-
return sizehint!(_inner(d), n)
112+
return sizehint!(d.inner, n)
126113
end
127114

128115
# Base.length
@@ -135,7 +122,7 @@ function Base.length(d::AbstractDoubleDict)
135122
return len
136123
end
137124

138-
Base.length(d::AbstractDoubleDictInner) = length(_inner(d))
125+
Base.length(d::AbstractDoubleDictInner) = length(d.inner)
139126

140127
# Base.haskey
141128

@@ -150,7 +137,7 @@ function Base.haskey(
150137
d::AbstractDoubleDictInner{F,S},
151138
key::MOI.ConstraintIndex{F,S},
152139
) where {F,S}
153-
return haskey(_inner(d), key.value)
140+
return haskey(d.inner, key.value)
154141
end
155142

156143
# Base.get
@@ -185,11 +172,10 @@ function Base.getindex(
185172
d::AbstractDoubleDictInner{F,S},
186173
key::MOI.ConstraintIndex{F,S},
187174
) where {F,S}
188-
inner = _inner(d)
189-
if !haskey(inner, key.value)
175+
if !haskey(d.inner, key.value)
190176
throw(KeyError(key))
191177
end
192-
return _typed_value(d, inner[key.value])
178+
return typed_value(d, d.inner[key.value])
193179
end
194180

195181
function Base.getindex(d::IndexDoubleDict, ::Type{F}, ::Type{S}) where {F,S}
@@ -213,7 +199,7 @@ function Base.setindex!(
213199
value::V,
214200
key::MOI.ConstraintIndex{F,S},
215201
) where {F,S,V}
216-
_inner(d)[key.value] = value
202+
d.inner[key.value] = value
217203
return value
218204
end
219205

@@ -232,7 +218,7 @@ function Base.setindex!(
232218
value::MOI.ConstraintIndex{F,S},
233219
key::MOI.ConstraintIndex{F,S},
234220
) where {F,S}
235-
_inner(d)[key.value] = value.value
221+
d.inner[key.value] = value.value
236222
return value
237223
end
238224

@@ -244,7 +230,7 @@ function Base.empty!(d::AbstractDoubleDict)
244230
end
245231

246232
function Base.empty!(d::AbstractDoubleDictInner)
247-
empty!(_inner(d))
233+
empty!(d.inner)
248234
return d
249235
end
250236

@@ -262,7 +248,7 @@ function Base.delete!(
262248
d::AbstractDoubleDictInner{F,S},
263249
key::MOI.ConstraintIndex{F,S},
264250
) where {F,S}
265-
delete!(_inner(d), key.value)
251+
delete!(d.inner, key.value)
266252
return d
267253
end
268254

@@ -272,7 +258,7 @@ function Base.isempty(d::AbstractDoubleDict)
272258
return isempty(d.dict) || all(isempty, values(d.dict))
273259
end
274260

275-
Base.isempty(d::AbstractDoubleDictInner) = isempty(_inner(d))
261+
Base.isempty(d::AbstractDoubleDictInner) = isempty(d.inner)
276262

277263
# Base.values
278264

@@ -285,7 +271,7 @@ function Base.values(d::AbstractDoubleDict{V})::Vector{V} where {V}
285271
end
286272

287273
function Base.values(d::DoubleDictInner{F,S,V})::Vector{V} where {F,S,V}
288-
return V[v for v in values(_inner(d))]
274+
return V[v for v in values(d.inner)]
289275
end
290276

291277
function Base.values(d::IndexDoubleDict)
@@ -297,7 +283,7 @@ function Base.values(d::IndexDoubleDict)
297283
end
298284

299285
function Base.values(d::IndexDoubleDictInner{F,S}) where {F,S}
300-
return MOI.ConstraintIndex{F,S}.(values(_inner(d)))
286+
return MOI.ConstraintIndex{F,S}.(values(d.inner))
301287
end
302288

303289
# Base.keys
@@ -311,7 +297,7 @@ function Base.keys(d::AbstractDoubleDict)
311297
end
312298

313299
function Base.keys(d::AbstractDoubleDictInner{F,S}) where {F,S}
314-
return MOI.ConstraintIndex{F,S}.(keys(_inner(d)))
300+
return MOI.ConstraintIndex{F,S}.(keys(d.inner))
315301
end
316302

317303
# Base.iterate
@@ -336,18 +322,18 @@ function Base.iterate(d::AbstractDoubleDict)
336322
inner_next = iterate(inner)
337323
end
338324
(k, v), inner_state = inner_next
339-
result = MOI.ConstraintIndex{F,S}(k) => _typed_value(getindex(d, F, S), v)
325+
result = MOI.ConstraintIndex{F,S}(k) => typed_value(getindex(d, F, S), v)
340326
return result, (inner_state, outer_next)
341327
end
342328

343329
function Base.iterate(d::AbstractDoubleDictInner{F,S}) where {F,S}
344-
next = iterate(_inner(d))
330+
next = iterate(d.inner)
345331
if next === nothing
346332
return
347333
end
348334
(k, v), inner_state = next
349-
result = MOI.ConstraintIndex{F,S}(k) => _typed_value(d, v)
350-
return result, (_inner(d), inner_state)
335+
result = MOI.ConstraintIndex{F,S}(k) => typed_value(d, v)
336+
return result, (d.inner, inner_state)
351337
end
352338

353339
function Base.iterate(d::AbstractDoubleDict, state)
@@ -366,7 +352,7 @@ function Base.iterate(d::AbstractDoubleDict, state)
366352
inner_next = iterate(inner)
367353
end
368354
(k, v), inner_state = inner_next
369-
result = MOI.ConstraintIndex{F,S}(k) => _typed_value(getindex(d, F, S), v)
355+
result = MOI.ConstraintIndex{F,S}(k) => typed_value(getindex(d, F, S), v)
370356
return result, (inner_state, outer_next)
371357
end
372358

@@ -377,7 +363,7 @@ function Base.iterate(d::AbstractDoubleDictInner{F,S}, state) where {F,S}
377363
return
378364
end
379365
((k, v), next_inner_state) = next
380-
result = MOI.ConstraintIndex{F,S}(k) => _typed_value(d, v)
366+
result = MOI.ConstraintIndex{F,S}(k) => typed_value(d, v)
381367
return result, (inner, next_inner_state)
382368
end
383369

src/Utilities/cachingoptimizer.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ function _reverse_dict(dest::AbstractDict, src::AbstractDict)
217217
return
218218
end
219219

220+
function _reverse_dict(
221+
dest::DoubleDicts.IndexDoubleDict,
222+
src::DoubleDicts.IndexDoubleDict,
223+
)
224+
for (key, value) in src.dict
225+
dest.dict[key] = MOI.Utilities._reverse_dict(value)
226+
end
227+
return
228+
end
229+
220230
function _reverse_dict(src::D) where {D<:Dict}
221231
return D(values(src) .=> keys(src))
222232
end

0 commit comments

Comments
 (0)