Skip to content

Commit

Permalink
Port to Missings
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Nov 18, 2017
1 parent ac9f915 commit bcd2f0f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.6
Nulls
Missings
14 changes: 7 additions & 7 deletions src/WeakRefStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module WeakRefStrings

export WeakRefString, WeakRefStringArray

using Nulls
using Missings

"""
A custom "weakref" string type that only points to external string data.
Expand Down Expand Up @@ -81,24 +81,24 @@ struct WeakRefStringArray{T, N} <: AbstractArray{T, N}
end

WeakRefStringArray(data::Vector{UInt8}, ::Type{T}, rows::Integer) where {T <: WeakRefString} = WeakRefStringArray(Any[data], zeros(T, rows))
WeakRefStringArray(data::Vector{UInt8}, ::Type{Union{Null, T}}, rows::Integer) where {T} = WeakRefStringArray(Any[data], Vector{Union{Null, T}}(rows))
WeakRefStringArray(data::Vector{UInt8}, A::Array{T}) where {T <: Union{WeakRefString, Null}} = WeakRefStringArray(Any[data], A)
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(::Null) = null
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::Null, i::Int) where {T, N} = setindex!(A.elements, v, i)
Base.setindex!(A::WeakRefStringArray{T, N}, v::Null, I::Vararg{Int, N}) where {T, N} = setindex!(A.elements, v, 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}, 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::Null) where {T} = (push!(a.elements, v); a)
Base.push!(a::WeakRefStringArray{T, 1}, 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
12 changes: 6 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using WeakRefStrings, Base.Test, Nulls
using WeakRefStrings, Base.Test, Missings

@testset "WeakRefString{UInt8}" begin
data = Vector{UInt8}("hey there sailor")
Expand Down Expand Up @@ -73,13 +73,13 @@ end
append!(A, B)
@test size(A) == (17,)

D = WeakRefStringArray(UInt8[], Union{Null, WeakRefString{UInt8}}, 0)
D = WeakRefStringArray(UInt8[], Union{Missing, WeakRefString{UInt8}}, 0)
push!(D, "hey")
push!(D, str)
push!(D, null)
push!(D, missing)
@test length(D) == 3
@test D[2] == str
@test D[3] === null
D[2] = null
@test D[2] === null
@test D[3] === missing
D[2] = missing
@test D[2] === missing
end

0 comments on commit bcd2f0f

Please sign in to comment.