Skip to content

add keepat! for in-place logical filtering #39528

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2436,6 +2436,26 @@ function filter!(f, a::AbstractVector)
return a
end

function mask!(a::AbstractVector, m::AbstractVector{Bool})
j = firstindex(a)
for i in eachindex(a, m)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q for anyone: is eachindex and nextind / iterate guaranteed to be in the same visit order?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@inbounds begin
ai = a[i]
mi = m[i]
a[j] = ai
end
j = ifelse(mi, nextind(a, j), j)
end
j > lastindex(a) && return a
if a isa Vector
resize!(a, j-1)
sizehint!(a, j-1)
else
deleteat!(a, j:lastindex(a))
end
return a
end

# set-like operators for vectors
# These are moderately efficient, preserve order, and remove dupes.

Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ export
mapfoldl,
mapfoldr,
mapreduce,
mask!,
merge!,
mergewith!,
merge,
Expand Down
26 changes: 26 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,32 @@ end
@test isempty(eoa)
end

@testset "mask!" begin
# base case w/ Vector
a = Vector(1:10)
mask!(a, [falses(5); trues(5)])
@test a == 6:10

# different subtype of AbstractVector
ba = rand(10) .> 0.5 #
@test isa(ba, BitArray)
mask!(ba, ba)
@test all(ba)

# empty array
ea = []
mask!(ea, Bool[])
@test isempty(ea)

# non-1-indexed array
# deleteat! is not supported for OffsetArrays

# empty non-1-indexed array
eoa = OffsetArray([], -5)
mask!(eoa, Bool[])
@test isempty(eoa)
end

@testset "deleteat!" begin
for idx in Any[1, 2, 5, 9, 10, 1:0, 2:1, 1:1, 2:2, 1:2, 2:4, 9:8, 10:9, 9:9, 10:10,
8:9, 9:10, 6:9, 7:10]
Expand Down