Skip to content

Commit

Permalink
fix ambiguities with Base/OffsetArrays (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin authored Jun 23, 2022
1 parent dfeeb75 commit b398788
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StructArrays"
uuid = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
version = "0.6.9"
version = "0.6.10"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
26 changes: 17 additions & 9 deletions src/structarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,6 @@ Base.convert(::Type{StructArray}, v::StructArray) = v
Base.convert(::Type{StructVector}, v::AbstractVector) = StructVector(v)
Base.convert(::Type{StructVector}, v::StructVector) = v

# Mimic OffsetArrays signatures
const OffsetAxisKnownLength = Union{Integer, AbstractUnitRange}
const OffsetAxis = Union{OffsetAxisKnownLength, Colon}

const OffsetShapeKnownLength = Tuple{OffsetAxisKnownLength,Vararg{OffsetAxisKnownLength}}
const OffsetShape = Tuple{OffsetAxis,Vararg{OffsetAxis}}

# Helper function to avoid adding too many dispatches to `Base.similar`
function _similar(s::StructArray{T}, ::Type{T}, sz) where {T}
return StructArray{T}(map(typ -> similar(typ, sz), components(s)))
Expand All @@ -296,7 +289,13 @@ function _similar(s::StructArray{T}, S::Type, sz) where {T}
return isnonemptystructtype(S) ? buildfromschema(typ -> similar(c1, typ, sz), S) : similar(c1, S, sz)
end

for type in (:Dims, :OffsetShapeKnownLength)
for type in (
:Dims,
# mimic OffsetArrays signature
:(Tuple{Union{Integer, AbstractUnitRange}, Vararg{Union{Integer, AbstractUnitRange}}}),
# disambiguation with Base
:(Tuple{Union{Integer, Base.OneTo}, Vararg{Union{Integer, Base.OneTo}}}),
)
@eval function Base.similar(::Type{<:StructArray{T, N, C}}, sz::$(type)) where {T, N, C}
return buildfromschema(typ -> similar(typ, sz), T, C)
end
Expand Down Expand Up @@ -457,7 +456,16 @@ end

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

for type in (:Dims, :OffsetShape)
for type in (
:Dims,
# mimic OffsetArrays signature
:(Tuple{Union{Integer, AbstractUnitRange, Colon}, Vararg{Union{Integer, AbstractUnitRange, Colon}}}),
# disambiguation with Base
:(Tuple{Union{Integer, Base.OneTo}, Vararg{Union{Integer, Base.OneTo}}}),
:(Tuple{Vararg{Union{Colon, Integer}}}),
:(Tuple{Vararg{Union{Colon, Int}}}),
:(Tuple{Colon}),
)
@eval function Base.reshape(s::StructArray{T}, d::$(type)) where {T}
StructArray{T}(map(x -> reshape(x, d), components(s)))
end
Expand Down
57 changes: 57 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,24 @@ end
s = similar(t, Float32, 0:1, 2)
@test s isa OffsetMatrix{Float32, Matrix{Float32}}
@test axes(s) == (0:1, 1:2)

s = similar(t, ComplexF64, (Base.OneTo(2),))
@test s isa StructArray
@test s.re isa Vector{Float64}
@test axes(s) == (1:2,)

s = similar(t, Int, (Base.OneTo(2),))
@test s isa Vector{Int}
@test axes(s) == (1:2,)

s = similar(t, ComplexF64, (Base.IdentityUnitRange(5:7),))
@test s isa StructArray
@test s.re isa OffsetVector{Float64}
@test axes(s) == (5:7,)

s = similar(t, Int, (Base.IdentityUnitRange(5:7),))
@test s isa OffsetVector{Int}
@test axes(s) == (5:7,)
end

@testset "similar type" begin
Expand Down Expand Up @@ -844,13 +862,30 @@ end

@testset "reshape" begin
s = StructArray(a=[1,2,3,4], b=["a","b","c","d"])

rs = reshape(s, (2, 2))
@test rs.a == [1 3; 2 4]
@test rs.b == ["a" "c"; "b" "d"]

rs = reshape(s, (:,))
@test rs.a == s.a
@test rs.b == s.b

rs = reshape(s, (2, :))
@test rs.a == [1 3; 2 4]
@test rs.b == ["a" "c"; "b" "d"]

rs = reshape(s, (2, Base.OneTo(2)))
@test rs.a == [1 3; 2 4]
@test rs.b == ["a" "c"; "b" "d"]

rs = reshape(s, (0:1, :))
@test rs.a == OffsetArray([1 3; 2 4], (-1, 0))
@test rs.b == OffsetArray(["a" "c"; "b" "d"], (-1, 0))

rs = reshape(s, (0:1, 1:2))
@test rs.a == OffsetArray([1 3; 2 4], (-1, 0))
@test rs.b == OffsetArray(["a" "c"; "b" "d"], (-1, 0))
end

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

@testset "map" begin
s = StructArray(a=[1, 2, 3])

t = @inferred(map(x -> x, s))
@test t isa StructArray
@test t == s

t = @inferred(map(x -> x.a, s))
@test t isa Vector
@test t == [1, 2, 3]

t = VERSION >= v"1.7" ? @inferred(map(x -> (a=x.a, b=2), s)) : map(x -> (a=x.a, b=2), s)
@test t isa StructArray
@test map(x -> (a=x.a, b=2), s) == [(a=1, b=2), (a=2, b=2), (a=3, b=2)]

so = reshape(s, Base.IdentityUnitRange(11:13))
to = @inferred(map(x -> x, so))
@test to isa StructArray
@test axes(to) == axes(so)
@test to == so
end

@testset "staticarrays" begin
# test that staticschema returns the right things
for StaticVectorType = [SVector, MVector, SizedVector]
Expand Down

2 comments on commit b398788

@piever
Copy link
Collaborator

@piever piever commented on b398788 Jun 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/62936

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.10 -m "<description of version>" b398788ec27da0597163cbb0ab06b96c4ac2d26c
git push origin v0.6.10

Please sign in to comment.