diff --git a/Project.toml b/Project.toml index 70e486a29..6f5f78873 100644 --- a/Project.toml +++ b/Project.toml @@ -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"] diff --git a/src/StructArrays.jl b/src/StructArrays.jl index 8a3476764..c9f52df1a 100644 --- a/src/StructArrays.jl +++ b/src/StructArrays.jl @@ -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 @@ -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") diff --git a/src/refarray.jl b/src/refarray.jl new file mode 100644 index 000000000..a08981784 --- /dev/null +++ b/src/refarray.jl @@ -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 diff --git a/src/sort.jl b/src/sort.jl index df54b4f89..46cbddfec 100644 --- a/src/sort.jl +++ b/src/sort.jl @@ -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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 8a50c566e..15d0cfdfd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -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 @@ -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