Skip to content

Commit 40add38

Browse files
authored
fix: support sparse storage with accumulators (#421)
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
1 parent 181a3a0 commit 40add38

4 files changed

Lines changed: 37 additions & 2 deletions

File tree

include/boost/histogram/storage_adaptor.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ struct map_impl : T {
171171
if (it != static_cast<T*>(map)->end()) {
172172
it->second += u;
173173
} else {
174-
map->emplace(idx, u);
174+
auto pair = map->emplace(idx, value_type{});
175+
pair.first->second += u;
175176
}
176177
return *this;
177178
}
@@ -183,7 +184,8 @@ struct map_impl : T {
183184
if (it != static_cast<T*>(map)->end()) {
184185
it->second -= u;
185186
} else {
186-
map->emplace(idx, -u);
187+
auto pair = map->emplace(idx, value_type{});
188+
pair.first->second -= u;
187189
}
188190
return *this;
189191
}

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ boost_test(TYPE run SOURCES unlimited_storage_test.cpp)
102102
boost_test(TYPE run SOURCES tools_test.cpp)
103103
boost_test(TYPE run SOURCES issue_327_test.cpp)
104104
boost_test(TYPE run SOURCES issue_353_test.cpp)
105+
boost_test(TYPE run SOURCES issue_416_test.cpp)
105106
boost_test(TYPE run SOURCES accumulators_fraction_test.cpp)
106107
boost_test(TYPE run SOURCES utility_binomial_proportion_interval_test.cpp)
107108
boost_test(TYPE run SOURCES utility_wald_interval_test.cpp)

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ alias cxx14 :
104104
[ run tools_test.cpp ]
105105
[ run issue_327_test.cpp ]
106106
[ run issue_353_test.cpp ]
107+
[ run issue_416_test.cpp ]
107108
[ run histogram_ostream_test.cpp : : :
108109
<testing.launcher>"set LANG=UTF" ]
109110
[ run histogram_ostream_ascii_test.cpp : : :

test/issue_416_test.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2026 Hans Dembinski
2+
//
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt
5+
// or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#include <boost/core/lightweight_test.hpp>
8+
#include <boost/histogram.hpp>
9+
#include <boost/histogram/algorithm/sum.hpp>
10+
#include <unordered_map>
11+
#include <vector>
12+
#include "throw_exception.hpp"
13+
14+
namespace bh = boost::histogram;
15+
16+
int main() {
17+
using cell_t = bh::accumulators::weighted_sum<double>;
18+
19+
auto h = bh::make_histogram_with(std::unordered_map<std::size_t, cell_t>(),
20+
bh::axis::regular<>(10, 0.0, 1.0));
21+
22+
const std::vector<double> values = {0.2, 0.3};
23+
const std::vector<double> weights = {1.0, 1.0};
24+
h.fill(values, bh::weight(weights));
25+
26+
const auto s = bh::algorithm::sum(h);
27+
BOOST_TEST_EQ(s.value(), 2.0);
28+
BOOST_TEST_EQ(s.variance(), 2.0);
29+
30+
return boost::report_errors();
31+
}

0 commit comments

Comments
 (0)