Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indices-aware find and fixes for logical indexing #18040

Merged
merged 2 commits into from
Aug 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ function _unsafe_getindex(::LinearFast, src::AbstractArray, I::AbstractArray{Boo

D = eachindex(dest)
Ds = start(D)
s = 0
s = first(linearindices(src))-1
for i in eachindex(I)
s += 1
@inbounds if I[i]
Expand Down
18 changes: 15 additions & 3 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let
# Basics
v0 = rand(4)
v = OffsetArray(v0, (-3,))
h = OffsetArray([-1,1,-2,2,0], (-3,))
@test indices(v) == (-2:1,)
@test_throws ErrorException size(v)
@test_throws ErrorException size(v, 1)
Expand Down Expand Up @@ -48,6 +49,16 @@ S = OffsetArray(view(A0, 1:2, 1:2), (-1,2)) # LinearSlow
@test eachindex(A) == 1:4
@test eachindex(S) == CartesianRange((0:1,3:4))

# logical indexing
@test A[A .> 2] == [3,4]
@test_throws BoundsError h[trues(2)]
@test_throws BoundsError h[trues(5)]
@test h[OffsetArray(trues(5), (-3,))] == parent(h)
@test h[OffsetArray([true,false,false,true,true], (-3,))] == parent(h)[[1,4,5]]
@test A[OffsetArray([true false; false true], A.offsets)] == [1,4]
@test A[OffsetArray([true true; false true], A.offsets)] == [1,3,4]
@test_throws BoundsError A[[true true; false true]]

# view
S = view(A, :, 3)
@test S == OffsetArray([1,2], (A.offsets[1],))
Expand Down Expand Up @@ -172,9 +183,6 @@ v = view(A0, i1, 1)
v = view(A0, 1:1, i1)
@test indices(v) === (Base.OneTo(1), -4:-3)

# logical indexing
@test A[A .> 2] == [3,4]

# copy! and fill!
a = OffsetArray{Int}((-3:-1,))
fill!(a, -1)
Expand Down Expand Up @@ -281,6 +289,10 @@ I,J,N = findnz(z)
@test I == [-1]
@test J == [0]
@test N == [2]
@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