Skip to content

Commit abff721

Browse files
committed
Change findfirst and findlast to return cartesian indices with HasShape iterators
This represents better the shape of the iterator, and makes generators over arrays consistent with their input array. Keep returning linear indices with HasShape{1} iterators for consistency with vectors.
1 parent 8da655f commit abff721

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

base/array.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,9 @@ cat(n::Integer, x::Integer...) = reshape([x...], (ntuple(x->1, n-1)..., length(x
15001500

15011501
_pairs(A::Union{AbstractArray, AbstractDict, AbstractString, Tuple, NamedTuple}) = pairs(A)
15021502
_pairs(iter) = _pairs(IteratorSize(iter), iter)
1503-
_pairs(::Union{HasLength, HasShape}, iter) = zip(1:length(iter), iter)
1503+
# includes HasShape{1} for consistency with keys(::AbstractVector)
1504+
_pairs(::Union{HasLength, HasShape{1}}, iter) = zip(1:length(iter), iter)
1505+
_pairs(::HasShape, iter) = zip(CartesianIndices(size(iter)), iter)
15041506
_pairs(::Union{SizeUnknown, IsInfinite}, iter) = zip(Iterators.countfrom(1), iter)
15051507

15061508
"""

test/arrayops.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,9 @@ end
498498
@test findlast(equalto(2), g2) === nothing
499499

500500
g3 = (i % 2 for i in 1:10, j in 1:2)
501-
@test findall(!iszero, g3) == 1:2:19
502-
@test findfirst(!iszero, g3) == 1
503-
@test findlast(!iszero, g3) == 19
501+
@test findall(!iszero, g3) == findall(!iszero, collect(g3))
502+
@test findfirst(!iszero, g3) == CartesianIndex(1, 1)
503+
@test findlast(!iszero, g3) == CartesianIndex(9, 2)
504504
@test findfirst(equalto(2), g3) === nothing
505505
@test findlast(equalto(2), g3) === nothing
506506
end

0 commit comments

Comments
 (0)