Skip to content

Commit e3d8766

Browse files
committed
fix: use std::all_of in sparse empty/equal and skip huge-axis test on 32-bit
Replace manual early-return loops in histogram_empty and histogram_equal with std::all_of to satisfy the readability-use-anyofallof clang-tidy rule. Skip test_huge_axis_ops_iterate_filled_cells_only on 32-bit Python: a 100000^3 histogram has ~10^15 cells, overflowing 32-bit std::size_t linear keys, so is_inner_cell misclassifies filled cells on those platforms. Assisted-by: ClaudeCode:claude-sonnet-4-6
1 parent ef75137 commit e3d8766

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

include/bh_python/histogram.hpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <boost/histogram/histogram.hpp>
2121
#include <boost/histogram/unsafe_access.hpp>
2222

23+
#include <algorithm>
2324
#include <unordered_map>
2425
#include <vector>
2526

@@ -320,18 +321,14 @@ bool histogram_empty(const bh::histogram<A, storage::sparse_storage<T>>& h, bool
320321
using map_type = std::unordered_map<std::size_t, T>;
321322
const auto& map = static_cast<const map_type&>(bh::unsafe_access::storage(h));
322323

323-
if(flow) {
324-
for(const auto& kv : map)
325-
if(kv.second != T{})
326-
return false;
327-
return true;
328-
}
324+
if(flow)
325+
return std::all_of(
326+
map.begin(), map.end(), [](const auto& kv) { return kv.second == T{}; });
329327

330328
const auto layout = detail::make_axis_layout(bh::unsafe_access::axes(h));
331-
for(const auto& kv : map)
332-
if(kv.second != T{} && detail::is_inner_cell(kv.first, layout))
333-
return false;
334-
return true;
329+
return std::all_of(map.begin(), map.end(), [&layout](const auto& kv) {
330+
return kv.second == T{} || !detail::is_inner_cell(kv.first, layout);
331+
});
335332
}
336333

337334
template <class A, class S>
@@ -358,10 +355,9 @@ bool histogram_equal(const bh::histogram<A, storage::sparse_storage<T>>& a,
358355
if(kv.second != (it == mb.end() ? T{} : it->second))
359356
return false;
360357
}
361-
for(const auto& kv : mb)
362-
if(kv.second != T{} && ma.find(kv.first) == ma.end())
363-
return false;
364-
return true;
358+
return std::all_of(mb.begin(), mb.end(), [&ma](const auto& kv) {
359+
return kv.second == T{} || ma.find(kv.first) != ma.end();
360+
});
365361
}
366362

367363
/// Compute the bin of an array from a runtime list

tests/test_sparse_storage.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import copy
44
import json
55
import operator
6+
import sys
67
import warnings
78
from pickle import dumps, loads
89

@@ -134,6 +135,7 @@ def test_repr_and_str_do_not_densify():
134135
assert "DoubleSparse" in str(h)
135136

136137

138+
@pytest.mark.skipif(sys.maxsize <= 2**32, reason="linear keys overflow on 32-bit")
137139
def test_huge_axis_ops_iterate_filled_cells_only():
138140
# sum/empty/== (and repr, which calls sum) must iterate the filled cells,
139141
# not the logical grid -- a dense iteration over 10**15 cells would never

0 commit comments

Comments
 (0)