Skip to content

Commit

Permalink
Remove unnecessary methods for CartesianIndices (JuliaLang#50258)
Browse files Browse the repository at this point in the history
* Remove unnecessary methods for CartesianIndices

* remove IteratorSize

* Add tests
  • Loading branch information
jishnub authored Jun 27, 2023
1 parent 269d350 commit 76c906e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
13 changes: 2 additions & 11 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

### Multidimensional iterators
module IteratorsMD
import .Base: eltype, length, size, first, last, in, getindex, setindex!, IndexStyle,
min, max, zero, oneunit, isless, eachindex, ndims, IteratorSize,
import .Base: eltype, length, size, first, last, in, getindex, setindex!,
min, max, zero, oneunit, isless, eachindex,
convert, show, iterate, promote_rule

import .Base: +, -, *, (:)
Expand Down Expand Up @@ -342,7 +342,6 @@ module IteratorsMD

# AbstractArray implementation
Base.axes(iter::CartesianIndices{N,R}) where {N,R} = map(Base.axes1, iter.indices)
Base.IndexStyle(::Type{CartesianIndices{N,R}}) where {N,R} = IndexCartesian()
Base.has_offset_axes(iter::CartesianIndices) = Base.has_offset_axes(iter.indices...)
@propagate_inbounds function isassigned(iter::CartesianIndices{N,R}, I::Vararg{Int, N}) where {N,R}
for i in 1:N
Expand Down Expand Up @@ -390,10 +389,6 @@ module IteratorsMD
getindex(c, C)
end

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) = CartesianIndices(axes(A))

@inline function eachindex(::IndexCartesian, A::AbstractArray, B::AbstractArray...)
Expand All @@ -402,10 +397,6 @@ module IteratorsMD
CartesianIndices(axsA)
end

eltype(::Type{CartesianIndices{N}}) where {N} = CartesianIndex{N}
eltype(::Type{CartesianIndices{N,TT}}) where {N,TT} = CartesianIndex{N}
IteratorSize(::Type{<:CartesianIndices{N}}) where {N} = Base.HasShape{N}()

@inline function iterate(iter::CartesianIndices)
iterfirst = first(iter)
if !all(map(in, iterfirst.I, iter.indices))
Expand Down
13 changes: 13 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,19 @@ end
end
end

@testset "AbstractArray fallbacks for CartesianIndices" begin
@test ndims(CartesianIndices{3}) == 3
@test eltype(CartesianIndices{3}) == CartesianIndex{3}
for t in ((1:2, 1:2), (3:4,), ())
C2 = CartesianIndices(t)
@test ndims(C2) == length(t)
@test ndims(typeof(C2)) == length(t)
@test IndexStyle(C2) == IndexCartesian()
@test eltype(C2) == CartesianIndex{length(t)}
@test Base.IteratorSize(C2) isa Base.HasShape{length(t)}
end
end

@testset "LinearIndices" begin
@testset "constructors" begin
for oinds in [
Expand Down

0 comments on commit 76c906e

Please sign in to comment.