Skip to content

Commit

Permalink
Merge pull request #59 from crstnbr/reconstruction
Browse files Browse the repository at this point in the history
Reconstruct LogBinner from LogBinner
  • Loading branch information
ffreyer authored Aug 22, 2020
2 parents c21112c + d9f7d27 commit efc98fd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/log/binning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,6 @@ function LogBinner(x::T;
return B
end



function _sum_type_heuristic(::Type{T}, elndims::Integer) where T
# heuristic to set sum type (#2)
S = if eltype(T)<:Real
Expand All @@ -216,6 +214,29 @@ function _sum_type_heuristic(::Type{T}, elndims::Integer) where T
return S
end

"""
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}(
tuple([i > M ? Compressor{S}(copy(el), false) : deepcopy(B.compressors[i]) for i in 1:N]...),
[i > M ? copy(el) : copy(B.x_sum[i]) for i in 1:N],
[i > M ? copy(el) : copy(B.x2_sum[i]) for i in 1:N],
[i > M ? 0 : copy(B.count[i]) for i in 1:N]
)
end



# TODO typing?
"""
Expand Down
22 changes: 22 additions & 0 deletions test/logbinning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@
@test B == B2
@test !(B != B2)

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

buffer = rand(1000)
append!(B, buffer)
@test length(B) == 1000
@test !isempty(B)
@test !(B == B2)
@test B != B2

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

append!(B2, buffer)
@test B == B2
@test !(B != B2)
Expand All @@ -43,13 +54,24 @@
@test B == B2
@test !(B != B2)

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

buffer = [rand(T, 2, 3) for _ in 1:1000]
append!(B, buffer)
@test length(B) == 1000
@test !isempty(B)
@test !(B == B2)
@test B != B2

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

append!(B2, buffer)
@test B == B2
@test !(B != B2)
Expand Down

0 comments on commit efc98fd

Please sign in to comment.