Skip to content

Commit

Permalink
Merge pull request #17 from baggepinnen/master
Browse files Browse the repository at this point in the history
Fix bug in increment!(::History)
  • Loading branch information
Evizero committed Aug 2, 2018
2 parents 5b0ab4b + be12ce5 commit fd438b9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/history.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ end
Increments the value for a given iteration if it exists, otherwise adds the iteration with an ordinary push.
"""
function increment!(trace::History{I,V}, iter::Number, val) where {I,V}
if !isempty(trace.iterations) && (trace.lastiter == iter || iter trace.iterations) # First check is most common case and makes it faster
trace.values[trace.iterations[iter]] += val
else
push!(trace, iter, val)
if !isempty(trace.iterations)
if trace.lastiter == iter # Check most common case to make it faster
i = length(trace.iterations)
else
i = findfirst(trace.iterations, iter)
end
if i >= 1
return (trace.values[i] += val)
end
end
push!(trace, iter, val)
end
4 changes: 2 additions & 2 deletions test/tst_history.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ end
@testset "History: increment!" begin
_history = History(Float64)
val = 1.
@test increment!(_history, 1, val) == val
@test increment!(_history, 1, val) == 2val
@test increment!(_history, 0, val) == val
@test increment!(_history, 0, val) == 2val
@test increment!(_history, 2, 4val) == 4val
@test increment!(_history, 10, 5val) == 5val
_history2 = QHistory(Float64)
Expand Down

0 comments on commit fd438b9

Please sign in to comment.