Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

similar with IdOffsetRanges may use the parent axes types #213

Merged
merged 9 commits into from
Apr 24, 2021

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented Feb 28, 2021

Currently

julia> s = @SVector[i for i in 1:3]
3-element SArray{Tuple{3},Int64,1,3} with indices SOneTo(3):
 1
 2
 3

julia> so = OffsetArray(s, 4);

julia> similar(so)
3-element OffsetArray(::Array{Int64,1}, 5:7) with eltype Int64 with indices 5:7:
 139957917106072
 139957916617440
 139957916868864

After this PR:

julia> similar(so)
3-element OffsetArray(::MArray{Tuple{3},Int64,1,3}, 5:7) with eltype Int64 with indices 5:7:
 139957418956176
               4
               0

The new array type is also statically sized, as the information about the size is preserved in the axes.

@codecov
Copy link

codecov bot commented Feb 28, 2021

Codecov Report

Merging #213 (2691937) into master (7a1e2b9) will decrease coverage by 3.70%.
The diff coverage is 70.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #213      +/-   ##
==========================================
- Coverage   98.28%   94.57%   -3.71%     
==========================================
  Files           5        5              
  Lines         291      332      +41     
==========================================
+ Hits          286      314      +28     
- Misses          5       18      +13     
Impacted Files Coverage Δ
src/OffsetArrays.jl 98.21% <62.50%> (+0.27%) ⬆️
src/utils.jl 100.00% <100.00%> (ø)
src/precompile.jl 0.00% <0.00%> (-92.86%) ⬇️
src/axes.jl 100.00% <0.00%> (ø)
src/origin.jl 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7a1e2b9...2691937. Read the comment docs.

@jishnub
Copy link
Member Author

jishnub commented Mar 5, 2021

Now a mix of SOneTo and IdOffsetRange(::SOneTo) axis types as arguments to similar leads to a StaticArray

Before:

julia> s = SArray{Tuple{2,2},Int,2,4}((1,2,3,4))
2×2 SArray{Tuple{2,2},Int64,2,4} with indices SOneTo(2)×SOneTo(2):
 1  3
 2  4

julia> so = OffsetArray(s, 2, 2);

julia> similar(so, axes(so,1), axes(s,2))
2×2 OffsetArray(::Array{Int64,2}, 3:4, 1:2) with eltype Int64 with indices 3:4×1:2:
 281474976710656  140349019967488
               0  140348924690432

After 8992fc9:

julia> similar(so, axes(so,1), axes(s,2))
2×2 OffsetArray(::MArray{Tuple{2,2},Int64,2,4}, 3:4, 1:2) with eltype Int64 with indices 3:4×1:2:
             139  19
 140348215528848   5

@jishnub
Copy link
Member Author

jishnub commented Mar 7, 2021

Before 8abaae2:

julia> s = SArray{Tuple{2,2},Int,2,4}((1,2,3,4));

julia> so = OffsetArray(s, 2, 2);

julia> so .+ 1
2×2 OffsetArray(::Array{Int64,2}, 3:4, 3:4) with eltype Int64 with indices 3:4×3:4:
 2  4
 3  5

After 8abaae2:

julia> so .+ 1
2×2 OffsetArray(::SizedArray{Tuple{2,2},Int64,2,2,Array{Int64,2}}, 3:4, 3:4) with eltype Int64 with indices 3:4×3:4:
 2  4
 3  5

julia> parent(so .+ 1) isa StaticArray
true

So now broadcasting preserves the static sizes.

Copy link
Member

@johnnychen94 johnnychen94 left a comment

Choose a reason for hiding this comment

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

No objection to the fix. You always write good test cases.

B = similar(A, T, map(_indexlength, inds))
# strip IdOffsetRanges to extract the parent range and use it to generate the array
# route through _similar to avoid a stack overflow if map(_maybeparent, inds) === inds
B = _similar(A, T, map(_maybeparent, inds), inds)
Copy link
Member

Choose a reason for hiding this comment

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

It's a little bit unintuitive to understand what _similar does, so how about:

new_inds = _strip_IdOffsetRange(inds)
B = similar(A, T, new_inds)

@jishnub jishnub removed the request for review from timholy April 24, 2021 19:21
@jishnub jishnub merged commit b92d2c8 into JuliaArrays:master Apr 24, 2021
@jishnub jishnub deleted the similarIdOffsetRange branch April 24, 2021 19:48
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.

None yet

2 participants