Skip to content

Commit f553a82

Browse files
committed
add fill!(CatArray, val)
1 parent e07a9a4 commit f553a82

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

src/array.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ Base.IndexStyle(::Type{<:CategoricalArray}) = IndexLinear()
338338
@inbounds A.refs[I...] = get!(A.pool, v)
339339
end
340340

341+
Base.fill!(A::CategoricalArray, v::Any) = (fill!(A.refs, get!(A.pool, v)); A)
342+
341343
function mergelevels(ordered, levels...)
342344
T = Base.promote_eltype(levels...)
343345
res = Array{T}(0)

src/missingarray.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ end
2222
@inbounds A.refs[I...] = 0
2323
end
2424

25+
Base.fill!(A::CategoricalArray{>:Missing}, ::Missing) = (fill!(A.refs, 0); A)
26+
2527
in(x::Missing, y::CategoricalArray) = false
2628
in(x::Missing, y::CategoricalArray{>:Missing}) = !all(v -> v > 0, y.refs)

test/11_array.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ using CategoricalArrays: DefaultRefType, catvaluetype, leveltype
136136
@test levels(x2) !== levels(x)
137137
@test isordered(x2) == isordered(x)
138138

139+
x2 = copy(x)
140+
@test fill!(x2, "a") === x2
141+
@test x2 == ["a", "a", "a"]
142+
@test levels(x2) == ["a", "b"]
143+
fill!(x2, "c")
144+
@test x2 == ["c", "c", "c"]
145+
@test levels(x2) == ["a", "b", "c"]
146+
139147
x[1] = x[2]
140148
@test x[1] === x.pool.valindex[2]
141149
@test x[2] === x.pool.valindex[2]

test/12_missingarray.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ const ≅ = isequal
146146
@test levels(x2) !== levels(x)
147147
@test isordered(x2) == isordered(x)
148148

149+
x2 = copy(x)
150+
@test fill!(x2, "a") === x2
151+
@test x2 == ["a", "a", "a"]
152+
@test levels(x2) == ["a", "b"]
153+
fill!(x2, "c")
154+
@test x2 == ["c", "c", "c"]
155+
@test levels(x2) == ["a", "b", "c"]
156+
fill!(x2, missing)
157+
@test all(ismissing, x2)
158+
@test levels(x2) == ["a", "b", "c"]
159+
149160
x[1] = x[2]
150161
@test x[1] === x.pool.valindex[2]
151162
@test x[2] === x.pool.valindex[2]

0 commit comments

Comments
 (0)