From 64b51e51c6d5220d66d731106cd25bff54977ea9 Mon Sep 17 00:00:00 2001 From: Nick Robinson Date: Tue, 26 Jul 2022 15:34:11 +0100 Subject: [PATCH 1/4] Allow `_AxisLookup` to fallback to Dict `getindex` - Allows e.g. looking up `String` key with another `AbstractString` --- src/Containers/DenseAxisArray.jl | 6 +++--- test/Containers/DenseAxisArray.jl | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Containers/DenseAxisArray.jl b/src/Containers/DenseAxisArray.jl index 960bb0e30e5..e0c503dd427 100644 --- a/src/Containers/DenseAxisArray.jl +++ b/src/Containers/DenseAxisArray.jl @@ -48,17 +48,17 @@ function build_lookup(ax) return _AxisLookup(d) end -Base.getindex(x::_AxisLookup{Dict{K,Int}}, key::K) where {K} = x.data[key] +Base.getindex(x::_AxisLookup{Dict{K,Int}}, key) where {K} = x.data[key] Base.getindex(::_AxisLookup{Dict{K,Int}}, key::Colon) where {K} = key function Base.getindex( x::_AxisLookup{Dict{K,Int}}, - keys::AbstractVector{<:K}, + keys::AbstractVector, ) where {K} return [x[key] for key in keys] end -function Base.get(x::_AxisLookup{Dict{K,Int}}, key::K, default) where {K} +function Base.get(x::_AxisLookup{Dict{K,Int}}, key, default) where {K} return get(x.data, key, default) end diff --git a/test/Containers/DenseAxisArray.jl b/test/Containers/DenseAxisArray.jl index 75a2074ab55..b3649193b4d 100644 --- a/test/Containers/DenseAxisArray.jl +++ b/test/Containers/DenseAxisArray.jl @@ -93,6 +93,13 @@ And data, a 2-element $(Vector{Float64}): @test 1 .+ A == correct_answer end + @testset "String index set" begin + A = @inferred DenseAxisArray([1.0, 2.0], ["a", "b"]) + @test (@inferred A["a"]) == (@inferred A[GenericString("a")]) == 1.0 + @test (@inferred A[["a", "b"]]) == + (@inferred A[[GenericString("a"), GenericString("b")]]) == A + end + @testset "Mixed range/symbol index sets" begin A = @inferred DenseAxisArray([1 2; 3 4], 2:3, [:a, :b]) @test size(A) == (2, 2) @@ -248,6 +255,11 @@ And data, a 0-dimensional $(Array{Int,0}): @test (@inferred C[2:3, [:a, :b]]) == C @test (@inferred C[2, [:a, :b]]) == DenseAxisArray([5.0, 6.0], [:a, :b]) @test (@inferred C[2:3, :b]) == DenseAxisArray([6.0, 8.0], 2:3) + + D = DenseAxisArray([5.0 6.0; 7.0 8.0], 2:3, ["a", "b"]) + @test (@inferred D[2, GenericString("b")]) == 6.0 + @test (@inferred D[2, [GenericString("a"), GenericString("b")]]) == + DenseAxisArray([5.0, 6.0], ["a", "b"]) end @testset "BitArray" begin x = DenseAxisArray([0 1; 1 0], [:a, :b], 1:2) From 2c3341429187bfeeaebc4dfa854ea7e0b2d0b6f9 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Wed, 27 Jul 2022 11:05:20 +1200 Subject: [PATCH 2/4] Apply suggestions from code review --- test/Containers/DenseAxisArray.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Containers/DenseAxisArray.jl b/test/Containers/DenseAxisArray.jl index b3649193b4d..bac84d06ed5 100644 --- a/test/Containers/DenseAxisArray.jl +++ b/test/Containers/DenseAxisArray.jl @@ -97,7 +97,7 @@ And data, a 2-element $(Vector{Float64}): A = @inferred DenseAxisArray([1.0, 2.0], ["a", "b"]) @test (@inferred A["a"]) == (@inferred A[GenericString("a")]) == 1.0 @test (@inferred A[["a", "b"]]) == - (@inferred A[[GenericString("a"), GenericString("b")]]) == A + (@inferred A[[GenericString("a"), GenericString("b")]]) == A end @testset "Mixed range/symbol index sets" begin @@ -259,7 +259,7 @@ And data, a 0-dimensional $(Array{Int,0}): D = DenseAxisArray([5.0 6.0; 7.0 8.0], 2:3, ["a", "b"]) @test (@inferred D[2, GenericString("b")]) == 6.0 @test (@inferred D[2, [GenericString("a"), GenericString("b")]]) == - DenseAxisArray([5.0, 6.0], ["a", "b"]) + DenseAxisArray([5.0, 6.0], ["a", "b"]) end @testset "BitArray" begin x = DenseAxisArray([0 1; 1 0], [:a, :b], 1:2) From 22d1be436d2770ec19a194e1800efaa058c6c646 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Wed, 27 Jul 2022 11:38:33 +1200 Subject: [PATCH 3/4] Update test/Containers/DenseAxisArray.jl --- test/Containers/DenseAxisArray.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/Containers/DenseAxisArray.jl b/test/Containers/DenseAxisArray.jl index bac84d06ed5..40425ae7622 100644 --- a/test/Containers/DenseAxisArray.jl +++ b/test/Containers/DenseAxisArray.jl @@ -97,7 +97,8 @@ And data, a 2-element $(Vector{Float64}): A = @inferred DenseAxisArray([1.0, 2.0], ["a", "b"]) @test (@inferred A["a"]) == (@inferred A[GenericString("a")]) == 1.0 @test (@inferred A[["a", "b"]]) == - (@inferred A[[GenericString("a"), GenericString("b")]]) == A + (@inferred A[[GenericString("a"), GenericString("b")]]) == + A end @testset "Mixed range/symbol index sets" begin From c85a2f32c2a402868520202e40e042f1406bca60 Mon Sep 17 00:00:00 2001 From: Nick Robinson Date: Wed, 27 Jul 2022 10:00:57 +0100 Subject: [PATCH 4/4] Add test for manually thrown KeyError --- test/Containers/DenseAxisArray.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/Containers/DenseAxisArray.jl b/test/Containers/DenseAxisArray.jl index 40425ae7622..94521351e1c 100644 --- a/test/Containers/DenseAxisArray.jl +++ b/test/Containers/DenseAxisArray.jl @@ -43,6 +43,7 @@ using Test @test isassigned(A, 2) @test !isassigned(A, 1) @test length.(axes(A)) == (2,) + @test_throws KeyError A["2"] correct_answer = DenseAxisArray([2.0, 3.0], 2:3) @test sprint(show, correct_answer) == """