Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions include/boost/histogram/storage_adaptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ struct map_impl : T {
if (it != static_cast<T*>(map)->end()) {
it->second += u;
} else {
auto pair = map->emplace(idx, value_type{});
pair.first->second += u;
value_type tmp{};
tmp += u;
if (!(tmp == value_type{})) map->emplace(idx, tmp);
}
return *this;
}
Expand All @@ -184,8 +185,9 @@ struct map_impl : T {
if (it != static_cast<T*>(map)->end()) {
it->second -= u;
} else {
auto pair = map->emplace(idx, value_type{});
pair.first->second -= u;
value_type tmp{};
tmp -= u;
if (!(tmp == value_type{})) map->emplace(idx, tmp);
}
return *this;
}
Expand Down
17 changes: 17 additions & 0 deletions test/storage_adaptor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,23 @@ int main() {
BOOST_TEST(std::isnan(static_cast<double>(a[1])));
}

// adding or subtracting zero must not create cells in map-based storage_adaptor
{
using map_t = std::map<std::size_t, double>;
auto a = storage_adaptor<map_t>();
a.reset(4);
a[0] += 0;
a[1] -= 0;
BOOST_TEST_EQ(static_cast<const map_t&>(a).size(), 0);
BOOST_TEST_EQ(a[0], 0);
BOOST_TEST_EQ(a[1], 0);
a[2] += 1;
a[3] -= 1;
BOOST_TEST_EQ(static_cast<const map_t&>(a).size(), 2);
BOOST_TEST_EQ(a[2], 1);
BOOST_TEST_EQ(a[3], -1);
}

// with accumulators::weighted_sum
{
auto a = storage_adaptor<std::vector<accumulators::weighted_sum<double>>>();
Expand Down
Loading