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

Implementing repeat for PooledArray #67

Merged
merged 9 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
11 changes: 11 additions & 0 deletions src/PooledArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -601,4 +601,15 @@ _perm(o::F, z::V) where {F, V} = Base.Order.Perm{F, V}(o, z)

Base.Order.Perm(o::Base.Order.ForwardOrdering, y::PooledArray) = _perm(o, fast_sortable(y))

function Base.repeat(x::PooledArray, m::Integer...)
Threads.atomic_add!(x.refcount, 1)
PooledArray(RefArray(repeat(x.refs, m...)), x.invpool, x.pool, x.refcount)
end

function Base.repeat(x::PooledArray; inner = nothing, outer = nothing)
sl-solution marked this conversation as resolved.
Show resolved Hide resolved
Threads.atomic_add!(x.refcount, 1)
PooledArray(RefArray(repeat(x.refs; inner = inner, outer = outer)),
x.invpool, x.pool, x.refcount)
end

end
42 changes: 42 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,45 @@ end
pa[:] = [1 2; 3 4]
@test pa == [1 2; 3 4]
end

@testset "repeat" begin
pa1 = PooledArray([1, 2, 3])
pa2 = repeat(pa1)
pa3 = repeat(pa1, 2)
pa4 = repeat(pa1, inner = 2)
@test pa2 == pa1
@test pa3 == [1, 2, 3, 1, 2, 3]
@test pa4 == [1, 1, 2, 2, 3, 3]
@test refpool(pa1) === refpool(pa2) === refpool(pa3) === refpool(pa4)
@test invrefpool(pa1) === invrefpool(pa2) === invrefpool(pa3) === invrefpool(pa4)
@test refcount(pa1) === refcount(pa2) === refcount(pa3) === refcount(pa4)
@test refcount(pa1)[] == 4

pa1 = PooledArray(["one", "two"])
# check it didn't mess the other pa
@test pa3 == [1, 2, 3, 1, 2, 3]

Copy link
Member

Choose a reason for hiding this comment

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

This doesn't check much AFAICT: no significant operation has been performed since the last check.

Suggested change
pa1 = PooledArray(["one", "two"])
# check it didn't mess the other pa
@test pa3 == [1, 2, 3, 1, 2, 3]
pa1 = PooledArray(["one", "two"])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I remove it?

Copy link
Member

Choose a reason for hiding this comment

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

What's what the suggestion does?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I am lost. What is your suggestion on lines 478-480?

Copy link
Member

Choose a reason for hiding this comment

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

Not sure why GitHub is confused about it, but I just suggested dropping lines 479-481.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see,
GitHub doesn't allow to commit suggestion (the suggestion is invalid...)!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

pa2 = repeat(pa1, outer = 3)
pa3 = repeat(pa1, inner = 3, outer = 2)
@test pa2 == ["one", "two", "one", "two", "one", "two"]
@test pa3 == ["one", "one", "one", "two", "two", "two", "one", "one", "one", "two", "two", "two"]

# missing shouldn't be a problem

pa1 = PooledArray(["one", missing, "two"])
pa2 = repeat(pa1, 2)
pa3 = repeat(pa1, 0)
@test isequal(pa2, ["one", missing, "two", "one", missing, "two"])
@test isequal(pa2.pool, ["one", missing, "two"])
@test size(pa3) == (0,)
@test isempty(pa3.refs)

# two dimensional
pa1 = PooledArray([true false; false true; true true])
pa2 = repeat(pa1, 2)
@test pa2 == Bool[1 0; 0 1; 1 1; 1 0; 0 1; 1 1]

pa1 = PooledArray([1 2; 3 4])
pa2 = repeat(pa1, inner = (2, 1))
@test pa2 == [1 2; 1 2; 3 4; 3 4]
end