Skip to content

Commit 2dab3fd

Browse files
author
Andy Ferris
committed
Rename indices as axes
1 parent a9bb7d3 commit 2dab3fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+305
-298
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,9 @@ Deprecated or removed
14061406
`similar(::Associative, ::Pair{K, V})` has been deprecated in favour of
14071407
`empty(::Associative, K, V)` ([#24390]).
14081408
1409+
* `indices(a)` and `indices(a,d)` have been deprecated in favor of `axes(a)` and
1410+
`axes(a, d)` ([#25057]).
1411+
14091412
Command-line option changes
14101413
---------------------------
14111414

base/abstractarray.jl

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,49 +34,49 @@ size(x, d1::Integer, d2::Integer, dx::Vararg{Integer, N}) where {N} =
3434
(size(x, d1), size(x, d2), ntuple(k->size(x, dx[k]), Val(N))...)
3535

3636
"""
37-
indices(A, d)
37+
axes(A, d)
3838
3939
Return the valid range of indices for array `A` along dimension `d`.
4040
4141
# Examples
4242
```jldoctest
4343
julia> A = ones(5,6,7);
4444
45-
julia> indices(A,2)
45+
julia> axes(A,2)
4646
Base.OneTo(6)
4747
```
4848
"""
49-
function indices(A::AbstractArray{T,N}, d) where {T,N}
49+
function axes(A::AbstractArray{T,N}, d) where {T,N}
5050
@_inline_meta
51-
d <= N ? indices(A)[d] : OneTo(1)
51+
d <= N ? axes(A)[d] : OneTo(1)
5252
end
5353

5454
"""
55-
indices(A)
55+
axes(A)
5656
5757
Return the tuple of valid indices for array `A`.
5858
5959
# Examples
6060
```jldoctest
6161
julia> A = ones(5,6,7);
6262
63-
julia> indices(A)
63+
julia> axes(A)
6464
(Base.OneTo(5), Base.OneTo(6), Base.OneTo(7))
6565
```
6666
"""
67-
function indices(A)
67+
function axes(A)
6868
@_inline_meta
6969
map(OneTo, size(A))
7070
end
7171

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

79-
unsafe_indices(A) = indices(A)
79+
unsafe_indices(A) = axes(A)
8080
unsafe_indices(r::AbstractRange) = (OneTo(unsafe_length(r)),) # Ranges use checked_sub for size
8181

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

107-
keys(a::AbstractArray) = CartesianRange(indices(a))
107+
keys(a::AbstractArray) = CartesianRange(axes(a))
108108
keys(a::AbstractVector) = linearindices(a)
109109

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

156156
"""
@@ -376,7 +376,7 @@ false
376376
"""
377377
function checkbounds(::Type{Bool}, A::AbstractArray, I...)
378378
@_inline_meta
379-
checkbounds_indices(Bool, indices(A), I)
379+
checkbounds_indices(Bool, axes(A), I)
380380
end
381381
# Linear indexing is explicitly allowed when there is only one (non-cartesian) index
382382
function checkbounds(::Type{Bool}, A::AbstractArray, i)
@@ -386,7 +386,7 @@ end
386386
# As a special extension, allow using logical arrays that match the source array exactly
387387
function checkbounds(::Type{Bool}, A::AbstractArray{<:Any,N}, I::AbstractArray{Bool,N}) where N
388388
@_inline_meta
389-
indices(A) == indices(I)
389+
axes(A) == axes(I)
390390
end
391391

392392
"""
@@ -519,7 +519,7 @@ julia> similar(falses(10), Float64, 2, 4)
519519
520520
"""
521521
similar(a::AbstractArray{T}) where {T} = similar(a, T)
522-
similar(a::AbstractArray, ::Type{T}) where {T} = similar(a, T, to_shape(indices(a)))
522+
similar(a::AbstractArray, ::Type{T}) where {T} = similar(a, T, to_shape(axes(a)))
523523
similar(a::AbstractArray{T}, dims::Tuple) where {T} = similar(a, T, to_shape(dims))
524524
similar(a::AbstractArray{T}, dims::DimOrInd...) where {T} = similar(a, T, to_shape(dims))
525525
similar(a::AbstractArray, ::Type{T}, dims::DimOrInd...) where {T} = similar(a, T, to_shape(dims))
@@ -545,20 +545,20 @@ argument. `storagetype` might be a type or a function.
545545
546546
**Examples**:
547547
548-
similar(Array{Int}, indices(A))
548+
similar(Array{Int}, axes(A))
549549
550550
creates an array that "acts like" an `Array{Int}` (and might indeed be
551551
backed by one), but which is indexed identically to `A`. If `A` has
552552
conventional indexing, this will be identical to
553553
`Array{Int}(uninitialized, size(A))`, but if `A` has unconventional indexing then the
554554
indices of the result will match `A`.
555555
556-
similar(BitArray, (indices(A, 2),))
556+
similar(BitArray, (axes(A, 2),))
557557
558558
would create a 1-dimensional logical array whose indices match those
559559
of the columns of `A`.
560560
561-
similar(dims->zeros(Int, dims), indices(A))
561+
similar(dims->zeros(Int, dims), axes(A))
562562
563563
would create an array of `Int`, initialized to zero, matching the
564564
indices of `A`.
@@ -869,7 +869,7 @@ convert(::Type{Array}, A::AbstractArray{T,N}) where {T,N} = convert(Array{T,N},
869869
870870
Represents the array `y` as an array having the same indices type as `x`.
871871
"""
872-
of_indices(x, y) = similar(dims->y, oftype(indices(x), indices(y)))
872+
of_indices(x, y) = similar(dims->y, oftype(axes(x), axes(y)))
873873

874874

875875
## range conversions ##
@@ -986,11 +986,11 @@ function _to_subscript_indices(A::AbstractArray{T,N}, I::Int...) where {T,N}
986986
_to_subscript_indices(A, J, Jrem)
987987
end
988988
_to_subscript_indices(A::AbstractArray, J::Tuple, Jrem::Tuple{}) =
989-
__to_subscript_indices(A, indices(A), J, Jrem)
989+
__to_subscript_indices(A, axes(A), J, Jrem)
990990
function __to_subscript_indices(A::AbstractArray,
991991
::Tuple{AbstractUnitRange,Vararg{AbstractUnitRange}}, J::Tuple, Jrem::Tuple{})
992992
@_inline_meta
993-
(J..., map(first, tail(_remaining_size(J, indices(A))))...)
993+
(J..., map(first, tail(_remaining_size(J, axes(A))))...)
994994
end
995995
_to_subscript_indices(A, J::Tuple, Jrem::Tuple) = J # already bounds-checked, safe to drop
996996
_to_subscript_indices(A::AbstractArray{T,N}, I::Vararg{Int,N}) where {T,N} = I
@@ -1200,7 +1200,7 @@ cat_size(A, d) = 1
12001200
cat_size(A::AbstractArray, d) = size(A, d)
12011201

12021202
cat_indices(A, d) = OneTo(1)
1203-
cat_indices(A::AbstractArray, d) = indices(A, d)
1203+
cat_indices(A::AbstractArray, d) = axes(A, d)
12041204

12051205
cat_similar(A, T, shape) = Array{T}(uninitialized, shape)
12061206
cat_similar(A::AbstractArray, T, shape) = similar(A, T, shape)
@@ -1543,7 +1543,7 @@ end
15431543

15441544
function isequal(A::AbstractArray, B::AbstractArray)
15451545
if A === B return true end
1546-
if indices(A) != indices(B)
1546+
if axes(A) != axes(B)
15471547
return false
15481548
end
15491549
if isa(A,AbstractRange) != isa(B,AbstractRange)
@@ -1566,7 +1566,7 @@ function lexcmp(A::AbstractArray, B::AbstractArray)
15661566
end
15671567

15681568
function (==)(A::AbstractArray, B::AbstractArray)
1569-
if indices(A) != indices(B)
1569+
if axes(A) != axes(B)
15701570
return false
15711571
end
15721572
if isa(A,AbstractRange) != isa(B,AbstractRange)
@@ -1588,7 +1588,7 @@ end
15881588
# fallbacks
15891589
function sub2ind(A::AbstractArray, I...)
15901590
@_inline_meta
1591-
sub2ind(indices(A), I...)
1591+
sub2ind(axes(A), I...)
15921592
end
15931593

15941594
"""
@@ -1609,7 +1609,7 @@ julia> ind2sub(A,70)
16091609
"""
16101610
function ind2sub(A::AbstractArray, ind)
16111611
@_inline_meta
1612-
ind2sub(indices(A), ind)
1612+
ind2sub(axes(A), ind)
16131613
end
16141614

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

1838-
dimsA = [indices(A)...]
1838+
dimsA = [axes(A)...]
18391839
ndimsA = ndims(A)
18401840
alldims = [1:ndimsA;]
18411841

18421842
otherdims = setdiff(alldims, dims)
18431843

1844-
idx = Any[first(ind) for ind in indices(A)]
1844+
idx = Any[first(ind) for ind in axes(A)]
18451845
itershape = tuple(dimsA[otherdims]...)
18461846
for d in dims
1847-
idx[d] = Slice(indices(A, d))
1847+
idx[d] = Slice(axes(A, d))
18481848
end
18491849

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

1874-
ridx = Any[map(first, indices(R))...]
1874+
ridx = Any[map(first, axes(R))...]
18751875
for d in dims
1876-
ridx[d] = indices(R,d)
1876+
ridx[d] = axes(R,d)
18771877
end
18781878

18791879
R[ridx...] = r1

base/abstractarraymath.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,18 @@ julia> squeeze(a,3)
6767
function squeeze(A::AbstractArray, dims::Dims)
6868
for i in 1:length(dims)
6969
1 <= dims[i] <= ndims(A) || throw(ArgumentError("squeezed dims must be in range 1:ndims(A)"))
70-
length(indices(A, dims[i])) == 1 || throw(ArgumentError("squeezed dims must all be size 1"))
70+
length(axes(A, dims[i])) == 1 || throw(ArgumentError("squeezed dims must all be size 1"))
7171
for j = 1:i-1
7272
dims[j] == dims[i] && throw(ArgumentError("squeezed dims must be unique"))
7373
end
7474
end
7575
d = ()
7676
for i = 1:ndims(A)
7777
if !in(i, dims)
78-
d = tuple(d..., indices(A, i))
78+
d = tuple(d..., axes(A, i))
7979
end
8080
end
81-
reshape(A, d::typeof(_sub(indices(A), dims)))
81+
reshape(A, d::typeof(_sub(axes(A), dims)))
8282
end
8383

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

126126
"""
@@ -149,7 +149,7 @@ function flipdim(A::AbstractArray, d::Integer)
149149
elseif nd == 1
150150
return reverse(A)
151151
end
152-
inds = indices(A)
152+
inds = axes(A)
153153
B = similar(A)
154154
nnd = 0
155155
for i = 1:nd
@@ -165,7 +165,7 @@ function flipdim(A::AbstractArray, d::Integer)
165165
return B
166166
end
167167
let B=B # workaround #15276
168-
alli = [ indices(B,n) for n in 1:nd ]
168+
alli = [ axes(B,n) for n in 1:nd ]
169169
for i in indsd
170170
B[[ n==d ? sd-i : alli[n] for n in 1:nd ]...] = slicedim(A, d, i)
171171
end
@@ -371,10 +371,10 @@ cat_fill!(R, X::AbstractArray, inds) = fill!(view(R, inds...), X)
371371

372372
# fill the first inner block
373373
if all(x -> x == 1, inner)
374-
R[indices(A)...] = A
374+
R[axes(A)...] = A
375375
else
376376
inner_indices = [1:n for n in inner]
377-
for c in CartesianRange(indices(A))
377+
for c in CartesianRange(axes(A))
378378
for i in 1:ndims(A)
379379
n = inner[i]
380380
inner_indices[i] = (1:n) .+ ((c[i] - 1) * n)

base/array.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ julia> collect(Float64, 1:2:5)
433433
collect(::Type{T}, itr) where {T} = _collect(T, itr, iteratorsize(itr))
434434

435435
_collect(::Type{T}, itr, isz::HasLength) where {T} = copy!(Vector{T}(uninitialized, Int(length(itr)::Integer)), itr)
436-
_collect(::Type{T}, itr, isz::HasShape) where {T} = copy!(similar(Array{T}, indices(itr)), itr)
436+
_collect(::Type{T}, itr, isz::HasShape) where {T} = copy!(similar(Array{T}, axes(itr)), itr)
437437
function _collect(::Type{T}, itr, isz::SizeUnknown) where T
438438
a = Vector{T}()
439439
for x in itr
@@ -445,7 +445,7 @@ end
445445
# make a collection similar to `c` and appropriate for collecting `itr`
446446
_similar_for(c::AbstractArray, T, itr, ::SizeUnknown) = similar(c, T, 0)
447447
_similar_for(c::AbstractArray, T, itr, ::HasLength) = similar(c, T, Int(length(itr)::Integer))
448-
_similar_for(c::AbstractArray, T, itr, ::HasShape) = similar(c, T, indices(itr))
448+
_similar_for(c::AbstractArray, T, itr, ::HasShape) = similar(c, T, axes(itr))
449449
_similar_for(c, T, itr, isz) = similar(c, T)
450450

451451
"""
@@ -470,7 +470,7 @@ julia> collect(1:2:13)
470470
"""
471471
collect(itr) = _collect(1:1 #= Array =#, itr, iteratoreltype(itr), iteratorsize(itr))
472472

473-
collect(A::AbstractArray) = _collect_indices(indices(A), A)
473+
collect(A::AbstractArray) = _collect_indices(axes(A), A)
474474

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

@@ -490,7 +490,7 @@ _collect_indices(indsA::Tuple{Vararg{OneTo}}, A) =
490490
copy!(Array{eltype(A)}(uninitialized, length.(indsA)), A)
491491
function _collect_indices(indsA, A)
492492
B = Array{eltype(A)}(uninitialized, length.(indsA))
493-
copy!(B, CartesianRange(indices(B)), A, CartesianRange(indsA))
493+
copy!(B, CartesianRange(axes(B)), A, CartesianRange(indsA))
494494
end
495495

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

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

515515
function collect(itr::Generator)
516516
isz = iteratorsize(itr.iter)
@@ -1799,7 +1799,7 @@ function findn(A::AbstractMatrix)
17991799
I = similar(A, Int, nnzA)
18001800
J = similar(A, Int, nnzA)
18011801
cnt = 1
1802-
for j=indices(A,2), i=indices(A,1)
1802+
for j=axes(A,2), i=axes(A,1)
18031803
if A[i,j] != 0
18041804
I[cnt] = i
18051805
J[cnt] = j
@@ -1834,7 +1834,7 @@ function findnz(A::AbstractMatrix{T}) where T
18341834
NZs = Vector{T}(uninitialized, nnzA)
18351835
cnt = 1
18361836
if nnzA > 0
1837-
for j=indices(A,2), i=indices(A,1)
1837+
for j=axes(A,2), i=axes(A,1)
18381838
Aij = A[i,j]
18391839
if Aij != 0
18401840
I[cnt] = i

0 commit comments

Comments
 (0)