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

Use to_indices in the OffsetArray constructor #157

Merged
merged 5 commits into from
Sep 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
simplify constructors
  • Loading branch information
jishnub committed Sep 27, 2020
commit 53ba85743c4e2db1fc1c39b904ef3e3c113e92b5
25 changes: 13 additions & 12 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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))
Copy link
Member Author

@jishnub jishnub Sep 27, 2020

Choose a reason for hiding this comment

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

Broadcasting treats values in Tuples as scalars as opposed to treating the Tuple 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.

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}})
Copy link
Member Author

Choose a reason for hiding this comment

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

This isn't really necessary given the dispatch priority of Tuple{Vararg{Integer}} over Tuple, but helps to clarify that this method doesn't handle empty tuples.

$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")
Expand All @@ -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

Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ end
@test_throws ArgumentError OffsetVector(zeros(2,2))
@test_throws ArgumentError OffsetVector(zeros(2,2), 2)
@test_throws ArgumentError OffsetVector(zeros(2,2), (2,))
@test_throws ArgumentError OffsetVector(zeros(2:3,2:3), 2, 3)
@test_throws ArgumentError OffsetVector(zeros(2:3,2:3), (2, 4))
@test_throws ArgumentError OffsetVector(zeros(2:3,2:3), ())
@test_throws ArgumentError OffsetVector(zeros(2:3,2:3))
end

@testset "OffsetMatrix" begin
Expand Down Expand Up @@ -406,6 +410,12 @@ end
@test_throws ArgumentError OffsetMatrix(zeros(2))
@test_throws ArgumentError OffsetMatrix(zeros(2), (1, 2))
@test_throws ArgumentError OffsetMatrix(zeros(2), 1, 2)
@test_throws ArgumentError OffsetMatrix(zeros(2:3), (2,))
@test_throws ArgumentError OffsetMatrix(zeros(2:3), 2)
@test_throws ArgumentError OffsetMatrix(zeros(2:3, 1:2, 1:2), (2,0,0))
@test_throws ArgumentError OffsetMatrix(zeros(2:3, 1:2, 1:2), 2,0,0)
@test_throws ArgumentError OffsetMatrix(zeros(2:3, 1:2, 1:2), ())
@test_throws ArgumentError OffsetMatrix(zeros(2:3, 1:2, 1:2))
end

# no need to duplicate the 2D case here,
Expand Down