From 8f388a069b06c6d9daeb01e1e36fd904bda185f7 Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Wed, 12 Aug 2020 18:40:28 +0200 Subject: [PATCH] Add OffsetMatrix type alias --- src/OffsetArrays.jl | 7 ++++++- test/runtests.jl | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/OffsetArrays.jl b/src/OffsetArrays.jl index 4bf7f393..0e0ea928 100644 --- a/src/OffsetArrays.jl +++ b/src/OffsetArrays.jl @@ -7,7 +7,7 @@ else using Base: IdentityUnitRange end -export OffsetArray, OffsetVector +export OffsetArray, OffsetMatrix, OffsetVector include("axes.jl") @@ -17,6 +17,7 @@ struct OffsetArray{T,N,AA<:AbstractArray} <: AbstractArray{T,N} offsets::NTuple{N,Int} end OffsetVector{T,AA<:AbstractArray} = OffsetArray{T,1,AA} +OffsetMatrix{T,AA<:AbstractArray} = OffsetArray{T,2,AA} ## OffsetArray constructors @@ -44,6 +45,10 @@ OffsetArray{T}(init::ArrayInitializer, inds::Vararg{AbstractUnitRange,N}) where OffsetVector(A::AbstractVector, offset) = OffsetArray(A, offset) OffsetVector{T}(init::ArrayInitializer, inds::AbstractUnitRange) where {T} = OffsetArray{T}(init, inds) +# OffsetMatrix constructors +OffsetMatrix(A::AbstractMatrix, offset1, offset2) = OffsetArray(A, offset1, offset2) +OffsetMatrix{T}(init::ArrayInitializer, inds1::AbstractUnitRange, inds2::AbstractUnitRange) where {T} = OffsetArray{T}(init, inds1, inds2) + """ OffsetArray(A, indices...) diff --git a/test/runtests.jl b/test/runtests.jl index 73f2d1ac..a46ade20 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -108,6 +108,13 @@ end @test typeof(OffsetVector{Float64}(undef, -2:2)) == typeof(OffsetArray{Float64}(undef, -2:2)) end +@testset "OffsetMatrix constructors" begin + local v = rand(5, 3) + @test OffsetMatrix(v, -2, -1) == OffsetArray(v, -2, -1) + @test OffsetMatrix(v, -2:2, -1:1) == OffsetArray(v, -2:2, -1:1) + @test typeof(OffsetMatrix{Float64}(undef, -2:2, -1:1)) == typeof(OffsetArray{Float64}(undef, -2:2, -1:1)) +end + @testset "undef, missing, and nothing constructors" begin y = OffsetArray{Float32}(undef, (IdentityUnitRange(-1:1),)) @test axes(y) == (IdentityUnitRange(-1:1),)