Skip to content

Commit

Permalink
Make find indices-aware
Browse files Browse the repository at this point in the history
(cherry picked from commit 6f1ad9c)
ref #18040
  • Loading branch information
timholy authored and tkelman committed Aug 20, 2016
1 parent c144c18 commit 0136e4f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1077,15 +1077,18 @@ function find(testf::Function, A)
# use a dynamic-length array to store the indexes, then copy to a non-padded
# array for the return
tmpI = Array{Int,1}(0)
inds = _index_remapper(A)
for (i,a) = enumerate(A)
if testf(a)
push!(tmpI, i)
push!(tmpI, inds[i])
end
end
I = Array{Int,1}(length(tmpI))
copy!(I, tmpI)
return I
end
_index_remapper(A::AbstractArray) = linearindices(A)
_index_remapper(iter) = Colon() # safe for objects that don't implement length

"""
find(A)
Expand All @@ -1110,9 +1113,10 @@ function find(A)
nnzA = countnz(A)
I = Vector{Int}(nnzA)
count = 1
inds = _index_remapper(A)
for (i,a) in enumerate(A)
if a != 0
I[count] = i
I[count] = inds[i]
count += 1
end
end
Expand Down
5 changes: 5 additions & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ I,J,N = findnz(z)
@test I == [-1]
@test J == [0]
@test N == [2]
h = OffsetArray([-1,1,-2,2,0], (-3,))
@test find(h) == [-2:1;]
@test find(x->x>0, h) == [-1,1]
@test find(x->x<0, h) == [-2,0]
@test find(x->x==0, h) == [2]

v = OffsetArray([1,1e100,1,-1e100], (-3,))*1000
v2 = OffsetArray([1,-1e100,1,1e100], (5,))*1000
Expand Down

0 comments on commit 0136e4f

Please sign in to comment.