From 3a3c0666f5f5b118e958f6d60d11aaaa8331be76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sat, 5 Sep 2020 17:27:01 +0200 Subject: [PATCH] Implement get for DoubleDict --- src/Utilities/DoubleDicts.jl | 11 +++++++++-- test/Utilities/DoubleDicts.jl | 19 +++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Utilities/DoubleDicts.jl b/src/Utilities/DoubleDicts.jl index c0e8e6e7cf..0a6e074f6a 100644 --- a/src/Utilities/DoubleDicts.jl +++ b/src/Utilities/DoubleDicts.jl @@ -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 diff --git a/test/Utilities/DoubleDicts.jl b/test/Utilities/DoubleDicts.jl index ba8fe77f76..97df7517f2 100644 --- a/test/Utilities/DoubleDicts.jl +++ b/test/Utilities/DoubleDicts.jl @@ -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) @@ -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] @@ -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 @@ -161,4 +176,4 @@ end (MOI.SingleVariable(MOI.VariableIndex(1)), MOI.ZeroOne()), ] basic_functionality(dict, keys, vals) -end \ No newline at end of file +end