Skip to content

Commit

Permalink
Rename indices as axes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Ferris committed Dec 14, 2017
1 parent a9bb7d3 commit 2dab3fd
Show file tree
Hide file tree
Showing 55 changed files with 305 additions and 298 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,9 @@ Deprecated or removed
`similar(::Associative, ::Pair{K, V})` has been deprecated in favour of
`empty(::Associative, K, V)` ([#24390]).

* `indices(a)` and `indices(a,d)` have been deprecated in favor of `axes(a)` and
`axes(a, d)` ([#25057]).

Command-line option changes
---------------------------

Expand Down
66 changes: 33 additions & 33 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,49 @@ size(x, d1::Integer, d2::Integer, dx::Vararg{Integer, N}) where {N} =
(size(x, d1), size(x, d2), ntuple(k->size(x, dx[k]), Val(N))...)

"""
indices(A, d)
axes(A, d)
Return the valid range of indices for array `A` along dimension `d`.
# Examples
```jldoctest
julia> A = ones(5,6,7);
julia> indices(A,2)
julia> axes(A,2)
Base.OneTo(6)
```
"""
function indices(A::AbstractArray{T,N}, d) where {T,N}
function axes(A::AbstractArray{T,N}, d) where {T,N}
@_inline_meta
d <= N ? indices(A)[d] : OneTo(1)
d <= N ? axes(A)[d] : OneTo(1)
end

"""
indices(A)
axes(A)
Return the tuple of valid indices for array `A`.
# Examples
```jldoctest
julia> A = ones(5,6,7);
julia> indices(A)
julia> axes(A)
(Base.OneTo(5), Base.OneTo(6), Base.OneTo(7))
```
"""
function indices(A)
function axes(A)
@_inline_meta
map(OneTo, size(A))
end

# Performance optimization: get rid of a branch on `d` in `indices(A, d)`
# Performance optimization: get rid of a branch on `d` in `axes(A, d)`
# for d=1. 1d arrays are heavily used, and the first dimension comes up
# in other applications.
indices1(A::AbstractArray{<:Any,0}) = OneTo(1)
indices1(A::AbstractArray) = (@_inline_meta; indices(A)[1])
indices1(A::AbstractArray) = (@_inline_meta; axes(A)[1])
indices1(iter) = OneTo(length(iter))

unsafe_indices(A) = indices(A)
unsafe_indices(A) = axes(A)
unsafe_indices(r::AbstractRange) = (OneTo(unsafe_length(r)),) # Ranges use checked_sub for size

"""
Expand All @@ -86,7 +86,7 @@ Return a `UnitRange` specifying the valid range of indices for `A[i]`
where `i` is an `Int`. For arrays with conventional indexing (indices
start at 1), or any multidimensional array, this is `1:length(A)`;
however, for one-dimensional arrays with unconventional indices, this
is `indices(A, 1)`.
is `axes(A, 1)`.
Calling this function is the "safe" way to write algorithms that
exploit linear indexing.
Expand All @@ -104,7 +104,7 @@ julia> extrema(b)
linearindices(A::AbstractArray) = (@_inline_meta; OneTo(_length(A)))
linearindices(A::AbstractVector) = (@_inline_meta; indices1(A))

keys(a::AbstractArray) = CartesianRange(indices(a))
keys(a::AbstractArray) = CartesianRange(axes(a))
keys(a::AbstractVector) = linearindices(a)

prevind(::AbstractArray, i::Integer) = Int(i)-1
Expand Down Expand Up @@ -150,7 +150,7 @@ julia> length([1 2; 3 4])
```
"""
length(t::AbstractArray) = (@_inline_meta; prod(size(t)))
_length(A::AbstractArray) = (@_inline_meta; prod(map(unsafe_length, indices(A)))) # circumvent missing size
_length(A::AbstractArray) = (@_inline_meta; prod(map(unsafe_length, axes(A)))) # circumvent missing size
_length(A) = (@_inline_meta; length(A))

"""
Expand Down Expand Up @@ -376,7 +376,7 @@ false
"""
function checkbounds(::Type{Bool}, A::AbstractArray, I...)
@_inline_meta
checkbounds_indices(Bool, indices(A), I)
checkbounds_indices(Bool, axes(A), I)
end
# Linear indexing is explicitly allowed when there is only one (non-cartesian) index
function checkbounds(::Type{Bool}, A::AbstractArray, i)
Expand All @@ -386,7 +386,7 @@ end
# As a special extension, allow using logical arrays that match the source array exactly
function checkbounds(::Type{Bool}, A::AbstractArray{<:Any,N}, I::AbstractArray{Bool,N}) where N
@_inline_meta
indices(A) == indices(I)
axes(A) == axes(I)
end

"""
Expand Down Expand Up @@ -519,7 +519,7 @@ julia> similar(falses(10), Float64, 2, 4)
"""
similar(a::AbstractArray{T}) where {T} = similar(a, T)
similar(a::AbstractArray, ::Type{T}) where {T} = similar(a, T, to_shape(indices(a)))
similar(a::AbstractArray, ::Type{T}) where {T} = similar(a, T, to_shape(axes(a)))
similar(a::AbstractArray{T}, dims::Tuple) where {T} = similar(a, T, to_shape(dims))
similar(a::AbstractArray{T}, dims::DimOrInd...) where {T} = similar(a, T, to_shape(dims))
similar(a::AbstractArray, ::Type{T}, dims::DimOrInd...) where {T} = similar(a, T, to_shape(dims))
Expand All @@ -545,20 +545,20 @@ argument. `storagetype` might be a type or a function.
**Examples**:
similar(Array{Int}, indices(A))
similar(Array{Int}, axes(A))
creates an array that "acts like" an `Array{Int}` (and might indeed be
backed by one), but which is indexed identically to `A`. If `A` has
conventional indexing, this will be identical to
`Array{Int}(uninitialized, size(A))`, but if `A` has unconventional indexing then the
indices of the result will match `A`.
similar(BitArray, (indices(A, 2),))
similar(BitArray, (axes(A, 2),))
would create a 1-dimensional logical array whose indices match those
of the columns of `A`.
similar(dims->zeros(Int, dims), indices(A))
similar(dims->zeros(Int, dims), axes(A))
would create an array of `Int`, initialized to zero, matching the
indices of `A`.
Expand Down Expand Up @@ -869,7 +869,7 @@ convert(::Type{Array}, A::AbstractArray{T,N}) where {T,N} = convert(Array{T,N},
Represents the array `y` as an array having the same indices type as `x`.
"""
of_indices(x, y) = similar(dims->y, oftype(indices(x), indices(y)))
of_indices(x, y) = similar(dims->y, oftype(axes(x), axes(y)))


## range conversions ##
Expand Down Expand Up @@ -986,11 +986,11 @@ function _to_subscript_indices(A::AbstractArray{T,N}, I::Int...) where {T,N}
_to_subscript_indices(A, J, Jrem)
end
_to_subscript_indices(A::AbstractArray, J::Tuple, Jrem::Tuple{}) =
__to_subscript_indices(A, indices(A), J, Jrem)
__to_subscript_indices(A, axes(A), J, Jrem)
function __to_subscript_indices(A::AbstractArray,
::Tuple{AbstractUnitRange,Vararg{AbstractUnitRange}}, J::Tuple, Jrem::Tuple{})
@_inline_meta
(J..., map(first, tail(_remaining_size(J, indices(A))))...)
(J..., map(first, tail(_remaining_size(J, axes(A))))...)
end
_to_subscript_indices(A, J::Tuple, Jrem::Tuple) = J # already bounds-checked, safe to drop
_to_subscript_indices(A::AbstractArray{T,N}, I::Vararg{Int,N}) where {T,N} = I
Expand Down Expand Up @@ -1200,7 +1200,7 @@ cat_size(A, d) = 1
cat_size(A::AbstractArray, d) = size(A, d)

cat_indices(A, d) = OneTo(1)
cat_indices(A::AbstractArray, d) = indices(A, d)
cat_indices(A::AbstractArray, d) = axes(A, d)

cat_similar(A, T, shape) = Array{T}(uninitialized, shape)
cat_similar(A::AbstractArray, T, shape) = similar(A, T, shape)
Expand Down Expand Up @@ -1543,7 +1543,7 @@ end

function isequal(A::AbstractArray, B::AbstractArray)
if A === B return true end
if indices(A) != indices(B)
if axes(A) != axes(B)
return false
end
if isa(A,AbstractRange) != isa(B,AbstractRange)
Expand All @@ -1566,7 +1566,7 @@ function lexcmp(A::AbstractArray, B::AbstractArray)
end

function (==)(A::AbstractArray, B::AbstractArray)
if indices(A) != indices(B)
if axes(A) != axes(B)
return false
end
if isa(A,AbstractRange) != isa(B,AbstractRange)
Expand All @@ -1588,7 +1588,7 @@ end
# fallbacks
function sub2ind(A::AbstractArray, I...)
@_inline_meta
sub2ind(indices(A), I...)
sub2ind(axes(A), I...)
end

"""
Expand All @@ -1609,7 +1609,7 @@ julia> ind2sub(A,70)
"""
function ind2sub(A::AbstractArray, ind)
@_inline_meta
ind2sub(indices(A), ind)
ind2sub(axes(A), ind)
end

# 0-dimensional arrays and indexing with []
Expand Down Expand Up @@ -1835,16 +1835,16 @@ function mapslices(f, A::AbstractArray, dims::AbstractVector)
return map(f,A)
end

dimsA = [indices(A)...]
dimsA = [axes(A)...]
ndimsA = ndims(A)
alldims = [1:ndimsA;]

otherdims = setdiff(alldims, dims)

idx = Any[first(ind) for ind in indices(A)]
idx = Any[first(ind) for ind in axes(A)]
itershape = tuple(dimsA[otherdims]...)
for d in dims
idx[d] = Slice(indices(A, d))
idx[d] = Slice(axes(A, d))
end

# Apply the function to the first slice in order to determine the next steps
Expand All @@ -1867,13 +1867,13 @@ function mapslices(f, A::AbstractArray, dims::AbstractVector)
if eltype(Rsize) == Int
Rsize[dims] = [size(r1)..., ntuple(d->1, nextra)...]
else
Rsize[dims] = [indices(r1)..., ntuple(d->OneTo(1), nextra)...]
Rsize[dims] = [axes(r1)..., ntuple(d->OneTo(1), nextra)...]
end
R = similar(r1, tuple(Rsize...,))

ridx = Any[map(first, indices(R))...]
ridx = Any[map(first, axes(R))...]
for d in dims
ridx[d] = indices(R,d)
ridx[d] = axes(R,d)
end

R[ridx...] = r1
Expand Down
16 changes: 8 additions & 8 deletions base/abstractarraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ julia> squeeze(a,3)
function squeeze(A::AbstractArray, dims::Dims)
for i in 1:length(dims)
1 <= dims[i] <= ndims(A) || throw(ArgumentError("squeezed dims must be in range 1:ndims(A)"))
length(indices(A, dims[i])) == 1 || throw(ArgumentError("squeezed dims must all be size 1"))
length(axes(A, dims[i])) == 1 || throw(ArgumentError("squeezed dims must all be size 1"))
for j = 1:i-1
dims[j] == dims[i] && throw(ArgumentError("squeezed dims must be unique"))
end
end
d = ()
for i = 1:ndims(A)
if !in(i, dims)
d = tuple(d..., indices(A, i))
d = tuple(d..., axes(A, i))
end
end
reshape(A, d::typeof(_sub(indices(A), dims)))
reshape(A, d::typeof(_sub(axes(A), dims)))
end

squeeze(A::AbstractArray, dim::Integer) = squeeze(A, (Int(dim),))
Expand Down Expand Up @@ -120,7 +120,7 @@ function slicedim(A::AbstractArray, d::Integer, i)
d >= 1 || throw(ArgumentError("dimension must be ≥ 1"))
nd = ndims(A)
d > nd && (i == 1 || throw_boundserror(A, (ntuple(k->Colon(),nd)..., ntuple(k->1,d-1-nd)..., i)))
A[setindex(indices(A), i, d)...]
A[setindex(axes(A), i, d)...]
end

"""
Expand Down Expand Up @@ -149,7 +149,7 @@ function flipdim(A::AbstractArray, d::Integer)
elseif nd == 1
return reverse(A)
end
inds = indices(A)
inds = axes(A)
B = similar(A)
nnd = 0
for i = 1:nd
Expand All @@ -165,7 +165,7 @@ function flipdim(A::AbstractArray, d::Integer)
return B
end
let B=B # workaround #15276
alli = [ indices(B,n) for n in 1:nd ]
alli = [ axes(B,n) for n in 1:nd ]
for i in indsd
B[[ n==d ? sd-i : alli[n] for n in 1:nd ]...] = slicedim(A, d, i)
end
Expand Down Expand Up @@ -371,10 +371,10 @@ cat_fill!(R, X::AbstractArray, inds) = fill!(view(R, inds...), X)

# fill the first inner block
if all(x -> x == 1, inner)
R[indices(A)...] = A
R[axes(A)...] = A
else
inner_indices = [1:n for n in inner]
for c in CartesianRange(indices(A))
for c in CartesianRange(axes(A))
for i in 1:ndims(A)
n = inner[i]
inner_indices[i] = (1:n) .+ ((c[i] - 1) * n)
Expand Down
14 changes: 7 additions & 7 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ julia> collect(Float64, 1:2:5)
collect(::Type{T}, itr) where {T} = _collect(T, itr, iteratorsize(itr))

_collect(::Type{T}, itr, isz::HasLength) where {T} = copy!(Vector{T}(uninitialized, Int(length(itr)::Integer)), itr)
_collect(::Type{T}, itr, isz::HasShape) where {T} = copy!(similar(Array{T}, indices(itr)), itr)
_collect(::Type{T}, itr, isz::HasShape) where {T} = copy!(similar(Array{T}, axes(itr)), itr)
function _collect(::Type{T}, itr, isz::SizeUnknown) where T
a = Vector{T}()
for x in itr
Expand All @@ -445,7 +445,7 @@ end
# make a collection similar to `c` and appropriate for collecting `itr`
_similar_for(c::AbstractArray, T, itr, ::SizeUnknown) = similar(c, T, 0)
_similar_for(c::AbstractArray, T, itr, ::HasLength) = similar(c, T, Int(length(itr)::Integer))
_similar_for(c::AbstractArray, T, itr, ::HasShape) = similar(c, T, indices(itr))
_similar_for(c::AbstractArray, T, itr, ::HasShape) = similar(c, T, axes(itr))
_similar_for(c, T, itr, isz) = similar(c, T)

"""
Expand All @@ -470,7 +470,7 @@ julia> collect(1:2:13)
"""
collect(itr) = _collect(1:1 #= Array =#, itr, iteratoreltype(itr), iteratorsize(itr))

collect(A::AbstractArray) = _collect_indices(indices(A), A)
collect(A::AbstractArray) = _collect_indices(axes(A), A)

collect_similar(cont, itr) = _collect(cont, itr, iteratoreltype(itr), iteratorsize(itr))

Expand All @@ -490,7 +490,7 @@ _collect_indices(indsA::Tuple{Vararg{OneTo}}, A) =
copy!(Array{eltype(A)}(uninitialized, length.(indsA)), A)
function _collect_indices(indsA, A)
B = Array{eltype(A)}(uninitialized, length.(indsA))
copy!(B, CartesianRange(indices(B)), A, CartesianRange(indsA))
copy!(B, CartesianRange(axes(B)), A, CartesianRange(indsA))
end

# define this as a macro so that the call to Inference
Expand All @@ -510,7 +510,7 @@ else
end

_array_for(::Type{T}, itr, ::HasLength) where {T} = Vector{T}(uninitialized, Int(length(itr)::Integer))
_array_for(::Type{T}, itr, ::HasShape) where {T} = similar(Array{T}, indices(itr))::Array{T}
_array_for(::Type{T}, itr, ::HasShape) where {T} = similar(Array{T}, axes(itr))::Array{T}

function collect(itr::Generator)
isz = iteratorsize(itr.iter)
Expand Down Expand Up @@ -1799,7 +1799,7 @@ function findn(A::AbstractMatrix)
I = similar(A, Int, nnzA)
J = similar(A, Int, nnzA)
cnt = 1
for j=indices(A,2), i=indices(A,1)
for j=axes(A,2), i=axes(A,1)
if A[i,j] != 0
I[cnt] = i
J[cnt] = j
Expand Down Expand Up @@ -1834,7 +1834,7 @@ function findnz(A::AbstractMatrix{T}) where T
NZs = Vector{T}(uninitialized, nnzA)
cnt = 1
if nnzA > 0
for j=indices(A,2), i=indices(A,1)
for j=axes(A,2), i=axes(A,1)
Aij = A[i,j]
if Aij != 0
I[cnt] = i
Expand Down
Loading

0 comments on commit 2dab3fd

Please sign in to comment.