Skip to content

Commit 0dcdeef

Browse files
author
Pietro Vertechi
authored
Add show method and doc cleanup (#73)
* Add showarg method * Test show method * Export replace_storage, update docs
1 parent 075ddc0 commit 0dcdeef

File tree

5 files changed

+77
-37
lines changed

5 files changed

+77
-37
lines changed

README.md

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ julia> using StructArrays, Random
1717
julia> Random.seed!(4);
1818

1919
julia> s = StructArray{ComplexF64}((rand(2,2), rand(2,2)))
20-
2×2 StructArray{Complex{Float64},2,NamedTuple{(:re, :im),Tuple{Array{Float64,2},Array{Float64,2}}}}:
20+
2×2 StructArray(::Array{Float64,2}, ::Array{Float64,2}) with eltype Complex{Float64}:
2121
0.680079+0.625239im 0.92407+0.267358im
2222
0.874437+0.737254im 0.929336+0.804478im
2323

@@ -37,7 +37,7 @@ Note that the same approach can be used directly from an `Array` of complex numb
3737

3838
```julia
3939
julia> StructArray([1+im, 3-2im])
40-
2-element StructArray{Complex{Int64},1,NamedTuple{(:re, :im),Tuple{Array{Int64,1},Array{Int64,1}}}}:
40+
2-element StructArray(::Array{Int64,1}, ::Array{Int64,1}) with eltype Complex{Int64}:
4141
1 + 1im
4242
3 - 2im
4343
```
@@ -48,7 +48,7 @@ One can also create a `StructArrray` from an iterable of structs without creatin
4848

4949
```julia
5050
julia> StructArray(log(j+2.0*im) for j in 1:10)
51-
10-element StructArray{Complex{Float64},1,NamedTuple{(:re, :im),Tuple{Array{Float64,1},Array{Float64,1}}}}:
51+
10-element StructArray(::Array{Float64,1}, ::Array{Float64,1}) with eltype Complex{Float64}:
5252
0.8047189562170501 + 1.1071487177940904im
5353
1.0397207708399179 + 0.7853981633974483im
5454
1.2824746787307684 + 0.5880026035475675im
@@ -65,14 +65,14 @@ Another option is to create an uninitialized `StructArray` and then fill it with
6565

6666
```julia
6767
julia> s = StructArray{ComplexF64}(undef, 2, 2)
68-
2×2 StructArray{Complex{Float64},2,NamedTuple{(:re, :im),Tuple{Array{Float64,2},Array{Float64,2}}}}:
68+
2×2 StructArray(::Array{Float64,2}, ::Array{Float64,2}) with eltype Complex{Float64}:
6969
6.91646e-310+6.91646e-310im 6.91646e-310+6.91646e-310im
7070
6.91646e-310+6.91646e-310im 6.91646e-310+6.91646e-310im
7171

7272
julia> rand!(s)
73-
2×2 StructArray{Complex{Float64},2,NamedTuple{(:re, :im),Tuple{Array{Float64,2},Array{Float64,2}}}}:
74-
0.446415+0.671453im 0.0797964+0.675723im
75-
0.0340059+0.420472im 0.907252+0.808263im
73+
2×2 StructArray(::Array{Float64,2}, ::Array{Float64,2}) with eltype Complex{Float64}:
74+
0.680079+0.874437im 0.625239+0.737254im
75+
0.92407+0.929336im 0.267358+0.804478im
7676
```
7777

7878
### Using custom array types
@@ -87,39 +87,53 @@ julia> a = CuArray(rand(Float32, 10));
8787
julia> b = CuArray(rand(Float32, 10));
8888

8989
julia> StructArray{ComplexF32}((a, b))
90-
10-element StructArray{Complex{Float32},1,NamedTuple{(:re, :im),Tuple{CuArray{Float32,1},CuArray{Float32,1}}}}:
91-
0.7539003f0 + 0.5406891f0im
92-
0.2818451f0 + 0.60345674f0im
93-
0.3271774f0 + 0.56674314f0im
94-
0.6943406f0 + 0.8360009f0im
95-
0.9609026f0 + 0.27519035f0im
96-
0.051933408f0 + 0.93443274f0im
97-
0.51335454f0 + 0.90320504f0im
98-
0.6588727f0 + 0.16270757f0im
99-
0.20075476f0 + 0.6591008f0im
100-
0.58832633f0 + 0.45309567f0im
90+
10-element StructArray(::CuArray{Float32,1}, ::CuArray{Float32,1}) with eltype Complex{Float32}:
91+
0.19555175f0 + 0.9604322f0im
92+
0.68348145f0 + 0.5778245f0im
93+
0.69664395f0 + 0.79825306f0im
94+
0.118531585f0 + 0.3031248f0im
95+
0.80057466f0 + 0.8964418f0im
96+
0.63772964f0 + 0.2923274f0im
97+
0.65374136f0 + 0.7932533f0im
98+
0.6043732f0 + 0.65964353f0im
99+
0.1106627f0 + 0.090207934f0im
100+
0.707458f0 + 0.1700114f0im
101101

102102
julia> c = CuArray(rand(ComplexF32, 10));
103103

104104
julia> StructArray(c)
105-
10-element StructArray{Complex{Float32},1,NamedTuple{(:re, :im),Tuple{CuArray{Float32,1},CuArray{Float32,1}}}}:
106-
0.76695776f0 + 0.31588173f0im
107-
0.9804857f0 + 0.15740407f0im
108-
0.85849273f0 + 0.51903546f0im
109-
0.106796384f0 + 0.9493377f0im
110-
0.38152885f0 + 0.8419838f0im
111-
0.8892112f0 + 0.5276251f0im
112-
0.11579132f0 + 0.79168653f0im
113-
0.16804445f0 + 0.40545344f0im
114-
0.42822742f0 + 0.61150527f0im
115-
0.29996157f0 + 0.94151044f0im
105+
10-element StructArray(::Array{Float32,1}, ::Array{Float32,1}) with eltype Complex{Float32}:
106+
0.7176411f0 + 0.864058f0im
107+
0.252609f0 + 0.14824867f0im
108+
0.26842773f0 + 0.9084332f0im
109+
0.33128333f0 + 0.5106474f0im
110+
0.6509278f0 + 0.87059164f0im
111+
0.9522146f0 + 0.053706646f0im
112+
0.899577f0 + 0.63242567f0im
113+
0.325814f0 + 0.59225655f0im
114+
0.56267905f0 + 0.21927536f0im
115+
0.49719965f0 + 0.754143f0im
116+
```
117+
118+
If you already have your data in a `StructArray` with field arrays of a given format (say plain `Array`) you can change them with `replace_storage`:
119+
120+
```julia
121+
julia> s = StructArray([1.0+im, 2.0-im])
122+
2-element StructArray(::Array{Float64,1}, ::Array{Float64,1}) with eltype Complex{Float64}:
123+
1.0 + 1.0im
124+
2.0 - 1.0im
125+
126+
julia> replace_storage(CuArray, s)
127+
2-element StructArray(::CuArray{Float64,1}, ::CuArray{Float64,1}) with eltype Complex{Float64}:
128+
1.0 + 1.0im
129+
2.0 - 1.0im
116130
```
117131

118132
## Example usage to store a data table
119133

120134
```julia
121135
julia> t = StructArray((a = [1, 2], b = ["x", "y"]))
122-
2-element StructArray{NamedTuple{(:a, :b),Tuple{Int64,String}},1,NamedTuple{(:a, :b),Tuple{Array{Int64,1},Array{String,1}}}}:
136+
2-element StructArray(::Array{Int64,1}, ::Array{String,1}) with eltype NamedTuple{(:a, :b),Tuple{Int64,String}}:
123137
(a = 1, b = "x")
124138
(a = 2, b = "y")
125139

@@ -132,7 +146,7 @@ julia> t.a
132146
2
133147

134148
julia> push!(t, (a = 3, b = "z"))
135-
3-element StructArray{NamedTuple{(:a, :b),Tuple{Int64,String}},1,NamedTuple{(:a, :b),Tuple{Array{Int64,1},Array{String,1}}}}:
149+
3-element StructArray(::Array{Int64,1}, ::Array{String,1}) with eltype NamedTuple{(:a, :b),Tuple{Int64,String}}:
136150
(a = 1, b = "x")
137151
(a = 2, b = "y")
138152
(a = 3, b = "z")
@@ -143,15 +157,16 @@ julia> push!(t, (a = 3, b = "z"))
143157
StructArrays also provides a `LazyRow` wrapper for lazy row iteration. `LazyRow(t, i)` does not materialize the i-th row but returns a lazy wrapper around it on which `getproperty` does the correct thing. This is useful when the row has many fields only some of which are necessary. It also allows changing columns in place.
144158

145159
```julia
160+
julia> t = StructArray((a = [1, 2], b = ["x", "y"]));
161+
146162
julia> LazyRow(t, 2).a
147163
2
148164

149165
julia> LazyRow(t, 2).a = 123
150166
123
151167

152168
julia> t
153-
2-element StructArray{NamedTuple{(:a, :b),Tuple{Int64,String}},1,NamedTuple{(:a, :b),Tu
154-
ple{Array{Int64,1},Array{String,1}}}}:
169+
2-element StructArray(::Array{Int64,1}, ::Array{String,1}) with eltype NamedTuple{(:a, :b),Tuple{Int64,String}}:
155170
(a = 1, b = "x")
156171
(a = 123, b = "y")
157172
```

src/StructArrays.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using PooledArrays: PooledArray
55

66
export StructArray, StructVector, LazyRow, LazyRows
77
export collect_structarray, fieldarrays
8+
export replace_storage
89

910
include("interface.jl")
1011
include("structarray.jl")

src/structarray.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,15 @@ Base.copy(s::StructArray{T,N,C}) where {T,N,C} = StructArray{T,N,C}(C(copy(x) fo
184184
function Base.reshape(s::StructArray{T}, d::Dims) where {T}
185185
StructArray{T}(map(x -> reshape(x, d), fieldarrays(s)))
186186
end
187+
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, ", ")
194+
end
195+
Base.showarg(io, last(fields), false)
196+
print(io, ")")
197+
toplevel && print(io, " with eltype ", T)
198+
end

src/utils.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ julia> s = StructArray(a=1:3, b = fill("string", 3));
106106
julia> s_pooled = StructArrays.replace_storage(s) do v
107107
isbitstype(eltype(v)) ? v : convert(PooledArray, v)
108108
end
109-
10-element StructArray{NamedTuple{(:a, :b),Tuple{Int64,String}},1,NamedTuple{(:a, :b),Tuple{UnitRange{Int64},PooledArray{String,UInt8,1,Array{UInt8,1}}}}}:
110-
(a = 1, b = "string")
111-
(a = 2, b = "string")
112-
(a = 3, b = "string")
109+
3-element StructArray(::UnitRange{Int64}, ::PooledArray{String,UInt8,1,Array{UInt8,1}}) with eltype NamedTuple{(:a, :b),Tuple{Int64,String}}:
110+
(a = 1, b = "string")
111+
(a = 2, b = "string")
112+
(a = 3, b = "string")
113113
```
114114
"""
115115
function replace_storage(f, s::StructArray{T}) where T

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,15 @@ end
505505
@test StructArrays.refs(s) isa WeakRefStrings.StringArray{Union{WeakRefStrings.WeakRefString{UInt8}, Missing}}
506506
@test all(isequal.(s, StructArrays.refs(s)))
507507
end
508+
509+
@testset "show" begin
510+
s = StructArray(Complex{Int64}[1+im, 2-im])
511+
io = IOBuffer()
512+
Base.showarg(io, s, true)
513+
str = String(take!(io))
514+
@test str == "StructArray(::Array{Int64,1}, ::Array{Int64,1}) with eltype Complex{Int64}"
515+
io = IOBuffer()
516+
Base.showarg(io, s, false)
517+
str = String(take!(io))
518+
@test str == "StructArray(::Array{Int64,1}, ::Array{Int64,1})"
519+
end

0 commit comments

Comments
 (0)