Skip to content
Permalink
Browse files

Fix isassigned for DenseAxisArray (#1932)

* Fix isassigned for DenseAxisArray

* Update DenseAxisArray.jl

* Update DenseAxisArray.jl

* Update DenseAxisArray.jl
  • Loading branch information...
odow committed Apr 9, 2019
1 parent 2e3eeed commit d23c61d4db332cc3f20e76fafc25fece91ff5fd0
Showing with 35 additions and 2 deletions.
  1. +18 −2 src/Containers/DenseAxisArray.jl
  2. +17 −0 test/DenseAxisArray.jl
@@ -111,9 +111,25 @@ Base.CartesianIndices(a::DenseAxisArray) = CartesianIndices(a.data)
# Indexing #
############

Base.isassigned(A::DenseAxisArray{T,N}, idx...) where {T,N} = length(idx) == N && all(t -> haskey(A.lookup[t[1]], t[2]), enumerate(idx))
function _is_assigned(A::DenseAxisArray{T, N}, idx...) where {T, N}
if length(idx) == N
keys = zeros(Int, N)
for (i, v) in enumerate(idx)
key = get(A.lookup[i], v, nothing)
key === nothing && return false
keys[i] = key
end
return isassigned(A.data, keys...)
end
return false
end
function Base.isassigned(A::DenseAxisArray{T, N}, idx...) where {T, N}
return _is_assigned(A, idx...)
end
# For ambiguity
Base.isassigned(A::DenseAxisArray{T,N}, idx::Int...) where {T,N} = length(idx) == N && all(t -> haskey(A.lookup[t[1]], t[2]), enumerate(idx))
function Base.isassigned(A::DenseAxisArray{T, N}, idx::Int...) where {T, N}
return _is_assigned(A, idx...)
end

Base.eachindex(A::DenseAxisArray) = CartesianIndices(size(A.data))

@@ -1,6 +1,9 @@
@testset "DenseAxisArray" begin
@testset "undef constructor" begin
A = @inferred DenseAxisArray{Int}(undef, [:a, :b], 1:2)
@test isassigned(A, :a, 1) # Because the eltype is Int, isassigned=true.
@test !isassigned(A, :c, 1)
@test !isassigned(A, :c, 1, :d)
A[:a, 1] = 1
A[:b, 1] = 2
A[:a, 2] = 3
@@ -9,6 +12,20 @@
@test A[:b, 1] == 2
@test A[:a, 2] == 3
@test A[:b, 2] == 4
@test isassigned(A, :a, 1)
@test !isassigned(A, :c, 1)
end

@testset "undef constructor (ii)" begin
A = @inferred DenseAxisArray{String}(undef, 1:2)
@test !isassigned(A, 1)
@test !isassigned(A, 2)
@test !isassigned(A, 3)
A[1] = "abc"
@test isassigned(A, 1)
@test !isassigned(A, 2)
@test !isassigned(A, 3)
@test !isassigned(A, 2, 2)
end

@testset "Range index set" begin

0 comments on commit d23c61d

Please sign in to comment.
You can’t perform that action at this time.