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: 2 additions & 0 deletions src/lazy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ struct LazyRows{T, N, C, I} <: AbstractArray{LazyRow{T, N, C, I}, N}
columns::StructArray{T, N, C, I}
end
Base.parent(v::LazyRows) = getfield(v, 1)
Base.dataids(v::LazyRows) = Base.dataids(parent(v))
Base.unaliascopy(v::LazyRows) = LazyRows(Base.unaliascopy(parent(v)))
Comment thread
aplavin marked this conversation as resolved.
Resolved
Comment thread
aplavin marked this conversation as resolved.
Resolved
components(v::LazyRows) = components(parent(v))

component(v::LazyRows, key) = component(parent(v), key)
Expand Down
1 change: 1 addition & 0 deletions src/structarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ function Broadcast.broadcast_unalias(dest::StructArray, src::AbstractArray)
end

Base.dataids(u::StructArray) = mapreduce(Base.dataids, (a, b) -> (a..., b...), values(components(u)), init=())
Base.unaliascopy(s::StructArray{T}) where {T} = StructArray{T}(map(Base.unaliascopy, components(s)))

# Since all the components have the same axes, we choose the type of the first one to
# define IteratorSize for a StructArray
Expand Down
27 changes: 27 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,24 @@ end
Base.showarg(io, rows, false)
str = String(take!(io))
@test str == "LazyRows(::Matrix{Float64}, ::Matrix{Float64})"

# LazyRows shares memory with its columns
s = StructArray(a=[1, 2, 3, 4], b=[10, 20, 30, 40])
rows = LazyRows(s)
@test Base.dataids(rows) == Base.dataids(s)
@test Base.mightalias(rows, s.a)
@test Base.mightalias(view(rows, 2:3), s.b)
@test !Base.mightalias(rows, [1, 2, 3, 4])
rc = Base.unaliascopy(rows)
@test rc isa typeof(rows)
@test parent(rc) == s
@test !Base.mightalias(rc, s)
s.a .= (r -> r.a).(view(rows, 4:-1:1))
@test s.a == [4, 3, 2, 1]
s = StructArray(a=view([1, 2, 3], :))
@test Base.unaliascopy(LazyRows(s)) isa typeof(LazyRows(s))
s.a .= (r -> r.a).(view(LazyRows(s), 3:-1:1))
@test s.a == [3, 2, 1]
end

@testset "refarray" begin
Expand Down Expand Up @@ -1361,6 +1379,15 @@ Base.BroadcastStyle(::Broadcast.ArrayStyle{MyArray2}, S::Broadcast.DefaultArrayS
# used inside of broadcast but we also test it here explicitly
@test isa(@inferred(Base.dataids(s)), NTuple{N, UInt} where {N})

# unaliasing keeps the exact type even when `copy` of a component would change it
u = StructArray(a=view([1, 2, 3], :), b=1:3, c=StructArray(x=reshape([1, 2, 3], 3)))
uc = @inferred Base.unaliascopy(u)
@test uc isa typeof(u)
@test uc == u
@test !Base.mightalias(uc, u)
u.a .= (r -> r.a + r.b + r.c.x).(view(u, 3:-1:1))
@test u.a == [9, 6, 3]


@testset "style conflict check" begin
using StructArrays: StructArrayStyle
Expand Down
Loading