Skip to content

Commit

Permalink
make recode! type stable
Browse files Browse the repository at this point in the history
  • Loading branch information
tiemvanderdeure committed Nov 1, 2024
1 parent 246e8f0 commit 1db6279
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/recode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@ function recode!(dest::AbstractArray{T}, src::AbstractArray, default::Any, pairs
throw(DimensionMismatch("dest and src must be of the same length (got $(length(dest)) and $(length(src)))"))
end

opt_pairs = map(optimize_pair, pairs)

@inbounds for i in eachindex(dest, src)
x = src[i]

for j in 1:length(opt_pairs)
p = opt_pairs[j]
for j in 1:length(pairs)
p = optimize_pair(pairs[j])
# we use isequal and recode_in because we cannot really distinguish scalars from collections
if x p.first || recode_in(x, p.first)
dest[i] = p.second
Expand Down Expand Up @@ -101,9 +99,7 @@ function recode!(dest::CategoricalArray{T}, src::AbstractArray, default::Any, pa
throw(DimensionMismatch("dest and src must be of the same length (got $(length(dest)) and $(length(src)))"))
end

opt_pairs = map(optimize_pair, pairs)

vals = T[p.second for p in opt_pairs]
vals = T[p.second for p in pairs]
default !== nothing && push!(vals, default)

levels!(dest.pool, filter!(!ismissing, unique(vals)))
Expand All @@ -117,8 +113,8 @@ function recode!(dest::CategoricalArray{T}, src::AbstractArray, default::Any, pa
@inbounds for i in eachindex(drefs, src)
x = src[i]

for j in 1:length(opt_pairs)
p = opt_pairs[j]
for j in 1:length(pairs)
p = optimize_pair(pairs[j])
# we use isequal and recode_in because we cannot really distinguish scalars from collections
if x p.first || recode_in(x, p.first)
drefs[i] = dupvals ? pairmap[j] : j
Expand Down

0 comments on commit 1db6279

Please sign in to comment.