Skip to content

Commit a16ea18

Browse files
author
Pietro Vertechi
authored
Merge pull request JuliaArrays#29 from piever/pv/sort
add permuting and copying
2 parents c6028d0 + bbb5b96 commit a16ea18

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

src/StructArrays.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ include("collect.jl")
1010

1111
function __init__()
1212
Requires.@require Tables="bd369af6-aec1-5ad0-b16a-f7cc5008161c" include("tables.jl")
13+
Requires.@require PooledArrays="2dfb63ee-cc39-5dd5-95bd-886bf059d720" begin
14+
isdiscrete(::PooledArrays.PooledArray) = true
15+
end
16+
Requires.@require WeakRefStrings="ea10d353-3f73-51f8-a26c-33c1cb351aa5" begin
17+
isdiscrete(::WeakRefStrings.StringArray) = true
18+
end
1319
end
1420

1521
end # module

src/sort.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
isdiscrete(v) = false
2+
3+
function Base.permute!(c::StructVector, p::AbstractVector)
4+
foreachcolumn(c) do v
5+
if isdiscrete(v) || v isa StructVector
6+
permute!(v, p)
7+
else
8+
copyto!(v, v[p])
9+
end
10+
end
11+
return c
12+
end

src/structarray.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,16 @@ end
106106

107107
function Base.push!(s::StructArray, vals)
108108
foreachcolumn(push!, s, vals)
109+
return s
109110
end
110111

111112
function Base.append!(s::StructArray, vals)
112113
foreachcolumn(append!, s, vals)
114+
return s
113115
end
114116

117+
Base.copyto!(I::StructArray, J::StructArray) = (foreachcolumn(copyto!, I, J); I)
118+
115119
function Base.cat(args::StructArray...; dims)
116120
f = key -> cat((getproperty(t, key) for t in args)...; dims=dims)
117121
T = mapreduce(eltype, promote_type, args)

test/REQUIRE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
Tables
2+
PooledArrays
3+
WeakRefStrings

test/runtests.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using StructArrays
2-
import Tables
2+
import Tables, PooledArrays, WeakRefStrings
33
using Test
44

55
# write your own tests here
@@ -18,6 +18,21 @@ end
1818
@test StructArrays.propertynames(StructArrays.fieldarrays(t)) == (:a, :b)
1919
end
2020

21+
@testset "permute" begin
22+
a = WeakRefStrings.StringVector(["a", "b", "c"])
23+
b = PooledArrays.PooledArray([1, 2, 3])
24+
c = [:a, :b, :c]
25+
s = StructArray(a=a, b=b, c=c)
26+
permute!(s, [2, 3, 1])
27+
@test s.a == ["b", "c", "a"]
28+
@test s.b == [2, 3, 1]
29+
@test s.c == [:b, :c, :a]
30+
s = StructArray(a=[1, 2], b=["a", "b"])
31+
t = StructArray(a=[3, 4], b=["c", "d"])
32+
copyto!(s, t)
33+
@test s == t
34+
end
35+
2136
@testset "similar" begin
2237
t = StructArray(a = rand(10), b = rand(Bool, 10))
2338
s = similar(t)

0 commit comments

Comments
 (0)