@@ -542,6 +542,46 @@ known_step(x) = known_step(typeof(x))
542542known_step (:: Type{T} ) where {T} = nothing
543543known_step (:: Type{<:AbstractUnitRange{T}} ) where {T} = one (T)
544544
545+
546+ """
547+ indices(x[, d]) -> AbstractRange
548+
549+ Given an array `x`, this returns the indices along dimension `d`. If `x` is a tuple
550+ of arrays then the indices corresponding to dimension `d` of all arrays in `x` are
551+ returned. If any indices are not equal along dimension `d` an error is thrown. A
552+ tuple may be used to specify a different dimension for each array. If `d` is not
553+ specified then indices for visiting each index of `x` is returned.
554+ """
555+ @inline indices (x) = eachindex (x)
556+
557+ indices (x, d) = indices (axes (x, d))
558+
559+ @inline function indices (x:: NTuple{N,<:Any} , dim) where {N}
560+ inds = indices (first (x), dim)
561+ @assert _check_indices (inds, Base. tail (x), dim) " The indices along dimension $dim are not equal for all $x "
562+ return inds
563+ end
564+
565+ @inline function indices (x:: NTuple{N,<:Any} , dim:: NTuple{N,<:Any} ) where {N}
566+ ind = indices (first (x), first (dim))
567+ @assert _check_indices (ind, Base. tail (x), Base. tail (dim)) " The indices along dimension $dim are not equal for all $x "
568+ return ind
569+ end
570+
571+ @inline function _check_indices (ind, x:: Tuple , dim:: Tuple )
572+ for (x_i, d_i) in zip (x, dim)
573+ ind == indices (x_i, d_i) || return false
574+ end
575+ return true
576+ end
577+
578+ @inline function _check_indices (ind, x:: Tuple , d)
579+ for x_i in x
580+ ind == indices (x_i, d) || return false
581+ end
582+ return true
583+ end
584+
545585function __init__ ()
546586
547587 @require SuiteSparse= " 4607b0f0-06f3-5cda-b6b1-a6196a1729e9" begin
0 commit comments