Skip to content

Commit ea4008f

Browse files
authored
Implicitly specify axes using colons in the constructor (#139)
In case certain axes of an array are not offset, they may be implicitly specified using Colons in the constructor. The colons would be replaced by the corresponding axes of the parent array.
1 parent 90f7b32 commit ea4008f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/OffsetArrays.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ end
7777
OffsetArray(A::AbstractArray{T,N}, inds::Vararg{AbstractUnitRange,N}) where {T,N} =
7878
OffsetArray(A, inds)
7979

80+
uncolonindices(A::AbstractArray{<:Any,N}, inds::NTuple{N,Any}) where {N} = uncolonindices(axes(A), inds)
81+
uncolonindices(ax::Tuple, inds::Tuple) = (first(inds), uncolonindices(tail(ax), tail(inds))...)
82+
uncolonindices(ax::Tuple, inds::Tuple{Colon, Vararg{Any}}) = (first(ax), uncolonindices(tail(ax), tail(inds))...)
83+
uncolonindices(::Tuple{}, ::Tuple{}) = ()
84+
85+
function OffsetArray(A::AbstractArray{T,N}, inds::NTuple{N,Union{AbstractUnitRange, Colon}}) where {T,N}
86+
OffsetArray(A, uncolonindices(A, inds))
87+
end
88+
OffsetArray(A::AbstractArray{T,N}, inds::Vararg{Union{AbstractUnitRange, Colon},N}) where {T,N} =
89+
OffsetArray(A, uncolonindices(A, inds))
90+
8091
# avoid a level of indirection when nesting OffsetArrays
8192
function OffsetArray(A::OffsetArray, offsets::NTuple{N,Int}) where {N}
8293
OffsetArray(parent(A), offsets .+ A.offsets)

test/runtests.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,31 @@ end
109109
@test OffsetVector(v, -2) == OffsetArray(v, -2)
110110
@test OffsetVector(v, -2:2) == OffsetArray(v, -2:2)
111111
@test typeof(OffsetVector{Float64}(undef, -2:2)) == typeof(OffsetArray{Float64}(undef, -2:2))
112+
113+
@test OffsetVector(v, :) == OffsetArray(v, (:,)) == OffsetArray(v, :) == OffsetArray(v, axes(v))
114+
@test axes(OffsetVector(v, :)) == axes(v)
115+
116+
w = zeros(5:6)
117+
@test OffsetVector(w, :) == OffsetArray(w, (:,)) == OffsetArray(w, :) == OffsetArray(w, axes(w))
118+
@test axes(OffsetVector(w, :)) == axes(w)
112119
end
113120

114121
@testset "OffsetMatrix constructors" begin
115122
local v = rand(5, 3)
116123
@test OffsetMatrix(v, -2, -1) == OffsetArray(v, -2, -1)
117124
@test OffsetMatrix(v, -2:2, -1:1) == OffsetArray(v, -2:2, -1:1)
118125
@test typeof(OffsetMatrix{Float64}(undef, -2:2, -1:1)) == typeof(OffsetArray{Float64}(undef, -2:2, -1:1))
126+
127+
@test OffsetMatrix(v, :, :) == OffsetArray(v, (:, :)) == OffsetArray(v, :, :) == OffsetArray(v, axes(v))
128+
@test OffsetMatrix(v, :, 2:4) == OffsetArray(v, axes(v,1), 2:4)
129+
@test OffsetMatrix(v, 3:7, :) == OffsetArray(v, 3:7, axes(v,2))
130+
@test axes(OffsetArray(v, :, :)) == axes(v)
131+
132+
w = zeros(3:4, 5:6)
133+
@test OffsetMatrix(w, :, :) == OffsetArray(w, (:, :)) == OffsetArray(w, :, :) == OffsetArray(w, axes(w))
134+
@test OffsetMatrix(w, :, 2:3) == OffsetArray(w, axes(w,1), 2:3)
135+
@test OffsetMatrix(w, 0:1, :) == OffsetArray(w, 0:1, axes(w,2))
136+
@test axes(OffsetArray(w, :, :)) == axes(w)
119137
end
120138

121139
@testset "undef, missing, and nothing constructors" begin

0 commit comments

Comments
 (0)