Skip to content

Commit 1bb9c95

Browse files
authored
optimize permute! and invpermute! (JuliaData#84)
1 parent 65e3316 commit 1bb9c95

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Project.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ julia = "1"
1212

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

1718
[targets]
18-
test = ["OffsetArrays", "Test"]
19+
test = ["OffsetArrays", "Random", "Test"]

src/PooledArrays.jl

+10
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,16 @@ function Base.reverse(x::PooledArray)
297297
PooledArray(RefArray(reverse(x.refs)), x.invpool, x.pool, x.refcount)
298298
end
299299

300+
function Base.permute!(x::PooledArray, p::AbstractVector{T}) where T<:Integer
301+
permute!(x.refs, p)
302+
return x
303+
end
304+
305+
function Base.invpermute!(x::PooledArray, p::AbstractVector{T}) where T<:Integer
306+
invpermute!(x.refs, p)
307+
return x
308+
end
309+
300310
function Base.permute!!(x::PooledArray, p::AbstractVector{T}) where T<:Integer
301311
Base.permute!!(x.refs, p)
302312
return x

test/runtests.jl

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ using Test, OffsetArrays
22
using PooledArrays
33
using DataAPI: refarray, refvalue, refpool, invrefpool
44
using PooledArrays: refcount
5+
using Random: randperm
6+
57
import Future.copy!
68

79
if Threads.nthreads() < 2
@@ -35,6 +37,17 @@ end
3537
@test sort(pc) == sort(c)
3638
@test sortperm(pc) == sortperm(c)
3739

40+
perm = randperm(length(pc))
41+
pc2 = copy(pc)
42+
pc3 = permute!(pc2, perm)
43+
@test pc2 === pc3
44+
@test pc2 == pc[perm]
45+
46+
pc2 = copy(pc)
47+
pc3 = invpermute!(pc2, perm)
48+
@test pc2 === pc3
49+
@test pc2[perm] == pc
50+
3851
push!(pc, -10)
3952
push!(c, -10)
4053
@test pc == c

0 commit comments

Comments
 (0)