Skip to content

Commit

Permalink
switch to DataAPI for refarray (JuliaArrays#109)
Browse files Browse the repository at this point in the history
* switch to DataAPI

* tests for refvalue
  • Loading branch information
Pietro Vertechi authored Dec 29, 2019
1 parent 583a8fb commit b8e5914
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
10 changes: 5 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ uuid = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
version = "0.4.1"

[deps]
PooledArrays = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
WeakRefStrings = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"

[compat]
PooledArrays = "0.5"
DataAPI = "1"
Tables = "0.2"
WeakRefStrings = "0.5, 0.6"
julia = "1"

[extras]
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
PooledArrays = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
WeakRefStrings = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"

[targets]
test = ["Test", "OffsetArrays"]
test = ["Test", "OffsetArrays", "PooledArrays", "WeakRefStrings"]
2 changes: 1 addition & 1 deletion src/StructArrays.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module StructArrays

using Base: tuple_type_cons, tuple_type_head, tuple_type_tail, tail
using PooledArrays: PooledArray

export StructArray, StructVector, LazyRow, LazyRows
export collect_structarray, fieldarrays
Expand All @@ -10,6 +9,7 @@ export replace_storage
include("interface.jl")
include("structarray.jl")
include("utils.jl")
include("refarray.jl")
include("collect.jl")
include("sort.jl")
include("groupjoin.jl")
Expand Down
7 changes: 7 additions & 0 deletions src/refarray.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import DataAPI: refarray, refvalue

refarray(s::StructArray) = StructArray(map(refarray, fieldarrays(s)))

function refvalue(s::StructArray{T}, v::Tup) where {T}
createinstance(T, map(refvalue, fieldarrays(s), v)...)
end
13 changes: 2 additions & 11 deletions src/sort.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
using Base.Sort, Base.Order
using WeakRefStrings: WeakRefString, StringArray

refs(v::PooledArray) = v.refs
function refs(a::StringArray{T}) where {T}
S = Union{WeakRefString{UInt8}, typeintersect(T, Missing)}
convert(StringArray{S}, a)
end
refs(v::AbstractArray) = v
refs(v::StructArray) = StructArray(map(refs, fieldarrays(v)))

function Base.permute!!(c::StructArray, p::AbstractVector{<:Integer})
Base.invoke(Base.permute!!, Tuple{AbstractArray, AbstractVector{<:Integer}}, refs(c), p)
Base.invoke(Base.permute!!, Tuple{AbstractArray, AbstractVector{<:Integer}}, refarray(c), p)
return c
end

Expand Down Expand Up @@ -42,7 +33,7 @@ Base.IteratorSize(::Type{<:GroupPerm}) = Base.SizeUnknown()
Base.eltype(::Type{<:GroupPerm}) = UnitRange{Int}

@inline function roweq(x::AbstractVector, i, j)
r = refs(x)
r = refarray(x)
@inbounds eq = isequal(r[i], r[j])
return eq
end
Expand Down
27 changes: 18 additions & 9 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using StructArrays
using StructArrays: staticschema, iscompatible, _promote_typejoin, append!!
using OffsetArrays: OffsetArray
import Tables, PooledArrays, WeakRefStrings
using DataAPI: refarray, refvalue
using Test

@testset "index" begin
Expand Down Expand Up @@ -121,9 +122,7 @@ end
b = PooledArrays.PooledArray(["1", "2", "3"])
c = [:a, :b, :c]
s = StructArray(a=a, b=b, c=c)
ref = StructArrays.refs(s)
@test ref[1].a isa WeakRefStrings.WeakRefString{UInt8}
@test ref[1].b isa Integer
ref = refarray(s)
Base.permute!!(ref, sortperm(s))
@test issorted(s)
end
Expand Down Expand Up @@ -623,16 +622,26 @@ end
@test str == "LazyRows(::Array{Float64,2}, ::Array{Float64,2})"
end

@testset "refs" begin
@testset "refarray" begin
s = PooledArrays.PooledArray(["a", "b", "c", "c"])
@test StructArrays.refs(s) == UInt8.([1, 2, 3, 3])
@test refarray(s) == UInt8.([1, 2, 3, 3])

s = WeakRefStrings.StringArray(["a", "b"])
@test StructArrays.refs(s) isa WeakRefStrings.StringArray{WeakRefStrings.WeakRefString{UInt8}}
@test all(isequal.(s, StructArrays.refs(s)))
@test refarray(s) isa WeakRefStrings.StringArray{WeakRefStrings.WeakRefString{UInt8}}
@test all(isequal.(s, refarray(s)))
s = WeakRefStrings.StringArray(["a", missing])
@test StructArrays.refs(s) isa WeakRefStrings.StringArray{Union{WeakRefStrings.WeakRefString{UInt8}, Missing}}
@test all(isequal.(s, StructArrays.refs(s)))
@test refarray(s) isa WeakRefStrings.StringArray{Union{WeakRefStrings.WeakRefString{UInt8}, Missing}}
@test all(isequal.(s, refarray(s)))
a = WeakRefStrings.StringVector(["a", "b", "c"])
b = PooledArrays.PooledArray(["1", "2", "3"])
c = [:a, :b, :c]
s = StructArray(a=a, b=b, c=c)
ref = refarray(s)
@test ref[1].a isa WeakRefStrings.WeakRefString{UInt8}
@test ref[1].b isa Integer
for i in 1:3
@test refvalue(s, ref[i]) == s[i]
end
end

@testset "show" begin
Expand Down

0 comments on commit b8e5914

Please sign in to comment.