Skip to content

Commit 4b5077d

Browse files
author
Pietro Vertechi
authored
Clean up lazy things (#74)
* Add LazyRows getproperty for nameless case and show methods * Tests * Style * Test to_tup * Don't check inferrability due to julia 1.0
1 parent 0dcdeef commit 4b5077d

File tree

5 files changed

+84
-10
lines changed

5 files changed

+84
-10
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ os:
55
- osx
66
julia:
77
- 1.0
8+
- 1.1
89
- nightly
910
notifications:
1011
email: false

src/lazy.jl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,24 @@ struct LazyRow{T, N, C, I}
33
index::I
44
end
55

6-
Base.getproperty(c::LazyRow, nm::Symbol) = getproperty(getfield(c, 1), nm)[getfield(c, 2)]
7-
Base.setproperty!(c::LazyRow, nm::Symbol, val) = (getproperty(getfield(c, 1), nm)[getfield(c, 2)] = val; nothing)
6+
for typ in [:Symbol, :Int]
7+
@eval begin
8+
Base.@propagate_inbounds function Base.getproperty(c::LazyRow, nm::$typ)
9+
return getproperty(getfield(c, 1), nm)[getfield(c, 2)]
10+
end
11+
Base.@propagate_inbounds function Base.setproperty!(c::LazyRow, nm::$typ, val)
12+
getproperty(getfield(c, 1), nm)[getfield(c, 2)] = val
13+
return
14+
end
15+
end
16+
end
817
Base.propertynames(c::LazyRow) = propertynames(getfield(c, 1))
918

19+
function Base.show(io::IO, c::LazyRow)
20+
print(io, "LazyRow")
21+
show(io, to_tup(c))
22+
end
23+
1024
staticschema(::Type{<:LazyRow{T}}) where {T} = staticschema(T)
1125
buildfromschema(f, ::Type{<:LazyRow{T}}) where {T} = buildfromschema(f, T)
1226

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

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

3045
Base.IndexStyle(::Type{<:LazyRows{<:Any, <:Any, <:Any, <:Integer}}) = IndexLinear()
46+
47+
function Base.showarg(io::IO, s::LazyRows{T}, toplevel) where T
48+
print(io, "LazyRows")
49+
showfields(io, Tuple(fieldarrays(s)))
50+
toplevel && print(io, " with eltype LazyRow{", T, "}")
51+
end

src/structarray.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,17 @@ function Base.reshape(s::StructArray{T}, d::Dims) where {T}
185185
StructArray{T}(map(x -> reshape(x, d), fieldarrays(s)))
186186
end
187187

188-
function Base.showarg(io::IO, s::StructArray{T}, toplevel) where T
189-
print(io, "StructArray(")
190-
fields = Tuple(fieldarrays(s))
191-
for field in fields[1:end-1]
192-
Base.showarg(io, field, false)
193-
print(io, ", ")
188+
function showfields(io::IO, fields::NTuple{N, Any}) where N
189+
print(io, "(")
190+
for (i, field) in enumerate(fields)
191+
Base.showarg(io, fields[i], false)
192+
i < N && print(io, ", ")
194193
end
195-
Base.showarg(io, last(fields), false)
196194
print(io, ")")
195+
end
196+
197+
function Base.showarg(io::IO, s::StructArray{T}, toplevel) where T
198+
print(io, "StructArray")
199+
showfields(io, Tuple(fieldarrays(s)))
197200
toplevel && print(io, " with eltype ", T)
198201
end

src/utils.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,9 @@ function replace_storage(f, s::StructArray{T}) where T
118118
StructArray{T}(newcols)
119119
end
120120

121+
to_tup(c::T) where {T} = to_tup(c, fields(T))
122+
function to_tup(c, fields::NTuple{N, Symbol}) where N
123+
t = ntuple(i -> getproperty(c, fields[i]), N)
124+
return NamedTuple{fields}(t)
125+
end
126+
to_tup(c, fields::NTuple{N, Int}) where {N} = ntuple(i -> _getproperty(c, fields[i]), N)

test/runtests.jl

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ end
255255
end
256256

257257
f_infer() = StructArray{ComplexF64}((rand(2,2), rand(2,2)))
258-
259258
g_infer() = StructArray([(a=(b="1",), c=2)], unwrap = t -> t <: NamedTuple)
260259
tup_infer() = StructArray([(1, 2), (3, 4)])
261260
cols_infer() = StructArray(([1, 2], [1.2, 2.3]))
@@ -271,6 +270,11 @@ cols_infer() = StructArray(([1, 2], [1.2, 2.3]))
271270
@inferred cols_infer()
272271
end
273272

273+
@testset "to_(named)tuple" begin
274+
@test StructArrays.to_tup((1, 2, 3)) == (1, 2, 3)
275+
@test StructArrays.to_tup(2 + 3im) == (re = 2, im = 3)
276+
end
277+
274278
@testset "propertynames" begin
275279
a = StructArray{ComplexF64}((Float64[], Float64[]))
276280
@test sort(collect(propertynames(a))) == [:im, :re]
@@ -490,8 +494,47 @@ end
490494
@test all(t -> t.re >= 0, s)
491495
@test all(t -> t.re >= 0, rows)
492496
rows[13].re = -12
497+
rows[13].im = 0
493498
@test !all(t -> t.re >= 0, s)
494499
@test !all(t -> t.re >= 0, rows)
500+
501+
io = IOBuffer()
502+
show(io, rows[13])
503+
str = String(take!(io))
504+
@test str == "LazyRow(re = -12.0, im = 0.0)"
505+
506+
io = IOBuffer()
507+
Base.showarg(io, rows, true)
508+
str = String(take!(io))
509+
@test str == "LazyRows(::Array{Float64,2}, ::Array{Float64,2}) with eltype LazyRow{Complex{Float64}}"
510+
io = IOBuffer()
511+
Base.showarg(io, rows, false)
512+
str = String(take!(io))
513+
@test str == "LazyRows(::Array{Float64,2}, ::Array{Float64,2})"
514+
515+
s = StructArray((rand(10, 10), rand(10, 10)))
516+
rows = LazyRows(s)
517+
@test IndexStyle(rows) isa IndexLinear
518+
@test all(t -> StructArrays._getproperty(t, 1) >= 0, s)
519+
@test all(t -> getproperty(t, 1) >= 0, rows)
520+
setproperty!(rows[13], 1, -12)
521+
setproperty!(rows[13], 2, 0)
522+
@test !all(t -> StructArrays._getproperty(t, 1) >= 0, s)
523+
@test !all(t -> getproperty(t, 1) >= 0, rows)
524+
525+
io = IOBuffer()
526+
show(io, rows[13])
527+
str = String(take!(io))
528+
@test str == "LazyRow(-12.0, 0.0)"
529+
530+
io = IOBuffer()
531+
Base.showarg(io, rows, true)
532+
str = String(take!(io))
533+
@test str == "LazyRows(::Array{Float64,2}, ::Array{Float64,2}) with eltype LazyRow{Tuple{Float64,Float64}}"
534+
io = IOBuffer()
535+
Base.showarg(io, rows, false)
536+
str = String(take!(io))
537+
@test str == "LazyRows(::Array{Float64,2}, ::Array{Float64,2})"
495538
end
496539

497540
@testset "refs" begin

0 commit comments

Comments
 (0)