JuliaArrays / OffsetArrays.jl

Fortran-like arrays with arbitrary, zero or negative starting indices.
Other
198 stars 40 forks source link

Add `isonebased` trait to opt out of `OffsetArray` wrappers #335

Open jishnub opened 1 year ago

jishnub commented 1 year ago

The idea is

julia> OffsetArrays.no_offset_view(SMatrix{2,2}(1:4))
2×2 OffsetArray(::SMatrix{2, 2, Int64, 4}, 1:2, 1:2) with eltype Int64 with indices 1:2×1:2:
 1  3
 2  4

julia> OffsetArrays.isonebased(::SOneTo) = true

julia> OffsetArrays.no_offset_view(SMatrix{2,2}(1:4)) # no wrapper anymore
2×2 SMatrix{2, 2, Int64, 4} with indices SOneTo(2)×SOneTo(2):
 1  3
 2  4

Also, in this case, the similar method in this package will forward the lengths of the axes to Base, so the result will usually be an Array instead of an OffsetArray:

julia> similar(rand(2), Int, Base.IdentityUnitRange(Base.OneTo(2)))
2-element Vector{Int64}:
              -1
 140522905523888
codecov[bot] commented 1 year ago

Codecov Report

Merging #335 (71565b6) into master (1cae070) will increase coverage by 0.02%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #335      +/-   ##
==========================================
+ Coverage   98.67%   98.69%   +0.02%     
==========================================
  Files           5        5              
  Lines         452      459       +7     
==========================================
+ Hits          446      453       +7     
  Misses          6        6              
Impacted Files Coverage Δ
src/OffsetArrays.jl 98.33% <100.00%> (+0.03%) :arrow_up:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more