Skip to content

Commit 8269413

Browse files
committed
Tweaks
1 parent e83a0e2 commit 8269413

File tree

1 file changed

+35
-41
lines changed

1 file changed

+35
-41
lines changed

src/Utilities/DoubleDicts.jl

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ mutable struct DoubleDictInner{F,S,V} <: AbstractDoubleDictInner{F,S,V}
5959
end
6060
end
6161

62+
function Base.getindex(d::DoubleDict, ::Type{F}, ::Type{S}) where {F,S}
63+
return DoubleDictInner{F,S}(d)
64+
end
65+
6266
"""
6367
IndexDoubleDict
6468
@@ -92,6 +96,10 @@ mutable struct IndexDoubleDictInner{F,S} <:
9296
end
9397
end
9498

99+
function Base.getindex(d::IndexDoubleDict, ::Type{F}, ::Type{S}) where {F,S}
100+
return IndexDoubleDictInner{F,S}(d)
101+
end
102+
95103
function typed_value(::IndexDoubleDictInner{F,S}, v::Int64) where {F,S}
96104
return MOI.ConstraintIndex{F,S}(v)
97105
end
@@ -130,7 +138,7 @@ function Base.haskey(
130138
d::AbstractDoubleDict,
131139
key::MOI.ConstraintIndex{F,S},
132140
) where {F,S}
133-
return haskey(getindex(d, F, S), key)
141+
return haskey(d[F, S], key)
134142
end
135143

136144
function Base.haskey(
@@ -147,11 +155,19 @@ function Base.get(
147155
key::MOI.ConstraintIndex{F,S},
148156
default,
149157
) where {F,S}
150-
inner = getindex(d, F, S)
151-
if haskey(inner, key)
152-
return inner[key]
158+
inner = d[F, S]
159+
return get(inner, key, default)
160+
end
161+
162+
function Base.get(
163+
d::AbstractDoubleDictInner,
164+
key::MOI.ConstraintIndex{F,S},
165+
default,
166+
) where {F,S}
167+
if !haskey(d, key)
168+
return default
153169
end
154-
return default
170+
return typed_value(d, d.inner[key.value])
155171
end
156172

157173
# Base.getindex
@@ -160,42 +176,34 @@ function Base.getindex(
160176
d::AbstractDoubleDict,
161177
key::MOI.ConstraintIndex{F,S},
162178
) where {F,S}
163-
inner = getindex(d, F, S)
179+
inner = d[F, S]
164180
return inner[key]
165181
end
166182

167-
function Base.getindex(d::DoubleDict, ::Type{F}, ::Type{S}) where {F,S}
168-
return DoubleDictInner{F,S}(d)
169-
end
170-
171183
function Base.getindex(
172184
d::AbstractDoubleDictInner{F,S},
173185
key::MOI.ConstraintIndex{F,S},
174186
) where {F,S}
175-
if !haskey(d.inner, key.value)
187+
if !haskey(d, key)
176188
throw(KeyError(key))
177189
end
178190
return typed_value(d, d.inner[key.value])
179191
end
180192

181-
function Base.getindex(d::IndexDoubleDict, ::Type{F}, ::Type{S}) where {F,S}
182-
return IndexDoubleDictInner{F,S}(d)
183-
end
184-
185193
# Base.setindex!
186194

187195
function Base.setindex!(
188196
d::AbstractDoubleDict{V},
189197
value::V,
190198
key::MOI.ConstraintIndex{F,S},
191199
) where {F,S,V}
192-
inner = getindex(d, F, S)
200+
inner = d[F, S]
193201
inner[key] = value
194202
return value
195203
end
196204

197205
function Base.setindex!(
198-
d::DoubleDictInner{F,S,V},
206+
d::AbstractDoubleDictInner{F,S,V},
199207
value::V,
200208
key::MOI.ConstraintIndex{F,S},
201209
) where {F,S,V}
@@ -208,7 +216,7 @@ function Base.setindex!(
208216
value::MOI.ConstraintIndex{F,S},
209217
key::MOI.ConstraintIndex{F,S},
210218
) where {F,S}
211-
inner = getindex(d, F, S)
219+
inner = d[F, S]
212220
inner[key] = value
213221
return value
214222
end
@@ -240,7 +248,7 @@ function Base.delete!(
240248
d::AbstractDoubleDict,
241249
key::MOI.ConstraintIndex{F,S},
242250
) where {F,S}
243-
delete!(getindex(d, F, S), key)
251+
delete!(d[F, S], key)
244252
return d
245253
end
246254

@@ -262,36 +270,22 @@ Base.isempty(d::AbstractDoubleDictInner) = isempty(d.inner)
262270

263271
# Base.values
264272

265-
function Base.values(d::AbstractDoubleDict{V})::Vector{V} where {V}
273+
function Base.values(d::AbstractDoubleDict{V}) where {V}
266274
out = V[]
267-
for inner in values(d.dict)
268-
append!(out, values(inner))
275+
for (F, S) in keys(d.dict)
276+
append!(out, values(d[F, S]))
269277
end
270278
return out
271279
end
272280

273-
function Base.values(d::DoubleDictInner{F,S,V})::Vector{V} where {F,S,V}
274-
return V[v for v in values(d.inner)]
275-
end
276-
277-
function Base.values(d::IndexDoubleDict)
278-
out = MOI.ConstraintIndex[]
279-
for ((F, S), inner) in d.dict
280-
append!(out, MOI.ConstraintIndex{F,S}.(values(inner)))
281-
end
282-
return out
283-
end
284-
285-
function Base.values(d::IndexDoubleDictInner{F,S}) where {F,S}
286-
return MOI.ConstraintIndex{F,S}.(values(d.inner))
287-
end
281+
Base.values(d::AbstractDoubleDictInner) = typed_value.(Ref(d), values(d.inner))
288282

289283
# Base.keys
290284

291285
function Base.keys(d::AbstractDoubleDict)
292286
out = MOI.ConstraintIndex[]
293-
for ((F, S), inner) in d.dict
294-
append!(out, MOI.ConstraintIndex{F,S}.(keys(inner)))
287+
for (F, S) in keys(d.dict)
288+
append!(out, keys(d[F, S]))
295289
end
296290
return out
297291
end
@@ -322,7 +316,7 @@ function Base.iterate(d::AbstractDoubleDict)
322316
inner_next = iterate(inner)
323317
end
324318
(k, v), inner_state = inner_next
325-
result = MOI.ConstraintIndex{F,S}(k) => typed_value(getindex(d, F, S), v)
319+
result = MOI.ConstraintIndex{F,S}(k) => typed_value(d[F, S], v)
326320
return result, (inner_state, outer_next)
327321
end
328322

@@ -352,7 +346,7 @@ function Base.iterate(d::AbstractDoubleDict, state)
352346
inner_next = iterate(inner)
353347
end
354348
(k, v), inner_state = inner_next
355-
result = MOI.ConstraintIndex{F,S}(k) => typed_value(getindex(d, F, S), v)
349+
result = MOI.ConstraintIndex{F,S}(k) => typed_value(d[F, S], v)
356350
return result, (inner_state, outer_next)
357351
end
358352

0 commit comments

Comments
 (0)