Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Utilities/DoubleDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,22 @@ function Base.length(d::AbstractDoubleDict)
end

function Base.haskey(dict::AbstractDoubleDict, key::CI{F,S}) where {F,S}
inner = get(dict.dict, (F,S), nothing)
inner = get(dict.dict, (F, S), nothing)
if inner !== nothing
inner = dict.dict[(F,S)]
return haskey(inner, key.value)
else
return false
end
end

function Base.get(dict::AbstractDoubleDict, key::CI{F,S}, default) where {F,S}
inner = get(dict.dict, (F, S), nothing)
if inner !== nothing && haskey(inner, key.value)
return typed_value(dict, inner[key.value], F, S)
end
return default
end

function Base.getindex(dict::AbstractDoubleDict, key::CI{F,S}) where {F,S}
inner = dict.dict[(F,S)]
k_value = key.value::Int64
Expand Down
19 changes: 17 additions & 2 deletions test/Utilities/DoubleDicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function test_iterator(dict)
@test isempty(setdiff(kk, keys(dict)))
@test length(vv) == length(values(dict))
@test isempty(setdiff(vv, values(dict)))
@test dict == Dict(kk .=> vv)
end

function basic_functionality(dict, k_values, v_values)
Expand All @@ -44,11 +45,25 @@ function basic_functionality(dict, k_values, v_values)
@test values(dict) == [v_values[1]]
@test keys(dict) == [k_values[1]]
@test dict[k_values[1]] == v_values[1]
@test get(dict, k_values[1], nothing) == v_values[1]
@test get(dict, k_values[1], v_values[2]) == v_values[1]
for i in eachindex(k_values)
if i != 1
@test get(dict, k_values[i], nothing) === nothing
# Test that the implementation does not only work with `nothing`
@test get(dict, k_values[i], v_values[i]) == v_values[i]
end
end

test_iterator(dict)

delete!(dict, k_values[1])
@test !haskey(dict, k_values[1])
for i in eachindex(k_values)
@test get(dict, k_values[i], nothing) === nothing
# Test that the implementation does not only work with `nothing`
@test get(dict, k_values[i], v_values[i]) == v_values[i]
end
@test isempty(dict)

dict[k_values[1]] = v_values[1]
Expand Down Expand Up @@ -142,7 +157,7 @@ end
dict[k] = v
end
dest = DoubleDicts.IndexDoubleDict()
MOIU._reverse_dict(dest, src)
MOI.Utilities._reverse_dict(dest, src)
for (k, v) in src
@test dest[v] == k
end
Expand All @@ -161,4 +176,4 @@ end
(MOI.SingleVariable(MOI.VariableIndex(1)), MOI.ZeroOne()),
]
basic_functionality(dict, keys, vals)
end
end