-
Notifications
You must be signed in to change notification settings - Fork 42
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
Use to_indices in the OffsetArray constructor #157
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d59a1e2
use to_indices to simplify constructor dispatch
jishnub 29efcd0
comment on error message optimization
jishnub 53ba857
simplify constructors
jishnub 2249dc3
remove references to internal functions in docs
jishnub e3f8fd9
Add example with a custom AbstractUnitRange
jishnub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
simplify constructors
- Loading branch information
commit 53ba85743c4e2db1fc1c39b904ef3e3c113e92b5
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,25 +124,20 @@ function overflow_check(r, offset::T) where T | |
end | ||
end | ||
|
||
# Tuples of integers are treated as offsets | ||
# Empty Tuples are handled here | ||
function OffsetArray(A::AbstractArray, offsets::Tuple{Vararg{Integer}}) | ||
_checkindices(A, offsets, "offsets") | ||
OffsetArray{eltype(A), ndims(A), typeof(A)}(A, offsets) | ||
end | ||
# Nested OffsetArrays may strip off the layer and collate the offsets | ||
function OffsetArray(A::OffsetArray, offsets::Tuple{Vararg{Integer}}) | ||
_checkindices(A, offsets, "offsets") | ||
OffsetArray(parent(A), A.offsets .+ offsets) | ||
end | ||
|
||
# These methods are necessary to disallow incompatible dimensions for | ||
# the OffsetVector and the OffsetMatrix constructors | ||
for (FT, ND) in ((:OffsetVector, :1), (:OffsetMatrix, :2)) | ||
@eval function $FT(A::AbstractArray{<:Any,$ND}, offsets::Tuple{Vararg{Integer}}) | ||
_checkindices(A, offsets, "offsets") | ||
OffsetArray{eltype(A), $ND, typeof(A)}(A, offsets) | ||
end | ||
@eval function $FT(A::OffsetArray{<:Any,$ND}, offsets::Tuple{Vararg{Integer}}) | ||
_checkindices(A, offsets, "offsets") | ||
$FT(parent(A), A.offsets .+ offsets) | ||
end | ||
FTstr = string(FT) | ||
@eval function $FT(A::AbstractArray, offsets::Tuple{Vararg{Integer}}) | ||
throw(ArgumentError($FTstr*" requires a "*string($ND)*"D array")) | ||
|
@@ -151,14 +146,18 @@ end | |
|
||
## OffsetArray constructors | ||
for FT in (:OffsetArray, :OffsetVector, :OffsetMatrix) | ||
# Nested OffsetArrays may strip off the wrapper and collate the offsets | ||
@eval function $FT(A::OffsetArray, offsets::Tuple{Vararg{Integer}}) | ||
_checkindices(A, offsets, "offsets") | ||
$FT(parent(A), map(+, A.offsets, offsets)) | ||
end | ||
|
||
# In general, indices get converted to AbstractUnitRanges. | ||
# CartesianIndices{N} get converted to N ranges | ||
@eval function $FT(A::AbstractArray, inds::Tuple) | ||
@eval function $FT(A::AbstractArray, inds::Tuple{Any,Vararg{Any}}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't really necessary given the dispatch priority of |
||
$FT(A, _toAbstractUnitRanges(to_indices(A, axes(A), inds))) | ||
end | ||
|
||
@eval $FT(A::AbstractArray, inds::Vararg) = $FT(A, inds) | ||
|
||
# convert ranges to offsets | ||
@eval function $FT(A::AbstractArray, inds::Tuple{AbstractUnitRange,Vararg{AbstractUnitRange}}) | ||
_checkindices(A, inds, "indices") | ||
|
@@ -170,6 +169,8 @@ for FT in (:OffsetArray, :OffsetVector, :OffsetMatrix) | |
$FT(A, map(_offset, axes(A), inds)) | ||
end | ||
|
||
@eval $FT(A::AbstractArray, inds::Vararg) = $FT(A, inds) | ||
|
||
@eval $FT(A::AbstractArray, origin::Origin) = $FT(A, origin(A)) | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Broadcasting treats values in
Tuple
s as scalars as opposed to treating theTuple
as a collection in itself, so something like(1,2) .+ (1,)
evaluates to(2,3)
.map
, on the other hand, operates elementwise. This is not strictly necessary as_checkindices
catches it anyway, but might help against future bugs.