Add an offset-preserving view function#187
Conversation
Codecov Report
@@ Coverage Diff @@
## master #187 +/- ##
==========================================
- Coverage 98.91% 98.29% -0.63%
==========================================
Files 5 5
Lines 277 293 +16
==========================================
+ Hits 274 288 +14
- Misses 3 5 +2
Continue to review full report at Codecov.
|
|
This PR is branched off from #185 as that bugfix was necessary for this, so perhaps I can rebase this against master after that is merged. This might make it easier to review. |
|
I'm a little skeptical about this. Let's just fix the bug in Base. (I'm busy with other things right now, sadly, so I haven't even looked.) EDIT: with #185 fixed correctly perhaps it would be worth pushing a "refreshed" version and then I can review. |
|
This is perhaps not necessary, as an index-preserving subarray may be achieved using a julia> a = reshape(1:12, 3, 4)
3×4 reshape(::UnitRange{Int64}, 3, 4) with eltype Int64:
1 4 7 10
2 5 8 11
3 6 9 12
julia> @view a[Base.IdentityUnitRange(2:3), 2]
2-element view(reshape(::UnitRange{Int64}, 3, 4), :, 2) with eltype Int64 with indices 2:3:
5
6Given that the wrapper only applies to |
|
That's how I've always done it, and as you point out it's much more flexible because you can apply it to a subset of axes. I'm happy having this closed. |
This PR adds an extra function
offset_viewand macro@offset_viewthat mirrorBase.view, except they preserve the indices used for the original indexing. This is achieved by wrapping the view in anOffsetArray.These functions are complementary
no_offset_viewUnfortunately this only works with index types that are compatible with the
OffsetArrayconstructor, such asAbstractUnitRanges,CartesianIndicesand such, and doesn't work forVectors and other dense index ranges. For example:(The error message may be improved).
There is another way to construct such an index-preserving view: by using
IdOffsetRangeaxes inview(A, indices...). Personally I would prefer this, as it returns aSubArraywith axes that are offset, but as #186 show this has approach is still buggy.