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

Allow PooledDataVector construction using a given pool. #52

Merged
merged 2 commits into from
Aug 13, 2012
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
39 changes: 39 additions & 0 deletions src/datavec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,45 @@ function PooledDataVec{T}(d::Vector{T}, m::Vector{Bool}, f::Bool, r::Bool, v::T)
end
PooledDataVec(newrefs, newpool, f, r, v)
end

# Allow a pool to be provided
function PooledDataVec{T}(d::Vector{T}, pool::Vector{T}, m::Vector{Bool}, f::Bool, r::Bool, v::T)
newrefs = Array(Uint16, length(d))
poolref = Dict{T,Uint16}(0)
maxref = 0

# loop through once to fill the poolref dict
for i = 1:length(pool)
if !m[i]
poolref[pool[i]] = 0
end
end

# fill positions in poolref
newpool = sort(keys(poolref))
i = 1
for p in newpool
poolref[p] = i
i += 1
end

# fill in newrefs
for i = 1:length(d)
if m[i]
newrefs[i] = 0
else
if has(poolref, d[i])
newrefs[i] = poolref[d[i]]
else
error("vector contains elements not in provided pool")
end
end
end
PooledDataVec(newrefs, newpool, f, r, v)
end

PooledDataVec{T}(d::Vector{T}, pool::Vector{T}) = PooledDataVec(d, pool, falses(length(pool)), false, false, zero(T))

PooledDataVec(dv::DataVec) = PooledDataVec(dv.data, dv.na, dv.filter, dv.replace, dv.replaceVal)
PooledDataVec(d::PooledDataVec) = d

Expand Down
10 changes: 10 additions & 0 deletions test/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ pdvstr = PooledDataVec["one", "one", "two", "two", NA, "one", "one"]
@test throws_exception(PooledDataVec["one", "one", 9], Exception)
@test PooledDataVec(pdvstr) == pdvstr

test_group("PooledDataVec creation with predetermined pool")
pdvpp = PooledDataVec([1,2,2,3], [1,2,3,4])
@test pdvpp.pool == [1,2,3,4]
@test string(pdvpp) == "[1,2,2,3]"
@test throws_exception(PooledDataVec([1,2,3], [1,2]), Exception)
pdvpp = PooledDataVec(["one","two","two"], ["one","two","three"])
@test pdvpp.pool == ["one","three","two"]
@test string(pdvpp) == "[\"one\",\"two\",\"two\"]"
@test throws_exception(PooledDataVec(["one","two","four"], ["one","two","three"]), Exception)

test_group("DataVec access")
@test dvint[1] == 1
@test isna(dvint[3])
Expand Down