Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/SparseArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ function sparse(S::StructArray{T}) where {T}
return createinstance.(T, sparse_components...)
end

issparse(S::StructArray) = all(issparse, components(S))
issparse(S::StructArray) = StructArrays._allcols(issparse, components(S))

end
5 changes: 2 additions & 3 deletions src/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ Base.eltype(::Type{<:GroupPerm}) = UnitRange{Int}
return eq
end

roweq(t::Tuple{}, i, j) = true
roweq(t::Tuple, i, j) = roweq(t[1], i, j) ? roweq(tail(t), i, j) : false
roweq(s::StructArray, i, j) = roweq(Tuple(components(s)), i, j)
roweq(t::Tup, i, j) = _allcols(c -> roweq(c, i, j), t)
roweq(s::StructArray, i, j) = roweq(components(s), i, j)

function uniquesorted(keys, perm=sortperm(keys))
(keys[perm[idxs[1]]] for idxs in GroupPerm(keys, perm))
Expand Down
15 changes: 5 additions & 10 deletions src/structarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ struct StructArray{T, N, C<:Tup, I} <: AbstractArray{T, N}
end
end

# compute optimal type to use for indexing as a function of components
index_type(components::NamedTuple) = index_type(values(components))
index_type(::Tuple{}) = Int
function index_type(components::Tuple)
f, ls = first(components), tail(components)
return IndexStyle(f) isa IndexCartesian ? CartesianIndex{ndims(f)} : index_type(ls)
end
# Only check first component if the all the component types match
index_type(components::NTuple) = invoke(index_type, Tuple{Tuple}, (first(components),))
# compute optimal type to use for indexing as a function of components,
# which all have the same `ndims` (guaranteed by the inner constructor)
index_type(components::Tup) =
_anycols(c -> IndexStyle(c) isa IndexCartesian, components) ? CartesianIndex{ndims(first(components))} : Int
# Return the index type parameter as a function of the StructArray type or instance
index_type(s::StructArray) = index_type(typeof(s))
index_type(::Type{StructArray{T, N, C, I}}) where {T, N, C, I} = I
Expand Down Expand Up @@ -646,7 +641,7 @@ end

# for aliasing analysis during broadcast
function Broadcast.broadcast_unalias(dest::StructArray, src::AbstractArray)
if dest === src || any(Base.Fix2(===, src), values(components(dest))) # values() works around Julia 1.13 allocation regression
if dest === src || _anycols(Base.Fix2(===, src), components(dest))
return src
else
return Base.unalias(dest, src)
Expand Down
9 changes: 7 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Unlike `map`, stays inferrable for any number of elements: `map` gives up on tup
@inline @generated _mapcols(f::F, cols::Tup) where {F} =
:($(strip_params(cols))(Base.Cartesian.@ntuple $(fieldcount(cols)) i -> f(cols[i])))

# Short-circuiting `all(f, cols)` and `any(f, cols)` that stay allocation-free for any number of elements, including NamedTuples
@inline @generated _allcols(f::F, cols::Tup) where {F} =
:(Base.Cartesian.@nall $(fieldcount(cols)) i -> f(cols[i]))
@inline @generated _anycols(f::F, cols::Tup) where {F} =
:(Base.Cartesian.@nany $(fieldcount(cols)) i -> f(cols[i]))

buildfromschema(initializer::F, ::Type{T}) where {F, T} = buildfromschema(initializer, T, staticschema(T))

"""
Expand Down Expand Up @@ -210,6 +216,5 @@ If not all values are equal, return `nothing`. Otherwise, return the unique valu
"""
function findconsistentvalue(f::F, cols::Tup) where F
val = f(first(cols))
isconsistent = all(_mapcols(isequal(val) ∘ f, cols))
return ifelse(isconsistent, val, nothing)
return _allcols(c -> isequal(val, f(c)), cols) ? val : nothing
end
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ end
cols = (a=rand(2), b=rand(2), c=rand(3))
@test isnothing(StructArrays.findconsistentvalue(length, cols))
@test isnothing(StructArrays.findconsistentvalue(length, Tuple(cols)))

# stops at the first inconsistent component
ncalls = Ref(0)
countlength(c) = (ncalls[] += 1; length(c))
@test isnothing(StructArrays.findconsistentvalue(countlength, ([1], [1, 2], [1], [1])))
@test ncalls[] == 3
end

@testset "indexstyle" begin
Expand Down Expand Up @@ -830,6 +836,16 @@ end
@test allocs(storerow!, out, t, 1) == 0
@test allocs(storepop!, out, pop!, copy(t)) == 0
@test allocs(storepop!, out, popfirst!, copy(t)) == 0
# equal rows, so that every column is compared
@test StructArrays.roweq(t, 1, 1)
@test !StructArrays.roweq(t, 1, 2)
@test allocs(StructArrays.roweq, t, 1, 1) == 0
# the last component, so that every component is checked
@test allocs(Base.Broadcast.broadcast_unalias, t, last(components(t))) == 0
# sparse components, so that `issparse` is called on every one
sp = StructArray(map(sparse, components(t)))
@test issparse(sp)
@test allocs(issparse, sp) == 0
end

s = StructArray{Wide64}(widecols(64, true))
Expand Down Expand Up @@ -1309,6 +1325,13 @@ end
@test sp isa SparseMatrixCSC{eltype(sa)}
@test sp == sa
end
@testset "issparse" begin
s = StructArray(a = sparse([1, 0]), b = sparse([0, 1]))
@test issparse(s)
@test !issparse(StructArray(a = sparse([1, 0]), b = [0, 1]))
allocs(f::F, args...) where {F} = (f(args...); @allocated f(args...))
@test allocs(issparse, s) == 0
end
end

@testset "metadata" begin
Expand Down