@@ -308,9 +308,8 @@ type:
308308The default is `IndexCartesian()`.
309309
310310Julia's internal indexing machinery will automatically (and invisibly)
311- convert all indexing operations into the preferred style using
312- [`sub2ind`](@ref) or [`ind2sub`](@ref). This allows users to access
313- elements of your array using any indexing style, even when explicit
311+ convert all indexing operations into the preferred style. This allows users
312+ to access elements of your array using any indexing style, even when explicit
314313methods have not been provided.
315314
316315If you define both styles of indexing for your `AbstractArray`, this
@@ -811,17 +810,6 @@ if all inputs have fast linear indexing, a [`CartesianRange`](@ref)
811810otherwise).
812811If the arrays have different sizes and/or dimensionalities, `eachindex` will return an
813812iterable that spans the largest range along each dimension.
814-
815- For a CartesianRange, this returns a reshaped range of the linear indices into
816- the range, e.g.:
817-
818- ```jldoctest
819- julia> eachindex(CartesianRange((1:2,1:3)))
820- 2×3 reshape(::Base.OneTo{Int64}, 2, 3) with eltype Int64:
821- 1 3 5
822- 2 4 6
823- ```
824-
825813"""
826814eachindex (A:: AbstractArray ) = (@_inline_meta (); eachindex (IndexStyle (A), A))
827815
955943_to_linear_index (A:: AbstractArray , i:: Int ) = i
956944_to_linear_index (A:: AbstractVector , i:: Int , I:: Int... ) = i
957945_to_linear_index (A:: AbstractArray ) = 1
958- _to_linear_index (A:: AbstractArray , I:: Int... ) = (@_inline_meta ; sub2ind (A, I... ))
946+ _to_linear_index (A:: AbstractArray , I:: Int... ) = (@_inline_meta ; _sub2ind (A, I... ))
959947
960948# # IndexCartesian Scalar indexing: Canonical method is full dimensionality of Ints
961949function _getindex (:: IndexCartesian , A:: AbstractArray , I:: Vararg{Int,M} ) where M
@@ -989,8 +977,8 @@ _to_subscript_indices(A, J::Tuple, Jrem::Tuple) = J # already bounds-checked, sa
989977_to_subscript_indices (A:: AbstractArray{T,N} , I:: Vararg{Int,N} ) where {T,N} = I
990978_remaining_size (:: Tuple{Any} , t:: Tuple ) = t
991979_remaining_size (h:: Tuple , t:: Tuple ) = (@_inline_meta ; _remaining_size (tail (h), tail (t)))
992- _unsafe_ind2sub (:: Tuple{} , i) = () # ind2sub may throw(BoundsError()) in this case
993- _unsafe_ind2sub (sz, i) = (@_inline_meta ; ind2sub (sz, i))
980+ _unsafe_ind2sub (:: Tuple{} , i) = () # _ind2sub may throw(BoundsError()) in this case
981+ _unsafe_ind2sub (sz, i) = (@_inline_meta ; _ind2sub (sz, i))
994982
995983# # Setindex! is defined similarly. We first dispatch to an internal _setindex!
996984# function that allows dispatch on array storage
@@ -1573,116 +1561,67 @@ function (==)(A::AbstractArray, B::AbstractArray)
15731561 return true
15741562end
15751563
1576- # sub2ind and ind2sub
1564+ # _sub2ind and _ind2sub
15771565# fallbacks
1578- function sub2ind (A:: AbstractArray , I... )
1566+ function _sub2ind (A:: AbstractArray , I... )
15791567 @_inline_meta
1580- sub2ind (indices (A), I... )
1568+ _sub2ind (indices (A), I... )
15811569end
15821570
1583- """
1584- ind2sub(a, index) -> subscripts
1585-
1586- Return a tuple of subscripts into array `a` corresponding to the linear index `index`.
1587-
1588- # Examples
1589- ```jldoctest
1590- julia> A = ones(5,6,7);
1591-
1592- julia> ind2sub(A,35)
1593- (5, 1, 2)
1594-
1595- julia> ind2sub(A,70)
1596- (5, 2, 3)
1597- ```
1598- """
1599- function ind2sub (A:: AbstractArray , ind)
1571+ function _ind2sub (A:: AbstractArray , ind)
16001572 @_inline_meta
1601- ind2sub (indices (A), ind)
1573+ _ind2sub (indices (A), ind)
16021574end
16031575
16041576# 0-dimensional arrays and indexing with []
1605- sub2ind (:: Tuple{} ) = 1
1606- sub2ind (:: DimsInteger ) = 1
1607- sub2ind (:: Indices ) = 1
1608- sub2ind (:: Tuple{} , I:: Integer... ) = (@_inline_meta ; _sub2ind ((), 1 , 1 , I... ))
1609- # Generic cases
1610-
1611- """
1612- sub2ind(dims, i, j, k...) -> index
1613-
1614- The inverse of [`ind2sub`](@ref), return the linear index corresponding to the provided subscripts.
1577+ _sub2ind (:: Tuple{} ) = 1
1578+ _sub2ind (:: DimsInteger ) = 1
1579+ _sub2ind (:: Indices ) = 1
1580+ _sub2ind (:: Tuple{} , I:: Integer... ) = (@_inline_meta ; _sub2ind_recurse ((), 1 , 1 , I... ))
16151581
1616- # Examples
1617- ```jldoctest
1618- julia> sub2ind((5,6,7),1,2,3)
1619- 66
1620-
1621- julia> sub2ind((5,6,7),1,6,3)
1622- 86
1623- ```
1624- """
1625- sub2ind (dims:: DimsInteger , I:: Integer... ) = (@_inline_meta ; _sub2ind (dims, 1 , 1 , I... ))
1626- sub2ind (inds:: Indices , I:: Integer... ) = (@_inline_meta ; _sub2ind (inds, 1 , 1 , I... ))
1582+ # Generic cases
1583+ _sub2ind (dims:: DimsInteger , I:: Integer... ) = (@_inline_meta ; _sub2ind_recurse (dims, 1 , 1 , I... ))
1584+ _sub2ind (inds:: Indices , I:: Integer... ) = (@_inline_meta ; _sub2ind_recurse (inds, 1 , 1 , I... ))
16271585# In 1d, there's a question of whether we're doing cartesian indexing
16281586# or linear indexing. Support only the former.
1629- sub2ind (inds:: Indices{1} , I:: Integer... ) =
1587+ _sub2ind (inds:: Indices{1} , I:: Integer... ) =
16301588 throw (ArgumentError (" Linear indexing is not defined for one-dimensional arrays" ))
1631- sub2ind (inds:: Tuple{OneTo} , I:: Integer... ) = (@_inline_meta ; _sub2ind (inds, 1 , 1 , I... )) # only OneTo is safe
1632- sub2ind (inds:: Tuple{OneTo} , i:: Integer ) = i
1589+ _sub2ind (inds:: Tuple{OneTo} , I:: Integer... ) = (@_inline_meta ; _sub2ind_recurse (inds, 1 , 1 , I... )) # only OneTo is safe
1590+ _sub2ind (inds:: Tuple{OneTo} , i:: Integer ) = i
16331591
1634- _sub2ind (:: Any , L, ind) = ind
1635- function _sub2ind (:: Tuple{} , L, ind, i:: Integer , I:: Integer... )
1592+ _sub2ind_recurse (:: Any , L, ind) = ind
1593+ function _sub2ind_recurse (:: Tuple{} , L, ind, i:: Integer , I:: Integer... )
16361594 @_inline_meta
1637- _sub2ind ((), L, ind+ (i- 1 )* L, I... )
1595+ _sub2ind_recurse ((), L, ind+ (i- 1 )* L, I... )
16381596end
1639- function _sub2ind (inds, L, ind, i:: Integer , I:: Integer... )
1597+ function _sub2ind_recurse (inds, L, ind, i:: Integer , I:: Integer... )
16401598 @_inline_meta
16411599 r1 = inds[1 ]
1642- _sub2ind (tail (inds), nextL (L, r1), ind+ offsetin (i, r1)* L, I... )
1600+ _sub2ind_recurse (tail (inds), nextL (L, r1), ind+ offsetin (i, r1)* L, I... )
16431601end
16441602
16451603nextL (L, l:: Integer ) = L* l
16461604nextL (L, r:: AbstractUnitRange ) = L* unsafe_length (r)
16471605offsetin (i, l:: Integer ) = i- 1
16481606offsetin (i, r:: AbstractUnitRange ) = i- first (r)
16491607
1650- ind2sub (:: Tuple{} , ind:: Integer ) = (@_inline_meta ; ind == 1 ? () : throw (BoundsError ()))
1651-
1652- """
1653- ind2sub(dims, index) -> subscripts
1654-
1655- Return a tuple of subscripts into an array with dimensions `dims`,
1656- corresponding to the linear index `index`.
1657-
1658- # Examples
1659- ```jldoctest
1660- julia> ind2sub((3,4),2)
1661- (2, 1)
1662-
1663- julia> ind2sub((3,4),3)
1664- (3, 1)
1665-
1666- julia> ind2sub((3,4),4)
1667- (1, 2)
1668- ```
1669- """
1670- ind2sub (dims:: DimsInteger , ind:: Integer ) = (@_inline_meta ; _ind2sub (dims, ind- 1 ))
1671- ind2sub (inds:: Indices , ind:: Integer ) = (@_inline_meta ; _ind2sub (inds, ind- 1 ))
1672- ind2sub (inds:: Indices{1} , ind:: Integer ) =
1608+ _ind2sub (:: Tuple{} , ind:: Integer ) = (@_inline_meta ; ind == 1 ? () : throw (BoundsError ()))
1609+ _ind2sub (dims:: DimsInteger , ind:: Integer ) = (@_inline_meta ; _ind2sub_recurse (dims, ind- 1 ))
1610+ _ind2sub (inds:: Indices , ind:: Integer ) = (@_inline_meta ; _ind2sub_recurse (inds, ind- 1 ))
1611+ _ind2sub (inds:: Indices{1} , ind:: Integer ) =
16731612 throw (ArgumentError (" Linear indexing is not defined for one-dimensional arrays" ))
1674- ind2sub (inds:: Tuple{OneTo} , ind:: Integer ) = (ind,)
1613+ _ind2sub (inds:: Tuple{OneTo} , ind:: Integer ) = (ind,)
16751614
1676- _ind2sub (:: Tuple{} , ind) = (ind+ 1 ,)
1677- function _ind2sub (indslast:: NTuple{1} , ind)
1615+ _ind2sub_recurse (:: Tuple{} , ind) = (ind+ 1 ,)
1616+ function _ind2sub_recurse (indslast:: NTuple{1} , ind)
16781617 @_inline_meta
16791618 (_lookup (ind, indslast[1 ]),)
16801619end
1681- function _ind2sub (inds, ind)
1620+ function _ind2sub_recurse (inds, ind)
16821621 @_inline_meta
16831622 r1 = inds[1 ]
16841623 indnext, f, l = _div (ind, r1)
1685- (ind- l* indnext+ f, _ind2sub (tail (inds), indnext)... )
1624+ (ind- l* indnext+ f, _ind2sub_recurse (tail (inds), indnext)... )
16861625end
16871626
16881627_lookup (ind, d:: Integer ) = ind+ 1
@@ -1691,12 +1630,12 @@ _div(ind, d::Integer) = div(ind, d), 1, d
16911630_div (ind, r:: AbstractUnitRange ) = (d = unsafe_length (r); (div (ind, d), first (r), d))
16921631
16931632# Vectorized forms
1694- function sub2ind (inds:: Indices{1} , I1:: AbstractVector{T} , I:: AbstractVector{T} ...) where T<: Integer
1633+ function _sub2ind (inds:: Indices{1} , I1:: AbstractVector{T} , I:: AbstractVector{T} ...) where T<: Integer
16951634 throw (ArgumentError (" Linear indexing is not defined for one-dimensional arrays" ))
16961635end
1697- sub2ind (inds:: Tuple{OneTo} , I1:: AbstractVector{T} , I:: AbstractVector{T} ...) where {T<: Integer } =
1636+ _sub2ind (inds:: Tuple{OneTo} , I1:: AbstractVector{T} , I:: AbstractVector{T} ...) where {T<: Integer } =
16981637 _sub2ind_vecs (inds, I1, I... )
1699- sub2ind (inds:: Union{DimsInteger,Indices} , I1:: AbstractVector{T} , I:: AbstractVector{T} ...) where {T<: Integer } =
1638+ _sub2ind (inds:: Union{DimsInteger,Indices} , I1:: AbstractVector{T} , I:: AbstractVector{T} ...) where {T<: Integer } =
17001639 _sub2ind_vecs (inds, I1, I... )
17011640function _sub2ind_vecs (inds, I:: AbstractVector... )
17021641 I1 = I[1 ]
@@ -1712,39 +1651,28 @@ end
17121651function _sub2ind! (Iout, inds, Iinds, I)
17131652 @_noinline_meta
17141653 for i in Iinds
1715- # Iout[i] = sub2ind (inds, map(Ij -> Ij[i], I)...)
1654+ # Iout[i] = _sub2ind (inds, map(Ij -> Ij[i], I)...)
17161655 Iout[i] = sub2ind_vec (inds, i, I)
17171656 end
17181657 Iout
17191658end
17201659
1721- sub2ind_vec (inds, i, I) = (@_inline_meta ; sub2ind (inds, _sub2ind_vec (i, I... )... ))
1660+ sub2ind_vec (inds, i, I) = (@_inline_meta ; _sub2ind (inds, _sub2ind_vec (i, I... )... ))
17221661_sub2ind_vec (i, I1, I... ) = (@_inline_meta ; (I1[i], _sub2ind_vec (i, I... )... ))
17231662_sub2ind_vec (i) = ()
17241663
1725- function ind2sub (inds:: Union{DimsInteger{N},Indices{N}} , ind:: AbstractVector{<:Integer} ) where N
1664+ function _ind2sub (inds:: Union{DimsInteger{N},Indices{N}} , ind:: AbstractVector{<:Integer} ) where N
17261665 M = length (ind)
17271666 t = ntuple (n-> similar (ind),Val (N))
17281667 for (i,idx) in pairs (IndexLinear (), ind)
1729- sub = ind2sub (inds, idx)
1668+ sub = _ind2sub (inds, idx)
17301669 for j = 1 : N
17311670 t[j][i] = sub[j]
17321671 end
17331672 end
17341673 t
17351674end
17361675
1737- function ind2sub! (sub:: Array{T} , dims:: Tuple{Vararg{T}} , ind:: T ) where T<: Integer
1738- ndims = length (dims)
1739- for i= 1 : ndims- 1
1740- ind2 = div (ind- 1 ,dims[i])+ 1
1741- sub[i] = ind - dims[i]* (ind2- 1 )
1742- ind = ind2
1743- end
1744- sub[ndims] = ind
1745- return sub
1746- end
1747-
17481676# # iteration utilities ##
17491677
17501678"""
0 commit comments