Skip to content

Commit a3f710e

Browse files
authored
Copy for CartesianIndices/LinearIndices need not materialize (#53901)
Currently, ```julia julia> C = CartesianIndices((1:2, 1:2)) CartesianIndices((1:2, 1:2)) julia> copy(C) 2×2 Matrix{CartesianIndex{2}}: CartesianIndex(1, 1) CartesianIndex(1, 2) CartesianIndex(2, 1) CartesianIndex(2, 2) ``` However, seeing that a `CartesianIndices` is equivalent to an n-D range, there doesn't seem to be a need to materialize the result. This PR also ensures that `copy(C)` returns the same type as `C`. After this PR: ```julia julia> C = CartesianIndices((1:2, 1:2)) CartesianIndices((1:2, 1:2)) julia> copy(C) CartesianIndices((1:2, 1:2)) ``` Also, a similar change for `LinearIndices` is added.
1 parent 313f933 commit a3f710e

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

base/indices.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ function getindex(iter::LinearIndices, i::AbstractRange{<:Integer})
565565
@boundscheck checkbounds(iter, i)
566566
@inbounds isa(iter, LinearIndices{1}) ? iter.indices[1][i] : (first(iter):last(iter))[i]
567567
end
568+
copy(iter::LinearIndices) = iter
568569
# More efficient iteration — predominantly for non-vector LinearIndices
569570
# but one-dimensional LinearIndices must be special-cased to support OffsetArrays
570571
iterate(iter::LinearIndices{1}, s...) = iterate(axes1(iter.indices[1]), s...)

base/multidimensional.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
module IteratorsMD
55
import .Base: eltype, length, size, first, last, in, getindex, setindex!,
66
min, max, zero, oneunit, isless, eachindex,
7-
convert, show, iterate, promote_rule, to_indices
7+
convert, show, iterate, promote_rule, to_indices, copy
88

99
import .Base: +, -, *, (:)
1010
import .Base: simd_outer_range, simd_inner_length, simd_index, setindex
@@ -476,6 +476,8 @@ module IteratorsMD
476476
@inline in(i::CartesianIndex, r::CartesianIndices) = false
477477
@inline in(i::CartesianIndex{N}, r::CartesianIndices{N}) where {N} = all(map(in, i.I, r.indices))
478478

479+
copy(iter::CartesianIndices) = iter
480+
479481
simd_outer_range(iter::CartesianIndices{0}) = iter
480482
function simd_outer_range(iter::CartesianIndices)
481483
CartesianIndices(tail(iter.indices))

test/abstractarray.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,13 @@ end
332332
end
333333
end
334334

335+
@testset "copy for LinearIndices/CartesianIndices" begin
336+
C = CartesianIndices((1:2, 1:4))
337+
@test copy(C) === C
338+
L = LinearIndices((1:2, 1:4))
339+
@test copy(L) === L
340+
end
341+
335342
# token type on which to dispatch testing methods in order to avoid potential
336343
# name conflicts elsewhere in the base test suite
337344
mutable struct TestAbstractArray end

0 commit comments

Comments
 (0)