Skip to content

Commit b398788

Browse files
authored
fix ambiguities with Base/OffsetArrays (#233)
1 parent dfeeb75 commit b398788

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StructArrays"
22
uuid = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
3-
version = "0.6.9"
3+
version = "0.6.10"
44

55
[deps]
66
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/structarray.jl

+17-9
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,6 @@ Base.convert(::Type{StructArray}, v::StructArray) = v
276276
Base.convert(::Type{StructVector}, v::AbstractVector) = StructVector(v)
277277
Base.convert(::Type{StructVector}, v::StructVector) = v
278278

279-
# Mimic OffsetArrays signatures
280-
const OffsetAxisKnownLength = Union{Integer, AbstractUnitRange}
281-
const OffsetAxis = Union{OffsetAxisKnownLength, Colon}
282-
283-
const OffsetShapeKnownLength = Tuple{OffsetAxisKnownLength,Vararg{OffsetAxisKnownLength}}
284-
const OffsetShape = Tuple{OffsetAxis,Vararg{OffsetAxis}}
285-
286279
# Helper function to avoid adding too many dispatches to `Base.similar`
287280
function _similar(s::StructArray{T}, ::Type{T}, sz) where {T}
288281
return StructArray{T}(map(typ -> similar(typ, sz), components(s)))
@@ -296,7 +289,13 @@ function _similar(s::StructArray{T}, S::Type, sz) where {T}
296289
return isnonemptystructtype(S) ? buildfromschema(typ -> similar(c1, typ, sz), S) : similar(c1, S, sz)
297290
end
298291

299-
for type in (:Dims, :OffsetShapeKnownLength)
292+
for type in (
293+
:Dims,
294+
# mimic OffsetArrays signature
295+
:(Tuple{Union{Integer, AbstractUnitRange}, Vararg{Union{Integer, AbstractUnitRange}}}),
296+
# disambiguation with Base
297+
:(Tuple{Union{Integer, Base.OneTo}, Vararg{Union{Integer, Base.OneTo}}}),
298+
)
300299
@eval function Base.similar(::Type{<:StructArray{T, N, C}}, sz::$(type)) where {T, N, C}
301300
return buildfromschema(typ -> similar(typ, sz), T, C)
302301
end
@@ -457,7 +456,16 @@ end
457456

458457
Base.copy(s::StructArray{T}) where {T} = StructArray{T}(map(copy, components(s)))
459458

460-
for type in (:Dims, :OffsetShape)
459+
for type in (
460+
:Dims,
461+
# mimic OffsetArrays signature
462+
:(Tuple{Union{Integer, AbstractUnitRange, Colon}, Vararg{Union{Integer, AbstractUnitRange, Colon}}}),
463+
# disambiguation with Base
464+
:(Tuple{Union{Integer, Base.OneTo}, Vararg{Union{Integer, Base.OneTo}}}),
465+
:(Tuple{Vararg{Union{Colon, Integer}}}),
466+
:(Tuple{Vararg{Union{Colon, Int}}}),
467+
:(Tuple{Colon}),
468+
)
461469
@eval function Base.reshape(s::StructArray{T}, d::$(type)) where {T}
462470
StructArray{T}(map(x -> reshape(x, d), components(s)))
463471
end

test/runtests.jl

+57
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,24 @@ end
354354
s = similar(t, Float32, 0:1, 2)
355355
@test s isa OffsetMatrix{Float32, Matrix{Float32}}
356356
@test axes(s) == (0:1, 1:2)
357+
358+
s = similar(t, ComplexF64, (Base.OneTo(2),))
359+
@test s isa StructArray
360+
@test s.re isa Vector{Float64}
361+
@test axes(s) == (1:2,)
362+
363+
s = similar(t, Int, (Base.OneTo(2),))
364+
@test s isa Vector{Int}
365+
@test axes(s) == (1:2,)
366+
367+
s = similar(t, ComplexF64, (Base.IdentityUnitRange(5:7),))
368+
@test s isa StructArray
369+
@test s.re isa OffsetVector{Float64}
370+
@test axes(s) == (5:7,)
371+
372+
s = similar(t, Int, (Base.IdentityUnitRange(5:7),))
373+
@test s isa OffsetVector{Int}
374+
@test axes(s) == (5:7,)
357375
end
358376

359377
@testset "similar type" begin
@@ -844,13 +862,30 @@ end
844862

845863
@testset "reshape" begin
846864
s = StructArray(a=[1,2,3,4], b=["a","b","c","d"])
865+
847866
rs = reshape(s, (2, 2))
848867
@test rs.a == [1 3; 2 4]
849868
@test rs.b == ["a" "c"; "b" "d"]
850869

870+
rs = reshape(s, (:,))
871+
@test rs.a == s.a
872+
@test rs.b == s.b
873+
874+
rs = reshape(s, (2, :))
875+
@test rs.a == [1 3; 2 4]
876+
@test rs.b == ["a" "c"; "b" "d"]
877+
878+
rs = reshape(s, (2, Base.OneTo(2)))
879+
@test rs.a == [1 3; 2 4]
880+
@test rs.b == ["a" "c"; "b" "d"]
881+
851882
rs = reshape(s, (0:1, :))
852883
@test rs.a == OffsetArray([1 3; 2 4], (-1, 0))
853884
@test rs.b == OffsetArray(["a" "c"; "b" "d"], (-1, 0))
885+
886+
rs = reshape(s, (0:1, 1:2))
887+
@test rs.a == OffsetArray([1 3; 2 4], (-1, 0))
888+
@test rs.b == OffsetArray(["a" "c"; "b" "d"], (-1, 0))
854889
end
855890

856891
@testset "lazy" begin
@@ -1072,6 +1107,28 @@ Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{MyArray}}, ::Type{El
10721107
@test @inferred(broadcast(el -> el.a, v)) == ["s1", "s2"]
10731108
end
10741109

1110+
@testset "map" begin
1111+
s = StructArray(a=[1, 2, 3])
1112+
1113+
t = @inferred(map(x -> x, s))
1114+
@test t isa StructArray
1115+
@test t == s
1116+
1117+
t = @inferred(map(x -> x.a, s))
1118+
@test t isa Vector
1119+
@test t == [1, 2, 3]
1120+
1121+
t = VERSION >= v"1.7" ? @inferred(map(x -> (a=x.a, b=2), s)) : map(x -> (a=x.a, b=2), s)
1122+
@test t isa StructArray
1123+
@test map(x -> (a=x.a, b=2), s) == [(a=1, b=2), (a=2, b=2), (a=3, b=2)]
1124+
1125+
so = reshape(s, Base.IdentityUnitRange(11:13))
1126+
to = @inferred(map(x -> x, so))
1127+
@test to isa StructArray
1128+
@test axes(to) == axes(so)
1129+
@test to == so
1130+
end
1131+
10751132
@testset "staticarrays" begin
10761133
# test that staticschema returns the right things
10771134
for StaticVectorType = [SVector, MVector, SizedVector]

0 commit comments

Comments
 (0)