Skip to content

Conversation

piever
Copy link
Collaborator

@piever piever commented Mar 18, 2022

Rebase + cleanup of #94. I've just noticed that before there was a bit of an ambiguity in the interaction with offset arrays.

In the previous version,

julia> using StructArrays, OffsetArrays

julia> s = StructArray(a=rand(4));

julia> similar(s, -1:2) # StructArray of OffsetArrays
4-element StructArray(OffsetArray(::Vector{Float64}, -1:2)) with eltype NamedTuple{(:a,), Tuple{Float64}} with indices -1:2:
 (a = 1.4080165e-315,)
 (a = 1.408020214e-315,)
 (a = 1.396197183e-315,)
 (a = 1.402099964e-315,)

julia> similar(typeof(s), -1:2) # OffsetArray of StructArrays
4-element OffsetArray(StructArray(::Vector{Float64}), -1:2) with eltype NamedTuple{(:a,), Tuple{Float64}} with indices -1:2:
 (a = 0.0,)
 (a = 1.39654137e-315,)
 (a = 1.476201036e-315,)
 (a = 0.0,)

whereas with this PR they both give OffsetArrays of StructArrays:

julia> using StructArrays, OffsetArrays

julia> s = StructArray(a=rand(4));

julia> similar(s, -1:2) # OffsetArrays of StructArrays
4-element OffsetArray(StructArray(::Vector{Float64}), -1:2) with eltype NamedTuple{(:a,), Tuple{Float64}} with indices -1:2:
 (a = 0.0,)
 (a = 1.39654137e-315,)
 (a = 1.396105643e-315,)
 (a = 0.0,)

julia> similar(typeof(s), -1:2) # OffsetArray of StructArrays
4-element OffsetArray(StructArray(::Vector{Float64}), -1:2) with eltype NamedTuple{(:a,), Tuple{Float64}} with indices -1:2:
 (a = 0.0,)
 (a = 1.39654137e-315,)
 (a = 1.396105643e-315,)
 (a = 0.0,)

This PR behavior is kind of the only one that is clean to achieve because of the way dispatch is set up (though getting both to return StructArray of OffsetArrays is probably also feasible but a bit more finicky). Is there a principled way to know which format similar(s::StructArray, rg::UnitRange) should return?

Keno and others added 4 commits June 12, 2022 12:16
Array wrappers such as `OffsetArray` assume that the three argument
version exists for its interior arrays.
@piever
Copy link
Collaborator Author

piever commented Jun 12, 2022

UPDATE: the PR has been updated to intercept OffsetArrays overloads and consistently return StructArrays whose components are OffsetArrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants