Skip to content
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
10 changes: 10 additions & 0 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@ include("axes.jl")
struct OffsetArray{T,N,AA<:AbstractArray} <: AbstractArray{T,N}
parent::AA
offsets::NTuple{N,Int}
function OffsetArray{T, N, AA}(parent::AA, offsets::NTuple{N, Int}) where {T, N, AA<:AbstractArray}
overflow_check.(axes(parent), offsets)
new{T, N, AA}(parent, offsets)
end
end
OffsetVector{T,AA<:AbstractArray} = OffsetArray{T,1,AA}
OffsetMatrix{T,AA<:AbstractArray} = OffsetArray{T,2,AA}

function overflow_check(r, offset::T) where T
throw_offseterror() = throw(ArgumentError("Boundary overflow detected: offset $offset should be smaller than $(typemax(T) - last(r))"))

Choose a reason for hiding this comment

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

Why define a function for this?

Copy link
Member

Choose a reason for hiding this comment

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

It appears to improve the performance, not entirely sure why

Copy link
Member Author

Choose a reason for hiding this comment

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

I opened a new issue in JuliaLang/julia#37558

In this PR I just switched back to the simple version and don't tweak it; IMO 5-10ns difference isn't critical to our use case.

if offset > 0 && last(r) > typemax(T) - offset
throw_offseterror()
end
end
## OffsetArray constructors

offset(axparent::AbstractUnitRange, ax::AbstractUnitRange) = first(ax) - first(axparent)
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using OffsetArrays
using OffsetArrays: IdentityUnitRange, no_offset_view
using OffsetArrays: IdOffsetRange
using Test, Aqua
using LinearAlgebra
using DelimitedFiles
Expand Down Expand Up @@ -125,6 +126,9 @@ end
w = zeros(5:6)
@test OffsetVector(w, :) == OffsetArray(w, (:,)) == OffsetArray(w, :) == OffsetArray(w, axes(w))
@test axes(OffsetVector(w, :)) == axes(w)

@test axes(OffsetVector(v, typemax(Int)-length(v))) == (IdOffsetRange(axes(v)[1], typemax(Int)-length(v)), )
@test_throws ArgumentError OffsetVector(v, typemax(Int)-length(v)+1)
end

@testset "OffsetMatrix constructors" begin
Expand Down Expand Up @@ -158,6 +162,10 @@ end

@test axes(OffsetMatrix(w, :, CartesianIndices((0:1,)))) == (3:4, 0:1)
@test axes(OffsetMatrix(w, CartesianIndices((0:1,)), :)) == (0:1, 5:6)

@test axes(OffsetMatrix(v, typemax(Int)-size(v, 1), 0)) == (IdOffsetRange(axes(v)[1], typemax(Int)-size(v, 1)), axes(v, 2))
@test_throws ArgumentError OffsetMatrix(v, typemax(Int)-size(v,1)+1, 0)
@test_throws ArgumentError OffsetMatrix(v, 0, typemax(Int)-size(v, 2)+1)
end

@testset "construct OffsetArray with CartesianIndices" begin
Expand Down