diff --git a/src/log/binning.jl b/src/log/binning.jl index 1310635..4d759bf 100644 --- a/src/log/binning.jl +++ b/src/log/binning.jl @@ -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 @@ -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? """ diff --git a/test/logbinning.jl b/test/logbinning.jl index cb600d1..0ba8f45 100644 --- a/test/logbinning.jl +++ b/test/logbinning.jl @@ -13,6 +13,10 @@ @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 @@ -20,6 +24,13 @@ @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) @@ -43,6 +54,10 @@ @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 @@ -50,6 +65,13 @@ @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)