Skip to content

Commit b8e5914

Browse files
author
Pietro Vertechi
authored
switch to DataAPI for refarray (#109)
* switch to DataAPI * tests for refvalue
1 parent 583a8fb commit b8e5914

File tree

5 files changed

+33
-26
lines changed

5 files changed

+33
-26
lines changed

Project.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ uuid = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
33
version = "0.4.1"
44

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

109
[compat]
11-
PooledArrays = "0.5"
10+
DataAPI = "1"
1211
Tables = "0.2"
13-
WeakRefStrings = "0.5, 0.6"
1412
julia = "1"
1513

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

2020
[targets]
21-
test = ["Test", "OffsetArrays"]
21+
test = ["Test", "OffsetArrays", "PooledArrays", "WeakRefStrings"]

src/StructArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module StructArrays
22

33
using Base: tuple_type_cons, tuple_type_head, tuple_type_tail, tail
4-
using PooledArrays: PooledArray
54

65
export StructArray, StructVector, LazyRow, LazyRows
76
export collect_structarray, fieldarrays
@@ -10,6 +9,7 @@ export replace_storage
109
include("interface.jl")
1110
include("structarray.jl")
1211
include("utils.jl")
12+
include("refarray.jl")
1313
include("collect.jl")
1414
include("sort.jl")
1515
include("groupjoin.jl")

src/refarray.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import DataAPI: refarray, refvalue
2+
3+
refarray(s::StructArray) = StructArray(map(refarray, fieldarrays(s)))
4+
5+
function refvalue(s::StructArray{T}, v::Tup) where {T}
6+
createinstance(T, map(refvalue, fieldarrays(s), v)...)
7+
end

src/sort.jl

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
using Base.Sort, Base.Order
2-
using WeakRefStrings: WeakRefString, StringArray
3-
4-
refs(v::PooledArray) = v.refs
5-
function refs(a::StringArray{T}) where {T}
6-
S = Union{WeakRefString{UInt8}, typeintersect(T, Missing)}
7-
convert(StringArray{S}, a)
8-
end
9-
refs(v::AbstractArray) = v
10-
refs(v::StructArray) = StructArray(map(refs, fieldarrays(v)))
112

123
function Base.permute!!(c::StructArray, p::AbstractVector{<:Integer})
13-
Base.invoke(Base.permute!!, Tuple{AbstractArray, AbstractVector{<:Integer}}, refs(c), p)
4+
Base.invoke(Base.permute!!, Tuple{AbstractArray, AbstractVector{<:Integer}}, refarray(c), p)
145
return c
156
end
167

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

4435
@inline function roweq(x::AbstractVector, i, j)
45-
r = refs(x)
36+
r = refarray(x)
4637
@inbounds eq = isequal(r[i], r[j])
4738
return eq
4839
end

test/runtests.jl

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using StructArrays
22
using StructArrays: staticschema, iscompatible, _promote_typejoin, append!!
33
using OffsetArrays: OffsetArray
44
import Tables, PooledArrays, WeakRefStrings
5+
using DataAPI: refarray, refvalue
56
using Test
67

78
@testset "index" begin
@@ -121,9 +122,7 @@ end
121122
b = PooledArrays.PooledArray(["1", "2", "3"])
122123
c = [:a, :b, :c]
123124
s = StructArray(a=a, b=b, c=c)
124-
ref = StructArrays.refs(s)
125-
@test ref[1].a isa WeakRefStrings.WeakRefString{UInt8}
126-
@test ref[1].b isa Integer
125+
ref = refarray(s)
127126
Base.permute!!(ref, sortperm(s))
128127
@test issorted(s)
129128
end
@@ -623,16 +622,26 @@ end
623622
@test str == "LazyRows(::Array{Float64,2}, ::Array{Float64,2})"
624623
end
625624

626-
@testset "refs" begin
625+
@testset "refarray" begin
627626
s = PooledArrays.PooledArray(["a", "b", "c", "c"])
628-
@test StructArrays.refs(s) == UInt8.([1, 2, 3, 3])
627+
@test refarray(s) == UInt8.([1, 2, 3, 3])
629628

630629
s = WeakRefStrings.StringArray(["a", "b"])
631-
@test StructArrays.refs(s) isa WeakRefStrings.StringArray{WeakRefStrings.WeakRefString{UInt8}}
632-
@test all(isequal.(s, StructArrays.refs(s)))
630+
@test refarray(s) isa WeakRefStrings.StringArray{WeakRefStrings.WeakRefString{UInt8}}
631+
@test all(isequal.(s, refarray(s)))
633632
s = WeakRefStrings.StringArray(["a", missing])
634-
@test StructArrays.refs(s) isa WeakRefStrings.StringArray{Union{WeakRefStrings.WeakRefString{UInt8}, Missing}}
635-
@test all(isequal.(s, StructArrays.refs(s)))
633+
@test refarray(s) isa WeakRefStrings.StringArray{Union{WeakRefStrings.WeakRefString{UInt8}, Missing}}
634+
@test all(isequal.(s, refarray(s)))
635+
a = WeakRefStrings.StringVector(["a", "b", "c"])
636+
b = PooledArrays.PooledArray(["1", "2", "3"])
637+
c = [:a, :b, :c]
638+
s = StructArray(a=a, b=b, c=c)
639+
ref = refarray(s)
640+
@test ref[1].a isa WeakRefStrings.WeakRefString{UInt8}
641+
@test ref[1].b isa Integer
642+
for i in 1:3
643+
@test refvalue(s, ref[i]) == s[i]
644+
end
636645
end
637646

638647
@testset "show" begin

0 commit comments

Comments
 (0)