Skip to content

Commit

Permalink
WeakRefStringArray: make eltype respect Missing
Browse files Browse the repository at this point in the history
enhances JuliaData#17 so that WeakRefStringArray allows missing iff
the underlying WeakRefString Array allows missing
  • Loading branch information
alyst committed Dec 20, 2017
1 parent 69d5ff8 commit 6c61112
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/WeakRefStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,33 @@ Base.convert(::Type{String}, x::WeakRefString) = convert(String, string(x))
Base.String(x::WeakRefString) = string(x)
Base.Symbol(x::WeakRefString{UInt8}) = ccall(:jl_symbol_n, Ref{Symbol}, (Ptr{UInt8}, Int), x.ptr, x.len)

struct WeakRefStringArray{T, N} <: AbstractArray{Union{String, Missing}, N}
struct WeakRefStringArray{T, N, U} <: AbstractArray{Union{String, U}, N}
data::Vector{Any}
elements::Array{T, N}
elements::Array{Union{T, U}, N}

WeakRefStringArray(data::Vector{UInt8}, ::Type{T}, rows::Integer) where {T <: WeakRefString} =
new{T, 1, Union{}}(Any[data], zeros(T, rows))
WeakRefStringArray(data::Vector{UInt8}, ::Type{Union{Missing, T}}, rows::Integer) where {T <: WeakRefString} =
new{T, 1, Missing}(Any[data], Vector{Union{T, Missing}}(rows))
WeakRefStringArray(data::Vector{UInt8}, A::Array{T, N}) where {T <: Union{WeakRefString, Missing}, N} =
new{Missings.T(T), N, T >: Missing ? Missing : Union{}}(Any[data], A)
end

WeakRefStringArray(data::Vector{UInt8}, ::Type{T}, rows::Integer) where {T <: WeakRefString} = WeakRefStringArray(Any[data], zeros(T, rows))
WeakRefStringArray(data::Vector{UInt8}, ::Type{Union{Missing, T}}, rows::Integer) where {T} = WeakRefStringArray(Any[data], Vector{Union{Missing, T}}(rows))
WeakRefStringArray(data::Vector{UInt8}, A::Array{T}) where {T <: Union{WeakRefString, Missing}} = WeakRefStringArray(Any[data], A)

wk(w::WeakRefString) = string(w)
wk(::Missing) = missing

Base.size(A::WeakRefStringArray) = size(A.elements)
Base.getindex(A::WeakRefStringArray, i::Int) = wk(A.elements[i])
Base.getindex(A::WeakRefStringArray{T, N}, I::Vararg{Int, N}) where {T, N} = wk.(A.elements[I...])
Base.setindex!(A::WeakRefStringArray{T, N}, v::Missing, i::Int) where {T, N} = setindex!(A.elements, v, i)
Base.setindex!(A::WeakRefStringArray{T, N}, v::Missing, I::Vararg{Int, N}) where {T, N} = setindex!(A.elements, v, I...)
Base.setindex!(A::WeakRefStringArray{T, N, Missing}, v::Missing, i::Int) where {T, N} = setindex!(A.elements, v, i)
Base.setindex!(A::WeakRefStringArray{T, N, Missing}, v::Missing, I::Vararg{Int, N}) where {T, N} = setindex!(A.elements, v, I...)
Base.setindex!(A::WeakRefStringArray{T, N}, v::WeakRefString, i::Int) where {T, N} = setindex!(A.elements, v, i)
Base.setindex!(A::WeakRefStringArray{T, N}, v::WeakRefString, I::Vararg{Int, N}) where {T, N} = setindex!(A.elements, v, I...)
Base.setindex!(A::WeakRefStringArray{T, N}, v::String, i::Int) where {T, N} = (push!(A.data, Vector{UInt8}(v)); setindex!(A.elements, v, i))
Base.setindex!(A::WeakRefStringArray{T, N}, v::String, I::Vararg{Int, N}) where {T, N} = (push!(A.data, Vector{UInt8}(v)); setindex!(A.elements, v, I...))
Base.resize!(A::WeakRefStringArray, i) = resize!(A.elements, i)

Base.push!(a::WeakRefStringArray{T, 1}, v::Missing) where {T} = (push!(a.elements, v); a)
Base.push!(a::WeakRefStringArray{T, 1, Missing}, v::Missing) where {T} = (push!(a.elements, v); a)
Base.push!(a::WeakRefStringArray{T, 1}, v::WeakRefString) where {T} = (push!(a.elements, v); a)
function Base.push!(A::WeakRefStringArray{T, 1}, v::String) where T
push!(A.data, Vector{UInt8}(v))
Expand Down

0 comments on commit 6c61112

Please sign in to comment.