Skip to content

Commit

Permalink
add docstring, Overflow check
Browse files Browse the repository at this point in the history
  • Loading branch information
ffreyer committed Aug 22, 2020
1 parent 15dd582 commit d9f7d27
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/log/binning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,18 @@ function _sum_type_heuristic(::Type{T}, elndims::Integer) where T
return S
end

function LogBinner(B::LogBinner{S, M}, capacity::Int64 = _nlvls2capacity(32)) where {S, M}
"""
LogBinner(B::LogBinner[; capacity::Int])
Creates a new `LogBinner` from an existing LogBinner, copying the data inside.
The new LogBinner may be larger or smaller than the given one.
"""
function LogBinner(B::LogBinner{S, M}; capacity::Int64 = _nlvls2capacity(32)) where {S, M}
N = _capacity2nlvls(capacity)
B.count[min(M, N)] > 0 && throw(OverflowError(
"The new LogBinner is too small to reconstruct the given LogBinner. " *
"New capacity = $capacity Old capacity = $(B.count[min(M, N)])"
))
el = zero(B.x_sum[1])

LogBinner{S, N}(
Expand Down
10 changes: 6 additions & 4 deletions test/logbinning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@test B == B2
@test !(B != B2)

B3 = LogBinner(B, 10_000)
B3 = LogBinner(B, capacity=10_000)
@test B3 == B
@test capacity(B3) == 16383

Expand All @@ -26,7 +26,8 @@

@test B3 != B
@test isempty(B3)
B3 = LogBinner(B, 10_000)
@test_throws OverflowError LogBinner(B, capacity=10)
B3 = LogBinner(B, capacity=10_000)
@test B3 == B
@test !isempty(B3)

Expand All @@ -53,7 +54,7 @@
@test B == B2
@test !(B != B2)

B3 = LogBinner(B, 10_000)
B3 = LogBinner(B, capacity=10_000)
@test B3 == B
@test capacity(B3) == 16383

Expand All @@ -66,7 +67,8 @@

@test B3 != B
@test isempty(B3)
B3 = LogBinner(B, 10_000)
@test_throws OverflowError LogBinner(B, capacity=10)
B3 = LogBinner(B, capacity=10_000)
@test B3 == B
@test !isempty(B3)

Expand Down

0 comments on commit d9f7d27

Please sign in to comment.