-
Notifications
You must be signed in to change notification settings - Fork 35
Make sure that new CategoricalArray allocates new pool and refs #110
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
Conversation
@nalimilan similarly here - errors are unrelated (due to changes in 0.7) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
src/array.jl
Outdated
@@ -154,14 +154,23 @@ CategoricalMatrix{T}(m::Int, n::Int; ordered=false) where {T} = | |||
|
|||
## Constructors from arrays | |||
|
|||
# This method is needed to ensure that a copy of the pool is always made | |||
# This method is needed to ensure that a copy of the refs and pool is always made | |||
# so that ordered!() does not affect the original array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just remove this line, now we make a copy because that's what the function is documented to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
src/array.jl
Outdated
needs_copy = true | ||
end | ||
if needs_copy | ||
res = CategoricalArray{V, N}(refs, pool) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better always call that constructor, anyway CategoricalArray
is an immutable and it should be cheap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
test/11_array.jl
Outdated
@@ -638,4 +638,14 @@ end | |||
@test unique(x) == collect(1500:-1:1) | |||
end | |||
|
|||
@testset "categorical() makes a copy of pool and refs" begin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put this in arraycommon.jl instead. It would make sense to test with an array supporting missing
too, and with the CategoricalArray{T, R}
form with equal and different types, just in case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
test/11_array.jl
Outdated
@testset "categorical() makes a copy of pool and refs" begin | ||
x = categorical(1:10) | ||
y = categorical(x) | ||
@test !(x.refs === y.refs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use !==
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
src/array.jl
Outdated
function CategoricalArray{T, N, R}(A::CategoricalArray{S, N, Q}; | ||
ordered=_isordered(A)) where {S, T, N, Q, R} | ||
V = unwrap_catvaluetype(T) | ||
res = convert(CategoricalArray{V, N, R}, A) | ||
if res.pool === A.pool # convert() only makes a copy when necessary | ||
res = CategoricalArray{V, N}(res.refs, deepcopy(res.pool)) | ||
needs_copy = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs_copy
is no longer needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
Codecov Report
@@ Coverage Diff @@
## master #110 +/- ##
==========================================
+ Coverage 97.78% 97.81% +0.02%
==========================================
Files 9 9
Lines 678 685 +7
==========================================
+ Hits 663 670 +7
Misses 15 15
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution!
Following Issue #107.
I test for new
refs
and newpool
separately as I guess it is safest.