Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jq/pushappend #9

Merged
merged 3 commits into from
Aug 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test that levels are preserved.

@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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remarks for this series of tests.

@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