Skip to content

Commit

Permalink
Add push!(), append!(), and empty!() methods (#9)
Browse files Browse the repository at this point in the history
Needed by DataStreams.jl. This uncovered a bug in _levels!() when the input contained missing values.
  • Loading branch information
quinnj authored and nalimilan committed Aug 30, 2016
1 parent f0fa6e8 commit 7aa1dea
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ function _levels!(A::CatOrdArray, newlevels::Vector; nullok=false)
levelsmap = indexin(oldindex, index(A.pool))

@inbounds for (i, x) in enumerate(A.refs)
j = levelsmap[x]
x > 0 && (A.refs[i] = j)
x > 0 && (A.refs[i] = levelsmap[x])
end
end

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

levels!(A::CatOrdArray, newlevels::Vector) = _levels!(A, newlevels)

function Base.push!(A::CatOrdArray, item)
resize!(A.refs, length(A.refs) + 1)
A[end] = item
return A
end

function Base.append!(A::CatOrdArray, B::CatOrdArray)
levels!(A, union(levels(A), levels(B)))
len = length(A.refs)
len2 = length(B.refs)
resize!(A.refs, len + length(B.refs))
for i = 1:len2
A[len + i] = B[i]
end
return A
end

Base.empty!(A::CatOrdArray) = (empty!(A.refs); return A)
53 changes: 53 additions & 0 deletions test/11_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,35 @@ for (A, V, M) in ((NominalArray, NominalVector, NominalMatrix),
@test x[3] === x.pool.valindex[1]
@test levels(x) == ["e", "a", "b", "c"]

push!(x, "a")
@test length(x) == 4
@test x[end] == "a"
@test levels(x) == ["e", "a", "b", "c"]

push!(x, "zz")
@test length(x) == 5
@test x[end] == "zz"
@test levels(x) == ["e", "a", "b", "c", "zz"]

push!(x, x[1])
@test length(x) == 6
@test x[1] == x[end]
@test levels(x) == ["e", "a", "b", "c", "zz"]

append!(x, x)
@test length(x) == 12
@test x == ["c", "a", "a", "a", "zz", "c", "c" ,"a", "a", "a", "zz", "c"]

b = ["z","y","x"]
y = V{String, R}(b)
append!(x, y)
@test length(x) == 15
@test x == ["c", "a", "a", "a", "zz", "c", "c" ,"a", "a", "a", "zz", "c", "z", "y", "x"]
@test levels(x) == ["e", "a", "b", "c", "zz", "z", "y", "x"]

empty!(x)
@test length(x) == 0
@test levels(x) == ["e", "a", "b", "c", "zz", "z", "y", "x"]

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

push!(x, 2.0)
@test length(x) == 5
@test x[end] == 2.0
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]

push!(x, x[1])
@test length(x) == 6
@test x[1] == x[end]
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]

append!(x, x)
@test length(x) == 12
@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]

b = [2.5, 3.0, -3.5]
y = V{Float64, R}(b)
append!(x, y)
@test length(x) == 15
@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]
@test levels(x) == [0.0,0.5,1.0,1.5,-1.0,2.0,2.5,3.0,-3.5]

empty!(x)
@test length(x) == 0
@test levels(x) == [0.0,0.5,1.0,1.5,-1.0,2.0,2.5,3.0,-3.5]

# Matrix
a = ["a" "b" "c"; "b" "a" "c"]
Expand Down
60 changes: 60 additions & 0 deletions test/12_nullablearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,42 @@ for (A, V, M) in ((NullableNominalArray, NullableNominalVector, NullableNominalM
@test x[2] === eltype(x)()
@test x[3] === eltype(x)()
@test levels(x) == ["e", "c"]

push!(x, "e")
@test length(x) == 4
@test isequal(x, NullableArray(["c", "", "", "e"], [false, true, true, false]))
@test levels(x) == ["e", "c"]

push!(x, "zz")
@test length(x) == 5
@test isequal(x, NullableArray(["c", "", "", "e", "zz"], [false, true, true, false, false]))
@test levels(x) == ["e", "c", "zz"]

push!(x, x[1])
@test length(x) == 6
@test isequal(x, NullableArray(["c", "", "", "e", "zz", "c"], [false, true, true, false, false, false]))
@test levels(x) == ["e", "c", "zz"]

push!(x, eltype(x)())
@test length(x) == 7
@test isequal(x, NullableArray(["c", "", "", "e", "zz", "c", ""], [false, true, true, false, false, false, true]))
@test isnull(x[end])
@test levels(x) == ["e", "c", "zz"]

append!(x, x)
@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]))
@test length(x) == 14

b = ["z","y","x"]
y = V{String, R}(b)
append!(x, y)
@test length(x) == 17
@test levels(x) == ["e", "c", "zz", "z", "y", "x"]
@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]))

empty!(x)
@test length(x) == 0
@test levels(x) == ["e", "c", "zz", "z", "y", "x"]
end


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

push!(x, 2.0)
@test length(x) == 5
@test isequal(x, NullableArray([-1.0, -1.0, 1.0, 1.5, 2.0]))
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]

push!(x, x[1])
@test length(x) == 6
@test isequal(x, NullableArray([-1.0, -1.0, 1.0, 1.5, 2.0, -1.0]))
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]

append!(x, x)
@test length(x) == 12
@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]))

b = [2.5, 3.0, -3.5]
y = V{Float64, R}(b)
append!(x, y)
@test length(x) == 15
@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]))
@test levels(x) == [0.0,0.5,1.0,1.5,-1.0,2.0,2.5,3.0,-3.5]

empty!(x)
@test length(x) == 0
@test levels(x) == [0.0,0.5,1.0,1.5,-1.0,2.0,2.5,3.0,-3.5]

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

0 comments on commit 7aa1dea

Please sign in to comment.