Skip to content

Commit

Permalink
fix one-to-many map: fixes JuliaData/JuliaDB.jl#193
Browse files Browse the repository at this point in the history
  • Loading branch information
Shashi Gowda committed Jun 20, 2018
1 parent 41dfeab commit 02dbbdd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/PooledArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,28 @@ Base.find(pdv::PooledVector{Bool}) = find(convert(Vector{Bool}, pdv, false))
function Base.map{T,R<:Integer}(f, x::PooledArray{T,R})
ks = collect(keys(x.pool))
vs = collect(values(x.pool))
newpool = Dict(zip(map(f, ks), vs))
PooledArray(RefArray(x.refs), newpool)
ks1 = map(f, ks)
uks = Set(ks1)
if length(uks) < length(ks1)
# this means some keys have repeated
newpool = Dict{eltype(ks), eltype(vs)}()
translate = Dict{eltype(vs), eltype(vs)}()
i = 1
for (k, k1) in zip(ks, ks1)
if haskey(newpool, k1)
translate[x.pool[k]] = newpool[k1]
else
newpool[k1] = i
translate[x.pool[k]] = i
i+=1
end
end
refarray = map(x->translate[x], x.refs)
else
newpool = Dict(zip(map(f, ks), vs))
refarray = copy(x.refs)
end
PooledArray(RefArray(refarray), newpool)
end

##############################################################################
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ let a = rand(10), b = rand(10,10), c = rand(1:10, 1000)
@test map(identity, pc) == pc
@test map(x->2x, pc) == map(x->2x, c)

# case where the outputs are one-to-many
pa = PooledArray([1,2,3,4,5,6])
@test map(isodd, pa) == [1,0,1,0,1,0]

px = PooledArray(rand(128))
py = PooledArray(rand(200))

Expand Down

0 comments on commit 02dbbdd

Please sign in to comment.