Skip to content

Commit 7aa1dea

Browse files
quinnjnalimilan
authored andcommitted
Add push!(), append!(), and empty!() methods (#9)
Needed by DataStreams.jl. This uncovered a bug in _levels!() when the input contained missing values.
1 parent f0fa6e8 commit 7aa1dea

File tree

3 files changed

+133
-2
lines changed

3 files changed

+133
-2
lines changed

src/array.jl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ function _levels!(A::CatOrdArray, newlevels::Vector; nullok=false)
164164
levelsmap = indexin(oldindex, index(A.pool))
165165

166166
@inbounds for (i, x) in enumerate(A.refs)
167-
j = levelsmap[x]
168-
x > 0 && (A.refs[i] = j)
167+
x > 0 && (A.refs[i] = levelsmap[x])
169168
end
170169
end
171170

@@ -190,3 +189,22 @@ function getindex(A::CatOrdArray, i::Int)
190189
end
191190

192191
levels!(A::CatOrdArray, newlevels::Vector) = _levels!(A, newlevels)
192+
193+
function Base.push!(A::CatOrdArray, item)
194+
resize!(A.refs, length(A.refs) + 1)
195+
A[end] = item
196+
return A
197+
end
198+
199+
function Base.append!(A::CatOrdArray, B::CatOrdArray)
200+
levels!(A, union(levels(A), levels(B)))
201+
len = length(A.refs)
202+
len2 = length(B.refs)
203+
resize!(A.refs, len + length(B.refs))
204+
for i = 1:len2
205+
A[len + i] = B[i]
206+
end
207+
return A
208+
end
209+
210+
Base.empty!(A::CatOrdArray) = (empty!(A.refs); return A)

test/11_array.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,35 @@ for (A, V, M) in ((NominalArray, NominalVector, NominalMatrix),
132132
@test x[3] === x.pool.valindex[1]
133133
@test levels(x) == ["e", "a", "b", "c"]
134134

135+
push!(x, "a")
136+
@test length(x) == 4
137+
@test x[end] == "a"
138+
@test levels(x) == ["e", "a", "b", "c"]
139+
140+
push!(x, "zz")
141+
@test length(x) == 5
142+
@test x[end] == "zz"
143+
@test levels(x) == ["e", "a", "b", "c", "zz"]
144+
145+
push!(x, x[1])
146+
@test length(x) == 6
147+
@test x[1] == x[end]
148+
@test levels(x) == ["e", "a", "b", "c", "zz"]
149+
150+
append!(x, x)
151+
@test length(x) == 12
152+
@test x == ["c", "a", "a", "a", "zz", "c", "c" ,"a", "a", "a", "zz", "c"]
153+
154+
b = ["z","y","x"]
155+
y = V{String, R}(b)
156+
append!(x, y)
157+
@test length(x) == 15
158+
@test x == ["c", "a", "a", "a", "zz", "c", "c" ,"a", "a", "a", "zz", "c", "z", "y", "x"]
159+
@test levels(x) == ["e", "a", "b", "c", "zz", "z", "y", "x"]
160+
161+
empty!(x)
162+
@test length(x) == 0
163+
@test levels(x) == ["e", "a", "b", "c", "zz", "z", "y", "x"]
135164

136165
# Vector created from range (i.e. non-Array AbstractArray),
137166
# direct conversion to a vector with different eltype
@@ -240,6 +269,30 @@ for (A, V, M) in ((NominalArray, NominalVector, NominalMatrix),
240269
@test x[4] === x.pool.valindex[4]
241270
@test levels(x) == vcat(unique(a), -1)
242271

272+
push!(x, 2.0)
273+
@test length(x) == 5
274+
@test x[end] == 2.0
275+
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]
276+
277+
push!(x, x[1])
278+
@test length(x) == 6
279+
@test x[1] == x[end]
280+
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]
281+
282+
append!(x, x)
283+
@test length(x) == 12
284+
@test x == [-1.0, -1.0, 1.0, 1.5, 2.0, -1.0, -1.0, -1.0, 1.0, 1.5, 2.0, -1.0]
285+
286+
b = [2.5, 3.0, -3.5]
287+
y = V{Float64, R}(b)
288+
append!(x, y)
289+
@test length(x) == 15
290+
@test x == [-1.0, -1.0, 1.0, 1.5, 2.0, -1.0, -1.0, -1.0, 1.0, 1.5, 2.0, -1.0, 2.5, 3.0, -3.5]
291+
@test levels(x) == [0.0,0.5,1.0,1.5,-1.0,2.0,2.5,3.0,-3.5]
292+
293+
empty!(x)
294+
@test length(x) == 0
295+
@test levels(x) == [0.0,0.5,1.0,1.5,-1.0,2.0,2.5,3.0,-3.5]
243296

244297
# Matrix
245298
a = ["a" "b" "c"; "b" "a" "c"]

test/12_nullablearray.jl

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,42 @@ for (A, V, M) in ((NullableNominalArray, NullableNominalVector, NullableNominalM
145145
@test x[2] === eltype(x)()
146146
@test x[3] === eltype(x)()
147147
@test levels(x) == ["e", "c"]
148+
149+
push!(x, "e")
150+
@test length(x) == 4
151+
@test isequal(x, NullableArray(["c", "", "", "e"], [false, true, true, false]))
152+
@test levels(x) == ["e", "c"]
153+
154+
push!(x, "zz")
155+
@test length(x) == 5
156+
@test isequal(x, NullableArray(["c", "", "", "e", "zz"], [false, true, true, false, false]))
157+
@test levels(x) == ["e", "c", "zz"]
158+
159+
push!(x, x[1])
160+
@test length(x) == 6
161+
@test isequal(x, NullableArray(["c", "", "", "e", "zz", "c"], [false, true, true, false, false, false]))
162+
@test levels(x) == ["e", "c", "zz"]
163+
164+
push!(x, eltype(x)())
165+
@test length(x) == 7
166+
@test isequal(x, NullableArray(["c", "", "", "e", "zz", "c", ""], [false, true, true, false, false, false, true]))
167+
@test isnull(x[end])
168+
@test levels(x) == ["e", "c", "zz"]
169+
170+
append!(x, x)
171+
@test isequal(x, NullableArray(["c", "", "", "e", "zz", "c", "", "c", "", "", "e", "zz", "c", ""], [false, true, true, false, false, false, true, false, true, true, false, false, false, true]))
172+
@test length(x) == 14
173+
174+
b = ["z","y","x"]
175+
y = V{String, R}(b)
176+
append!(x, y)
177+
@test length(x) == 17
178+
@test levels(x) == ["e", "c", "zz", "z", "y", "x"]
179+
@test isequal(x, NullableArray(["c", "", "", "e", "zz", "c", "", "c", "", "", "e", "zz", "c", "", "z", "y", "x"], [false, true, true, false, false, false, true, false, true, true, false, false, false, true, false, false, false]))
180+
181+
empty!(x)
182+
@test length(x) == 0
183+
@test levels(x) == ["e", "c", "zz", "z", "y", "x"]
148184
end
149185

150186

@@ -361,6 +397,30 @@ for (A, V, M) in ((NullableNominalArray, NullableNominalVector, NullableNominalM
361397
@test x[4] === Nullable(x.pool.valindex[4])
362398
@test levels(x) == vcat(unique(a), -1)
363399

400+
push!(x, 2.0)
401+
@test length(x) == 5
402+
@test isequal(x, NullableArray([-1.0, -1.0, 1.0, 1.5, 2.0]))
403+
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]
404+
405+
push!(x, x[1])
406+
@test length(x) == 6
407+
@test isequal(x, NullableArray([-1.0, -1.0, 1.0, 1.5, 2.0, -1.0]))
408+
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]
409+
410+
append!(x, x)
411+
@test length(x) == 12
412+
@test isequal(x, NullableArray([-1.0, -1.0, 1.0, 1.5, 2.0, -1.0, -1.0, -1.0, 1.0, 1.5, 2.0, -1.0]))
413+
414+
b = [2.5, 3.0, -3.5]
415+
y = V{Float64, R}(b)
416+
append!(x, y)
417+
@test length(x) == 15
418+
@test isequal(x, NullableArray([-1.0, -1.0, 1.0, 1.5, 2.0, -1.0, -1.0, -1.0, 1.0, 1.5, 2.0, -1.0, 2.5, 3.0, -3.5]))
419+
@test levels(x) == [0.0,0.5,1.0,1.5,-1.0,2.0,2.5,3.0,-3.5]
420+
421+
empty!(x)
422+
@test length(x) == 0
423+
@test levels(x) == [0.0,0.5,1.0,1.5,-1.0,2.0,2.5,3.0,-3.5]
364424

365425
# Matrix with no null values
366426
for a in (["a" "b" "c"; "b" "a" "c"],

0 commit comments

Comments
 (0)