diff --git a/base/abstractarray.jl b/base/abstractarray.jl index c393f3fed70d7..9dd5f1165a6f6 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -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(axes(a)) +keys(a::AbstractArray) = CartesianIndices(axes(a)) keys(a::AbstractVector) = linearindices(a) prevind(::AbstractArray, i::Integer) = Int(i)-1 @@ -773,7 +773,7 @@ zero(x::AbstractArray{T}) where {T} = fill!(similar(x), zero(T)) # Allows fast iteration by default for both IndexLinear and IndexCartesian arrays # While the definitions for IndexLinear are all simple enough to inline on their -# own, IndexCartesian's CartesianRange is more complicated and requires explicit +# own, IndexCartesian's CartesianIndices is more complicated and requires explicit # inlining. start(A::AbstractArray) = (@_inline_meta; itr = eachindex(A); (itr, start(itr))) next(A::AbstractArray, i) = (@_propagate_inbounds_meta; (idx, s) = next(i[1], i[2]); (A[idx], (i[1], s))) @@ -824,7 +824,7 @@ A[iter] = 0 If you supply more than one `AbstractArray` argument, `eachindex` will create an iterable object that is fast for all arguments (a `UnitRange` -if all inputs have fast linear indexing, a [`CartesianRange`](@ref) +if all inputs have fast linear indexing, a [`CartesianIndices`](@ref) otherwise). If the arrays have different sizes and/or dimensionalities, `eachindex` will return an iterable that spans the largest range along each dimension. @@ -1815,7 +1815,7 @@ function mapslices(f, A::AbstractArray, dims::AbstractVector) R[ridx...] = r1 nidx = length(otherdims) - indices = Iterators.drop(CartesianRange(itershape), 1) + indices = Iterators.drop(CartesianIndices(itershape), 1) inner_mapslices!(safe_for_reuse, indices, nidx, idx, otherdims, ridx, Aslice, A, f, R) end diff --git a/base/abstractarraymath.jl b/base/abstractarraymath.jl index 8a8650da28485..ee208780fffa2 100644 --- a/base/abstractarraymath.jl +++ b/base/abstractarraymath.jl @@ -374,7 +374,7 @@ cat_fill!(R, X::AbstractArray, inds) = fill!(view(R, inds...), X) R[axes(A)...] = A else inner_indices = [1:n for n in inner] - for c in CartesianRange(axes(A)) + for c in CartesianIndices(axes(A)) for i in 1:ndims(A) n = inner[i] inner_indices[i] = (1:n) .+ ((c[i] - 1) * n) diff --git a/base/array.jl b/base/array.jl index 289382bc7ae04..dfb427963ae9d 100644 --- a/base/array.jl +++ b/base/array.jl @@ -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(axes(B)), A, CartesianRange(indsA)) + copy!(B, CartesianIndices(axes(B)), A, CartesianIndices(indsA)) end # define this as a macro so that the call to Inference diff --git a/base/arrayshow.jl b/base/arrayshow.jl index b5e1f20f04ae9..b20f9286c86a8 100644 --- a/base/arrayshow.jl +++ b/base/arrayshow.jl @@ -261,7 +261,7 @@ function show_nd(io::IO, a::AbstractArray, print_matrix::Function, label_slices: end tailinds = tail(tail(axes(a))) nd = ndims(a)-2 - for I in CartesianRange(tailinds) + for I in CartesianIndices(tailinds) idxs = I.I if limit for i = 1:nd diff --git a/base/bitarray.jl b/base/bitarray.jl index 4b95491fa6f08..031b30c44f8d7 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -548,7 +548,7 @@ gen_bitarray(isz::IteratorSize, itr) = gen_bitarray_from_itr(itr, start(itr)) # generic iterable with known shape function gen_bitarray(::HasShape, itr) B = BitArray(uninitialized, size(itr)) - for (I,x) in zip(CartesianRange(axes(itr)), itr) + for (I,x) in zip(CartesianIndices(axes(itr)), itr) B[I] = x end return B diff --git a/base/broadcast.jl b/base/broadcast.jl index a3d6d57547d4c..afa4285f5609d 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -454,7 +454,7 @@ as in `broadcast!(f, A, A, B)` to perform `A[:] = broadcast(f, A, B)`. shape = broadcast_indices(C) @boundscheck check_broadcast_indices(shape, A, Bs...) keeps, Idefaults = map_newindexer(shape, A, Bs) - iter = CartesianRange(shape) + iter = CartesianIndices(shape) _broadcast!(f, C, keeps, Idefaults, A, Bs, Val(N), iter) return C end @@ -616,7 +616,7 @@ end # accommodate later values. function broadcast_nonleaf(f, s::NonleafHandlingTypes, ::Type{ElType}, shape::Indices, As...) where ElType nargs = length(As) - iter = CartesianRange(shape) + iter = CartesianIndices(shape) if isempty(iter) return Base.similar(Array{ElType}, shape) end diff --git a/base/deprecated.jl b/base/deprecated.jl index 558d2b7acb08f..229fa8dcdab8a 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1381,10 +1381,10 @@ import .LinAlg: lufact, lufact!, qrfact, qrfact!, cholfact, cholfact! @deprecate read(s::IO, t::Type, d1::Integer, dims::Integer...) read!(s, Array{t}(uninitialized, convert(Tuple{Vararg{Int}},tuple(d1,dims...)))) @deprecate read(s::IO, t::Type, dims::Dims) read!(s, Array{t}(uninitialized, dims)) -function CartesianRange(start::CartesianIndex{N}, stop::CartesianIndex{N}) where N +function CartesianIndices(start::CartesianIndex{N}, stop::CartesianIndex{N}) where N inds = map((f,l)->f:l, start.I, stop.I) - depwarn("the internal representation of CartesianRange has changed, use CartesianRange($inds) (or other more approriate AbstractUnitRange type) instead.", :CartesianRange) - CartesianRange(inds) + depwarn("the internal representation of CartesianIndices has changed, use CartesianIndices($inds) (or other more approriate AbstractUnitRange type) instead.", :CartesianIndices) + CartesianIndices(inds) end # PR #20005 @@ -3263,25 +3263,28 @@ end @deprecate_moved unsafe_get "Nullables" # sub2ind and ind2sub deprecation (PR #24715) -@deprecate ind2sub(A::AbstractArray, ind) CartesianRange(A)[ind] -@deprecate ind2sub(::Tuple{}, ind::Integer) CartesianRange()[ind] -@deprecate ind2sub(dims::Tuple{Vararg{Integer,N}} where N, ind::Integer) CartesianRange(dims)[ind] -@deprecate ind2sub(inds::Tuple{Base.OneTo}, ind::Integer) CartesianRange(inds)[ind] -@deprecate ind2sub(inds::Tuple{AbstractUnitRange}, ind::Integer) CartesianRange(inds)[ind] -@deprecate ind2sub(inds::Tuple{Vararg{AbstractUnitRange,N}} where N, ind::Integer) CartesianRange(inds)[ind] -@deprecate ind2sub(inds::Union{DimsInteger{N},Indices{N}} where N, ind::AbstractVector{<:Integer}) CartesianRange(inds)[ind] - -@deprecate sub2ind(A::AbstractArray, I...) CartesianToLinear(A)[I...] -@deprecate sub2ind(dims::Tuple{}) CartesianToLinear(dims)[] -@deprecate sub2ind(dims::DimsInteger) CartesianToLinear(dims)[] -@deprecate sub2ind(dims::Indices) CartesianToLinear(dims)[] -@deprecate sub2ind(dims::Tuple{}, I::Integer...) CartesianToLinear(dims)[I...] -@deprecate sub2ind(dims::DimsInteger, I::Integer...) CartesianToLinear(dims)[I...] -@deprecate sub2ind(inds::Indices, I::Integer...) CartesianToLinear(inds)[I...] -@deprecate sub2ind(inds::Tuple{OneTo}, I::Integer...) CartesianToLinear(inds)[I...] -@deprecate sub2ind(inds::Tuple{OneTo}, i::Integer) CartesianToLinear(inds)[i] -@deprecate sub2ind(inds::Tuple{OneTo}, I1::AbstractVector{T}, I::AbstractVector{T}...) where {T<:Integer} CartesianToLinear(inds)[CartesianIndex.(I1, I...)] -@deprecate sub2ind(inds::Union{DimsInteger,Indices}, I1::AbstractVector{T}, I::AbstractVector{T}...) where {T<:Integer} CartesianToLinear(inds)[CartesianIndex.(I1, I...)] +@deprecate ind2sub(A::AbstractArray, ind) CartesianIndices(A)[ind] +@deprecate ind2sub(::Tuple{}, ind::Integer) CartesianIndices()[ind] +@deprecate ind2sub(dims::Tuple{Vararg{Integer,N}} where N, ind::Integer) CartesianIndices(dims)[ind] +@deprecate ind2sub(inds::Tuple{Base.OneTo}, ind::Integer) CartesianIndices(inds)[ind] +@deprecate ind2sub(inds::Tuple{AbstractUnitRange}, ind::Integer) CartesianIndices(inds)[ind] +@deprecate ind2sub(inds::Tuple{Vararg{AbstractUnitRange,N}} where N, ind::Integer) CartesianIndices(inds)[ind] +@deprecate ind2sub(inds::Union{DimsInteger{N},Indices{N}} where N, ind::AbstractVector{<:Integer}) CartesianIndices(inds)[ind] + +@deprecate sub2ind(A::AbstractArray, I...) LinearIndices(A)[I...] +@deprecate sub2ind(dims::Tuple{}) LinearIndices(dims)[] +@deprecate sub2ind(dims::DimsInteger) LinearIndices(dims)[] +@deprecate sub2ind(dims::Indices) LinearIndices(dims)[] +@deprecate sub2ind(dims::Tuple{}, I::Integer...) LinearIndices(dims)[I...] +@deprecate sub2ind(dims::DimsInteger, I::Integer...) LinearIndices(dims)[I...] +@deprecate sub2ind(inds::Indices, I::Integer...) LinearIndices(inds)[I...] +@deprecate sub2ind(inds::Tuple{OneTo}, I::Integer...) LinearIndices(inds)[I...] +@deprecate sub2ind(inds::Tuple{OneTo}, i::Integer) LinearIndices(inds)[i] +@deprecate sub2ind(inds::Tuple{OneTo}, I1::AbstractVector{T}, I::AbstractVector{T}...) where {T<:Integer} LinearIndices(inds)[CartesianIndex.(I1, I...)] +@deprecate sub2ind(inds::Union{DimsInteger,Indices}, I1::AbstractVector{T}, I::AbstractVector{T}...) where {T<:Integer} LinearIndices(inds)[CartesianIndex.(I1, I...)] + +# PR #25113 +@deprecate_binding CartesianRange CartesianIndices # END 0.7 deprecations diff --git a/base/exports.jl b/base/exports.jl index 0eb27e0866f27..8d3f77f7961c4 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -38,8 +38,8 @@ export BitVector, BufferStream, CartesianIndex, - CartesianRange, - CartesianToLinear, + CartesianIndices, + LinearIndices, Channel, Cmd, Colon, diff --git a/base/io.jl b/base/io.jl index 961975cce4bf2..6f3424c7f0bf2 100644 --- a/base/io.jl +++ b/base/io.jl @@ -535,14 +535,14 @@ function write(s::IO, a::SubArray{T,N,<:Array}) where {T,N} elsz = sizeof(T) colsz = size(a,1) * elsz @gc_preserve a if stride(a,1) != 1 - for idxs in CartesianRange(size(a)) + for idxs in CartesianIndices(size(a)) unsafe_write(s, pointer(a, idxs.I), elsz) end return elsz * length(a) elseif N <= 1 return unsafe_write(s, pointer(a, 1), colsz) else - for idxs in CartesianRange((1, size(a)[2:end]...)) + for idxs in CartesianIndices((1, size(a)[2:end]...)) unsafe_write(s, pointer(a, idxs.I), colsz) end return colsz * trailingsize(a,2) diff --git a/base/iterators.jl b/base/iterators.jl index ce2517bb29c0f..2daaa49ae497e 100644 --- a/base/iterators.jl +++ b/base/iterators.jl @@ -191,7 +191,7 @@ CartesianIndex(2, 2) e See also: [`IndexStyle`](@ref), [`axes`](@ref). """ pairs(::IndexLinear, A::AbstractArray) = IndexValue(A, linearindices(A)) -pairs(::IndexCartesian, A::AbstractArray) = IndexValue(A, CartesianRange(axes(A))) +pairs(::IndexCartesian, A::AbstractArray) = IndexValue(A, CartesianIndices(axes(A))) # faster than zip(keys(a), values(a)) for arrays pairs(A::AbstractArray) = pairs(IndexCartesian(), A) diff --git a/base/multidimensional.jl b/base/multidimensional.jl index 3b008a6699b75..3417e9fa68515 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -11,7 +11,7 @@ module IteratorsMD using Base: IndexLinear, IndexCartesian, AbstractCartesianIndex, fill_to_length, tail using Base.Iterators: Reverse - export CartesianIndex, CartesianRange, CartesianToLinear + export CartesianIndex, CartesianIndices, LinearIndices """ CartesianIndex(i, j, k...) -> I @@ -27,7 +27,7 @@ module IteratorsMD dimensionality. A `CartesianIndex` is sometimes produced by [`eachindex`](@ref), and - always when iterating with an explicit [`CartesianRange`](@ref). + always when iterating with an explicit [`CartesianIndices`](@ref). # Examples ```jldoctest @@ -144,7 +144,7 @@ module IteratorsMD # nextind with CartesianIndex function Base.nextind(a::AbstractArray{<:Any,N}, i::CartesianIndex{N}) where {N} - _, ni = next(CartesianRange(axes(a)), i) + _, ni = next(CartesianIndices(axes(a)), i) return ni end @@ -155,8 +155,8 @@ module IteratorsMD # Iteration """ - CartesianRange(sz::Dims) -> R - CartesianRange(istart:istop, jstart:jstop, ...) -> R + CartesianIndices(sz::Dims) -> R + CartesianIndices(istart:istop, jstart:jstop, ...) -> R Define a region `R` spanning a multidimensional rectangular range of integer indices. These are most commonly encountered in the @@ -172,14 +172,14 @@ module IteratorsMD Consequently these can be useful for writing algorithms that work in arbitrary dimensions. - CartesianRange(A::AbstractArray) -> R + CartesianIndices(A::AbstractArray) -> R - As a convenience, constructing a CartesianRange from an array makes a + As a convenience, constructing a CartesianIndices from an array makes a range of its indices. # Examples ```jldoctest - julia> foreach(println, CartesianRange((2, 2, 2))) + julia> foreach(println, CartesianIndices((2, 2, 2))) CartesianIndex(1, 1, 1) CartesianIndex(2, 1, 1) CartesianIndex(1, 2, 1) @@ -189,8 +189,8 @@ module IteratorsMD CartesianIndex(1, 2, 2) CartesianIndex(2, 2, 2) - julia> CartesianRange(ones(2,3)) - 2×3 CartesianRange{2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}: + julia> CartesianIndices(ones(2,3)) + 2×3 CartesianIndices{2,Tuple{Base.OneTo{Int64},Base.OneTo{Int64}}}: CartesianIndex(1, 1) CartesianIndex(1, 2) CartesianIndex(1, 3) CartesianIndex(2, 1) CartesianIndex(2, 2) CartesianIndex(2, 3) ``` @@ -198,11 +198,11 @@ module IteratorsMD ## Conversion between linear and cartesian indices Linear index to cartesian index conversion exploits the fact that a - `CartesianRange` is an `AbstractArray` and can be indexed linearly: + `CartesianIndices` is an `AbstractArray` and can be indexed linearly: ```jldoctest subarray - julia> cartesian = CartesianRange(1:3,1:2) - 3×2 CartesianRange{2,Tuple{UnitRange{Int64},UnitRange{Int64}}}: + julia> cartesian = CartesianIndices(1:3,1:2) + 3×2 CartesianIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}}: CartesianIndex(1, 1) CartesianIndex(1, 2) CartesianIndex(2, 1) CartesianIndex(2, 2) CartesianIndex(3, 1) CartesianIndex(3, 2) @@ -211,76 +211,76 @@ module IteratorsMD CartesianIndex(1, 2) ``` - For cartesian to linear index conversion, see [`CartesianToLinear`](@ref). + For cartesian to linear index conversion, see [`LinearIndices`](@ref). """ - struct CartesianRange{N,R<:NTuple{N,AbstractUnitRange{Int}}} <: AbstractArray{CartesianIndex{N},N} + struct CartesianIndices{N,R<:NTuple{N,AbstractUnitRange{Int}}} <: AbstractArray{CartesianIndex{N},N} indices::R end - CartesianRange(::Tuple{}) = CartesianRange{0,typeof(())}(()) - CartesianRange(inds::NTuple{N,AbstractUnitRange{Int}}) where {N} = - CartesianRange{N,typeof(inds)}(inds) - CartesianRange(inds::Vararg{AbstractUnitRange{Int},N}) where {N} = - CartesianRange(inds) - CartesianRange(inds::NTuple{N,AbstractUnitRange{<:Integer}}) where {N} = - CartesianRange(map(r->convert(AbstractUnitRange{Int}, r), inds)) - CartesianRange(inds::Vararg{AbstractUnitRange{<:Integer},N}) where {N} = - CartesianRange(inds) - - CartesianRange(index::CartesianIndex) = CartesianRange(index.I) - CartesianRange(sz::NTuple{N,<:Integer}) where {N} = CartesianRange(map(Base.OneTo, sz)) - CartesianRange(inds::NTuple{N,Union{<:Integer,AbstractUnitRange{<:Integer}}}) where {N} = - CartesianRange(map(i->first(i):last(i), inds)) - - CartesianRange(A::AbstractArray) = CartesianRange(axes(A)) - - convert(::Type{Tuple{}}, R::CartesianRange{0}) = () - convert(::Type{NTuple{N,AbstractUnitRange{Int}}}, R::CartesianRange{N}) where {N} = + CartesianIndices(::Tuple{}) = CartesianIndices{0,typeof(())}(()) + CartesianIndices(inds::NTuple{N,AbstractUnitRange{Int}}) where {N} = + CartesianIndices{N,typeof(inds)}(inds) + CartesianIndices(inds::Vararg{AbstractUnitRange{Int},N}) where {N} = + CartesianIndices(inds) + CartesianIndices(inds::NTuple{N,AbstractUnitRange{<:Integer}}) where {N} = + CartesianIndices(map(r->convert(AbstractUnitRange{Int}, r), inds)) + CartesianIndices(inds::Vararg{AbstractUnitRange{<:Integer},N}) where {N} = + CartesianIndices(inds) + + CartesianIndices(index::CartesianIndex) = CartesianIndices(index.I) + CartesianIndices(sz::NTuple{N,<:Integer}) where {N} = CartesianIndices(map(Base.OneTo, sz)) + CartesianIndices(inds::NTuple{N,Union{<:Integer,AbstractUnitRange{<:Integer}}}) where {N} = + CartesianIndices(map(i->first(i):last(i), inds)) + + CartesianIndices(A::AbstractArray) = CartesianIndices(axes(A)) + + convert(::Type{Tuple{}}, R::CartesianIndices{0}) = () + convert(::Type{NTuple{N,AbstractUnitRange{Int}}}, R::CartesianIndices{N}) where {N} = R.indices - convert(::Type{NTuple{N,AbstractUnitRange}}, R::CartesianRange{N}) where {N} = + convert(::Type{NTuple{N,AbstractUnitRange}}, R::CartesianIndices{N}) where {N} = convert(NTuple{N,AbstractUnitRange{Int}}, R) - convert(::Type{NTuple{N,UnitRange{Int}}}, R::CartesianRange{N}) where {N} = + convert(::Type{NTuple{N,UnitRange{Int}}}, R::CartesianIndices{N}) where {N} = UnitRange{Int}.(convert(NTuple{N,AbstractUnitRange}, R)) - convert(::Type{NTuple{N,UnitRange}}, R::CartesianRange{N}) where {N} = + convert(::Type{NTuple{N,UnitRange}}, R::CartesianIndices{N}) where {N} = UnitRange.(convert(NTuple{N,AbstractUnitRange}, R)) - convert(::Type{Tuple{Vararg{AbstractUnitRange{Int}}}}, R::CartesianRange{N}) where {N} = + convert(::Type{Tuple{Vararg{AbstractUnitRange{Int}}}}, R::CartesianIndices{N}) where {N} = convert(NTuple{N,AbstractUnitRange{Int}}, R) - convert(::Type{Tuple{Vararg{AbstractUnitRange}}}, R::CartesianRange) = + convert(::Type{Tuple{Vararg{AbstractUnitRange}}}, R::CartesianIndices) = convert(Tuple{Vararg{AbstractUnitRange{Int}}}, R) - convert(::Type{Tuple{Vararg{UnitRange{Int}}}}, R::CartesianRange{N}) where {N} = + convert(::Type{Tuple{Vararg{UnitRange{Int}}}}, R::CartesianIndices{N}) where {N} = convert(NTuple{N,UnitRange{Int}}, R) - convert(::Type{Tuple{Vararg{UnitRange}}}, R::CartesianRange) = + convert(::Type{Tuple{Vararg{UnitRange}}}, R::CartesianIndices) = convert(Tuple{Vararg{UnitRange{Int}}}, R) # AbstractArray implementation - Base.IndexStyle(::Type{CartesianRange{N,R}}) where {N,R} = IndexCartesian() - @inline Base.getindex(iter::CartesianRange{N,R}, I::Vararg{Int, N}) where {N,R} = CartesianIndex(first.(iter.indices) .- 1 .+ I) + Base.IndexStyle(::Type{CartesianIndices{N,R}}) where {N,R} = IndexCartesian() + @inline Base.getindex(iter::CartesianIndices{N,R}, I::Vararg{Int, N}) where {N,R} = CartesianIndex(first.(iter.indices) .- 1 .+ I) - ndims(R::CartesianRange) = ndims(typeof(R)) - ndims(::Type{CartesianRange{N}}) where {N} = N - ndims(::Type{CartesianRange{N,TT}}) where {N,TT} = N + ndims(R::CartesianIndices) = ndims(typeof(R)) + ndims(::Type{CartesianIndices{N}}) where {N} = N + ndims(::Type{CartesianIndices{N,TT}}) where {N,TT} = N - eachindex(::IndexCartesian, A::AbstractArray) = CartesianRange(axes(A)) + eachindex(::IndexCartesian, A::AbstractArray) = CartesianIndices(axes(A)) @inline function eachindex(::IndexCartesian, A::AbstractArray, B::AbstractArray...) axsA = axes(A) all(x->axes(x) == axsA, B) || Base.throw_eachindex_mismatch(IndexCartesian(), A, B...) - CartesianRange(axsA) + CartesianIndices(axsA) end - eltype(R::CartesianRange) = eltype(typeof(R)) - eltype(::Type{CartesianRange{N}}) where {N} = CartesianIndex{N} - eltype(::Type{CartesianRange{N,TT}}) where {N,TT} = CartesianIndex{N} - iteratorsize(::Type{<:CartesianRange}) = Base.HasShape() + eltype(R::CartesianIndices) = eltype(typeof(R)) + eltype(::Type{CartesianIndices{N}}) where {N} = CartesianIndex{N} + eltype(::Type{CartesianIndices{N,TT}}) where {N,TT} = CartesianIndex{N} + iteratorsize(::Type{<:CartesianIndices}) = Base.HasShape() - @inline function start(iter::CartesianRange) + @inline function start(iter::CartesianIndices) iterfirst, iterlast = first(iter), last(iter) if any(map(>, iterfirst.I, iterlast.I)) return iterlast+1 end iterfirst end - @inline function next(iter::CartesianRange, state) + @inline function next(iter::CartesianIndices, state) state, CartesianIndex(inc(state.I, first(iter).I, last(iter).I)) end # increment & carry @@ -293,37 +293,37 @@ module IteratorsMD newtail = inc(tail(state), tail(start), tail(stop)) (start[1], newtail...) end - @inline done(iter::CartesianRange, state) = state.I[end] > last(iter.indices[end]) + @inline done(iter::CartesianIndices, state) = state.I[end] > last(iter.indices[end]) # 0-d cartesian ranges are special-cased to iterate once and only once - start(iter::CartesianRange{0}) = false - next(iter::CartesianRange{0}, state) = CartesianIndex(), true - done(iter::CartesianRange{0}, state) = state + start(iter::CartesianIndices{0}) = false + next(iter::CartesianIndices{0}, state) = CartesianIndex(), true + done(iter::CartesianIndices{0}, state) = state - size(iter::CartesianRange) = map(dimlength, first(iter).I, last(iter).I) + size(iter::CartesianIndices) = map(dimlength, first(iter).I, last(iter).I) dimlength(start, stop) = stop-start+1 - length(iter::CartesianRange) = prod(size(iter)) + length(iter::CartesianIndices) = prod(size(iter)) - first(iter::CartesianRange) = CartesianIndex(map(first, iter.indices)) - last(iter::CartesianRange) = CartesianIndex(map(last, iter.indices)) + first(iter::CartesianIndices) = CartesianIndex(map(first, iter.indices)) + last(iter::CartesianIndices) = CartesianIndex(map(last, iter.indices)) - @inline function in(i::CartesianIndex{N}, r::CartesianRange{N}) where {N} + @inline function in(i::CartesianIndex{N}, r::CartesianIndices{N}) where {N} _in(true, i.I, first(r).I, last(r).I) end _in(b, ::Tuple{}, ::Tuple{}, ::Tuple{}) = b @inline _in(b, i, start, stop) = _in(b & (start[1] <= i[1] <= stop[1]), tail(i), tail(start), tail(stop)) - simd_outer_range(iter::CartesianRange{0}) = iter - function simd_outer_range(iter::CartesianRange) - CartesianRange(tail(iter.indices)) + simd_outer_range(iter::CartesianIndices{0}) = iter + function simd_outer_range(iter::CartesianIndices) + CartesianIndices(tail(iter.indices)) end - simd_inner_length(iter::CartesianRange{0}, ::CartesianIndex) = 1 - simd_inner_length(iter::CartesianRange, I::CartesianIndex) = length(iter.indices[1]) + simd_inner_length(iter::CartesianIndices{0}, ::CartesianIndex) = 1 + simd_inner_length(iter::CartesianIndices, I::CartesianIndex) = length(iter.indices[1]) - simd_index(iter::CartesianRange{0}, ::CartesianIndex, I1::Int) = first(iter) - @inline function simd_index(iter::CartesianRange, Ilast::CartesianIndex, I1::Int) + simd_index(iter::CartesianIndices{0}, ::CartesianIndex, I1::Int) = first(iter) + @inline function simd_index(iter::CartesianIndices, Ilast::CartesianIndex, I1::Int) CartesianIndex((I1+first(iter.indices[1]), Ilast.I...)) end @@ -346,20 +346,20 @@ module IteratorsMD i, j = split(I.I, V) CartesianIndex(i), CartesianIndex(j) end - function split(R::CartesianRange, V::Val) + function split(R::CartesianIndices, V::Val) i, j = split(R.indices, V) - CartesianRange(i), CartesianRange(j) + CartesianIndices(i), CartesianIndices(j) end - # reversed CartesianRange iteration - @inline function start(r::Reverse{<:CartesianRange}) + # reversed CartesianIndices iteration + @inline function start(r::Reverse{<:CartesianIndices}) iterfirst, iterlast = last(r.itr), first(r.itr) if any(map(<, iterfirst.I, iterlast.I)) return iterlast-1 end iterfirst end - @inline function next(r::Reverse{<:CartesianRange}, state) + @inline function next(r::Reverse{<:CartesianIndices}, state) state, CartesianIndex(dec(state.I, last(r.itr).I, first(r.itr).I)) end # decrement & carry @@ -372,26 +372,26 @@ module IteratorsMD newtail = dec(tail(state), tail(start), tail(stop)) (start[1], newtail...) end - @inline done(r::Reverse{<:CartesianRange}, state) = state.I[end] < first(r.itr.indices[end]) + @inline done(r::Reverse{<:CartesianIndices}, state) = state.I[end] < first(r.itr.indices[end]) # 0-d cartesian ranges are special-cased to iterate once and only once - start(iter::Reverse{<:CartesianRange{0}}) = false - next(iter::Reverse{<:CartesianRange{0}}, state) = CartesianIndex(), true - done(iter::Reverse{<:CartesianRange{0}}, state) = state + start(iter::Reverse{<:CartesianIndices{0}}) = false + next(iter::Reverse{<:CartesianIndices{0}}, state) = CartesianIndex(), true + done(iter::Reverse{<:CartesianIndices{0}}, state) = state """ - CartesianToLinear(inds::CartesianRange) -> R - CartesianToLinear(sz::Dims) -> R - CartesianToLinear(istart:istop, jstart:jstop, ...) -> R + LinearIndices(inds::CartesianIndices) -> R + LinearIndices(sz::Dims) -> R + LinearIndices(istart:istop, jstart:jstop, ...) -> R - Define a mapping between cartesian indices and the corresponding linear index into a CartesianRange + Define a mapping between cartesian indices and the corresponding linear index into a CartesianIndices # Example The main purpose of this type is intuitive conversion from cartesian to linear indexing: ```jldoctest subarray - julia> linear = CartesianToLinear(1:3,1:2) - CartesianToLinear{2,Tuple{UnitRange{Int64},UnitRange{Int64}}} with indices 1:3×1:2: + julia> linear = LinearIndices(1:3,1:2) + LinearIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}} with indices 1:3×1:2: 1 4 2 5 3 6 @@ -400,25 +400,25 @@ module IteratorsMD 4 ``` """ - struct CartesianToLinear{N,R<:NTuple{N,AbstractUnitRange{Int}}} <: AbstractArray{Int,N} + struct LinearIndices{N,R<:NTuple{N,AbstractUnitRange{Int}}} <: AbstractArray{Int,N} indices::R end - CartesianToLinear(inds::CartesianRange{N,R}) where {N,R} = CartesianToLinear{N,R}(inds.indices) - CartesianToLinear(::Tuple{}) = CartesianToLinear(CartesianRange(())) - CartesianToLinear(inds::NTuple{N,AbstractUnitRange{Int}}) where {N} = CartesianToLinear(CartesianRange(inds)) - CartesianToLinear(inds::Vararg{AbstractUnitRange{Int},N}) where {N} = CartesianToLinear(CartesianRange(inds)) - CartesianToLinear(inds::NTuple{N,AbstractUnitRange{<:Integer}}) where {N} = CartesianToLinear(CartesianRange(inds)) - CartesianToLinear(inds::Vararg{AbstractUnitRange{<:Integer},N}) where {N} = CartesianToLinear(CartesianRange(inds)) - CartesianToLinear(index::CartesianIndex) = CartesianToLinear(CartesianRange(index)) - CartesianToLinear(sz::NTuple{N,<:Integer}) where {N} = CartesianToLinear(CartesianRange(sz)) - CartesianToLinear(inds::NTuple{N,Union{<:Integer,AbstractUnitRange{<:Integer}}}) where {N} = CartesianToLinear(CartesianRange(inds)) - CartesianToLinear(A::AbstractArray) = CartesianToLinear(CartesianRange(A)) + LinearIndices(inds::CartesianIndices{N,R}) where {N,R} = LinearIndices{N,R}(inds.indices) + LinearIndices(::Tuple{}) = LinearIndices(CartesianIndices(())) + LinearIndices(inds::NTuple{N,AbstractUnitRange{Int}}) where {N} = LinearIndices(CartesianIndices(inds)) + LinearIndices(inds::Vararg{AbstractUnitRange{Int},N}) where {N} = LinearIndices(CartesianIndices(inds)) + LinearIndices(inds::NTuple{N,AbstractUnitRange{<:Integer}}) where {N} = LinearIndices(CartesianIndices(inds)) + LinearIndices(inds::Vararg{AbstractUnitRange{<:Integer},N}) where {N} = LinearIndices(CartesianIndices(inds)) + LinearIndices(index::CartesianIndex) = LinearIndices(CartesianIndices(index)) + LinearIndices(sz::NTuple{N,<:Integer}) where {N} = LinearIndices(CartesianIndices(sz)) + LinearIndices(inds::NTuple{N,Union{<:Integer,AbstractUnitRange{<:Integer}}}) where {N} = LinearIndices(CartesianIndices(inds)) + LinearIndices(A::AbstractArray) = LinearIndices(CartesianIndices(A)) # AbstractArray implementation - Base.IndexStyle(::Type{CartesianToLinear{N,R}}) where {N,R} = IndexCartesian() - Base.axes(iter::CartesianToLinear{N,R}) where {N,R} = iter.indices - @inline function Base.getindex(iter::CartesianToLinear{N,R}, I::Vararg{Int, N}) where {N,R} + Base.IndexStyle(::Type{LinearIndices{N,R}}) where {N,R} = IndexCartesian() + Base.axes(iter::LinearIndices{N,R}) where {N,R} = iter.indices + @inline function Base.getindex(iter::LinearIndices{N,R}, I::Vararg{Int, N}) where {N,R} dims = length.(iter.indices) #without the inbounds, this is slower than Base._sub2ind(iter.indices, I...) @inbounds result = reshape(1:prod(dims), dims)[(I .- first.(iter.indices) .+ 1)...] @@ -544,7 +544,7 @@ show(io::IO, r::LogicalIndex) = print(io, "Base.LogicalIndex(", r.mask, ")") return (r, start(r), 1) end @inline function start(L::LogicalIndex{<:CartesianIndex}) - r = CartesianRange(axes(L.mask)) + r = CartesianIndices(axes(L.mask)) return (r, start(r), 1) end @propagate_inbounds function next(L::LogicalIndex, s) @@ -967,7 +967,7 @@ function accumulate!(op, B, A, dim::Integer) # We can accumulate to a temporary variable, which allows # register usage and will be slightly faster ind1 = inds_t[1] - @inbounds for I in CartesianRange(tail(inds_t)) + @inbounds for I in CartesianIndices(tail(inds_t)) tmp = convert(eltype(B), A[first(ind1), I]) B[first(ind1), I] = tmp for i_1 = first(ind1)+1:last(ind1) @@ -976,8 +976,8 @@ function accumulate!(op, B, A, dim::Integer) end end else - R1 = CartesianRange(axes(A)[1:dim-1]) # not type-stable - R2 = CartesianRange(axes(A)[dim+1:end]) + R1 = CartesianIndices(axes(A)[1:dim-1]) # not type-stable + R2 = CartesianIndices(axes(A)[dim+1:end]) _accumulate!(op, B, A, R1, inds_t[dim], R2) # use function barrier end return B @@ -1113,8 +1113,8 @@ function copy!(dest::AbstractArray{T,N}, src::AbstractArray{T,N}) where {T,N} dest end -function copy!(dest::AbstractArray{T1,N}, Rdest::CartesianRange{N}, - src::AbstractArray{T2,N}, Rsrc::CartesianRange{N}) where {T1,T2,N} +function copy!(dest::AbstractArray{T1,N}, Rdest::CartesianIndices{N}, + src::AbstractArray{T2,N}, Rsrc::CartesianIndices{N}) where {T1,T2,N} isempty(Rdest) && return dest if size(Rdest) != size(Rsrc) throw(ArgumentError("source and destination must have same size (got $(size(Rsrc)) and $(size(Rdest)))")) @@ -1139,12 +1139,12 @@ function copy!(dest::AbstractArray{T1,N}, Rdest::CartesianRange{N}, end """ - copy!(dest, Rdest::CartesianRange, src, Rsrc::CartesianRange) -> dest + copy!(dest, Rdest::CartesianIndices, src, Rsrc::CartesianIndices) -> dest Copy the block of `src` in the range of `Rsrc` to the block of `dest` in the range of `Rdest`. The sizes of the two regions must match. """ -copy!(::AbstractArray, ::CartesianRange, ::AbstractArray, ::CartesianRange) +copy!(::AbstractArray, ::CartesianIndices, ::AbstractArray, ::CartesianIndices) # circshift! circshift!(dest::AbstractArray, src, ::Tuple{}) = copy!(dest, src) @@ -1197,7 +1197,7 @@ circshift!(dest::AbstractArray, src, shiftamt) = circshift!(dest, src, (shiftamt end # At least one of inds, shiftamt is empty function _circshift!(dest, rdest, src, rsrc, inds, shiftamt) - copy!(dest, CartesianRange(rdest), src, CartesianRange(rsrc)) + copy!(dest, CartesianIndices(rdest), src, CartesianIndices(rsrc)) end # circcopy! @@ -1260,7 +1260,7 @@ end # At least one of indsdest, indssrc are empty (and both should be, since we've checked) function _circcopy!(dest, rdest, indsdest, src, rsrc, indssrc) - copy!(dest, CartesianRange(rdest), src, CartesianRange(rsrc)) + copy!(dest, CartesianIndices(rdest), src, CartesianIndices(rsrc)) end ### BitArrays @@ -1751,12 +1751,12 @@ end @noinline function extrema!(B, A) sA = size(A) sB = size(B) - for I in CartesianRange(sB) + for I in CartesianIndices(sB) AI = A[I] B[I] = (AI, AI) end Bmax = CartesianIndex(sB) - @inbounds @simd for I in CartesianRange(sA) + @inbounds @simd for I in CartesianIndices(sA) J = min(Bmax,I) BJ = B[J] AI = A[I] diff --git a/base/permuteddimsarray.jl b/base/permuteddimsarray.jl index 20e5fba8579ae..211d6155f7ae4 100644 --- a/base/permuteddimsarray.jl +++ b/base/permuteddimsarray.jl @@ -162,16 +162,16 @@ function _copy!(P::PermutedDimsArray{T,N,perm}, src) where {T,N,perm} if d == ndims(src) copy!(parent(P), src) # it's not permuted else - R1 = CartesianRange(axes(src)[1:d]) + R1 = CartesianIndices(axes(src)[1:d]) d1 = findfirst(equalto(d+1), perm) # first permuted dim of dest - R2 = CartesianRange(axes(src)[d+2:d1-1]) - R3 = CartesianRange(axes(src)[d1+1:end]) + R2 = CartesianIndices(axes(src)[d+2:d1-1]) + R3 = CartesianIndices(axes(src)[d1+1:end]) _permutedims!(P, src, R1, R2, R3, d+1, d1) end return P end -@noinline function _permutedims!(P::PermutedDimsArray, src, R1::CartesianRange{0}, R2, R3, ds, dp) +@noinline function _permutedims!(P::PermutedDimsArray, src, R1::CartesianIndices{0}, R2, R3, ds, dp) ip, is = axes(src, dp), axes(src, ds) for jo in first(ip):8:last(ip), io in first(is):8:last(is) for I3 in R3, I2 in R2 diff --git a/base/reducedim.jl b/base/reducedim.jl index 7c5bcb1e3c45a..28ea3895b40d1 100644 --- a/base/reducedim.jl +++ b/base/reducedim.jl @@ -228,7 +228,7 @@ function _mapreducedim!(f, op, R::AbstractArray, A::AbstractArray) if reducedim1(R, A) # keep the accumulator as a local variable when reducing along the first dimension i1 = first(indices1(R)) - @inbounds for IA in CartesianRange(indsAt) + @inbounds for IA in CartesianIndices(indsAt) IR = Broadcast.newindex(IA, keep, Idefault) r = R[i1,IR] @simd for i in axes(A, 1) @@ -237,7 +237,7 @@ function _mapreducedim!(f, op, R::AbstractArray, A::AbstractArray) R[i1,IR] = r end else - @inbounds for IA in CartesianRange(indsAt) + @inbounds for IA in CartesianIndices(indsAt) IR = Broadcast.newindex(IA, keep, Idefault) @simd for i in axes(A, 1) R[i,IR] = op(R[i,IR], f(A[i,IA])) @@ -652,7 +652,7 @@ function findminmax!(f, Rval, Rind, A::AbstractArray{T,N}) where {T,N} zi = zero(eltype(ks)) if reducedim1(Rval, A) i1 = first(indices1(Rval)) - @inbounds for IA in CartesianRange(indsAt) + @inbounds for IA in CartesianIndices(indsAt) IR = Broadcast.newindex(IA, keep, Idefault) tmpRv = Rval[i1,IR] tmpRi = Rind[i1,IR] @@ -668,7 +668,7 @@ function findminmax!(f, Rval, Rind, A::AbstractArray{T,N}) where {T,N} Rind[i1,IR] = tmpRi end else - @inbounds for IA in CartesianRange(indsAt) + @inbounds for IA in CartesianIndices(indsAt) IR = Broadcast.newindex(IA, keep, Idefault) for i in axes(A, 1) tmpAv = A[i,IA] diff --git a/base/statistics.jl b/base/statistics.jl index 593f7c3743053..85f5026a18b10 100644 --- a/base/statistics.jl +++ b/base/statistics.jl @@ -144,7 +144,7 @@ function centralize_sumabs2!(R::AbstractArray{S}, A::AbstractArray, means::Abstr keep, Idefault = Broadcast.shapeindexer(indsAt, indsRt) if reducedim1(R, A) i1 = first(indices1(R)) - @inbounds for IA in CartesianRange(indsAt) + @inbounds for IA in CartesianIndices(indsAt) IR = Broadcast.newindex(IA, keep, Idefault) r = R[i1,IR] m = means[i1,IR] @@ -154,7 +154,7 @@ function centralize_sumabs2!(R::AbstractArray{S}, A::AbstractArray, means::Abstr R[i1,IR] = r end else - @inbounds for IA in CartesianRange(indsAt) + @inbounds for IA in CartesianIndices(indsAt) IR = Broadcast.newindex(IA, keep, Idefault) @simd for i in axes(A, 1) R[i,IR] += abs2(A[i,IA] - means[i,IR]) diff --git a/test/TestHelpers.jl b/test/TestHelpers.jl index ef6d5610bc200..f10a10bf1dfaf 100644 --- a/test/TestHelpers.jl +++ b/test/TestHelpers.jl @@ -162,7 +162,7 @@ Base.parent(A::OffsetArray) = A.parent errmsg(A) = error("size not supported for arrays with indices $(axes(A)); see https://docs.julialang.org/en/latest/devdocs/offset-arrays/") Base.size(A::OffsetArray) = errmsg(A) Base.size(A::OffsetArray, d) = errmsg(A) -Base.eachindex(::IndexCartesian, A::OffsetArray) = CartesianRange(axes(A)) +Base.eachindex(::IndexCartesian, A::OffsetArray) = CartesianIndices(axes(A)) Base.eachindex(::IndexLinear, A::OffsetVector) = axes(A, 1) # Implementations of indices and indices1. Since bounds-checking is diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 2a5bf5f6d69d1..baf3d40e67ae4 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -114,20 +114,20 @@ end @testset "index conversion" begin @testset "0-dimensional" begin - @test CartesianToLinear()[1] == 1 - @test_throws BoundsError CartesianToLinear()[2] - @test CartesianToLinear()[1,1] == 1 - @test CartesianRange()[1] == CartesianIndex() - @test_throws BoundsError CartesianRange()[2] + @test LinearIndices()[1] == 1 + @test_throws BoundsError LinearIndices()[2] + @test LinearIndices()[1,1] == 1 + @test CartesianIndices()[1] == CartesianIndex() + @test_throws BoundsError CartesianIndices()[2] end @testset "1-dimensional" begin for i = 1:3 - @test CartesianToLinear((3,))[i] == i - @test CartesianRange((3,))[i] == CartesianIndex(i,) + @test LinearIndices((3,))[i] == i + @test CartesianIndices((3,))[i] == CartesianIndex(i,) end - @test CartesianToLinear((3,))[2,1] == 2 - @test_throws BoundsError CartesianRange((3,))[2,2] + @test LinearIndices((3,))[2,1] == 2 + @test_throws BoundsError CartesianIndices((3,))[2,2] # ambiguity btw cartesian indexing and linear indexing in 1d when # indices may be nontraditional @test_throws ArgumentError Base._sub2ind((1:3,), 2) @@ -136,34 +136,34 @@ end @testset "2-dimensional" begin k = 0 - cartesian = CartesianRange((4,3)) - linear = CartesianToLinear(cartesian) + cartesian = CartesianIndices((4,3)) + linear = LinearIndices(cartesian) for j = 1:3, i = 1:4 @test linear[i,j] == (k+=1) @test cartesian[k] == CartesianIndex(i,j) - @test CartesianToLinear(0:3,3:5)[i-1,j+2] == k - @test CartesianRange(0:3,3:5)[k] == CartesianIndex(i-1,j+2) + @test LinearIndices(0:3,3:5)[i-1,j+2] == k + @test CartesianIndices(0:3,3:5)[k] == CartesianIndex(i-1,j+2) end end @testset "3-dimensional" begin l = 0 for k = 1:2, j = 1:3, i = 1:4 - @test CartesianToLinear((4,3,2))[i,j,k] == (l+=1) - @test CartesianRange((4,3,2))[l] == CartesianIndex(i,j,k) - @test CartesianToLinear(1:4,1:3,1:2)[i,j,k] == l - @test CartesianRange(1:4,1:3,1:2)[l] == CartesianIndex(i,j,k) - @test CartesianToLinear(0:3,3:5,-101:-100)[i-1,j+2,k-102] == l - @test CartesianRange(0:3,3:5,-101:-100)[l] == CartesianIndex(i-1, j+2, k-102) + @test LinearIndices((4,3,2))[i,j,k] == (l+=1) + @test CartesianIndices((4,3,2))[l] == CartesianIndex(i,j,k) + @test LinearIndices(1:4,1:3,1:2)[i,j,k] == l + @test CartesianIndices(1:4,1:3,1:2)[l] == CartesianIndex(i,j,k) + @test LinearIndices(0:3,3:5,-101:-100)[i-1,j+2,k-102] == l + @test CartesianIndices(0:3,3:5,-101:-100)[l] == CartesianIndex(i-1, j+2, k-102) end local A = reshape(collect(1:9), (3,3)) - @test CartesianRange(size(A))[6] == CartesianIndex(3,2) - @test CartesianToLinear(size(A))[3, 2] == 6 - @test CartesianRange(A)[6] == CartesianIndex(3,2) - @test CartesianToLinear(A)[3, 2] == 6 + @test CartesianIndices(size(A))[6] == CartesianIndex(3,2) + @test LinearIndices(size(A))[3, 2] == 6 + @test CartesianIndices(A)[6] == CartesianIndex(3,2) + @test LinearIndices(A)[3, 2] == 6 for i in 1:length(A) - @test CartesianToLinear(A)[CartesianRange(A)[i]] == i + @test LinearIndices(A)[CartesianIndices(A)[i]] == i end @testset "PR #9256" begin @@ -222,7 +222,7 @@ Base.convert(::Type{TSlow }, X::AbstractArray{T,N}) where {T,N } = convert( Base.convert(::Type{TSlow{T }}, X::AbstractArray{_,N}) where {T,N,_} = convert(TSlow{T,N}, X) Base.convert(::Type{TSlow{T,N}}, X::AbstractArray ) where {T,N } = begin A = TSlow(T, size(X)) - for I in CartesianRange(size(X)) + for I in CartesianIndices(size(X)) A[I.I...] = X[I.I...] end A @@ -618,7 +618,7 @@ function test_ind2sub(::Type{TestAbstractArray}) dims = tuple(rand(1:5, n)...) len = prod(dims) A = reshape(collect(1:len), dims...) - I = CartesianRange(dims) + I = CartesianIndices(dims) for i in 1:len @test A[I[i]] == A[i] end @@ -828,10 +828,10 @@ end @test isempty(v3::Vector{Float64}) end -@testset "CartesianRange" begin +@testset "CartesianIndices" begin xrng = 2:4 yrng = 1:5 - CR = CartesianRange((xrng,yrng)) + CR = CartesianIndices((xrng,yrng)) for (i,i_idx) in enumerate(xrng) for (j,j_idx) in enumerate(yrng) @@ -845,6 +845,6 @@ end @test CR[i_lin] == CartesianIndex(xrng[i],yrng[j]) end - @test CartesianRange(ones(2,3)) == CartesianRange((2,3)) - @test CartesianToLinear((2,3)) == [1 3 5; 2 4 6] + @test CartesianIndices(ones(2,3)) == CartesianIndices((2,3)) + @test LinearIndices((2,3)) == [1 3 5; 2 4 6] end diff --git a/test/arrayops.jl b/test/arrayops.jl index 2165bcdef2eb4..6c4a55ce33825 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1211,7 +1211,7 @@ end @testset "eachindexvalue" begin A14 = [11 13; 12 14] - R = CartesianRange(axes(A14)) + R = CartesianIndices(axes(A14)) @test [a for (a,b) in pairs(IndexLinear(), A14)] == [1,2,3,4] @test [a for (a,b) in pairs(IndexCartesian(), A14)] == vec(collect(R)) @test [b for (a,b) in pairs(IndexLinear(), A14)] == [11,12,13,14] @@ -1565,7 +1565,7 @@ end @test !isless(CartesianIndex((1,2)), CartesianIndex((2,1))) a = spzeros(2,3) - @test CartesianRange(size(a)) == eachindex(a) + @test CartesianIndices(size(a)) == eachindex(a) a[CartesianIndex{2}(2,3)] = 5 @test a[2,3] == 5 b = view(a, 1:2, 2:3) @@ -1573,10 +1573,10 @@ end @test a[1,2] == 7 @test 2*CartesianIndex{3}(1,2,3) == CartesianIndex{3}(2,4,6) - R = CartesianRange(2:5, 3:5) + R = CartesianIndices(2:5, 3:5) @test eltype(R) <: CartesianIndex{2} @test eltype(typeof(R)) <: CartesianIndex{2} - @test eltype(CartesianRange{2}) <: CartesianIndex{2} + @test eltype(CartesianIndices{2}) <: CartesianIndex{2} indices = collect(R) @test indices[1] == CartesianIndex{2}(2,3) @test indices[2] == CartesianIndex{2}(3,3) @@ -1598,8 +1598,8 @@ end @test @inferred(convert(NTuple{2,UnitRange}, R)) === (2:5, 3:5) @test @inferred(convert(Tuple{Vararg{UnitRange}}, R)) === (2:5, 3:5) - @test CartesianRange((3:5,-7:7)) == CartesianRange(3:5,-7:7) - @test CartesianRange((3,-7:7)) == CartesianRange(3:3,-7:7) + @test CartesianIndices((3:5,-7:7)) == CartesianIndices(3:5,-7:7) + @test CartesianIndices((3,-7:7)) == CartesianIndices(3:3,-7:7) end # All we really care about is that we have an optimized @@ -1629,19 +1629,19 @@ end @test done(itr, state) end -R = CartesianRange((1,3)) +R = CartesianIndices((1,3)) @test done(R, start(R)) == false -R = CartesianRange((0,3)) +R = CartesianIndices((0,3)) @test done(R, start(R)) == true -R = CartesianRange((3,0)) +R = CartesianIndices((3,0)) @test done(R, start(R)) == true @testset "multi-array eachindex" begin local a = zeros(2,2) local b = view(zeros(3,2), 1:2, :) - @test @inferred(eachindex(Base.IndexCartesian(), a, b)) == CartesianRange((2,2)) + @test @inferred(eachindex(Base.IndexCartesian(), a, b)) == CartesianIndices((2,2)) @test @inferred(eachindex(Base.IndexLinear(), a, b)) == 1:4 - @test @inferred(eachindex(a, b)) == CartesianRange((2,2)) + @test @inferred(eachindex(a, b)) == CartesianIndices((2,2)) @test @inferred(eachindex(a, a)) == 1:4 @test_throws DimensionMismatch eachindex(a, rand(3,3)) @test_throws DimensionMismatch eachindex(b, rand(3,3)) diff --git a/test/copy.jl b/test/copy.jl index 96b6f9a706455..43ac928b6cf7c 100644 --- a/test/copy.jl +++ b/test/copy.jl @@ -49,15 +49,15 @@ chnlprod(x) = Channel(c->for i in x; put!(c,i); end) end end -@testset "with CartesianRange" begin +@testset "with CartesianIndices" begin let A = reshape(1:6, 3, 2), B = similar(A) - RA = CartesianRange(axes(A)) + RA = CartesianIndices(axes(A)) copy!(B, RA, A, RA) @test B == A end let A = reshape(1:6, 3, 2), B = zeros(8,8) - RA = CartesianRange(axes(A)) - copy!(B, CartesianRange((5:7,2:3)), A, RA) + RA = CartesianIndices(axes(A)) + copy!(B, CartesianIndices((5:7,2:3)), A, RA) @test B[5:7,2:3] == A B[5:7,2:3] = 0 @test all(x->x==0, B) diff --git a/test/euler.jl b/test/euler.jl index 7e81d05b635a2..84d29c6de1929 100644 --- a/test/euler.jl +++ b/test/euler.jl @@ -65,7 +65,7 @@ end #11: 70600674 function euler11(grid,n) m = typemin(eltype(grid)) - tolinear = CartesianToLinear(size(grid)) + tolinear = LinearIndices(size(grid)) for i = n:size(grid,1)-n+1, j = n:size(grid,2)-n+1, di = -1:1, dj = -1:1 diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 09943ef91ae14..7aefd7a360aca 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -78,7 +78,7 @@ for i = 1:9 @test A_3_3[i] == i end @test_throws BoundsError A[CartesianIndex(1,1)] @test_throws BoundsError S[CartesianIndex(1,1)] @test eachindex(A) == 1:4 -@test eachindex(S) == CartesianRange((0:1,3:4)) +@test eachindex(S) == CartesianIndices((0:1,3:4)) # logical indexing @test A[A .> 2] == [3,4] diff --git a/test/perf/array/indexing.jl b/test/perf/array/indexing.jl index eda9b1a71541a..d77e2d15669e3 100644 --- a/test/perf/array/indexing.jl +++ b/test/perf/array/indexing.jl @@ -39,7 +39,7 @@ end function sumcartesian(A, n) s = zero(eltype(A)) + zero(eltype(A)) for k = 1:n - for I in CartesianRange(size(A)) + for I in CartesianIndices(size(A)) val = unsafe_getindex(A, I) s += val end diff --git a/test/perf/array/perf.jl b/test/perf/array/perf.jl index 001b69c078b62..e0ed745e294dc 100644 --- a/test/perf/array/perf.jl +++ b/test/perf/array/perf.jl @@ -13,7 +13,7 @@ for Ar in Alist @timeit sumelt(Ar, 10^5) string("sumeltIs ", briefname(Ar)) string("for a in A indexing, ", briefname(Ar)) sz @timeit sumeach(Ar, 10^5) string("sumeachIs ", briefname(Ar)) string("for I in eachindex(A), ", briefname(Ar)) sz @timeit sumlinear(Ar, 10^5) string("sumlinearIs ", briefname(Ar)) string("for I in 1:length(A), ", briefname(Ar)) sz - @timeit sumcartesian(Ar, 10^5) string("sumcartesianIs ", briefname(Ar)) string("for I in CartesianRange(size(A)), ", briefname(Ar)) sz + @timeit sumcartesian(Ar, 10^5) string("sumcartesianIs ", briefname(Ar)) string("for I in CartesianIndices(size(A)), ", briefname(Ar)) sz @timeit sumcolon(Ar, 10^5) string("sumcolonIs ", briefname(Ar)) string("colon indexing, ", briefname(Ar)) sz @timeit sumrange(Ar, 10^5) string("sumrangeIs ", briefname(Ar)) string("range indexing, ", briefname(Ar)) sz @timeit sumlogical(Ar, 10^5) string("sumlogicalIs ", briefname(Ar)) string("logical indexing, ", briefname(Ar)) sz @@ -25,7 +25,7 @@ for Ar in Alist @timeit sumelt(Ar, 10^5) string("sumeltFs ", briefname(Ar)) string("for a in A indexing, ", briefname(Ar)) sz @timeit sumeach(Ar, 10^5) string("sumeachFs ", briefname(Ar)) string("for I in eachindex(A), ", briefname(Ar)) sz @timeit sumlinear(Ar, 10^5) string("sumlinearFs ", briefname(Ar)) string("for I in 1:length(A), ", briefname(Ar)) sz - @timeit sumcartesian(Ar, 10^5) string("sumcartesianFs ", briefname(Ar)) string("for I in CartesianRange(size(A)), ", briefname(Ar)) sz + @timeit sumcartesian(Ar, 10^5) string("sumcartesianFs ", briefname(Ar)) string("for I in CartesianIndices(size(A)), ", briefname(Ar)) sz @timeit sumcolon(Ar, 10^5) string("sumcolonFs ", briefname(Ar)) string("colon indexing, ", briefname(Ar)) sz @timeit sumrange(Ar, 10^5) string("sumrangeFs ", briefname(Ar)) string("range indexing, ", briefname(Ar)) sz @timeit sumlogical(Ar, 10^5) string("sumlogicalFs ", briefname(Ar)) string("logical indexing, ", briefname(Ar)) sz @@ -39,7 +39,7 @@ for Ar in Alist @timeit sumelt(Ar, 100) string("sumeltIb ", briefname(Ar)) string("for a in A indexing, ", briefname(Ar)) sz @timeit sumeach(Ar, 100) string("sumeachIb ", briefname(Ar)) string("for I in eachindex(A), ", briefname(Ar)) sz @timeit sumlinear(Ar, 100) string("sumlinearIb ", briefname(Ar)) string("for I in 1:length(A), ", briefname(Ar)) sz - @timeit sumcartesian(Ar, 100) string("sumcartesianIb ", briefname(Ar)) string("for I in CartesianRange(size(A)), ", briefname(Ar)) sz + @timeit sumcartesian(Ar, 100) string("sumcartesianIb ", briefname(Ar)) string("for I in CartesianIndices(size(A)), ", briefname(Ar)) sz @timeit sumcolon(Ar, 100) string("sumcolonIb ", briefname(Ar)) string("colon indexing, ", briefname(Ar)) sz @timeit sumrange(Ar, 100) string("sumrangeIb ", briefname(Ar)) string("range indexing, ", briefname(Ar)) sz @timeit sumlogical(Ar, 100) string("sumlogicalIb ", briefname(Ar)) string("logical indexing, ", briefname(Ar)) sz @@ -51,7 +51,7 @@ for Ar in Alist @timeit sumelt(Ar, 100) string("sumeltFb ", briefname(Ar)) string("for a in A indexing, ", briefname(Ar)) sz @timeit sumeach(Ar, 100) string("sumeachFb ", briefname(Ar)) string("for I in eachindex(A), ", briefname(Ar)) sz @timeit sumlinear(Ar, 100) string("sumlinearFb ", briefname(Ar)) string("for I in 1:length(A), ", briefname(Ar)) sz - @timeit sumcartesian(Ar, 100) string("sumcartesianFb ", briefname(Ar)) string("for I in CartesianRange(size(A)), ", briefname(Ar)) sz + @timeit sumcartesian(Ar, 100) string("sumcartesianFb ", briefname(Ar)) string("for I in CartesianIndices(size(A)), ", briefname(Ar)) sz @timeit sumcolon(Ar, 100) string("sumcolonFb ", briefname(Ar)) string("colon indexing, ", briefname(Ar)) sz @timeit sumrange(Ar, 100) string("sumrangeFb ", briefname(Ar)) string("range indexing, ", briefname(Ar)) sz @timeit sumlogical(Ar, 100) string("sumlogicalFb ", briefname(Ar)) string("logical indexing, ", briefname(Ar)) sz diff --git a/test/simdloop.jl b/test/simdloop.jl index 55b6c4cf36ae1..4909101cc108d 100644 --- a/test/simdloop.jl +++ b/test/simdloop.jl @@ -115,23 +115,23 @@ function simd_cartesian_range!(indices, crng) indices end -crng = CartesianRange(2:4, 0:1, 1:1, 3:5) +crng = CartesianIndices(2:4, 0:1, 1:1, 3:5) indices = simd_cartesian_range!(Vector{eltype(crng)}(), crng) @test indices == vec(collect(crng)) -crng = CartesianRange(-1:1, 1:3) +crng = CartesianIndices(-1:1, 1:3) indices = simd_cartesian_range!(Vector{eltype(crng)}(), crng) @test indices == vec(collect(crng)) -crng = CartesianRange(-1:-1, 1:3) +crng = CartesianIndices(-1:-1, 1:3) indices = simd_cartesian_range!(Vector{eltype(crng)}(), crng) @test indices == vec(collect(crng)) -crng = CartesianRange(2:4) +crng = CartesianIndices(2:4) indices = simd_cartesian_range!(Vector{eltype(crng)}(), crng) @test indices == collect(crng) -crng = CartesianRange() +crng = CartesianIndices() indices = simd_cartesian_range!(Vector{eltype(crng)}(), crng) @test indices == vec(collect(crng)) diff --git a/test/subarray.jl b/test/subarray.jl index be85d3bacef68..f6a653537ebb7 100644 --- a/test/subarray.jl +++ b/test/subarray.jl @@ -132,7 +132,7 @@ function _test_mixed(@nospecialize(A), @nospecialize(B)) m = size(A, 1) n = size(A, 2) isgood = true - for J in CartesianRange(size(A)[2:end]), i in 1:m + for J in CartesianIndices(size(A)[2:end]), i in 1:m if A[i,J] != B[i,J] isgood = false break