Skip to content

Commit 0be8c7b

Browse files
committed
simpler
1 parent 924b000 commit 0be8c7b

File tree

8 files changed

+234
-190
lines changed

8 files changed

+234
-190
lines changed

include/boost/histogram/axis.hpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
#include <boost/version.hpp>
1616
#if BOOST_VERSION < 106100
1717
#include <boost/utility/string_ref.hpp>
18-
namespace boost { using string_view = string_ref; }
18+
namespace boost {
19+
using string_view = string_ref;
20+
}
1921
#else
2022
#include <boost/utility/string_view.hpp>
2123
#endif
@@ -38,10 +40,7 @@ namespace boost {
3840
namespace histogram {
3941
namespace axis {
4042

41-
enum {
42-
with_uoflow = true,
43-
without_uoflow = false
44-
};
43+
enum { with_uoflow = true, without_uoflow = false };
4544

4645
template <typename Value> struct bin {
4746
int idx;
@@ -308,7 +307,8 @@ class regular : public axis_base<with_uoflow>,
308307
* bins for this axis. Binning is a O(1) operation.
309308
*/
310309
template <typename RealType = double>
311-
class circular : public axis_base<without_uoflow>, boost::operators<regular<RealType>> {
310+
class circular : public axis_base<without_uoflow>,
311+
boost::operators<regular<RealType>> {
312312
public:
313313
using value_type = RealType;
314314
using const_iterator = axis_iterator<circular>;
@@ -323,7 +323,8 @@ class circular : public axis_base<without_uoflow>, boost::operators<regular<Real
323323
explicit circular(unsigned n, value_type phase = 0.0,
324324
value_type perimeter = math::double_constants::two_pi,
325325
string_view label = string_view())
326-
: axis_base<without_uoflow>(n, label), phase_(phase), perimeter_(perimeter) {}
326+
: axis_base<without_uoflow>(n, label), phase_(phase),
327+
perimeter_(perimeter) {}
327328

328329
circular() = default;
329330
circular(const circular &) = default;
@@ -369,7 +370,8 @@ class circular : public axis_base<without_uoflow>, boost::operators<regular<Real
369370
* and the problem domain allows it, prefer a regular.
370371
*/
371372
template <typename RealType = double>
372-
class variable : public axis_base<with_uoflow>, boost::operators<variable<RealType>> {
373+
class variable : public axis_base<with_uoflow>,
374+
boost::operators<variable<RealType>> {
373375
public:
374376
using value_type = RealType;
375377
using const_iterator = axis_iterator<variable>;
@@ -392,8 +394,8 @@ class variable : public axis_base<with_uoflow>, boost::operators<variable<RealTy
392394
}
393395

394396
template <typename Iterator>
395-
variable(Iterator begin, Iterator end,
396-
string_view label = string_view(), bool uoflow = true)
397+
variable(Iterator begin, Iterator end, string_view label = string_view(),
398+
bool uoflow = true)
397399
: axis_base<with_uoflow>(std::distance(begin, end) - 1, label, uoflow),
398400
x_(new value_type[std::distance(begin, end)]) {
399401
std::copy(begin, end, x_.get());
@@ -460,7 +462,8 @@ class variable : public axis_base<with_uoflow>, boost::operators<variable<RealTy
460462
* faster than a regular.
461463
*/
462464
template <typename IntType = int>
463-
class integer : public axis_base<with_uoflow>, boost::operators<integer<IntType>> {
465+
class integer : public axis_base<with_uoflow>,
466+
boost::operators<integer<IntType>> {
464467
public:
465468
using value_type = IntType;
466469
using const_iterator = axis_iterator<integer>;
@@ -470,8 +473,8 @@ class integer : public axis_base<with_uoflow>, boost::operators<integer<IntType>
470473
* \param min smallest integer of the covered range.
471474
* \param max largest integer of the covered range.
472475
*/
473-
integer(value_type min, value_type max,
474-
string_view label = string_view(), bool uoflow = true)
476+
integer(value_type min, value_type max, string_view label = string_view(),
477+
bool uoflow = true)
475478
: axis_base<with_uoflow>(max + 1 - min, label, uoflow), min_(min) {
476479
if (min > max) {
477480
throw std::logic_error("min <= max required");
@@ -525,8 +528,7 @@ class category : public axis_base<without_uoflow>, boost::operators<category> {
525528
using const_iterator = axis_iterator<category>;
526529

527530
template <typename Iterator>
528-
category(Iterator begin, Iterator end,
529-
string_view label = string_view())
531+
category(Iterator begin, Iterator end, string_view label = string_view())
530532
: axis_base<without_uoflow>(std::distance(begin, end), label),
531533
ptr_(new std::string[bins()]) {
532534
std::copy(begin, end, ptr_.get());
@@ -555,7 +557,8 @@ class category : public axis_base<without_uoflow>, boost::operators<category> {
555557
}
556558

557559
category(category &&other)
558-
: axis_base<without_uoflow>(std::move(other)), ptr_(std::move(other.ptr_)) {}
560+
: axis_base<without_uoflow>(std::move(other)),
561+
ptr_(std::move(other.ptr_)) {}
559562

560563
category &operator=(category &&other) {
561564
if (this != &other) {
@@ -598,8 +601,8 @@ class category : public axis_base<without_uoflow>, boost::operators<category> {
598601
} // namespace axis
599602

600603
using builtin_axes =
601-
mpl::vector<axis::regular<>, axis::circular<>,
602-
axis::variable<>, axis::integer<>, axis::category>;
604+
mpl::vector<axis::regular<>, axis::circular<>, axis::variable<>,
605+
axis::integer<>, axis::category>;
603606

604607
} // namespace histogram
605608
} // namespace boost

include/boost/histogram/detail/meta.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ template <typename Ns> std::vector<bool> bool_mask(unsigned n, bool v) {
6565
template <typename Axes, typename Ns> struct axes_assign_subset_op {
6666
const Axes &axes_;
6767
template <int N, typename R>
68-
auto operator()(mpl::int_<N>, R &r) const -> mpl::int_<N+1> {
68+
auto operator()(mpl::int_<N>, R &r) const -> mpl::int_<N + 1> {
6969
using I2 = typename mpl::at_c<Ns, N>::type;
7070
r = fusion::at_c<I2::value>(axes_);
7171
return {};
@@ -74,8 +74,7 @@ template <typename Axes, typename Ns> struct axes_assign_subset_op {
7474

7575
template <typename Ns, typename Axes1, typename Axes>
7676
void axes_assign_subset(Axes1 &axes1, const Axes &axes) {
77-
fusion::fold(axes1, mpl::int_<0>(),
78-
axes_assign_subset_op<Axes, Ns>{axes});
77+
fusion::fold(axes1, mpl::int_<0>(), axes_assign_subset_op<Axes, Ns>{axes});
7978
}
8079

8180
template <typename Ns>

include/boost/histogram/detail/utility.hpp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,33 @@ inline void escape(std::ostream &os, const String &s) {
2929
os << '\'';
3030
}
3131

32-
template <typename A, typename> struct lin {
33-
static inline void apply(std::size_t &out, std::size_t &stride, const A &a,
34-
int j) noexcept {
35-
// the following is highly optimized code that runs in a hot loop;
36-
// please measure the performance impact of changes
37-
const int uoflow = a.uoflow();
38-
// set stride to zero if 'j' is not in range,
39-
// this communicates the out-of-range condition to the caller
40-
stride *= (j >= -uoflow) & (j < (a.bins() + uoflow));
41-
j += (j < 0) * (a.bins() + 2); // wrap around if in < 0
42-
out += j * stride;
32+
template <typename A>
33+
inline void lin(std::size_t &out, std::size_t &stride, const A &a,
34+
int j) noexcept {
35+
// the following is highly optimized code that runs in a hot loop;
36+
// please measure the performance impact of changes
37+
const int uoflow = a.uoflow();
38+
// set stride to zero if 'j' is not in range,
39+
// this communicates the out-of-range condition to the caller
40+
stride *= (j >= -uoflow) & (j < (a.bins() + uoflow));
41+
j += (j < 0) * (a.bins() + 2); // wrap around if in < 0
42+
out += j * stride;
4343
#pragma GCC diagnostic ignored "-Wstrict-overflow"
44-
stride *= a.shape();
45-
}
46-
};
44+
stride *= a.shape();
45+
}
4746

48-
template <typename A, typename T> struct xlin {
49-
static inline void apply(std::size_t &out, std::size_t &stride, const A &a,
50-
typename call_traits<T>::param_type x) noexcept {
51-
// the following is highly optimized code that runs in a hot loop;
52-
// please measure the performance impact of changes
53-
int j = a.index(x);
54-
// j is guaranteed to be in range [-1, bins]
55-
j += (j < 0) * (a.bins() + 2); // wrap around if j < 0
56-
out += j * stride;
47+
template <typename A, typename X>
48+
inline void xlin(std::size_t &out, std::size_t &stride, const A &a,
49+
X &&x) noexcept {
50+
// the following is highly optimized code that runs in a hot loop;
51+
// please measure the performance impact of changes
52+
int j = a.index(std::forward<X>(x));
53+
// j is guaranteed to be in range [-1, bins]
54+
j += (j < 0) * (a.bins() + 2); // wrap around if j < 0
55+
out += j * stride;
5756
#pragma GCC diagnostic ignored "-Wstrict-overflow"
58-
stride *= (j < a.shape()) * a.shape(); // stride == 0 indicates out-of-range
59-
}
60-
};
57+
stride *= (j < a.shape()) * a.shape(); // stride == 0 indicates out-of-range
58+
}
6159

6260
struct index_mapper {
6361
std::size_t first = 0, second = 0;

include/boost/histogram/histogram_fwd.hpp

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
#define _BOOST_HISTOGRAM_HISTOGRAM_FWD_HPP_
99

1010
#include <boost/histogram/detail/meta.hpp>
11-
#include <boost/mpl/vector.hpp>
1211
#include <boost/mpl/int.hpp>
12+
#include <boost/mpl/vector.hpp>
13+
#include <initializer_list>
1314
#include <set>
1415
#include <type_traits>
15-
#include <initializer_list>
1616

1717
namespace boost {
1818
namespace histogram {
@@ -26,37 +26,30 @@ class adaptive_storage;
2626
template <class Variant, class Axes, class Storage = adaptive_storage<>>
2727
class histogram;
2828

29-
class weight {
30-
public:
31-
explicit weight(double v) : value(v) {}
32-
explicit operator double() const { return value; }
33-
34-
private:
29+
struct weight {
30+
weight(double w) : value(w) {}
3531
double value;
3632
};
3733

38-
class count {
39-
public:
40-
explicit count(unsigned v) : value(v) {}
41-
explicit operator unsigned() const { return value; }
42-
43-
private:
34+
struct count {
35+
count(unsigned n) : value(n) {}
4436
unsigned value;
4537
};
4638

4739
// for static and dynamic histogram
4840
template <int N, typename... Rest>
49-
inline auto keep(mpl::int_<N>, Rest...) -> detail::unique_sorted<mpl::vector<mpl::int_<N>, Rest...>> {
41+
inline auto keep(mpl::int_<N>, Rest...)
42+
-> detail::unique_sorted<mpl::vector<mpl::int_<N>, Rest...>> {
5043
return {};
5144
}
5245

5346
// for dynamic histogram only
5447
namespace detail {
5548
using keep_dynamic = std::set<unsigned>;
56-
inline void insert(keep_dynamic&) {} // end recursion
57-
template <typename First, typename... Rest>
58-
inline void insert(keep_dynamic& s, First f, Rest... rest) {
59-
s.insert(static_cast<unsigned>(f));
49+
inline void insert(keep_dynamic &) {} // end recursion
50+
template <typename... Rest>
51+
inline void insert(keep_dynamic &s, unsigned i, Rest... rest) {
52+
s.insert(i);
6053
insert(s, rest...);
6154
}
6255
} // namespace detail
@@ -67,9 +60,9 @@ inline detail::keep_dynamic keep(Iterator begin, Iterator end) {
6760
}
6861

6962
template <typename... Rest>
70-
inline detail::keep_dynamic keep(unsigned dim, Rest... rest) {
63+
inline detail::keep_dynamic keep(unsigned i, Rest... rest) {
7164
detail::keep_dynamic s;
72-
detail::insert(s, dim, rest...);
65+
detail::insert(s, i, rest...);
7366
return s;
7467
}
7568

0 commit comments

Comments
 (0)