Skip to content

Commit

Permalink
Add resize!()
Browse files Browse the repository at this point in the history
Also fix too broad signature for append!() and push!(), and introduce a new
CatVector alias.
  • Loading branch information
nalimilan committed Feb 25, 2017
1 parent 388575a commit 74bf6cb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -607,13 +607,22 @@ using the ordering of levels. Return the modified `A`.
"""
ordered!(A::CatArray, ordered) = (ordered!(A.pool, ordered); return A)

function Base.push!(A::CatArray, item)
function Base.resize!(A::CatVector, n::Integer)
n_orig = length(A)
resize!(A.refs, n)
if n > n_orig
A.refs[n_orig+1:end] = 0
end
A
end

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

function Base.append!(A::CatArray, B::CatArray)
function Base.append!(A::CatVector, B::CatArray)
levels!(A, union(levels(A), levels(B)))
len = length(A.refs)
len2 = length(B.refs)
Expand Down
1 change: 1 addition & 0 deletions src/typedefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,4 @@ end
## Type Aliases

@compat CatArray{T, N, R} = Union{CategoricalArray{T, N, R}, NullableCategoricalArray{T, N, R}}
@compat CatVector{T, R} = Union{CategoricalVector{T, R}, NullableCategoricalVector{T, R}}
12 changes: 12 additions & 0 deletions test/13_arraycommon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ for (CA, A) in ((CategoricalArray, Array), (NullableCategoricalArray, NullableAr
@test_throws BoundsError copy!(x, 1, y, 4, 2)
@test x == a

# Test resize!()
x = CA(["Old", "Young", "Middle", "Young"])
@test resize!(x, 3) === x
@test x == A(["Old", "Young", "Middle"])
@test resize!(x, 4) === x
if CA === NullableCategoricalArray
@test x == A(["Old", "Young", "Middle", Nullable()])
else
@test x[1:3] == A(["Old", "Young", "Middle"])
@test !isassigned(x, 4)
end

for (sstart, dstart, n) in ((1, 1, 4), (1, 2, 3))
# Conflicting orders: check that the destination wins and that result is not ordered
x = CA(["Old", "Young", "Middle", "Young"])
Expand Down

0 comments on commit 74bf6cb

Please sign in to comment.