Skip to content
Merged
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ os:
- osx
julia:
- 1.0
- 1.1
- nightly
notifications:
email: false
Expand Down
25 changes: 23 additions & 2 deletions src/lazy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@ struct LazyRow{T, N, C, I}
index::I
end

Base.getproperty(c::LazyRow, nm::Symbol) = getproperty(getfield(c, 1), nm)[getfield(c, 2)]
Base.setproperty!(c::LazyRow, nm::Symbol, val) = (getproperty(getfield(c, 1), nm)[getfield(c, 2)] = val; nothing)
for typ in [:Symbol, :Int]
@eval begin
Base.@propagate_inbounds function Base.getproperty(c::LazyRow, nm::$typ)
return getproperty(getfield(c, 1), nm)[getfield(c, 2)]
end
Base.@propagate_inbounds function Base.setproperty!(c::LazyRow, nm::$typ, val)
getproperty(getfield(c, 1), nm)[getfield(c, 2)] = val
return
end
end
end
Base.propertynames(c::LazyRow) = propertynames(getfield(c, 1))

function Base.show(io::IO, c::LazyRow)
print(io, "LazyRow")
show(io, to_tup(c))
end

staticschema(::Type{<:LazyRow{T}}) where {T} = staticschema(T)
buildfromschema(f, ::Type{<:LazyRow{T}}) where {T} = buildfromschema(f, T)

Expand All @@ -22,9 +36,16 @@ LazyRows(s::S) where {S<:StructArray} = LazyRows(IndexStyle(S), s)
LazyRows(::IndexLinear, s::StructArray{T, N, C}) where {T, N, C} = LazyRows{T, N, C, Int}(s)
LazyRows(::IndexCartesian, s::StructArray{T, N, C}) where {T, N, C} = LazyRows{T, N, C, CartesianIndex{N}}(s)
Base.parent(v::LazyRows) = getfield(v, 1)
fieldarrays(v::LazyRows) = fieldarrays(parent(v))

Base.size(v::LazyRows) = size(parent(v))
Base.getindex(v::LazyRows{<:Any, <:Any, <:Any, <:Integer}, i::Integer) = LazyRow(parent(v), i)
Base.getindex(v::LazyRows{<:Any, <:Any, <:Any, <:CartesianIndex}, i::Integer...) = LazyRow(parent(v), CartesianIndex(i))

Base.IndexStyle(::Type{<:LazyRows{<:Any, <:Any, <:Any, <:Integer}}) = IndexLinear()

function Base.showarg(io::IO, s::LazyRows{T}, toplevel) where T
print(io, "LazyRows")
showfields(io, Tuple(fieldarrays(s)))
toplevel && print(io, " with eltype LazyRow{", T, "}")
end
17 changes: 10 additions & 7 deletions src/structarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,17 @@ function Base.reshape(s::StructArray{T}, d::Dims) where {T}
StructArray{T}(map(x -> reshape(x, d), fieldarrays(s)))
end

function Base.showarg(io::IO, s::StructArray{T}, toplevel) where T
print(io, "StructArray(")
fields = Tuple(fieldarrays(s))
for field in fields[1:end-1]
Base.showarg(io, field, false)
print(io, ", ")
function showfields(io::IO, fields::NTuple{N, Any}) where N
print(io, "(")
for (i, field) in enumerate(fields)
Base.showarg(io, fields[i], false)
i < N && print(io, ", ")
end
Base.showarg(io, last(fields), false)
print(io, ")")
end

function Base.showarg(io::IO, s::StructArray{T}, toplevel) where T
print(io, "StructArray")
showfields(io, Tuple(fieldarrays(s)))
toplevel && print(io, " with eltype ", T)
end
6 changes: 6 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,9 @@ function replace_storage(f, s::StructArray{T}) where T
StructArray{T}(newcols)
end

to_tup(c::T) where {T} = to_tup(c, fields(T))
function to_tup(c, fields::NTuple{N, Symbol}) where N
t = ntuple(i -> getproperty(c, fields[i]), N)
return NamedTuple{fields}(t)
end
to_tup(c, fields::NTuple{N, Int}) where {N} = ntuple(i -> _getproperty(c, fields[i]), N)
45 changes: 44 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ end
end

f_infer() = StructArray{ComplexF64}((rand(2,2), rand(2,2)))

g_infer() = StructArray([(a=(b="1",), c=2)], unwrap = t -> t <: NamedTuple)
tup_infer() = StructArray([(1, 2), (3, 4)])
cols_infer() = StructArray(([1, 2], [1.2, 2.3]))
Expand All @@ -271,6 +270,11 @@ cols_infer() = StructArray(([1, 2], [1.2, 2.3]))
@inferred cols_infer()
end

@testset "to_(named)tuple" begin
@test StructArrays.to_tup((1, 2, 3)) == (1, 2, 3)
@test StructArrays.to_tup(2 + 3im) == (re = 2, im = 3)
end

@testset "propertynames" begin
a = StructArray{ComplexF64}((Float64[], Float64[]))
@test sort(collect(propertynames(a))) == [:im, :re]
Expand Down Expand Up @@ -490,8 +494,47 @@ end
@test all(t -> t.re >= 0, s)
@test all(t -> t.re >= 0, rows)
rows[13].re = -12
rows[13].im = 0
@test !all(t -> t.re >= 0, s)
@test !all(t -> t.re >= 0, rows)

io = IOBuffer()
show(io, rows[13])
str = String(take!(io))
@test str == "LazyRow(re = -12.0, im = 0.0)"

io = IOBuffer()
Base.showarg(io, rows, true)
str = String(take!(io))
@test str == "LazyRows(::Array{Float64,2}, ::Array{Float64,2}) with eltype LazyRow{Complex{Float64}}"
io = IOBuffer()
Base.showarg(io, rows, false)
str = String(take!(io))
@test str == "LazyRows(::Array{Float64,2}, ::Array{Float64,2})"

s = StructArray((rand(10, 10), rand(10, 10)))
rows = LazyRows(s)
@test IndexStyle(rows) isa IndexLinear
@test all(t -> StructArrays._getproperty(t, 1) >= 0, s)
@test all(t -> getproperty(t, 1) >= 0, rows)
setproperty!(rows[13], 1, -12)
setproperty!(rows[13], 2, 0)
@test !all(t -> StructArrays._getproperty(t, 1) >= 0, s)
@test !all(t -> getproperty(t, 1) >= 0, rows)

io = IOBuffer()
show(io, rows[13])
str = String(take!(io))
@test str == "LazyRow(-12.0, 0.0)"

io = IOBuffer()
Base.showarg(io, rows, true)
str = String(take!(io))
@test str == "LazyRows(::Array{Float64,2}, ::Array{Float64,2}) with eltype LazyRow{Tuple{Float64,Float64}}"
io = IOBuffer()
Base.showarg(io, rows, false)
str = String(take!(io))
@test str == "LazyRows(::Array{Float64,2}, ::Array{Float64,2})"
end

@testset "refs" begin
Expand Down