-
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
Add constructors for CartesianIndices #142
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -47,6 +47,7 @@ OffsetVector{T}(init::ArrayInitializer, inds::AbstractUnitRange) where {T} = Off | |
|
||
# OffsetMatrix constructors | ||
OffsetMatrix(A::AbstractMatrix, offset1, offset2) = OffsetArray(A, offset1, offset2) | ||
OffsetMatrix(A::AbstractMatrix, I::CartesianIndices{2}) = OffsetArray(A, I) | ||
OffsetMatrix{T}(init::ArrayInitializer, inds1::AbstractUnitRange, inds2::AbstractUnitRange) where {T} = OffsetArray{T}(init, inds1, inds2) | ||
|
||
""" | ||
|
@@ -82,12 +83,63 @@ uncolonindices(ax::Tuple, inds::Tuple) = (first(inds), uncolonindices(tail(ax), | |
uncolonindices(ax::Tuple, inds::Tuple{Colon, Vararg{Any}}) = (first(ax), uncolonindices(tail(ax), tail(inds))...) | ||
uncolonindices(::Tuple{}, ::Tuple{}) = () | ||
|
||
function OffsetArray(A::AbstractArray{T,N}, inds::NTuple{N,Union{AbstractUnitRange, Colon}}) where {T,N} | ||
function OffsetArray(A::AbstractArray{T,N}, inds::NTuple{N,Union{AbstractUnitRange, CartesianIndices{1}, Colon}}) where {T,N} | ||
OffsetArray(A, uncolonindices(A, inds)) | ||
end | ||
OffsetArray(A::AbstractArray{T,N}, inds::Vararg{Union{AbstractUnitRange, Colon},N}) where {T,N} = | ||
OffsetArray(A::AbstractArray{T,N}, inds::Vararg{Union{AbstractUnitRange, CartesianIndices{1}, Colon},N}) where {T,N} = | ||
OffsetArray(A, uncolonindices(A, inds)) | ||
|
||
# Specify offsets using CartesianIndices (issue #71) | ||
# Support a mix of AbstractUnitRanges and CartesianIndices{1} | ||
# Extract the range r from CartesianIndices((r,)) | ||
function stripCartesianIndices(inds::Tuple{CartesianIndices{1},Vararg{Any}}) | ||
I = first(inds) | ||
Ir = convert(Tuple{AbstractUnitRange{Int}}, I) |> first | ||
(Ir, stripCartesianIndices(tail(inds))...) | ||
end | ||
stripCartesianIndices(inds::Tuple)= (first(inds), stripCartesianIndices(tail(inds))...) | ||
stripCartesianIndices(::Tuple{}) = () | ||
|
||
OffsetArray(A::AbstractArray{<:Any,N}, inds::NTuple{N,Union{CartesianIndices{1}, AbstractUnitRange}}) where {N} = | ||
OffsetArray(A, stripCartesianIndices(inds)) | ||
OffsetArray(A::AbstractArray{<:Any,N}, inds::Vararg{Union{CartesianIndices{1}, AbstractUnitRange},N}) where {N} = | ||
OffsetArray(A, inds) | ||
|
||
# Support an arbitrary CartesianIndices alongside colons and ranges | ||
# The total number of indices should equal ndims(arr) | ||
# We split the CartesianIndices{N} into N CartesianIndices{1} indices to facilitate dispatch | ||
splitCartesianIndices(c::CartesianIndices{0}) = () | ||
function splitCartesianIndices(c::CartesianIndices) | ||
c1, ct = Base.IteratorsMD.split(c, Val(1)) | ||
(c1, splitCartesianIndices(ct)...) | ||
end | ||
Comment on lines
+112
to
+115
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. Good to know this I tried the following, too. But this one is much slower than your version. splitCartesianIndices(c::CartesianIndices) = IdOffsetRange.(c.indices)
|
||
function splitCartesianIndices(t::Tuple{CartesianIndices, Vararg{Any}}) | ||
(splitCartesianIndices(first(t))..., splitCartesianIndices(tail(t))...) | ||
end | ||
function splitCartesianIndices(t::Tuple) | ||
(first(t), splitCartesianIndices(tail(t))...) | ||
end | ||
splitCartesianIndices(::Tuple{}) = () | ||
|
||
function OffsetArray(A::AbstractArray, inds::Tuple{Vararg{Union{AbstractUnitRange, CartesianIndices, Colon}}}) | ||
OffsetArray(A, splitCartesianIndices(inds)) | ||
end | ||
function OffsetArray(A::AbstractArray, inds::Vararg{Union{AbstractUnitRange, CartesianIndices, Colon}}) | ||
OffsetArray(A, inds) | ||
end | ||
|
||
# Add methods to initialize OffsetArrays using CartesianIndices (issue #71) | ||
function OffsetArray{T,N}(init::ArrayInitializer, inds::Tuple{Vararg{Union{AbstractUnitRange, CartesianIndices}}}) where {T,N} | ||
indsN = stripCartesianIndices(splitCartesianIndices(inds)) | ||
OffsetArray{T,N}(init, indsN) | ||
end | ||
function OffsetArray{T}(init::ArrayInitializer, inds::Tuple{Vararg{Union{AbstractUnitRange, CartesianIndices}}}) where {T} | ||
indsN = stripCartesianIndices(splitCartesianIndices(inds)) | ||
OffsetArray{T}(init, indsN) | ||
end | ||
OffsetArray{T,N}(init::ArrayInitializer, inds::Vararg{Union{AbstractUnitRange, CartesianIndices}}) where {T,N} = OffsetArray{T,N}(init, inds) | ||
OffsetArray{T}(init::ArrayInitializer, inds::Vararg{Union{AbstractUnitRange, CartesianIndices}}) where {T} = OffsetArray{T}(init, inds) | ||
|
||
# avoid a level of indirection when nesting OffsetArrays | ||
function OffsetArray(A::OffsetArray, offsets::NTuple{N,Int}) where {N} | ||
OffsetArray(parent(A), offsets .+ A.offsets) | ||
|
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.
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.
I was trying to avoid using
CartesianIndices(..).indices
, as I'm not entirely sure that it's future-proof. Theconvert
route was suggested by Tim Holy to get around this, and seems to produce the same lowered codeI can condense these a bit if that helps in legibility.
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.
Oh sorry, I missed that part of the conversation. These two are actually equivalent.
Future-proof seems okay to me.