Skip to content

Commit

Permalink
fix bug when vector empty
Browse files Browse the repository at this point in the history
  • Loading branch information
guimarqu committed Jan 5, 2020
1 parent 98150a5 commit b788ad6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/pma.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function PackedMemoryArray(keys::Vector{K}, values::Vector{T}; sort = true) wher
end

function PackedMemoryArray(::Type{K}, ::Type{T}) where {K,T} # empty pma
expected_nb_elems = 20
expected_nb_elems = 100
t_h, t_0, p_h, p_0 = 0.7, 0.92, 0.3, 0.08
capacity = 2^ceil(Int, log2(ceil(expected_nb_elems/t_h)))
array = Elements{K,T}(nothing, capacity)
Expand Down Expand Up @@ -177,7 +177,7 @@ function _look_for_rebalance!(pma::PackedMemoryArray, pos::Int)
if density > t
_extend!(pma)
end
if density < p
if density < p && pma.height > 1
# We must pack before shrinking otherwise we loose data
pack!(pma.array, 1, length(pma.array)/2, nb_cells)
_shrink!(pma)
Expand Down
1 change: 1 addition & 0 deletions test/functional/functionaltests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function functional_tests()
# Dynamic Sparse Vector (pma)
@testset "dynsparsevector (PackedMemoryArray) - func - simple use" begin
dynsparsevec_simple_use()
dynsparsevec_fill_empty()
end
@testset "Insertions & finds in dyn sparse vector - performance" begin
dynsparsevec_insertions_and_gets()
Expand Down
33 changes: 33 additions & 0 deletions test/functional/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,39 @@ function dynsparsevec_simple_use()
return
end

function fill(vec, kv)
n = 0
for (k, v) in kv
vec[k] = v
n += 1
@test vec.nb_elements == n
end
return
end

function empty(vec, kv)
n = length(kv)
for (k, v) in kv
vec[k] = 0.0
n -= 1
@test vec.nb_elements == n
end
return
end

function dynsparsevec_fill_empty()
# Start with an empty sparse vector
vec = dynamicsparsevec(Int[], Float64[])
for n in [20, 100, 1000, 10000, 100000]
kv = Dict{Int, Float64}(
rand(rng, 1:10000000000) => rand(rng, 1:0.1:10000) for i in 1:n
)
fill(vec, kv)
empty(vec, kv)
end
return
end

function dynsparsevec_insertions_and_gets()
kv1 = Dict{Int, Float64}(
rand(rng, 1:10000000000) => rand(rng, 1:0.1:10000) for i in 1:1000000
Expand Down

0 comments on commit b788ad6

Please sign in to comment.