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

optimize permute! and invpermute! #84

Merged
merged 1 commit into from
Jun 23, 2023
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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"

[targets]
test = ["OffsetArrays", "Test"]
test = ["OffsetArrays", "Random", "Test"]
10 changes: 10 additions & 0 deletions src/PooledArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,16 @@ function Base.reverse(x::PooledArray)
PooledArray(RefArray(reverse(x.refs)), x.invpool, x.pool, x.refcount)
end

function Base.permute!(x::PooledArray, p::AbstractVector{T}) where T<:Integer
permute!(x.refs, p)
return x
end

function Base.invpermute!(x::PooledArray, p::AbstractVector{T}) where T<:Integer
invpermute!(x.refs, p)
return x
end

function Base.permute!!(x::PooledArray, p::AbstractVector{T}) where T<:Integer
Base.permute!!(x.refs, p)
return x
Expand Down
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ using Test, OffsetArrays
using PooledArrays
using DataAPI: refarray, refvalue, refpool, invrefpool
using PooledArrays: refcount
using Random: randperm

import Future.copy!

if Threads.nthreads() < 2
Expand Down Expand Up @@ -35,6 +37,17 @@ end
@test sort(pc) == sort(c)
@test sortperm(pc) == sortperm(c)

perm = randperm(length(pc))
pc2 = copy(pc)
pc3 = permute!(pc2, perm)
@test pc2 === pc3
@test pc2 == pc[perm]

pc2 = copy(pc)
pc3 = invpermute!(pc2, perm)
@test pc2 === pc3
@test pc2[perm] == pc

push!(pc, -10)
push!(c, -10)
@test pc == c
Expand Down