Skip to content

Commit

Permalink
Support 1d filter!(), fixes #105
Browse files Browse the repository at this point in the history
  • Loading branch information
davidavdav committed May 24, 2021
1 parent 64b4b79 commit 938a4a9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/rearrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,18 @@ function _sortslices(A::NamedArray, d::Val{dims}; kws...) where dims
B
end
end

function Base.filter!(f, n::NamedVector)
j = firstindex(n)
for (name, i) in n.dicts[1]
if f(n.array[i])
n.array[j] = n.array[i]
n.dicts[1][name] = j
j += 1
else
delete!(n.dicts[1], name)
end
end
deleteat!(n.array, j:length(n.array))
return n
end
7 changes: 7 additions & 0 deletions test/bugs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ n = NamedArray([1 2; 3 4], ([11,12], [13,14]))
s = sum(n, dims=1)
table = n ./ s
@test namesanddim(table) == namesanddim(n)

#105
a = [10, 20, 30]
n = NamedArray(a)
filter!(x -> x > 15, n)
@test n == a == [20, 30]
@test names(n, 1) == ["2", "3"]
5 changes: 5 additions & 0 deletions test/bugs/#105.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using NamedArrays

a = [10, 20, 30]
n = NamedArray(a)
filter!(x -> x > 15, n)

0 comments on commit 938a4a9

Please sign in to comment.