From e7e8a1105fd08b157e9e58506388fbfef1b21aa8 Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Tue, 30 Nov 2021 08:52:51 +0100 Subject: [PATCH 1/4] document require_one_based_indexing --- base/abstractarray.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 2733b52222e37..97b193292479f 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -101,12 +101,20 @@ end Return `true` if the indices of `A` start with something other than 1 along any axis. If multiple arguments are passed, equivalent to `has_offset_axes(A) | has_offset_axes(B) | ...`. +See also [`require_one_based_indexing`](@ref) """ has_offset_axes(A) = _tuple_any(x->Int(first(x))::Int != 1, axes(A)) has_offset_axes(A::AbstractVector) = Int(firstindex(A))::Int != 1 # improve performance of a common case (ranges) has_offset_axes(A...) = _tuple_any(has_offset_axes, A) has_offset_axes(::Colon) = false +""" + require_one_based_indexing(A::AbstractArray) + require_one_based_indexing(A,B...) + +Throw an `ArgumentError` if the indices of any argument start with something other than one along any axis. +See also [`has_offset_axes`](@ref). +""" require_one_based_indexing(A...) = !has_offset_axes(A...) || throw(ArgumentError("offset arrays are not supported but got an array with index other than 1")) # Performance optimization: get rid of a branch on `d` in `axes(A, d)` From 2fcc3bb586e83e731de70118d2c6d28bcad8d059 Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Tue, 30 Nov 2021 10:14:51 +0100 Subject: [PATCH 2/4] Update base/abstractarray.jl Co-authored-by: Johnny Chen --- base/abstractarray.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 97b193292479f..bec41e29a6713 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -101,7 +101,7 @@ end Return `true` if the indices of `A` start with something other than 1 along any axis. If multiple arguments are passed, equivalent to `has_offset_axes(A) | has_offset_axes(B) | ...`. -See also [`require_one_based_indexing`](@ref) +See also [`require_one_based_indexing`](@ref). """ has_offset_axes(A) = _tuple_any(x->Int(first(x))::Int != 1, axes(A)) has_offset_axes(A::AbstractVector) = Int(firstindex(A))::Int != 1 # improve performance of a common case (ranges) From 5ed6929ef310362d90e5dc3df26329c3167856e9 Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Tue, 30 Nov 2021 10:19:39 +0100 Subject: [PATCH 3/4] add compat for require_one_based_indexing --- base/abstractarray.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 97b193292479f..ba4200d38cf70 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -114,6 +114,9 @@ has_offset_axes(::Colon) = false Throw an `ArgumentError` if the indices of any argument start with something other than one along any axis. See also [`has_offset_axes`](@ref). + +!!! compat "Julia 1.2" + This function requires at least Julia 1.2. """ require_one_based_indexing(A...) = !has_offset_axes(A...) || throw(ArgumentError("offset arrays are not supported but got an array with index other than 1")) From f4111fe469b85d4448abc2c3a23f076255f2c99c Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Tue, 17 May 2022 12:52:51 +0200 Subject: [PATCH 4/4] small updates --- base/abstractarray.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 8bd792040ad61..6e39bb50800f4 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -101,6 +101,7 @@ end Return `true` if the indices of `A` start with something other than 1 along any axis. If multiple arguments are passed, equivalent to `has_offset_axes(A) | has_offset_axes(B) | ...`. + See also [`require_one_based_indexing`](@ref). """ has_offset_axes(A) = _tuple_any(x->Int(first(x))::Int != 1, axes(A)) @@ -112,7 +113,7 @@ has_offset_axes(::Colon) = false require_one_based_indexing(A::AbstractArray) require_one_based_indexing(A,B...) -Throw an `ArgumentError` if the indices of any argument start with something other than one along any axis. +Throw an `ArgumentError` if the indices of any argument start with something other than `1` along any axis. See also [`has_offset_axes`](@ref). !!! compat "Julia 1.2"