Skip to content

Commit

Permalink
optimize permute! and invpermute! (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
piever authored Jun 23, 2023
1 parent 65e3316 commit 1bb9c95
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
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

0 comments on commit 1bb9c95

Please sign in to comment.