Skip to content

Commit

Permalink
allow comparison optimizations in TiedIndices (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
piever authored Mar 17, 2019
1 parent 28f1525 commit 3e2ee9d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
julia 0.7
PooledArrays 0.5
Requires
5 changes: 2 additions & 3 deletions src/StructArrays.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module StructArrays

import Requires
using PooledArrays: PooledArray

export StructArray, StructVector, LazyRow, LazyRows
export collect_structarray, fieldarrays

Expand All @@ -13,9 +15,6 @@ include("lazy.jl")

function __init__()
Requires.@require Tables="bd369af6-aec1-5ad0-b16a-f7cc5008161c" include("tables.jl")
Requires.@require PooledArrays="2dfb63ee-cc39-5dd5-95bd-886bf059d720" begin
fastpermute!(v::PooledArrays.PooledArray, p::AbstractVector) = permute!(v, p)
end
Requires.@require WeakRefStrings="ea10d353-3f73-51f8-a26c-33c1cb351aa5" begin
fastpermute!(v::WeakRefStrings.StringArray, p::AbstractVector) = permute!(v, p)
end
Expand Down
10 changes: 9 additions & 1 deletion src/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ using Base.Sort, Base.Order

fastpermute!(v::AbstractArray, p::AbstractVector) = copyto!(v, v[p])
fastpermute!(v::StructArray, p::AbstractVector) = permute!(v, p)
fastpermute!(v::PooledArray, p::AbstractVector) = permute!(v, p)

optimize_isequal(v::AbstractArray) = v
optimize_isequal(v::PooledArray) = v.refs
optimize_isequal(v::StructArray{<:Union{Tuple, NamedTuple}}) = StructArray(map(optimize_isequal, fieldarrays(v)))

pool(v::AbstractArray, condition = !isbitstypeeltype) = condition(v) ? convert(PooledArray, v) : v
pool(v::StructArray, condition = !isbitstypeeltype) = replace_storage(t -> pool(t, condition), v)

function Base.permute!(c::StructArray, p::AbstractVector)
foreachfield(v -> fastpermute!(v, p), c)
Expand Down Expand Up @@ -76,7 +84,7 @@ function refine_perm!(p, cols, c, x, y′, lo, hi)
order = Perm(Forward, y′)
y = something(forward_vec(order), y′)
nc = length(cols)
for (_, idxs) in TiedIndices(x, p, lo:hi)
for (_, idxs) in TiedIndices(optimize_isequal(x), p, lo:hi)
i, i1 = extrema(idxs)
if i1 > i
sort_sub_by!(p, i, i1, y, order, temp)
Expand Down
1 change: 0 additions & 1 deletion test/REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Tables
PooledArrays
WeakRefStrings
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ end
@test all(v.b .== v_pooled.b)
@test !isa(v_pooled.a, PooledArrays.PooledArray)
@test isa(v_pooled.b, PooledArrays.PooledArray)
@test v_pooled == StructArrays.pool(v)
end

@testset "optimize_isequal" begin
a = ["a", "b", "a", "a"]
b = PooledArrays.PooledArray(["x", "y", "z", "x"])
s = StructArray((a, b))
t = StructArrays.optimize_isequal(s)
@test t[1] != t[2]
@test t[1] != t[3]
@test t[1] == t[4]
@test t[1][2] isa Integer
end

@testset "namedtuple" begin
Expand Down

0 comments on commit 3e2ee9d

Please sign in to comment.