Skip to content

Commit 0d8906c

Browse files
authored
Merge pull request #8 from alsam/reshape
Turn on precompilation, error testing in constructor, and a reshape fix
2 parents b795ca7 + bce81fb commit 0d8906c

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/OffsetArrays.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
__precompile__()
2+
13
module OffsetArrays
24

35
Base.@deprecate_binding (..) Colon()
@@ -27,8 +29,12 @@ OffsetArray{T,N}(::Type{T}, inds::Vararg{UnitRange{Int},N}) = OffsetArray{T,N}(i
2729
# second method should not be necessary.
2830
OffsetArray{T}(A::AbstractArray{T,0}, inds::Tuple{}) = OffsetArray{T,0,typeof(A)}(A, ())
2931
OffsetArray{T,N}(A::AbstractArray{T,N}, inds::Tuple{}) = error("this should never be called")
30-
OffsetArray{T,N}(A::AbstractArray{T,N}, inds::NTuple{N,AbstractUnitRange}) =
32+
function OffsetArray{T,N}(A::AbstractArray{T,N}, inds::NTuple{N,AbstractUnitRange})
33+
lA = map(length, indices(A))
34+
lI = map(length, inds)
35+
lA == lI || throw(DimensionMismatch("supplied indices do not agree with the size of the array (got size $lA for the array and $lI for the indices"))
3136
OffsetArray(A, map(indexoffset, inds))
37+
end
3238
OffsetArray{T,N}(A::AbstractArray{T,N}, inds::Vararg{AbstractUnitRange,N}) =
3339
OffsetArray(A, inds)
3440

@@ -66,6 +72,11 @@ Base.similar(f::Union{Function,DataType}, shape::Tuple{UnitRange,Vararg{UnitRang
6672
Base.reshape(A::AbstractArray, inds::Tuple{UnitRange,Vararg{UnitRange}}) = OffsetArray(reshape(A, map(length, inds)), map(indexoffset, inds))
6773

6874
Base.reshape(A::OffsetArray, inds::Tuple{UnitRange,Vararg{UnitRange}}) = OffsetArray(reshape(parent(A), map(length, inds)), map(indexoffset, inds))
75+
76+
function Base.reshape(A::OffsetArray, inds::Tuple{UnitRange,Vararg{Union{UnitRange,Int,Base.OneTo}}})
77+
throw(ArgumentError("reshape must supply UnitRange indices, got $(typeof(inds)).\n Note that reshape(A, Val{N}) is not supported for OffsetArrays."))
78+
end
79+
6980
# Don't allow bounds-checks to be removed during Julia 0.5
7081
@inline function Base.getindex{T,N}(A::OffsetArray{T,N}, I::Vararg{Int,N})
7182
checkbounds(A, I...)

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ S = OffsetArray(view(A0, 1:2, 1:2), (-1,2)) # LinearSlow
3434
@test indices(A) == indices(S) == (0:1, 3:4)
3535
@test_throws ErrorException size(A)
3636
@test_throws ErrorException size(A, 1)
37+
@test A == OffsetArray(A0, 0:1, 3:4)
38+
@test_throws DimensionMismatch OffsetArray(A0, 0:2, 3:4)
39+
@test_throws DimensionMismatch OffsetArray(A0, 0:1, 2:4)
3740

3841
# Scalar indexing
3942
@test A[0,3] == A[1] == S[0,3] == S[1] == 1
@@ -161,6 +164,9 @@ b = reshape(A, -7:-4)
161164
@test indices(b) == (-7:-4,)
162165
@test isa(parent(b), Vector{Int})
163166
@test parent(b) == A0[:]
167+
a = OffsetArray(rand(3,3,3), -1:1, 0:2, 3:5)
168+
@test_throws ArgumentError reshape(a, Val{2})
169+
@test_throws ArgumentError reshape(a, Val{4})
164170

165171
# Indexing with OffsetArray indices
166172
i1 = OffsetArray([2,1], (-5,))

0 commit comments

Comments
 (0)