@@ -49,16 +49,27 @@ A user defined type could override this method to define an appropriate test fun
49
49
@inline recode_in (x, collection:: Set ) = x in collection
50
50
@inline recode_in (x, collection) = any (x ≅ y for y in collection)
51
51
52
+
53
+ function optimize_pair (pair:: Pair ):: Pair
54
+ if typeof (pair. first) <: AbstractArray
55
+ pair = Set (pair. first) => pair. second
56
+ end
57
+ return pair;
58
+ end
59
+
60
+
52
61
function recode! (dest:: AbstractArray{T} , src:: AbstractArray , default:: Any , pairs:: Pair... ) where {T}
53
62
if length (dest) != length (src)
54
63
throw (DimensionMismatch (" dest and src must be of the same length (got $(length (dest)) and $(length (src)) )" ))
55
64
end
56
65
66
+ opt_pairs = map (optimize_pair, pairs);
67
+
57
68
@inbounds for i in eachindex (dest, src)
58
69
x = src[i]
59
70
60
- for j in 1 : length (pairs )
61
- p = pairs [j]
71
+ for j in 1 : length (opt_pairs )
72
+ p = opt_pairs [j]
62
73
# we use isequal and recode_in because we cannot really distinguish scalars from collections
63
74
if x ≅ p. first || recode_in (x, p. first)
64
75
dest[i] = p. second
@@ -96,7 +107,9 @@ function recode!(dest::CategoricalArray{T}, src::AbstractArray, default::Any, pa
96
107
throw (DimensionMismatch (" dest and src must be of the same length (got $(length (dest)) and $(length (src)) )" ))
97
108
end
98
109
99
- vals = T[p. second for p in pairs]
110
+ opt_pairs = map (optimize_pair, pairs);
111
+
112
+ vals = T[p. second for p in opt_pairs]
100
113
default != = nothing && push! (vals, default)
101
114
102
115
levels! (dest. pool, filter! (! ismissing, unique (vals)))
@@ -110,8 +123,8 @@ function recode!(dest::CategoricalArray{T}, src::AbstractArray, default::Any, pa
110
123
@inbounds for i in eachindex (drefs, src)
111
124
x = src[i]
112
125
113
- for j in 1 : length (pairs )
114
- p = pairs [j]
126
+ for j in 1 : length (opt_pairs )
127
+ p = opt_pairs [j]
115
128
# we use isequal and recode_in because we cannot really distinguish scalars from collections
116
129
if x ≅ p. first || recode_in (x, p. first)
117
130
drefs[i] = dupvals ? pairmap[j] : j
0 commit comments