From 15dd582aad98f761b90530e7cbbdc463ed310e9e Mon Sep 17 00:00:00 2001 From: F Freyer Date: Sat, 22 Aug 2020 15:21:57 +0200 Subject: [PATCH 1/2] add LogBinner(LogBinner) --- src/log/binning.jl | 15 +++++++++++++-- test/logbinning.jl | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/log/binning.jl b/src/log/binning.jl index 1310635..0194f8b 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,19 @@ 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} + N = _capacity2nlvls(capacity) + 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..71d8fc5 100644 --- a/test/logbinning.jl +++ b/test/logbinning.jl @@ -13,6 +13,10 @@ @test B == B2 @test !(B != B2) + B3 = LogBinner(B, 10_000) + @test B3 == B + @test capacity(B3) == 16383 + buffer = rand(1000) append!(B, buffer) @test length(B) == 1000 @@ -20,6 +24,12 @@ @test !(B == B2) @test B != B2 + @test B3 != B + @test isempty(B3) + B3 = LogBinner(B, 10_000) + @test B3 == B + @test !isempty(B3) + append!(B2, buffer) @test B == B2 @test !(B != B2) @@ -43,6 +53,10 @@ @test B == B2 @test !(B != B2) + B3 = LogBinner(B, 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 +64,12 @@ @test !(B == B2) @test B != B2 + @test B3 != B + @test isempty(B3) + B3 = LogBinner(B, 10_000) + @test B3 == B + @test !isempty(B3) + append!(B2, buffer) @test B == B2 @test !(B != B2) From d9f7d275773c434fb3a665187e2e76e58e110332 Mon Sep 17 00:00:00 2001 From: F Freyer Date: Sat, 22 Aug 2020 15:38:58 +0200 Subject: [PATCH 2/2] add docstring, Overflow check --- src/log/binning.jl | 12 +++++++++++- test/logbinning.jl | 10 ++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/log/binning.jl b/src/log/binning.jl index 0194f8b..4d759bf 100644 --- a/src/log/binning.jl +++ b/src/log/binning.jl @@ -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}( diff --git a/test/logbinning.jl b/test/logbinning.jl index 71d8fc5..0ba8f45 100644 --- a/test/logbinning.jl +++ b/test/logbinning.jl @@ -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 @@ -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) @@ -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 @@ -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)