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

make recode! type stable #407

Merged
merged 17 commits into from
Jan 3, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
whitespace and comments
  • Loading branch information
tiemvanderdeure committed Jan 3, 2025
commit 04695faef233f7cd6c66955ff3b8fca5ef96180e
11 changes: 8 additions & 3 deletions src/recode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ function recode!(dest::AbstractArray, src::AbstractArray, default::Any, pairs::P
_recode!(dest, src, default, opt_pairs)
end

function _recode!(dest::AbstractArray{T}, src::AbstractArray, default, pairs::NTuple{<:Any, Pair}) where {T}
function _recode!(dest::AbstractArray{T}, src::AbstractArray, default,
pairs::NTuple{<:Any, Pair}) where {T}
recode_to = last.(pairs)
recode_from = first.(pairs)

Expand Down Expand Up @@ -101,7 +102,7 @@ function _recode!(dest::AbstractArray{T}, src::AbstractArray, default, pairs::NT
end

function _recode!(dest::CategoricalArray{T, <:Any, R}, src::AbstractArray, default::Any,
pairs::NTuple{<:Any, Pair}) where {T, R}
pairs::NTuple{<:Any, Pair}) where {T, R}
recode_from = first.(pairs)
vals = T[p.second for p in pairs]
tiemvanderdeure marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -119,11 +120,15 @@ function _recode!(dest::CategoricalArray{T, <:Any, R}, src::AbstractArray, defau
@inbounds for i in eachindex(drefs, src)
x = src[i]

# @inline is needed for type stability and Compat for compatibility before julia v1.8
# we use isequal and recode_in because we cannot really
tiemvanderdeure marked this conversation as resolved.
Show resolved Hide resolved
# distinguish scalars from collections
j = Compat.@inline findfirst(y -> isequal(x, y) || recode_in(x, y), recode_from)

# Value in one of the pairs
if j !== nothing
drefs[i] = dupvals ? pairmap[j] : j
# Value not in any of the pairs
elseif ismissing(x)
eltype(dest) >: Missing ||
throw(MissingException("missing value found, but dest does not support them: " *
Expand Down Expand Up @@ -164,7 +169,7 @@ function _recode!(dest::CategoricalArray{T, <:Any, R}, src::AbstractArray, defau
end

function _recode!(dest::CategoricalArray{T, N, R}, src::CategoricalArray,
default::Any, pairs::NTuple{<:Any, Pair}) where {T, N, R<:Integer}
default::Any, pairs::NTuple{<:Any, Pair}) where {T, N, R<:Integer}
recode_from = first.(pairs)
vals = T[p.second for p in pairs]

Expand Down
Loading