diff --git a/appveyor.yml b/.appveyor.yml similarity index 100% rename from appveyor.yml rename to .appveyor.yml diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..1a5cac0c --- /dev/null +++ b/.clang-format @@ -0,0 +1,7 @@ +--- +Language: Cpp +BasedOnStyle: Chromium +SpacesBeforeTrailingComments: 1 +DerivePointerAlignment: false +PointerAlignment: Left +... diff --git a/build/CMakeLists.txt b/build/CMakeLists.txt index 7a3560e0..029b681b 100644 --- a/build/CMakeLists.txt +++ b/build/CMakeLists.txt @@ -191,12 +191,6 @@ file(GLOB_RECURSE ../test/*_test.cpp ../include/*.hpp ) -add_custom_target(clf - COMMAND clang-format - -i - ${ALL_SOURCE_FILES} - ) - get_property(INCLUDE_DIRS DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES) set(TIDY_INCLUDE) foreach(x ${INCLUDE_DIRS}) diff --git a/include/boost/histogram/arithmetic_operators.hpp b/include/boost/histogram/arithmetic_operators.hpp index aed12d42..f0913872 100644 --- a/include/boost/histogram/arithmetic_operators.hpp +++ b/include/boost/histogram/arithmetic_operators.hpp @@ -13,67 +13,67 @@ namespace boost { namespace histogram { template -histogram &&operator+(histogram &&a, - const histogram &b) { +histogram&& operator+(histogram&& a, + const histogram& b) { a += b; return std::move(a); } template -histogram &&operator+(histogram &&a, histogram &&b) { +histogram&& operator+(histogram&& a, histogram&& b) { a += b; return std::move(a); } template -histogram &&operator+(const histogram &a, - histogram &&b) { +histogram&& operator+(const histogram& a, + histogram&& b) { b += a; return std::move(b); } template -histogram operator+(const histogram &a, - const histogram &b) { +histogram operator+(const histogram& a, + const histogram& b) { histogram r(a); r += b; return r; } template -histogram &&operator*(histogram &&a, const double x) { +histogram&& operator*(histogram&& a, const double x) { a *= x; return std::move(a); } template -histogram &&operator*(const double x, histogram &&b) { +histogram&& operator*(const double x, histogram&& b) { b *= x; return std::move(b); } template -histogram operator*(const histogram &a, const double x) { +histogram operator*(const histogram& a, const double x) { auto r = a; r *= x; return r; } template -histogram operator*(const double x, const histogram &b) { +histogram operator*(const double x, const histogram& b) { auto r = b; r *= x; return r; } template -histogram &&operator/(histogram &&a, const double x) { +histogram&& operator/(histogram&& a, const double x) { a /= x; return std::move(a); } template -histogram operator/(const histogram &a, const double x) { +histogram operator/(const histogram& a, const double x) { auto r = a; r /= x; return r; diff --git a/include/boost/histogram/axis/any.hpp b/include/boost/histogram/axis/any.hpp index a4082c70..643a6e24 100644 --- a/include/boost/histogram/axis/any.hpp +++ b/include/boost/histogram/axis/any.hpp @@ -39,7 +39,8 @@ struct offset_visitor : public boost::static_visitor { const char* ptr; template offset_visitor(const T* t) : ptr(reinterpret_cast(t)) {} - template int operator()(const Axis& a) const { + template + int operator()(const Axis& a) const { return reinterpret_cast(&a) - ptr; } }; @@ -47,13 +48,16 @@ struct offset_visitor : public boost::static_visitor { struct index_visitor : public boost::static_visitor { const double x; explicit index_visitor(const double arg) : x(arg) {} - template int operator()(const Axis &a) const { + template + int operator()(const Axis& a) const { return impl(std::is_convertible(), a); } - template int impl(std::true_type, const Axis &a) const { + template + int impl(std::true_type, const Axis& a) const { return a.index(static_cast(x)); } - template int impl(std::false_type, const Axis &) const { + template + int impl(std::false_type, const Axis&) const { throw std::runtime_error(boost::histogram::detail::cat( "cannot convert double to value_type ", boost::typeindex::type_id().pretty_name(), @@ -64,7 +68,8 @@ struct index_visitor : public boost::static_visitor { struct lower_visitor : public boost::static_visitor { int idx; lower_visitor(int i) : idx(i) {} - template double operator()(const Axis &a) const { + template + double operator()(const Axis& a) const { return impl( std::integral_constant< bool, @@ -73,10 +78,12 @@ struct lower_visitor : public boost::static_visitor { axis::interval_view>::value)>(), a); } - template double impl(std::true_type, const Axis &a) const { + template + double impl(std::true_type, const Axis& a) const { return a.lower(idx); } - template double impl(std::false_type, const Axis &) const { + template + double impl(std::false_type, const Axis&) const { throw std::runtime_error(boost::histogram::detail::cat( "cannot use ", boost::typeindex::type_id().pretty_name(), " with generic boost::histogram::axis::any interface, use" @@ -86,26 +93,32 @@ struct lower_visitor : public boost::static_visitor { struct bicmp_visitor : public boost::static_visitor { template - bool operator()(const T &, const U &) const { + bool operator()(const T&, const U&) const { return false; } - template bool operator()(const T &a, const T &b) const { + template + bool operator()(const T& a, const T& b) const { return a == b; } }; template struct assign_visitor : public boost::static_visitor { - T &t; - assign_visitor(T &tt) : t(tt) {} - template void operator()(const U &u) const { + T& t; + assign_visitor(T& tt) : t(tt) {} + template + void operator()(const U& u) const { impl(mp11::mp_contains(), u); } - template void impl(mp11::mp_true, const U &u) const { t = u; } + template + void impl(mp11::mp_true, const U& u) const { + t = u; + } - template void impl(mp11::mp_false, const U &) const { + template + void impl(mp11::mp_false, const U&) const { throw std::invalid_argument(boost::histogram::detail::cat( "argument ", boost::typeindex::type_id().pretty_name(), " is not a bounded type of ", @@ -116,61 +129,66 @@ struct assign_visitor : public boost::static_visitor { } // namespace detail /// Polymorphic axis type -template class any : public boost::variant { +template +class any : public boost::variant { static_assert(boost::histogram::detail::is_common_base::value, "all bounded types of axis::any must derive from axis::base"); -public: + public: using types = mp11::mp_list; using value_type = double; using bin_type = interval_view; using const_iterator = iterator_over; using const_reverse_iterator = reverse_iterator_over; -private: + private: using base_type = boost::variant; template using requires_bounded_type = mp11::mp_if< - mp11::mp_contains>, void>; + mp11::mp_contains>, + void>; - const base &cast_to_base() const { + const base& cast_to_base() const { // evil implementation until proper support can be added to boost::variant if (offset_ == -1) { offset_ = boost::apply_visitor(detail::offset_visitor(this), *this); } - return *reinterpret_cast(reinterpret_cast(this) + offset_); + return *reinterpret_cast(reinterpret_cast(this) + + offset_); } - base &cast_to_base() { + base& cast_to_base() { // evil implementation until proper support can be added to boost::variant if (offset_ == -1) { offset_ = boost::apply_visitor(detail::offset_visitor(this), *this); } - return *reinterpret_cast(reinterpret_cast(this) + offset_); + return *reinterpret_cast(reinterpret_cast(this) + offset_); } -public: + public: any() = default; - any(const any &) = default; - any &operator=(const any &) = default; - any(any &&) = default; - any &operator=(any &&) = default; + any(const any&) = default; + any& operator=(const any&) = default; + any(any&&) = default; + any& operator=(any&&) = default; template > - any(T &&t) : base_type(std::forward(t)) {} + any(T&& t) : base_type(std::forward(t)) {} template > - any &operator=(T &&t) { + any& operator=(T&& t) { base_type::operator=(std::forward(t)); return *this; } - template any(const any &u) { + template + any(const any& u) { boost::apply_visitor(detail::assign_visitor(*this), u); } - template any &operator=(const any &u) { + template + any& operator=(const any& u) { boost::apply_visitor(detail::assign_visitor(*this), u); return *this; } @@ -198,22 +216,24 @@ public: bin_type operator[](const int idx) const { return bin_type(idx, *this); } - bool operator==(const any &rhs) const { - return base_type::operator==(static_cast(rhs)); + bool operator==(const any& rhs) const { + return base_type::operator==(static_cast(rhs)); } - template bool operator==(const any &u) const { + template + bool operator==(const any& u) const { return boost::apply_visitor(detail::bicmp_visitor(), *this, u); } template > - bool operator==(const T &t) const { + bool operator==(const T& t) const { // variant::operator==(T) is implemented, but only to fail, cannot use it auto tp = boost::get>(this); return tp && *tp == t; } - template bool operator!=(T &&t) const { + template + bool operator!=(T&& t) const { return !operator==(std::forward(t)); } @@ -226,37 +246,43 @@ public: return const_reverse_iterator(*this, 0); } -private: + private: mutable int offset_ = -1; friend class boost::serialization::access; - template void serialize(Archive &, unsigned); + template + void serialize(Archive&, unsigned); }; // dynamic casts -template T &&cast(any &&x) { +template +T&& cast(any&& x) { return boost::strict_get(x); } -template T &cast(any &x) { +template +T& cast(any& x) { return boost::strict_get(x); } -template const T &cast(const any &x) { +template +const T& cast(const any& x) { return boost::strict_get(x); } -template T *cast(any *x) { +template +T* cast(any* x) { return boost::strict_get(&x); } -template const T *cast(const any *x) { +template +const T* cast(const any* x) { return boost::strict_get(&x); } // pass-through for generic programming, to keep code workgin when // you switch from dynamic to static histogram template -auto cast(U &&u) -> decltype(std::forward(u)) { +auto cast(U&& u) -> decltype(std::forward(u)) { return std::forward(u); } diff --git a/include/boost/histogram/axis/base.hpp b/include/boost/histogram/axis/base.hpp index 2d844f0f..91c4bdf0 100644 --- a/include/boost/histogram/axis/base.hpp +++ b/include/boost/histogram/axis/base.hpp @@ -28,7 +28,7 @@ enum class uoflow { off = 0, only_oflow = 1, on = 2 }; /// Base class for all axes class base { -public: + public: /// Returns the number of bins without overflow/underflow. int size() const noexcept { return size_; } /// Returns the number of bins, including overflow/underflow if enabled. @@ -40,9 +40,10 @@ public: /// Change the label of an axis. void label(string_view label) { label_.assign(label.begin(), label.end()); } -protected: + protected: base(unsigned size, string_view label, axis::uoflow uo) - : size_(size), shape_(size + static_cast(uo)), + : size_(size), + shape_(size + static_cast(uo)), label_(label.begin(), label.end()) { if (size_ == 0) { throw std::invalid_argument("bins > 0 required"); @@ -50,14 +51,14 @@ protected: } base() = default; - base(const base &) = default; - base &operator=(const base &) = default; - base(base &&rhs) : - size_(rhs.size_), shape_(rhs.shape_), - label_(std::move(rhs.label_)) { - rhs.size_ = 0; rhs.shape_ = 0; + base(const base&) = default; + base& operator=(const base&) = default; + base(base&& rhs) + : size_(rhs.size_), shape_(rhs.shape_), label_(std::move(rhs.label_)) { + rhs.size_ = 0; + rhs.shape_ = 0; } - base &operator=(base &&rhs) { + base& operator=(base&& rhs) { if (this != &rhs) { size_ = rhs.size_; shape_ = rhs.shape_; @@ -68,39 +69,41 @@ protected: return *this; } - bool operator==(const base &rhs) const noexcept { + bool operator==(const base& rhs) const noexcept { return size_ == rhs.size_ && shape_ == rhs.shape_ && label_ == rhs.label_; } -private: + private: int size_ = 0, shape_ = 0; std::string label_; friend class ::boost::serialization::access; - template void serialize(Archive &, unsigned); + template + void serialize(Archive&, unsigned); }; /// Iterator mixin, uses CRTP to inject iterator logic into Derived. template class iterator_mixin { -public: + public: using const_iterator = iterator_over; using const_reverse_iterator = reverse_iterator_over; const_iterator begin() const noexcept { - return const_iterator(*static_cast(this), 0); + return const_iterator(*static_cast(this), 0); } const_iterator end() const noexcept { - return const_iterator(*static_cast(this), static_cast(this)->size()); + return const_iterator(*static_cast(this), + static_cast(this)->size()); } const_reverse_iterator rbegin() const noexcept { - return const_reverse_iterator(*static_cast(this), static_cast(this)->size()); + return const_reverse_iterator(*static_cast(this), + static_cast(this)->size()); } const_reverse_iterator rend() const noexcept { - return const_reverse_iterator(*static_cast(this), 0); + return const_reverse_iterator(*static_cast(this), 0); } }; - } } } diff --git a/include/boost/histogram/axis/interval_view.hpp b/include/boost/histogram/axis/interval_view.hpp index e7c7f11d..87ca3db4 100644 --- a/include/boost/histogram/axis/interval_view.hpp +++ b/include/boost/histogram/axis/interval_view.hpp @@ -14,37 +14,38 @@ namespace boost { namespace histogram { namespace axis { -template class interval_view { -public: - interval_view(int idx, const Axis &axis) : idx_(idx), axis_(axis) {} +template +class interval_view { + public: + interval_view(int idx, const Axis& axis) : idx_(idx), axis_(axis) {} - interval_view(const interval_view &) = default; - interval_view &operator=(const interval_view &) = default; - interval_view(interval_view &&) = default; - interval_view &operator=(interval_view &&) = default; + interval_view(const interval_view&) = default; + interval_view& operator=(const interval_view&) = default; + interval_view(interval_view&&) = default; + interval_view& operator=(interval_view&&) = default; int idx() const noexcept { return idx_; } - auto lower() const noexcept -> decltype(std::declval().lower(0)) { + auto lower() const noexcept -> decltype(std::declval().lower(0)) { return axis_.lower(idx_); } - auto upper() const noexcept -> decltype(std::declval().lower(0)) { + auto upper() const noexcept -> decltype(std::declval().lower(0)) { return axis_.lower(idx_ + 1); } typename Axis::value_type width() const noexcept { return upper() - lower(); } - bool operator==(const interval_view &rhs) const noexcept { + bool operator==(const interval_view& rhs) const noexcept { return idx_ == rhs.idx_ && axis_ == rhs.axis_; } - bool operator!=(const interval_view &rhs) const noexcept { + bool operator!=(const interval_view& rhs) const noexcept { return !operator==(rhs); } explicit operator int() const noexcept { return idx_; } -private: + private: const int idx_; - const Axis &axis_; + const Axis& axis_; }; } // namespace axis diff --git a/include/boost/histogram/axis/iterator.hpp b/include/boost/histogram/axis/iterator.hpp index e1fa9332..a2eb0668 100644 --- a/include/boost/histogram/axis/iterator.hpp +++ b/include/boost/histogram/axis/iterator.hpp @@ -14,59 +14,60 @@ namespace histogram { namespace axis { template -class iterator_over - : public iterator_facade, typename Axis::bin_type, - random_access_traversal_tag, - typename Axis::bin_type> { -public: - explicit iterator_over(const Axis &axis, int idx) : axis_(axis), idx_(idx) {} +class iterator_over : public iterator_facade, + typename Axis::bin_type, + random_access_traversal_tag, + typename Axis::bin_type> { + public: + explicit iterator_over(const Axis& axis, int idx) : axis_(axis), idx_(idx) {} - iterator_over(const iterator_over &) = default; - iterator_over &operator=(const iterator_over &) = default; + iterator_over(const iterator_over&) = default; + iterator_over& operator=(const iterator_over&) = default; -protected: + protected: void increment() noexcept { ++idx_; } void decrement() noexcept { --idx_; } void advance(int n) noexcept { idx_ += n; } - int distance_to(const iterator_over &other) const noexcept { + int distance_to(const iterator_over& other) const noexcept { return other.idx_ - idx_; } - bool equal(const iterator_over &other) const noexcept { + bool equal(const iterator_over& other) const noexcept { return &axis_ == &other.axis_ && idx_ == other.idx_; } typename Axis::bin_type dereference() const { return axis_[idx_]; } friend class ::boost::iterator_core_access; - const Axis &axis_; + const Axis& axis_; int idx_; }; template class reverse_iterator_over - : public iterator_facade< - reverse_iterator_over, typename Axis::bin_type, - random_access_traversal_tag, typename Axis::bin_type> { -public: - explicit reverse_iterator_over(const Axis &axis, int idx) + : public iterator_facade, + typename Axis::bin_type, + random_access_traversal_tag, + typename Axis::bin_type> { + public: + explicit reverse_iterator_over(const Axis& axis, int idx) : axis_(axis), idx_(idx) {} - reverse_iterator_over(const reverse_iterator_over &) = default; - reverse_iterator_over &operator=(const reverse_iterator_over &) = default; + reverse_iterator_over(const reverse_iterator_over&) = default; + reverse_iterator_over& operator=(const reverse_iterator_over&) = default; -protected: + protected: void increment() noexcept { --idx_; } void decrement() noexcept { ++idx_; } void advance(int n) noexcept { idx_ -= n; } - int distance_to(const reverse_iterator_over &other) const noexcept { + int distance_to(const reverse_iterator_over& other) const noexcept { return other.idx_ - idx_; } - bool equal(const reverse_iterator_over &other) const noexcept { + bool equal(const reverse_iterator_over& other) const noexcept { return &axis_ == &other.axis_ && idx_ == other.idx_; } typename Axis::bin_type dereference() const { return axis_[idx_ - 1]; } friend class ::boost::iterator_core_access; - const Axis &axis_; + const Axis& axis_; int idx_; }; diff --git a/include/boost/histogram/axis/ostream_operators.hpp b/include/boost/histogram/axis/ostream_operators.hpp index 64ebfb61..b8ca4906 100644 --- a/include/boost/histogram/axis/ostream_operators.hpp +++ b/include/boost/histogram/axis/ostream_operators.hpp @@ -20,26 +20,32 @@ namespace histogram { namespace axis { namespace detail { -inline string_view to_string(const transform::identity &) { return {}; } -inline string_view to_string(const transform::log &) { return {"_log", 4}; } -inline string_view to_string(const transform::sqrt &) { return {"_sqrt", 5}; } +inline string_view to_string(const transform::identity&) { + return {}; +} +inline string_view to_string(const transform::log&) { + return {"_log", 4}; +} +inline string_view to_string(const transform::sqrt&) { + return {"_sqrt", 5}; +} } // namespace detail template -inline std::ostream &operator<<(std::ostream &os, const interval_view &i) { +inline std::ostream& operator<<(std::ostream& os, const interval_view& i) { os << "[" << i.lower() << ", " << i.upper() << ")"; return os; } template -inline std::ostream &operator<<(std::ostream &os, const value_view &i) { +inline std::ostream& operator<<(std::ostream& os, const value_view& i) { os << i.value(); return os; } template -inline std::ostream &operator<<(std::ostream &os, - const regular &a) { +inline std::ostream& operator<<(std::ostream& os, + const regular& a) { os << "regular" << detail::to_string(Transform()) << "(" << a.size() << ", " << a[0].lower() << ", " << a[a.size()].lower(); if (!a.label().empty()) { @@ -54,8 +60,9 @@ inline std::ostream &operator<<(std::ostream &os, } template -inline std::ostream & -operator<<(std::ostream &os, const regular &a) { +inline std::ostream& operator<<( + std::ostream& os, + const regular& a) { os << "regular_pow(" << a.size() << ", " << a[0].lower() << ", " << a[a.size()].lower() << ", " << a.transform().power; if (!a.label().empty()) { @@ -70,7 +77,7 @@ operator<<(std::ostream &os, const regular &a) { } template -inline std::ostream &operator<<(std::ostream &os, const circular &a) { +inline std::ostream& operator<<(std::ostream& os, const circular& a) { os << "circular(" << a.size(); if (a.phase() != 0.0) { os << ", phase=" << a.phase(); @@ -87,7 +94,7 @@ inline std::ostream &operator<<(std::ostream &os, const circular &a) { } template -inline std::ostream &operator<<(std::ostream &os, const variable &a) { +inline std::ostream& operator<<(std::ostream& os, const variable& a) { os << "variable(" << a[0].lower(); for (int i = 1; i <= a.size(); ++i) { os << ", " << a[i].lower(); @@ -104,7 +111,7 @@ inline std::ostream &operator<<(std::ostream &os, const variable &a) { } template -inline std::ostream &operator<<(std::ostream &os, const integer &a) { +inline std::ostream& operator<<(std::ostream& os, const integer& a) { os << "integer(" << a[0].lower() << ", " << a[a.size()].lower(); if (!a.label().empty()) { os << ", label="; @@ -118,7 +125,7 @@ inline std::ostream &operator<<(std::ostream &os, const integer &a) { } template -inline std::ostream &operator<<(std::ostream &os, const category &a) { +inline std::ostream& operator<<(std::ostream& os, const category& a) { os << "category("; for (int i = 0; i < a.size(); ++i) { os << a[i] << (i == (a.size() - 1) ? "" : ", "); @@ -132,8 +139,8 @@ inline std::ostream &operator<<(std::ostream &os, const category &a) { } template <> -inline std::ostream &operator<<(std::ostream &os, - const category &a) { +inline std::ostream& operator<<(std::ostream& os, + const category& a) { os << "category("; for (int i = 0; i < a.size(); ++i) { ::boost::histogram::detail::escape(os, a.value(i)); diff --git a/include/boost/histogram/axis/types.hpp b/include/boost/histogram/axis/types.hpp index fcd3766b..744ac154 100644 --- a/include/boost/histogram/axis/types.hpp +++ b/include/boost/histogram/axis/types.hpp @@ -36,24 +36,43 @@ namespace axis { namespace transform { namespace detail { struct stateless { - bool operator==(const stateless &) const noexcept { return true; } - template void serialize(Archive &, unsigned) {} + bool operator==(const stateless&) const noexcept { return true; } + template + void serialize(Archive&, unsigned) {} }; } // namespace detail struct identity : public detail::stateless { - template static T &&forward(T &&v) { return std::forward(v); } - template static T &&inverse(T &&v) { return std::forward(v); } + template + static T&& forward(T&& v) { + return std::forward(v); + } + template + static T&& inverse(T&& v) { + return std::forward(v); + } }; struct log : public detail::stateless { - template static T forward(T v) { return std::log(v); } - template static T inverse(T v) { return std::exp(v); } + template + static T forward(T v) { + return std::log(v); + } + template + static T inverse(T v) { + return std::exp(v); + } }; struct sqrt : public detail::stateless { - template static T forward(T v) { return std::sqrt(v); } - template static T inverse(T v) { return v * v; } + template + static T forward(T v) { + return std::sqrt(v); + } + template + static T inverse(T v) { + return v * v; + } }; // struct cos : public detail::stateless { @@ -66,17 +85,22 @@ struct pow { pow() = default; pow(double p) : power(p) {} - template T forward(T v) const { return std::pow(v, power); } - template T inverse(T v) const { + template + T forward(T v) const { + return std::pow(v, power); + } + template + T inverse(T v) const { return std::pow(v, 1.0 / power); } - bool operator==(const pow &other) const noexcept { + bool operator==(const pow& other) const noexcept { return power == other.power; } -private: + private: friend ::boost::serialization::access; - template void serialize(Archive &, unsigned); + template + void serialize(Archive&, unsigned); }; } // namespace transform @@ -90,7 +114,7 @@ template class regular : public base, public iterator_mixin>, Transform { -public: + public: using value_type = RealType; using bin_type = interval_view; @@ -103,10 +127,15 @@ public: * \param uoflow whether to add under-/overflow bins. * \param trans arguments passed to the transform. */ - regular(unsigned n, value_type lower, value_type upper, - string_view label = {}, axis::uoflow uo = axis::uoflow::on, + regular(unsigned n, + value_type lower, + value_type upper, + string_view label = {}, + axis::uoflow uo = axis::uoflow::on, Transform trans = Transform()) - : base(n, label, uo), Transform(trans), min_(trans.forward(lower)), + : base(n, label, uo), + Transform(trans), + min_(trans.forward(lower)), delta_((trans.forward(upper) - trans.forward(lower)) / n) { if (lower < upper) { BOOST_ASSERT(!std::isnan(min_)); @@ -117,10 +146,10 @@ public: } regular() = default; - regular(const regular &) = default; - regular &operator=(const regular &) = default; - regular(regular &&) = default; - regular &operator=(regular &&) = default; + regular(const regular&) = default; + regular& operator=(const regular&) = default; + regular(regular&&) = default; + regular& operator=(regular&&) = default; /// Returns the bin index for the passed argument. int index(value_type x) const noexcept { @@ -147,21 +176,22 @@ public: bin_type operator[](int idx) const noexcept { return bin_type(idx, *this); } - bool operator==(const regular &o) const noexcept { + bool operator==(const regular& o) const noexcept { return base::operator==(o) && Transform::operator==(o) && min_ == o.min_ && delta_ == o.delta_; } /// Access properties of the transform. - const Transform &transform() const noexcept { - return static_cast(*this); + const Transform& transform() const noexcept { + return static_cast(*this); } -private: + private: value_type min_ = 0.0, delta_ = 1.0; friend class ::boost::serialization::access; - template void serialize(Archive &, unsigned); + template + void serialize(Archive&, unsigned); }; /** Axis for real values on a circle. @@ -172,7 +202,7 @@ private: */ template class circular : public base, public iterator_mixin> { -public: + public: using value_type = RealType; using bin_type = interval_view; @@ -183,17 +213,19 @@ public: * \param perimeter range after which value wraps around. * \param label description of the axis. */ - explicit circular(unsigned n, value_type phase = 0.0, + explicit circular(unsigned n, + value_type phase = 0.0, value_type perimeter = boost::histogram::detail::two_pi, string_view label = {}) - : base(n, label, axis::uoflow::off), phase_(phase), + : base(n, label, axis::uoflow::off), + phase_(phase), perimeter_(perimeter) {} circular() = default; - circular(const circular &) = default; - circular &operator=(const circular &) = default; - circular(circular &&) = default; - circular &operator=(circular &&) = default; + circular(const circular&) = default; + circular& operator=(const circular&) = default; + circular(circular&&) = default; + circular& operator=(circular&&) = default; /// Returns the bin index for the passed argument. int index(value_type x) const noexcept { @@ -210,7 +242,7 @@ public: bin_type operator[](int idx) const noexcept { return bin_type(idx, *this); } - bool operator==(const circular &o) const noexcept { + bool operator==(const circular& o) const noexcept { return base::operator==(o) && phase_ == o.phase_ && perimeter_ == o.perimeter_; } @@ -218,11 +250,12 @@ public: value_type perimeter() const { return perimeter_; } value_type phase() const { return phase_; } -private: + private: value_type phase_ = 0.0, perimeter_ = 1.0; friend class ::boost::serialization::access; - template void serialize(Archive &, unsigned); + template + void serialize(Archive&, unsigned); }; /** Axis for non-equidistant bins on the real line. @@ -232,7 +265,7 @@ private: */ template class variable : public base, public iterator_mixin> { -public: + public: using value_type = RealType; using bin_type = interval_view; @@ -242,7 +275,8 @@ public: * \param label description of the axis. * \param uoflow whether to add under-/overflow bins. */ - variable(std::initializer_list x, string_view label = {}, + variable(std::initializer_list x, + string_view label = {}, axis::uoflow uo = axis::uoflow::on) : base(x.size() - 1, label, uo), x_(new value_type[x.size()]) { if (x.size() >= 2) { @@ -254,7 +288,9 @@ public: } template - variable(Iterator begin, Iterator end, string_view label = {}, + variable(Iterator begin, + Iterator end, + string_view label = {}, axis::uoflow uo = axis::uoflow::on) : base(std::distance(begin, end) - 1, label, uo), x_(new value_type[std::distance(begin, end)]) { @@ -263,10 +299,10 @@ public: } variable() = default; - variable(const variable &o) : base(o), x_(new value_type[base::size() + 1]) { + variable(const variable& o) : base(o), x_(new value_type[base::size() + 1]) { std::copy(o.x_.get(), o.x_.get() + base::size() + 1, x_.get()); } - variable &operator=(const variable &o) { + variable& operator=(const variable& o) { if (this != &o) { base::operator=(o); x_.reset(new value_type[base::size() + 1]); @@ -274,8 +310,8 @@ public: } return *this; } - variable(variable &&) = default; - variable &operator=(variable &&) = default; + variable(variable&&) = default; + variable& operator=(variable&&) = default; /// Returns the bin index for the passed argument. int index(value_type x) const noexcept { @@ -296,18 +332,19 @@ public: bin_type operator[](int idx) const noexcept { return bin_type(idx, *this); } - bool operator==(const variable &o) const noexcept { + bool operator==(const variable& o) const noexcept { if (!base::operator==(o)) { return false; } return std::equal(x_.get(), x_.get() + base::size() + 1, o.x_.get()); } -private: + private: std::unique_ptr x_; // smaller size compared to std::vector friend class ::boost::serialization::access; - template void serialize(Archive &, unsigned); + template + void serialize(Archive&, unsigned); }; /** Axis for an interval of integral values with unit steps. @@ -317,7 +354,7 @@ private: */ template class integer : public base, public iterator_mixin> { -public: + public: using value_type = IntType; using bin_type = interval_view; @@ -328,7 +365,9 @@ public: * \param label description of the axis. * \param uoflow whether to add under-/overflow bins. */ - integer(value_type lower, value_type upper, string_view label = {}, + integer(value_type lower, + value_type upper, + string_view label = {}, axis::uoflow uo = axis::uoflow::on) : base(upper - lower, label, uo), min_(lower) { if (!(lower < upper)) { @@ -337,10 +376,10 @@ public: } integer() = default; - integer(const integer &) = default; - integer &operator=(const integer &) = default; - integer(integer &&) = default; - integer &operator=(integer &&) = default; + integer(const integer&) = default; + integer& operator=(const integer&) = default; + integer(integer&&) = default; + integer& operator=(integer&&) = default; /// Returns the bin index for the passed argument. int index(value_type x) const noexcept { @@ -361,15 +400,16 @@ public: bin_type operator[](int idx) const noexcept { return bin_type(idx, *this); } - bool operator==(const integer &o) const noexcept { + bool operator==(const integer& o) const noexcept { return base::operator==(o) && min_ == o.min_; } -private: + private: value_type min_ = 0; friend class ::boost::serialization::access; - template void serialize(Archive &, unsigned); + template + void serialize(Archive&, unsigned); }; /** Axis which maps unique single values to bins (one on one). @@ -383,21 +423,21 @@ template class category : public base, public iterator_mixin> { using map_type = bimap; -public: + public: using value_type = T; using bin_type = value_view; category() = default; - category(const category &rhs) : base(rhs), map_(new map_type(*rhs.map_)) {} - category &operator=(const category &rhs) { + category(const category& rhs) : base(rhs), map_(new map_type(*rhs.map_)) {} + category& operator=(const category& rhs) { if (this != &rhs) { base::operator=(rhs); map_.reset(new map_type(*rhs.map_)); } return *this; } - category(category &&rhs) = default; - category &operator=(category &&rhs) = default; + category(category&& rhs) = default; + category& operator=(category&& rhs) = default; /** Construct from an initializer list of strings. * @@ -406,7 +446,7 @@ public: category(std::initializer_list seq, string_view label = {}) : base(seq.size(), label, axis::uoflow::off), map_(new map_type()) { int index = 0; - for (const auto &x : seq) + for (const auto& x : seq) map_->insert({x, index++}); if (index == 0) throw std::invalid_argument("sequence is empty"); @@ -425,7 +465,7 @@ public: } /// Returns the bin index for the passed argument. - int index(const value_type &x) const noexcept { + int index(const value_type& x) const noexcept { auto it = map_->left.find(x); if (it == map_->left.end()) return base::size(); @@ -433,7 +473,7 @@ public: } /// Returns the value for the bin index (performs a range check). - const value_type &value(int idx) const { + const value_type& value(int idx) const { auto it = map_->right.find(idx); if (it == map_->right.end()) throw std::out_of_range("category index out of range"); @@ -442,16 +482,17 @@ public: bin_type operator[](int idx) const noexcept { return bin_type(idx, *this); } - bool operator==(const category &o) const noexcept { + bool operator==(const category& o) const noexcept { return base::operator==(o) && std::equal(map_->begin(), map_->end(), o.map_->begin()); } -private: + private: std::unique_ptr map_; friend class ::boost::serialization::access; - template void serialize(Archive &, unsigned); + template + void serialize(Archive&, unsigned); }; } // namespace axis } // namespace histogram diff --git a/include/boost/histogram/axis/value_view.hpp b/include/boost/histogram/axis/value_view.hpp index b0975c43..5c0626c2 100644 --- a/include/boost/histogram/axis/value_view.hpp +++ b/include/boost/histogram/axis/value_view.hpp @@ -14,33 +14,34 @@ namespace boost { namespace histogram { namespace axis { -template class value_view { -public: - value_view(int idx, const Axis &axis) : idx_(idx), axis_(axis) {} +template +class value_view { + public: + value_view(int idx, const Axis& axis) : idx_(idx), axis_(axis) {} - value_view(const value_view &) = default; - value_view &operator=(const value_view &) = default; - value_view(value_view &&) = default; - value_view &operator=(value_view &&) = default; + value_view(const value_view&) = default; + value_view& operator=(const value_view&) = default; + value_view(value_view&&) = default; + value_view& operator=(value_view&&) = default; int idx() const noexcept { return idx_; } - auto value() const -> decltype(std::declval().value(0)) { + auto value() const -> decltype(std::declval().value(0)) { return axis_.value(idx_); } - bool operator==(const value_view &rhs) const noexcept { + bool operator==(const value_view& rhs) const noexcept { return idx_ == rhs.idx_ && axis_ == rhs.axis_; } - bool operator!=(const value_view &rhs) const noexcept { + bool operator!=(const value_view& rhs) const noexcept { return !operator==(rhs); } explicit operator int() const noexcept { return idx_; } -private: + private: const int idx_; - const Axis &axis_; + const Axis& axis_; }; } // namespace axis diff --git a/include/boost/histogram/detail/axis_visitor.hpp b/include/boost/histogram/detail/axis_visitor.hpp index 9232825c..7a221885 100644 --- a/include/boost/histogram/detail/axis_visitor.hpp +++ b/include/boost/histogram/detail/axis_visitor.hpp @@ -20,68 +20,76 @@ namespace detail { namespace { -template struct axes_equal_tuple_vecvar { - bool &equal; - const Tuple &t; - const VecVar &v; - axes_equal_tuple_vecvar(bool &eq, const Tuple &tt, const VecVar &vv) +template +struct axes_equal_tuple_vecvar { + bool& equal; + const Tuple& t; + const VecVar& v; + axes_equal_tuple_vecvar(bool& eq, const Tuple& tt, const VecVar& vv) : equal(eq), t(tt), v(vv) {} - template void operator()(Int) const { + template + void operator()(Int) const { using T = mp11::mp_at; auto tp = ::boost::get(&v[Int::value]); equal &= (tp && *tp == std::get(t)); } }; -template struct axes_assign_tuple_vecvar { - Tuple &t; - const VecVar &v; - axes_assign_tuple_vecvar(Tuple &tt, const VecVar &vv) : t(tt), v(vv) {} - template void operator()(Int) const { +template +struct axes_assign_tuple_vecvar { + Tuple& t; + const VecVar& v; + axes_assign_tuple_vecvar(Tuple& tt, const VecVar& vv) : t(tt), v(vv) {} + template + void operator()(Int) const { using T = mp11::mp_at; std::get(t) = ::boost::get(v[Int::value]); } }; -template struct axes_assign_vecvar_tuple { - VecVar &v; - const Tuple &t; - axes_assign_vecvar_tuple(VecVar &vv, const Tuple &tt) : v(vv), t(tt) {} - template void operator()(Int) const { +template +struct axes_assign_vecvar_tuple { + VecVar& v; + const Tuple& t; + axes_assign_vecvar_tuple(VecVar& vv, const Tuple& tt) : v(vv), t(tt) {} + template + void operator()(Int) const { v[Int::value] = std::get(t); } }; template -inline bool axes_equal_impl(mp11::mp_true, const std::tuple &t, - const std::tuple &u) { +inline bool axes_equal_impl(mp11::mp_true, + const std::tuple& t, + const std::tuple& u) { return t == u; } template -inline bool axes_equal_impl(mp11::mp_false, const std::tuple &, - const std::tuple &) { +inline bool axes_equal_impl(mp11::mp_false, + const std::tuple&, + const std::tuple&) { return false; } } // namespace template -inline bool axes_equal(const std::tuple &t, const std::tuple &u) { +inline bool axes_equal(const std::tuple& t, const std::tuple& u) { return axes_equal_impl( mp11::mp_same, mp11::mp_list>(), t, u); } template -inline void axes_assign(std::tuple &t, const std::tuple &u) { +inline void axes_assign(std::tuple& t, const std::tuple& u) { static_assert(std::is_same, mp11::mp_list>::value, "cannot assign incompatible axes"); t = u; } template -inline bool axes_equal(const std::tuple &t, - const std::vector> &u) { +inline bool axes_equal(const std::tuple& t, + const std::vector>& u) { if (sizeof...(Ts) != u.size()) return false; bool equal = true; @@ -93,22 +101,22 @@ inline bool axes_equal(const std::tuple &t, } template -inline void axes_assign(std::tuple &t, - const std::vector> &u) { +inline void axes_assign(std::tuple& t, + const std::vector>& u) { auto fn = axes_assign_tuple_vecvar, std::vector>>(t, u); mp11::mp_for_each>(fn); } template -inline bool axes_equal(const std::vector> &t, - const std::tuple &u) { +inline bool axes_equal(const std::vector>& t, + const std::tuple& u) { return axes_equal(u, t); } template -inline void axes_assign(std::vector> &t, - const std::tuple &u) { +inline void axes_assign(std::vector>& t, + const std::tuple& u) { t.resize(sizeof...(Us)); auto fn = axes_assign_vecvar_tuple>, std::tuple>(t, u); @@ -116,8 +124,8 @@ inline void axes_assign(std::vector> &t, } template -inline bool axes_equal(const std::vector> &t, - const std::vector> &u) { +inline bool axes_equal(const std::vector>& t, + const std::vector>& u) { if (t.size() != u.size()) return false; for (std::size_t i = 0; i < t.size(); ++i) { @@ -128,8 +136,8 @@ inline bool axes_equal(const std::vector> &t, } template -inline void axes_assign(std::vector> &t, - const std::vector> &u) { +inline void axes_assign(std::vector>& t, + const std::vector>& u) { for (std::size_t i = 0; i < t.size(); ++i) { t[i] = u[i]; } @@ -137,20 +145,28 @@ inline void axes_assign(std::vector> &t, struct field_count_visitor : public static_visitor { std::size_t value = 1; - template void operator()(const T &t) { value *= t.shape(); } + template + void operator()(const T& t) { + value *= t.shape(); + } }; -template struct unary_visitor : public static_visitor { - Unary &unary; - unary_visitor(Unary &u) : unary(u) {} - template void operator()(const Axis &a) const { unary(a); } +template +struct unary_visitor : public static_visitor { + Unary& unary; + unary_visitor(Unary& u) : unary(u) {} + template + void operator()(const Axis& a) const { + unary(a); + } }; struct shape_vector_visitor { std::vector shapes; std::vector::iterator iter; shape_vector_visitor(unsigned n) : shapes(n) { iter = shapes.begin(); } - template void operator()(const Axis &a) { + template + void operator()(const Axis& a) { *iter++ = a.shape(); } }; diff --git a/include/boost/histogram/detail/cat.hpp b/include/boost/histogram/detail/cat.hpp index e29b74be..55f9ed87 100644 --- a/include/boost/histogram/detail/cat.hpp +++ b/include/boost/histogram/detail/cat.hpp @@ -17,16 +17,17 @@ namespace boost { namespace histogram { namespace detail { namespace { -__attribute__((unused)) void cat_impl(std::ostringstream &) {} +__attribute__((unused)) void cat_impl(std::ostringstream&) {} template -void cat_impl(std::ostringstream &os, const T &t, const Ts &... ts) { +void cat_impl(std::ostringstream& os, const T& t, const Ts&... ts) { os << t; cat_impl(os, ts...); } } // namespace -template std::string cat(const Ts &... args) { +template +std::string cat(const Ts&... args) { std::ostringstream os; cat_impl(os, args...); return os.str(); diff --git a/include/boost/histogram/detail/meta.hpp b/include/boost/histogram/detail/meta.hpp index b6be1108..696ca2f9 100644 --- a/include/boost/histogram/detail/meta.hpp +++ b/include/boost/histogram/detail/meta.hpp @@ -20,29 +20,34 @@ namespace boost { namespace histogram { namespace detail { -#define BOOST_HISTOGRAM_MAKE_SFINAE(name, cond) \ - template struct name##_impl { \ - template struct SFINAE {}; \ - template static std::true_type Test(SFINAE *); \ - template static std::false_type Test(...); \ - using type = decltype(Test(nullptr)); \ - }; \ - template using name = typename name##_impl::type +#define BOOST_HISTOGRAM_MAKE_SFINAE(name, cond) \ + template \ + struct name##_impl { \ + template \ + struct SFINAE {}; \ + template \ + static std::true_type Test(SFINAE*); \ + template \ + static std::false_type Test(...); \ + using type = decltype(Test(nullptr)); \ + }; \ + template \ + using name = typename name##_impl::type BOOST_HISTOGRAM_MAKE_SFINAE(has_variance_support, - (std::declval().value(), - std::declval().variance())); + (std::declval().value(), + std::declval().variance())); -BOOST_HISTOGRAM_MAKE_SFINAE(has_method_lower, (std::declval().lower(0))); +BOOST_HISTOGRAM_MAKE_SFINAE(has_method_lower, (std::declval().lower(0))); BOOST_HISTOGRAM_MAKE_SFINAE(is_dynamic_container, - (std::begin(std::declval()))); + (std::begin(std::declval()))); BOOST_HISTOGRAM_MAKE_SFINAE(is_static_container, - (std::get<0>(std::declval()))); + (std::get<0>(std::declval()))); BOOST_HISTOGRAM_MAKE_SFINAE(is_castable_to_int, - (static_cast(std::declval()))); + (static_cast(std::declval()))); struct static_container_tag {}; struct dynamic_container_tag {}; @@ -50,35 +55,42 @@ struct no_container_tag {}; template using classify_container = typename std::conditional< - is_static_container::value, static_container_tag, + is_static_container::value, + static_container_tag, typename std::conditional::value, dynamic_container_tag, no_container_tag>::type>::type; -template ().size(), - std::declval().increase(0), - std::declval()[0])> +template ().size(), + std::declval().increase(0), + std::declval()[0])> struct requires_storage {}; template (), ++std::declval())> + typename = decltype(*std::declval(), ++std::declval())> struct requires_iterator {}; template -using requires_axis = - decltype(std::declval().size(), std::declval().shape(), - std::declval().uoflow(), std::declval().label(), - std::declval()[0]); +using requires_axis = decltype(std::declval().size(), + std::declval().shape(), + std::declval().uoflow(), + std::declval().label(), + std::declval()[0]); namespace { struct bool_mask_impl { - std::vector &b; + std::vector& b; bool v; - template void operator()(Int) const { b[Int::value] = v; } + template + void operator()(Int) const { + b[Int::value] = v; + } }; } -template std::vector bool_mask(unsigned n, bool v) { +template +std::vector bool_mask(unsigned n, bool v) { std::vector b(n, !v); mp11::mp_for_each>(bool_mask_impl{b, v}); return b; @@ -88,18 +100,21 @@ template using rm_cv_ref = typename std::remove_cv::type>::type; -template using mp_size = mp11::mp_size>; +template +using mp_size = mp11::mp_size>; template using mp_at_c = mp11::mp_at_c, D>; template using copy_qualifiers = mp11::mp_if< - std::is_rvalue_reference, T2 &&, + std::is_rvalue_reference, + T2&&, mp11::mp_if< std::is_lvalue_reference, mp11::mp_if::type>, - const T2 &, T2 &>, + const T2&, + T2&>, mp11::mp_if, const T2, T2>>>; template @@ -112,8 +127,10 @@ using mp_set_union = mp11::mp_apply_q, L>; namespace { -template struct selection_impl { - template using at = mp11::mp_at; +template +struct selection_impl { + template + using at = mp11::mp_at; using N = mp11::mp_list; using LNs = mp11::mp_assign; using type = mp11::mp_transform; @@ -127,17 +144,19 @@ template using unique_sorted = mp11::mp_unique>; namespace { -template struct sub_tuple_assign_impl { - const Src &src; - Dst &dst; - template void operator()(std::pair) const { +template +struct sub_tuple_assign_impl { + const Src& src; + Dst& dst; + template + void operator()(std::pair) const { std::get(dst) = std::get(src); } }; } template -selection make_sub_tuple(const T &t) { +selection make_sub_tuple(const T& t) { using U = selection; U u; using N1 = mp11::mp_list; diff --git a/include/boost/histogram/detail/utility.hpp b/include/boost/histogram/detail/utility.hpp index 2b83be23..1367b48f 100644 --- a/include/boost/histogram/detail/utility.hpp +++ b/include/boost/histogram/detail/utility.hpp @@ -27,7 +27,7 @@ namespace detail { // two_pi can be found in boost/math, but it is defined here to reduce deps constexpr double two_pi = 6.283185307179586; -inline void escape(std::ostream &os, const string_view s) { +inline void escape(std::ostream& os, const string_view s) { os << '\''; for (auto sit = s.begin(); sit != s.end(); ++sit) { if (*sit == '\'' && (sit == s.begin() || *(sit - 1) != '\\')) { @@ -41,8 +41,11 @@ inline void escape(std::ostream &os, const string_view s) { // the following is highly optimized code that runs in a hot loop; // please measure the performance impact of changes -inline void lin(std::size_t &out, std::size_t &stride, const int axis_size, - const int axis_shape, int j) noexcept { +inline void lin(std::size_t& out, + std::size_t& stride, + const int axis_size, + const int axis_shape, + int j) noexcept { BOOST_ASSERT_MSG(stride == 0 || (-1 <= j && j <= axis_size), "index must be in bounds for this algorithm"); j += (j < 0) * (axis_size + 2); // wrap around if j < 0 @@ -61,8 +64,9 @@ struct index_cache { struct dim_visitor { mutable std::size_t stride; - mutable dim_t *dims; - template void operator()(const Axis &a) const noexcept { + mutable dim_t* dims; + template + void operator()(const Axis& a) const noexcept { *dims++ = dim_t{0, a.size(), stride}; stride *= a.shape(); } @@ -73,14 +77,14 @@ struct index_cache { std::unique_ptr dims_; index_cache() = default; - index_cache(index_cache &&) = default; - index_cache &operator=(index_cache &&) = default; + index_cache(index_cache&&) = default; + index_cache& operator=(index_cache&&) = default; - index_cache(const index_cache &o) : dim_(o.dim_), dims_(new dim_t[o.dim_]) { + index_cache(const index_cache& o) : dim_(o.dim_), dims_(new dim_t[o.dim_]) { std::copy(o.dims_.get(), o.dims_.get() + dim_, dims_.get()); } - index_cache &operator=(const index_cache &o) { + index_cache& operator=(const index_cache& o) { if (this != &o) { if (o.dim_ != dim_) { dim_ = o.dim_; @@ -91,7 +95,8 @@ struct index_cache { return *this; } - template void reset(const H &h) { + template + void reset(const H& h) { if (h.dim() != dim_) { dim_ = h.dim(); dims_.reset(new dim_t[dim_]); @@ -121,12 +126,12 @@ struct index_cache { struct index_mapper { std::size_t first = 0, second = 0; - index_mapper(const std::vector &nvec, - const std::vector &bvec) { + index_mapper(const std::vector& nvec, + const std::vector& bvec) { dims.reserve(nvec.size()); std::size_t s1 = 1, s2 = 1; auto bi = bvec.begin(); - for (const auto &ni : nvec) { + for (const auto& ni : nvec) { if (*bi) { dims.push_back({s1, s2}); s2 *= ni; @@ -137,7 +142,7 @@ struct index_mapper { ++bi; } std::sort(dims.begin(), dims.end(), - [](const dim &a, const dim &b) { return a.stride1 > b.stride1; }); + [](const dim& a, const dim& b) { return a.stride1 > b.stride1; }); nfirst = s1; } @@ -145,7 +150,7 @@ struct index_mapper { ++first; second = 0; auto f = first; - for (const auto &d : dims) { + for (const auto& d : dims) { auto i = f / d.stride1; f -= i * d.stride1; second += i * d.stride2; @@ -153,7 +158,7 @@ struct index_mapper { return first < nfirst; } -private: + private: std::size_t nfirst; struct dim { std::size_t stride1, stride2; @@ -163,13 +168,13 @@ private: template inline typename std::enable_if<(is_castable_to_int::value), int>::type -indirect_int_cast(T &&t) noexcept { +indirect_int_cast(T&& t) noexcept { return static_cast(std::forward(t)); } template inline typename std::enable_if::value), int>::type -indirect_int_cast(T &&) noexcept { +indirect_int_cast(T&&) noexcept { // Cannot use static_assert here, because this function is created as a // side-effect of TMP. It must be valid at compile-time. BOOST_ASSERT_MSG(false, "bin argument not convertible to int"); @@ -177,16 +182,17 @@ indirect_int_cast(T &&) noexcept { } template -inline void fill_storage(S &s, std::size_t idx, weight &&w) { +inline void fill_storage(S& s, std::size_t idx, weight&& w) { s.add(idx, w); } -template inline void fill_storage(S &s, std::size_t idx) { +template +inline void fill_storage(S& s, std::size_t idx) { s.increase(idx); } template -inline auto storage_get(const S &s, std::size_t idx, bool error) -> +inline auto storage_get(const S& s, std::size_t idx, bool error) -> typename S::const_reference { if (error) throw std::out_of_range("bin index out of range"); diff --git a/include/boost/histogram/dynamic_histogram.hpp b/include/boost/histogram/dynamic_histogram.hpp index d5c1b22c..f6b03782 100644 --- a/include/boost/histogram/dynamic_histogram.hpp +++ b/include/boost/histogram/dynamic_histogram.hpp @@ -52,7 +52,7 @@ template class histogram { static_assert(mp11::mp_size::value > 0, "at least one axis required"); -public: + public: using any_axis_type = mp11::mp_rename; using axes_type = std::vector; using element_type = typename Storage::element_type; @@ -60,23 +60,24 @@ public: using const_iterator = iterator_over; using iterator = const_iterator; -public: + public: histogram() = default; - histogram(const histogram &) = default; - histogram(histogram &&) = default; - histogram &operator=(const histogram &) = default; - histogram &operator=(histogram &&) = default; + histogram(const histogram&) = default; + histogram(histogram&&) = default; + histogram& operator=(const histogram&) = default; + histogram& operator=(histogram&&) = default; - template > - explicit histogram(Axis0 &&axis0, Axis &&... axis) + explicit histogram(Axis0&& axis0, Axis&&... axis) : axes_({any_axis_type(std::forward(axis0)), any_axis_type(std::forward(axis))...}) { storage_ = Storage(size_from_axes()); index_cache_.reset(*this); } - explicit histogram(axes_type &&axes) : axes_(std::move(axes)) { + explicit histogram(axes_type&& axes) : axes_(std::move(axes)) { storage_ = Storage(size_from_axes()); index_cache_.reset(*this); } @@ -89,14 +90,14 @@ public: } template - explicit histogram(const histogram &rhs) : storage_(rhs.storage_) { + explicit histogram(const histogram& rhs) : storage_(rhs.storage_) { detail::axes_assign(axes_, rhs.axes_); index_cache_.reset(*this); } template - histogram &operator=(const histogram &rhs) { - if (static_cast(this) != static_cast(&rhs)) { + histogram& operator=(const histogram& rhs) { + if (static_cast(this) != static_cast(&rhs)) { detail::axes_assign(axes_, rhs.axes_); storage_ = rhs.storage_; index_cache_.reset(*this); @@ -105,13 +106,14 @@ public: } template - explicit histogram(dynamic_histogram &&rhs) + explicit histogram(dynamic_histogram&& rhs) : axes_(std::move(rhs.axes_)), storage_(std::move(rhs.storage_)) { index_cache_.reset(*this); } - template histogram &operator=(dynamic_histogram &&rhs) { - if (static_cast(this) != static_cast(&rhs)) { + template + histogram& operator=(dynamic_histogram&& rhs) { + if (static_cast(this) != static_cast(&rhs)) { axes_ = std::move(rhs.axes_); storage_ = std::move(rhs.storage_); index_cache_.reset(*this); @@ -120,34 +122,37 @@ public: } template - bool operator==(const histogram &rhs) const noexcept { + bool operator==(const histogram& rhs) const noexcept { return detail::axes_equal(axes_, rhs.axes_) && storage_ == rhs.storage_; } template - bool operator!=(const histogram &rhs) const noexcept { + bool operator!=(const histogram& rhs) const noexcept { return !operator==(rhs); } template - histogram &operator+=(const histogram &rhs) { + histogram& operator+=(const histogram& rhs) { if (!detail::axes_equal(axes_, rhs.axes_)) throw std::invalid_argument("axes of histograms differ"); storage_ += rhs.storage_; return *this; } - template histogram &operator*=(const T &rhs) { + template + histogram& operator*=(const T& rhs) { storage_ *= rhs; return *this; } - template histogram &operator/=(const T &rhs) { + template + histogram& operator/=(const T& rhs) { storage_ *= 1.0 / rhs; return *this; } - template void operator()(Ts &&... ts) { + template + void operator()(Ts&&... ts) { // case with one argument is ambiguous, is specialized below BOOST_ASSERT_MSG(dim() == sizeof...(Ts), "fill arguments does not match histogram dimension " @@ -159,7 +164,8 @@ public: } } - template void operator()(T &&t) { + template + void operator()(T&& t) { // check whether T is unpackable if (dim() == 1) { fill_impl(detail::no_container_tag(), std::forward(t)); @@ -169,7 +175,7 @@ public: } template - void operator()(detail::weight &&w, Ts &&... ts) { + void operator()(detail::weight&& w, Ts&&... ts) { // case with one argument is ambiguous, is specialized below BOOST_ASSERT_MSG(dim() == sizeof...(Ts), "fill arguments does not match histogram dimension"); @@ -181,7 +187,7 @@ public: } template - void operator()(detail::weight &&w, T &&t) { + void operator()(detail::weight&& w, T&& t) { // check whether T is unpackable if (dim() == 1) { fill_impl(detail::no_container_tag(), std::forward(t), std::move(w)); @@ -191,7 +197,8 @@ public: } } - template const_reference at(Ts &&... ts) const { + template + const_reference at(Ts&&... ts) const { // case with one argument is ambiguous, is specialized below BOOST_ASSERT_MSG(dim() == sizeof...(Ts), "bin arguments does not match histogram dimension"); @@ -200,12 +207,14 @@ public: return detail::storage_get(storage_, idx, stride == 0); } - template const_reference at(T &&t) const { + template + const_reference at(T&& t) const { // check whether T is unpackable return at_impl(detail::classify_container(), std::forward(t)); } - template const_reference operator[](T &&t) const { + template + const_reference operator[](T&& t) const { // check whether T is unpackable return at_impl(detail::classify_container(), std::forward(t)); } @@ -220,20 +229,21 @@ public: void reset() { storage_ = Storage(size_from_axes()); } /// Return axis \a i - any_axis_type &axis(unsigned i = 0) { + any_axis_type& axis(unsigned i = 0) { BOOST_ASSERT_MSG(i < dim(), "axis index out of range"); return axes_[i]; } /// Return axis \a i (const version) - const any_axis_type &axis(unsigned i = 0) const { + const any_axis_type& axis(unsigned i = 0) const { BOOST_ASSERT_MSG(i < dim(), "axis index out of range"); return axes_[i]; } /// Apply unary functor/function to each axis - template void for_each_axis(Unary &&unary) const { - for (const auto &a : axes_) { + template + void for_each_axis(Unary&& unary) const { + for (const auto& a : axes_) { apply_visitor(detail::unary_visitor(unary), a); } } @@ -246,9 +256,10 @@ public: } /// Return a lower dimensional histogram - template histogram reduce_to(int n, Ts... ts) const { + template + histogram reduce_to(int n, Ts... ts) const { std::vector b(dim(), false); - for (const auto &i : {n, int(ts)...}) + for (const auto& i : {n, int(ts)...}) b[i] = true; return reduce_impl(b); } @@ -268,7 +279,7 @@ public: return const_iterator(*this, storage_.size()); } -private: + private: axes_type axes_; Storage storage_; mutable detail::index_cache index_cache_; @@ -280,7 +291,7 @@ private: } template - void fill_impl(detail::dynamic_container_tag, T &&t, Ts &&... ts) { + void fill_impl(detail::dynamic_container_tag, T&& t, Ts&&... ts) { BOOST_ASSERT_MSG(dim() == std::distance(std::begin(t), std::end(t)), "fill container does not match histogram dimension"); std::size_t idx = 0, stride = 1; @@ -291,7 +302,7 @@ private: } template - void fill_impl(detail::static_container_tag, T &&t, Ts &&... ts) { + void fill_impl(detail::static_container_tag, T&& t, Ts&&... ts) { BOOST_ASSERT_MSG(dim() == detail::mp_size::value, "fill container does not match histogram dimension"); std::size_t idx = 0, stride = 1; @@ -303,7 +314,7 @@ private: } template - void fill_impl(detail::no_container_tag, T &&t, Ts &&... ts) { + void fill_impl(detail::no_container_tag, T&& t, Ts&&... ts) { BOOST_ASSERT_MSG(dim() == 1, "fill argument does not match histogram dimension"); std::size_t idx = 0, stride = 1; @@ -314,7 +325,7 @@ private: } template - const_reference at_impl(detail::dynamic_container_tag, T &&t) const { + const_reference at_impl(detail::dynamic_container_tag, T&& t) const { BOOST_ASSERT_MSG(dim() == std::distance(std::begin(t), std::end(t)), "bin container does not match histogram dimension"); std::size_t idx = 0, stride = 1; @@ -323,7 +334,7 @@ private: } template - const_reference at_impl(detail::static_container_tag, T &&t) const { + const_reference at_impl(detail::static_container_tag, T&& t) const { BOOST_ASSERT_MSG(dim() == detail::mp_size::value, "bin container does not match histogram dimension"); std::size_t idx = 0, stride = 1; @@ -333,7 +344,7 @@ private: } template - const_reference at_impl(detail::no_container_tag, T &&t) const { + const_reference at_impl(detail::no_container_tag, T&& t) const { BOOST_ASSERT_MSG(dim() == 1, "bin argument does not match histogram dimension"); std::size_t idx = 0, stride = 1; @@ -342,14 +353,15 @@ private: } struct lin_visitor : public static_visitor { - std::size_t &idx; - std::size_t &stride; + std::size_t& idx; + std::size_t& stride; const int j; - lin_visitor(std::size_t &i, std::size_t &s, const int x) noexcept + lin_visitor(std::size_t& i, std::size_t& s, const int x) noexcept : idx(i), stride(s), j(x) {} - template void operator()(const A &a) const noexcept { + template + void operator()(const A& a) const noexcept { const auto a_size = a.size(); const auto a_shape = a.shape(); stride *= (-1 <= j && j <= a_size); // set stride to zero, if j is invalid @@ -357,33 +369,38 @@ private: } }; - template void lin(std::size_t &, std::size_t &) const noexcept {} + template + void lin(std::size_t&, std::size_t&) const noexcept {} template - void lin(std::size_t &idx, std::size_t &stride, int x, Ts... ts) const + void lin(std::size_t& idx, std::size_t& stride, int x, Ts... ts) const noexcept { apply_visitor(lin_visitor{idx, stride, x}, axes_[D]); lin(idx, stride, ts...); } - template struct xlin_visitor : public static_visitor { - std::size_t &idx; - std::size_t &stride; - const Value &val; - xlin_visitor(std::size_t &i, std::size_t &s, const Value &v) + template + struct xlin_visitor : public static_visitor { + std::size_t& idx; + std::size_t& stride; + const Value& val; + xlin_visitor(std::size_t& i, std::size_t& s, const Value& v) : idx(i), stride(s), val(v) {} - template void operator()(const Axis &a) const { + template + void operator()(const Axis& a) const { impl(std::is_convertible(), a); } - template void impl(std::true_type, const Axis &a) const { + template + void impl(std::true_type, const Axis& a) const { const auto a_size = a.size(); const auto a_shape = a.shape(); const auto j = a.index(val); detail::lin(idx, stride, a_size, a_shape, j); } - template void impl(std::false_type, const Axis &) const { + template + void impl(std::false_type, const Axis&) const { throw std::invalid_argument( detail::cat("fill argument not convertible to axis value type: ", boost::typeindex::type_id().pretty_name(), ", ", @@ -391,36 +408,39 @@ private: } }; - template void xlin(std::size_t &, std::size_t &) const {} + template + void xlin(std::size_t&, std::size_t&) const {} template - void xlin(std::size_t &idx, std::size_t &stride, T &&t, Ts &&... ts) const { + void xlin(std::size_t& idx, std::size_t& stride, T&& t, Ts&&... ts) const { apply_visitor(xlin_visitor{idx, stride, t}, axes_[D]); xlin<(D + 1)>(idx, stride, std::forward(ts)...); } template - void lin_iter(std::size_t &idx, std::size_t &stride, Iterator iter) const + void lin_iter(std::size_t& idx, std::size_t& stride, Iterator iter) const noexcept { - for (const auto &a : axes_) { + for (const auto& a : axes_) { apply_visitor(lin_visitor(idx, stride, *iter++), a); } } template - void xlin_iter(std::size_t &idx, std::size_t &stride, Iterator iter) const { - for (const auto &a : axes_) { + void xlin_iter(std::size_t& idx, std::size_t& stride, Iterator iter) const { + for (const auto& a : axes_) { apply_visitor(xlin_visitor{idx, stride, *iter++}, a); } } template - void xlin_get(mp11::mp_int<0>, std::size_t &, std::size_t &, T &&) const + void xlin_get(mp11::mp_int<0>, std::size_t&, std::size_t&, T&&) const noexcept {} template - void xlin_get(mp11::mp_int, std::size_t &idx, std::size_t &stride, - T &&t) const { + void xlin_get(mp11::mp_int, + std::size_t& idx, + std::size_t& stride, + T&& t) const { constexpr unsigned D = detail::mp_size::value - N; apply_visitor( xlin_visitor>{idx, stride, std::get(t)}, @@ -429,24 +449,26 @@ private: } template - void lin_get(mp11::mp_int<0>, std::size_t &, std::size_t &, T &&) const + void lin_get(mp11::mp_int<0>, std::size_t&, std::size_t&, T&&) const noexcept {} template - void lin_get(mp11::mp_int, std::size_t &idx, std::size_t &stride, - T &&t) const noexcept { + void lin_get(mp11::mp_int, + std::size_t& idx, + std::size_t& stride, + T&& t) const noexcept { constexpr unsigned D = detail::mp_size::value - N; apply_visitor(lin_visitor{idx, stride, static_cast(std::get(t))}, axes_[D]); lin_get(mp11::mp_int<(N - 1)>(), idx, stride, std::forward(t)); } - histogram reduce_impl(const std::vector &b) const { + histogram reduce_impl(const std::vector& b) const { axes_type axes; std::vector n(b.size()); auto axes_iter = axes_.begin(); auto n_iter = n.begin(); - for (const auto &bi : b) { + for (const auto& bi : b) { if (bi) axes.emplace_back(*axes_iter); *n_iter = axes_iter->shape(); @@ -461,17 +483,20 @@ private: return h; } - template friend class histogram; - template friend class iterator_over; + template + friend class histogram; + template + friend class iterator_over; friend class ::boost::python::access; friend class ::boost::serialization::access; - template void serialize(Archive &, unsigned); + template + void serialize(Archive&, unsigned); }; template dynamic_histogram< mp11::mp_set_push_back...>> -make_dynamic_histogram(Axis &&... axis) { +make_dynamic_histogram(Axis&&... axis) { return dynamic_histogram< mp11::mp_set_push_back...>>( std::forward(axis)...); @@ -479,8 +504,9 @@ make_dynamic_histogram(Axis &&... axis) { template dynamic_histogram< - mp11::mp_set_push_back...>, Storage> -make_dynamic_histogram_with(Axis &&... axis) { + mp11::mp_set_push_back...>, + Storage> +make_dynamic_histogram_with(Axis&&... axis) { return dynamic_histogram< mp11::mp_set_push_back...>, Storage>( std::forward(axis)...); @@ -495,7 +521,8 @@ make_dynamic_histogram(Iterator begin, Iterator end) { begin, end); } -template > dynamic_histogram< detail::mp_set_union, diff --git a/include/boost/histogram/histogram_fwd.hpp b/include/boost/histogram/histogram_fwd.hpp index f7373f16..9252dfd9 100644 --- a/include/boost/histogram/histogram_fwd.hpp +++ b/include/boost/histogram/histogram_fwd.hpp @@ -15,7 +15,8 @@ namespace boost { namespace histogram { class adaptive_storage; -template class array_storage; +template +class array_storage; namespace axis { @@ -28,20 +29,27 @@ struct pow; template class regular; -template class circular; -template class variable; -template class integer; -template class category; +template +class circular; +template +class variable; +template +class integer; +template +class category; using types = mp11::mp_list, axis::regular, axis::regular, axis::regular, - axis::circular, axis::variable, - axis::integer, axis::category, + axis::circular, + axis::variable, + axis::integer, + axis::category, axis::category>; -template class any; +template +class any; using any_std = mp11::mp_rename; } // namespace axis @@ -58,18 +66,30 @@ template using static_histogram = histogram; namespace detail { -template struct weight { T value; }; +template +struct weight { + T value; +}; // template struct is_weight : std::false_type {}; // template struct is_weight> : std::true_type {}; -template struct sample { T value; }; +template +struct sample { + T value; +}; // template struct is_sample : std::false_type {}; // template struct is_sample> : std::true_type {}; } // namespace detail -template detail::weight weight(T &&t) { return {t}; } +template +detail::weight weight(T&& t) { + return {t}; +} -template detail::sample sample(T &&t) { return {t}; } +template +detail::sample sample(T&& t) { + return {t}; +} } // namespace histogram } // namespace boost diff --git a/include/boost/histogram/iterator.hpp b/include/boost/histogram/iterator.hpp index 755369de..f653347a 100644 --- a/include/boost/histogram/iterator.hpp +++ b/include/boost/histogram/iterator.hpp @@ -21,15 +21,16 @@ namespace histogram { template class iterator_over - : public iterator_facade< - iterator_over, typename Histogram::element_type, - random_access_traversal_tag, typename Histogram::const_reference> { -public: - iterator_over(const Histogram &h, std::size_t idx) + : public iterator_facade, + typename Histogram::element_type, + random_access_traversal_tag, + typename Histogram::const_reference> { + public: + iterator_over(const Histogram& h, std::size_t idx) : histogram_(h), idx_(idx) {} - iterator_over(const iterator_over &) = default; - iterator_over &operator=(const iterator_over &) = default; + iterator_over(const iterator_over&) = default; + iterator_over& operator=(const iterator_over&) = default; unsigned dim() const noexcept { return histogram_.dim(); } @@ -39,23 +40,23 @@ public: } auto bin() const - -> decltype(std::declval().axis(mp11::mp_int<0>())[0]) { + -> decltype(std::declval().axis(mp11::mp_int<0>())[0]) { return histogram_.axis(mp11::mp_int<0>())[idx(0)]; } template auto bin(mp11::mp_int) const - -> decltype(std::declval().axis(mp11::mp_int())[0]) { + -> decltype(std::declval().axis(mp11::mp_int())[0]) { return histogram_.axis(mp11::mp_int())[idx(Dim)]; } template // use SFINAE for this method - auto bin(unsigned dim) const -> decltype(std::declval().axis(dim)[0]) { + auto bin(unsigned dim) const -> decltype(std::declval().axis(dim)[0]) { return histogram_.axis(dim)[idx(dim)]; } -private: - bool equal(const iterator_over &rhs) const noexcept { + private: + bool equal(const iterator_over& rhs) const noexcept { return &histogram_ == &rhs.histogram_ && idx_ == rhs.idx_; } @@ -68,7 +69,7 @@ private: return histogram_.storage_[idx_]; } - const Histogram &histogram_; + const Histogram& histogram_; std::size_t idx_; friend class ::boost::iterator_core_access; }; diff --git a/include/boost/histogram/literals.hpp b/include/boost/histogram/literals.hpp index 3ab0793f..a9cb2d8e 100644 --- a/include/boost/histogram/literals.hpp +++ b/include/boost/histogram/literals.hpp @@ -13,21 +13,56 @@ namespace boost { namespace histogram { namespace literals { namespace detail { -template struct char2int; -template <> struct char2int<'0'> { static constexpr int value = 0; }; -template <> struct char2int<'1'> { static constexpr int value = 1; }; -template <> struct char2int<'2'> { static constexpr int value = 2; }; -template <> struct char2int<'3'> { static constexpr int value = 3; }; -template <> struct char2int<'4'> { static constexpr int value = 4; }; -template <> struct char2int<'5'> { static constexpr int value = 5; }; -template <> struct char2int<'6'> { static constexpr int value = 6; }; -template <> struct char2int<'7'> { static constexpr int value = 7; }; -template <> struct char2int<'8'> { static constexpr int value = 8; }; -template <> struct char2int<'9'> { static constexpr int value = 9; }; +template +struct char2int; +template <> +struct char2int<'0'> { + static constexpr int value = 0; +}; +template <> +struct char2int<'1'> { + static constexpr int value = 1; +}; +template <> +struct char2int<'2'> { + static constexpr int value = 2; +}; +template <> +struct char2int<'3'> { + static constexpr int value = 3; +}; +template <> +struct char2int<'4'> { + static constexpr int value = 4; +}; +template <> +struct char2int<'5'> { + static constexpr int value = 5; +}; +template <> +struct char2int<'6'> { + static constexpr int value = 6; +}; +template <> +struct char2int<'7'> { + static constexpr int value = 7; +}; +template <> +struct char2int<'8'> { + static constexpr int value = 8; +}; +template <> +struct char2int<'9'> { + static constexpr int value = 9; +}; -template constexpr int parse() { return N; } +template +constexpr int parse() { + return N; +} -template constexpr int parse() { +template +constexpr int parse() { return parse::value, Rest...>(); } } // namespace detail diff --git a/include/boost/histogram/ostream_operators.hpp b/include/boost/histogram/ostream_operators.hpp index 9c0b4565..fde35639 100644 --- a/include/boost/histogram/ostream_operators.hpp +++ b/include/boost/histogram/ostream_operators.hpp @@ -16,16 +16,17 @@ namespace histogram { namespace detail { struct axis_ostream_visitor { - std::ostream &os_; - explicit axis_ostream_visitor(std::ostream &os) : os_(os) {} - template void operator()(const Axis &a) const { + std::ostream& os_; + explicit axis_ostream_visitor(std::ostream& os) : os_(os) {} + template + void operator()(const Axis& a) const { os_ << "\n " << a << ","; } }; } // namespace detail template -inline std::ostream &operator<<(std::ostream &os, const histogram &h) { +inline std::ostream& operator<<(std::ostream& os, const histogram& h) { os << "histogram("; h.for_each_axis(detail::axis_ostream_visitor(os)); os << (h.dim() ? "\n)" : ")"); @@ -33,7 +34,7 @@ inline std::ostream &operator<<(std::ostream &os, const histogram &h) { } template -inline std::ostream &operator<<(std::ostream &os, const weight_counter &x) { +inline std::ostream& operator<<(std::ostream& os, const weight_counter& x) { os << "weight_counter(" << x.value() << ", " << x.variance() << ")"; return os; } diff --git a/include/boost/histogram/serialization.hpp b/include/boost/histogram/serialization.hpp index 862b30bb..ab7a7156 100644 --- a/include/boost/histogram/serialization.hpp +++ b/include/boost/histogram/serialization.hpp @@ -28,89 +28,94 @@ namespace boost { namespace histogram { namespace detail { -template struct serialize_helper { - Archive &ar_; - explicit serialize_helper(Archive &ar) : ar_(ar) {} - template void operator()(T &t) const { ar_ &t; } +template +struct serialize_helper { + Archive& ar_; + explicit serialize_helper(Archive& ar) : ar_(ar) {} + template + void operator()(T& t) const { + ar_& t; + } }; } // namespace detail template template -void weight_counter::serialize(Archive &ar, unsigned /* version */) { - ar &w; - ar &w2; +void weight_counter::serialize(Archive& ar, unsigned /* version */) { + ar& w; + ar& w2; } template -void serialize(Archive &ar, array_storage &store, +void serialize(Archive& ar, + array_storage& store, unsigned /* version */) { - ar &store.array_; + ar& store.array_; } template -void adaptive_storage::serialize(Archive &ar, unsigned /* version */) { +void adaptive_storage::serialize(Archive& ar, unsigned /* version */) { auto size = this->size(); - ar &size; + ar& size; if (Archive::is_loading::value) { - auto tid = 0u; - ar &tid; - if (tid == 0u) { + auto type_id = 0u; + ar& type_id; + if (type_id == 0u) { buffer_ = detail::array(size); - } else if (tid == 1u) { + } else if (type_id == 1u) { detail::array a(size); - ar &serialization::make_array(a.begin(), size); + ar& serialization::make_array(a.begin(), size); buffer_ = std::move(a); - } else if (tid == 2u) { + } else if (type_id == 2u) { detail::array a(size); - ar &serialization::make_array(a.begin(), size); + ar& serialization::make_array(a.begin(), size); buffer_ = std::move(a); - } else if (tid == 3u) { + } else if (type_id == 3u) { detail::array a(size); - ar &serialization::make_array(a.begin(), size); + ar& serialization::make_array(a.begin(), size); buffer_ = std::move(a); - } else if (tid == 4u) { + } else if (type_id == 4u) { detail::array a(size); - ar &serialization::make_array(a.begin(), size); + ar& serialization::make_array(a.begin(), size); buffer_ = std::move(a); - } else if (tid == 5u) { + } else if (type_id == 5u) { detail::array a(size); - ar &serialization::make_array(a.begin(), size); + ar& serialization::make_array(a.begin(), size); buffer_ = std::move(a); - } else if (tid == 6u) { + } else if (type_id == 6u) { detail::array a(size); - ar &serialization::make_array(a.begin(), size); + ar& serialization::make_array(a.begin(), size); buffer_ = std::move(a); } } else { - auto tid = 0u; + auto type_id = 0u; if (get>(&buffer_)) { - tid = 0u; - ar &tid; - } else if (auto *a = get>(&buffer_)) { - tid = 1u; - ar &tid; - ar &serialization::make_array(a->begin(), size); - } else if (auto *a = get>(&buffer_)) { - tid = 2u; - ar &tid; - ar &serialization::make_array(a->begin(), size); - } else if (auto *a = get>(&buffer_)) { - tid = 3u; - ar &tid; - ar &serialization::make_array(a->begin(), size); - } else if (auto *a = get>(&buffer_)) { - tid = 4u; - ar &tid; - ar &serialization::make_array(a->begin(), size); - } else if (auto *a = get>(&buffer_)) { - tid = 5u; - ar &tid; - ar &serialization::make_array(a->begin(), size); - } else if (auto *a = get>(&buffer_)) { - tid = 6u; - ar &tid; - ar &serialization::make_array(a->begin(), size); + type_id = 0u; + ar& type_id; + } else if (auto* a = get>(&buffer_)) { + type_id = 1u; + ar& type_id; + ar& serialization::make_array(a->begin(), size); + } else if (auto* a = get>(&buffer_)) { + type_id = 2u; + ar& type_id; + ar& serialization::make_array(a->begin(), size); + } else if (auto* a = get>(&buffer_)) { + type_id = 3u; + ar& type_id; + ar& serialization::make_array(a->begin(), size); + } else if (auto* a = get>(&buffer_)) { + type_id = 4u; + ar& type_id; + ar& serialization::make_array(a->begin(), size); + } else if (auto* a = get>(&buffer_)) { + type_id = 5u; + ar& type_id; + ar& serialization::make_array(a->begin(), size); + } else if (auto* a = get>(&buffer_)) { + type_id = 6u; + ar& type_id; + ar& serialization::make_array(a->begin(), size); } } } @@ -118,84 +123,84 @@ void adaptive_storage::serialize(Archive &ar, unsigned /* version */) { namespace axis { template -void base::serialize(Archive &ar, unsigned /* version */) { - ar &size_; - ar &shape_; - ar &label_; +void base::serialize(Archive& ar, unsigned /* version */) { + ar& size_; + ar& shape_; + ar& label_; } namespace transform { template -void pow::serialize(Archive &ar, unsigned /* version */) { - ar &power; +void pow::serialize(Archive& ar, unsigned /* version */) { + ar& power; } } // namespace transform template template -void regular::serialize(Archive &ar, +void regular::serialize(Archive& ar, unsigned /* version */) { - ar &boost::serialization::base_object(*this); - ar &boost::serialization::base_object(*this); - ar &min_; - ar &delta_; + ar& boost::serialization::base_object(*this); + ar& boost::serialization::base_object(*this); + ar& min_; + ar& delta_; } template template -void circular::serialize(Archive &ar, unsigned /* version */) { - ar &boost::serialization::base_object(*this); - ar &phase_; - ar &perimeter_; +void circular::serialize(Archive& ar, unsigned /* version */) { + ar& boost::serialization::base_object(*this); + ar& phase_; + ar& perimeter_; } template template -void variable::serialize(Archive &ar, unsigned /* version */) { - ar &boost::serialization::base_object(*this); +void variable::serialize(Archive& ar, unsigned /* version */) { + ar& boost::serialization::base_object(*this); if (Archive::is_loading::value) { x_.reset(new RealType[base::size() + 1]); } - ar &boost::serialization::make_array(x_.get(), base::size() + 1); + ar& boost::serialization::make_array(x_.get(), base::size() + 1); } template template -void integer::serialize(Archive &ar, unsigned /* version */) { - ar &boost::serialization::base_object(*this); - ar &min_; +void integer::serialize(Archive& ar, unsigned /* version */) { + ar& boost::serialization::base_object(*this); + ar& min_; } template template -void category::serialize(Archive &ar, unsigned /* version */) { - ar &boost::serialization::base_object(*this); - ar &map_; +void category::serialize(Archive& ar, unsigned /* version */) { + ar& boost::serialization::base_object(*this); + ar& map_; } template template -void any::serialize(Archive &ar, unsigned /* version */) { - ar &boost::serialization::base_object>(*this); +void any::serialize(Archive& ar, unsigned /* version */) { + ar& boost::serialization::base_object>(*this); } } // namespace axis template template -void histogram::serialize(Archive &ar, +void histogram::serialize(Archive& ar, unsigned /* version */) { detail::serialize_helper sh(ar); mp11::tuple_for_each(axes_, sh); - ar &storage_; + ar& storage_; } template template -void histogram::serialize(Archive &ar, +void histogram::serialize(Archive& ar, unsigned /* version */) { - ar &axes_; - ar &storage_; + ar& axes_; + ar& storage_; } } // namespace histogram diff --git a/include/boost/histogram/static_histogram.hpp b/include/boost/histogram/static_histogram.hpp index e5fc38a2..0441f438 100644 --- a/include/boost/histogram/static_histogram.hpp +++ b/include/boost/histogram/static_histogram.hpp @@ -37,7 +37,7 @@ class histogram { using axes_size = mp11::mp_size; static_assert(axes_size::value > 0, "at least one axis required"); -public: + public: using axes_type = mp11::mp_rename; using element_type = typename Storage::element_type; using const_reference = typename Storage::const_reference; @@ -45,33 +45,34 @@ public: using iterator = const_iterator; histogram() = default; - histogram(const histogram &rhs) = default; - histogram(histogram &&rhs) = default; - histogram &operator=(const histogram &rhs) = default; - histogram &operator=(histogram &&rhs) = default; + histogram(const histogram& rhs) = default; + histogram(histogram&& rhs) = default; + histogram& operator=(const histogram& rhs) = default; + histogram& operator=(histogram&& rhs) = default; - template > - explicit histogram(Axis0 &&axis0, Axis &&... axis) + explicit histogram(Axis0&& axis0, Axis&&... axis) : axes_(std::forward(axis0), std::forward(axis)...) { storage_ = Storage(size_from_axes()); index_cache_.reset(*this); } - explicit histogram(axes_type &&axes) : axes_(std::move(axes)) { + explicit histogram(axes_type&& axes) : axes_(std::move(axes)) { storage_ = Storage(size_from_axes()); index_cache_.reset(*this); } template - explicit histogram(const static_histogram &rhs) + explicit histogram(const static_histogram& rhs) : axes_(rhs.axes_), storage_(rhs.storage_) { index_cache_.reset(*this); } template - histogram &operator=(const static_histogram &rhs) { - if (static_cast(this) != static_cast(&rhs)) { + histogram& operator=(const static_histogram& rhs) { + if (static_cast(this) != static_cast(&rhs)) { axes_ = rhs.axes_; storage_ = rhs.storage_; } @@ -79,15 +80,15 @@ public: } template - explicit histogram(const dynamic_histogram &rhs) + explicit histogram(const dynamic_histogram& rhs) : storage_(rhs.storage_) { detail::axes_assign(axes_, rhs.axes_); index_cache_.reset(*this); } template - histogram &operator=(const dynamic_histogram &rhs) { - if (static_cast(this) != static_cast(&rhs)) { + histogram& operator=(const dynamic_histogram& rhs) { + if (static_cast(this) != static_cast(&rhs)) { detail::axes_assign(axes_, rhs.axes_); storage_ = rhs.storage_; index_cache_.reset(*this); @@ -96,27 +97,27 @@ public: } template - bool operator==(const static_histogram &) const noexcept { + bool operator==(const static_histogram&) const noexcept { return false; } template - bool operator==(const static_histogram &rhs) const noexcept { + bool operator==(const static_histogram& rhs) const noexcept { return detail::axes_equal(axes_, rhs.axes_) && storage_ == rhs.storage_; } template - bool operator==(const dynamic_histogram &rhs) const noexcept { + bool operator==(const dynamic_histogram& rhs) const noexcept { return detail::axes_equal(axes_, rhs.axes_) && storage_ == rhs.storage_; } template - bool operator!=(const histogram &rhs) const noexcept { + bool operator!=(const histogram& rhs) const noexcept { return !operator==(rhs); } template - histogram &operator+=(const static_histogram &rhs) { + histogram& operator+=(const static_histogram& rhs) { if (!detail::axes_equal(axes_, rhs.axes_)) throw std::invalid_argument("axes of histograms differ"); storage_ += rhs.storage_; @@ -124,24 +125,27 @@ public: } template - histogram &operator+=(const dynamic_histogram &rhs) { + histogram& operator+=(const dynamic_histogram& rhs) { if (!detail::axes_equal(axes_, rhs.axes_)) throw std::invalid_argument("axes of histograms differ"); storage_ += rhs.storage_; return *this; } - template histogram &operator*=(const T &rhs) { + template + histogram& operator*=(const T& rhs) { storage_ *= rhs; return *this; } - template histogram &operator/=(const T &rhs) { + template + histogram& operator/=(const T& rhs) { storage_ *= 1.0 / rhs; return *this; } - template void operator()(Ts &&... ts) { + template + void operator()(Ts&&... ts) { // case with one argument is ambiguous, is specialized below static_assert(sizeof...(Ts) == axes_size::value, "fill arguments do not match histogram dimension"); @@ -151,7 +155,8 @@ public: detail::fill_storage(storage_, idx); } - template void operator()(T &&t) { + template + void operator()(T&& t) { // check whether we need to unpack argument fill_impl(mp11::mp_if_c<(axes_size::value == 1), detail::no_container_tag, detail::classify_container>(), @@ -160,7 +165,7 @@ public: // TODO: merge this with unpacking template - void operator()(detail::weight &&w, Ts &&... ts) { + void operator()(detail::weight&& w, Ts&&... ts) { // case with one argument is ambiguous, is specialized below std::size_t idx = 0, stride = 1; xlin<0>(idx, stride, ts...); @@ -170,14 +175,15 @@ public: // TODO: remove as obsolete template - void operator()(detail::weight &&w, T &&t) { + void operator()(detail::weight&& w, T&& t) { // check whether we need to unpack argument fill_impl(mp11::mp_if_c<(axes_size::value == 1), detail::no_container_tag, detail::classify_container>(), std::forward(t), std::move(w)); } - template const_reference at(Ts &&... ts) const { + template + const_reference at(Ts&&... ts) const { // case with one argument is ambiguous, is specialized below static_assert(sizeof...(ts) == axes_size::value, "bin arguments do not match histogram dimension"); @@ -186,12 +192,14 @@ public: return detail::storage_get(storage_, idx, stride == 0); } - template const_reference operator[](T &&t) const { + template + const_reference operator[](T&& t) const { // check whether we need to unpack argument return at_impl(detail::classify_container(), std::forward(t)); } - template const_reference at(T &&t) const { + template + const_reference at(T&& t) const { // check whether we need to unpack argument return at_impl(detail::classify_container(), std::forward(t)); } @@ -207,33 +215,34 @@ public: /// Get N-th axis (const version) template - typename std::add_const::type>::type - &axis(mp11::mp_int) const { + typename std::add_const::type>:: + type& axis(mp11::mp_int) const { static_assert(N < axes_size::value, "axis index out of range"); return std::get(axes_); } /// Get N-th axis template - typename std::tuple_element::type &axis(mp11::mp_int) { + typename std::tuple_element::type& axis(mp11::mp_int) { static_assert(N < axes_size::value, "axis index out of range"); return std::get(axes_); } // Get first axis (convenience for 1-d histograms, const version) constexpr typename std::add_const< - typename std::tuple_element<0, axes_type>::type>::type & + typename std::tuple_element<0, axes_type>::type>::type& axis() const { return std::get<0>(axes_); } // Get first axis (convenience for 1-d histograms) - typename std::tuple_element<0, axes_type>::type &axis() { + typename std::tuple_element<0, axes_type>::type& axis() { return std::get<0>(axes_); } /// Apply unary functor/function to each axis - template void for_each_axis(Unary &&unary) const { + template + void for_each_axis(Unary&& unary) const { mp11::tuple_for_each(axes_, std::forward(unary)); } @@ -255,7 +264,7 @@ public: const_iterator end() const noexcept { return const_iterator(*this, size()); } -private: + private: axes_type axes_; Storage storage_; mutable detail::index_cache index_cache_; @@ -267,10 +276,10 @@ private: } template - void fill_impl(detail::dynamic_container_tag, T &&t, Ts &&... ts) { - BOOST_ASSERT_MSG(std::distance(std::begin(t), std::end(t)) == - axes_size::value, - "fill container does not match histogram dimension"); + void fill_impl(detail::dynamic_container_tag, T&& t, Ts&&... ts) { + BOOST_ASSERT_MSG( + std::distance(std::begin(t), std::end(t)) == axes_size::value, + "fill container does not match histogram dimension"); std::size_t idx = 0, stride = 1; xlin_iter(axes_size(), idx, stride, std::begin(t)); if (stride) { @@ -279,7 +288,7 @@ private: } template - void fill_impl(detail::static_container_tag, T &&t, Ts &&... ts) { + void fill_impl(detail::static_container_tag, T&& t, Ts&&... ts) { static_assert(detail::mp_size::value == axes_size::value, "fill container does not match histogram dimension"); std::size_t idx = 0, stride = 1; @@ -290,7 +299,7 @@ private: } template - void fill_impl(detail::no_container_tag, T &&t, Ts &&... ts) { + void fill_impl(detail::no_container_tag, T&& t, Ts&&... ts) { static_assert(axes_size::value == 1, "fill argument does not match histogram dimension"); std::size_t idx = 0, stride = 1; @@ -301,17 +310,17 @@ private: } template - const_reference at_impl(detail::dynamic_container_tag, T &&t) const { - BOOST_ASSERT_MSG(std::distance(std::begin(t), std::end(t)) == - axes_size::value, - "bin container does not match histogram dimension"); + const_reference at_impl(detail::dynamic_container_tag, T&& t) const { + BOOST_ASSERT_MSG( + std::distance(std::begin(t), std::end(t)) == axes_size::value, + "bin container does not match histogram dimension"); std::size_t idx = 0, stride = 1; lin_iter(axes_size(), idx, stride, std::begin(t)); return detail::storage_get(storage_, idx, stride == 0); } template - const_reference at_impl(detail::static_container_tag, T &&t) const { + const_reference at_impl(detail::static_container_tag, T&& t) const { static_assert(mp11::mp_size::value == axes_size::value, "bin container does not match histogram dimension"); std::size_t idx = 0, stride = 1; @@ -320,17 +329,17 @@ private: } template - const_reference at_impl(detail::no_container_tag, T &&t) const { + const_reference at_impl(detail::no_container_tag, T&& t) const { std::size_t idx = 0, stride = 1; lin<0>(idx, stride, detail::indirect_int_cast(t)); return detail::storage_get(storage_, idx, stride == 0); } template - void xlin(std::size_t &, std::size_t &) const noexcept {} + void xlin(std::size_t&, std::size_t&) const noexcept {} template - void xlin(std::size_t &idx, std::size_t &stride, T &&t, Ts &&... ts) const { + void xlin(std::size_t& idx, std::size_t& stride, T&& t, Ts&&... ts) const { const auto a_size = std::get(axes_).size(); const auto a_shape = std::get(axes_).shape(); const int j = std::get(axes_).index(t); @@ -339,11 +348,13 @@ private: } template - void xlin_iter(mp11::mp_size_t<0>, std::size_t &, std::size_t &, - Iterator) const noexcept {} + void xlin_iter(mp11::mp_size_t<0>, std::size_t&, std::size_t&, Iterator) const + noexcept {} template - void xlin_iter(mp11::mp_size_t, std::size_t &idx, std::size_t &stride, + void xlin_iter(mp11::mp_size_t, + std::size_t& idx, + std::size_t& stride, Iterator iter) const { constexpr unsigned D = axes_size::value - N; const auto a_size = std::get(axes_).size(); @@ -353,10 +364,11 @@ private: xlin_iter(mp11::mp_size_t(), idx, stride, ++iter); } - template void lin(std::size_t &, std::size_t &) const noexcept {} + template + void lin(std::size_t&, std::size_t&) const noexcept {} template - void lin(std::size_t &idx, std::size_t &stride, int j, Ts... ts) const + void lin(std::size_t& idx, std::size_t& stride, int j, Ts... ts) const noexcept { const auto a_size = std::get(axes_).size(); const auto a_shape = std::get(axes_).shape(); @@ -366,11 +378,13 @@ private: } template - void lin_iter(mp11::mp_size_t<0>, std::size_t &, std::size_t &, - Iterator) const noexcept {} + void lin_iter(mp11::mp_size_t<0>, std::size_t&, std::size_t&, Iterator) const + noexcept {} template - void lin_iter(mp11::mp_size_t, std::size_t &idx, std::size_t &stride, + void lin_iter(mp11::mp_size_t, + std::size_t& idx, + std::size_t& stride, Iterator iter) const noexcept { constexpr unsigned D = axes_size::value - N; const auto a_size = std::get(axes_).size(); @@ -382,12 +396,14 @@ private: } template - void xlin_get(mp11::mp_size_t<0>, std::size_t &, std::size_t &, T &&) const + void xlin_get(mp11::mp_size_t<0>, std::size_t&, std::size_t&, T&&) const noexcept {} template - void xlin_get(mp11::mp_size_t, std::size_t &idx, std::size_t &stride, - T &&t) const { + void xlin_get(mp11::mp_size_t, + std::size_t& idx, + std::size_t& stride, + T&& t) const { constexpr unsigned D = detail::mp_size::value - N; const auto a_size = std::get(axes_).size(); const auto a_shape = std::get(axes_).shape(); @@ -397,12 +413,14 @@ private: } template - void lin_get(mp11::mp_size_t<0>, std::size_t &, std::size_t &, T &&) const + void lin_get(mp11::mp_size_t<0>, std::size_t&, std::size_t&, T&&) const noexcept {} template - void lin_get(mp11::mp_size_t, std::size_t &idx, std::size_t &stride, - T &&t) const noexcept { + void lin_get(mp11::mp_size_t, + std::size_t& idx, + std::size_t& stride, + T&& t) const noexcept { constexpr unsigned D = detail::mp_size::value - N; const auto a_size = std::get(axes_).size(); const auto a_shape = std::get(axes_).shape(); @@ -413,7 +431,7 @@ private: } template - void reduce_impl(H &h, const std::vector &b) const { + void reduce_impl(H& h, const std::vector& b) const { detail::shape_vector_visitor v(dim()); for_each_axis(v); detail::index_mapper m(v.shapes, b); @@ -422,16 +440,19 @@ private: } while (m.next()); } - template friend class histogram; - template friend class iterator_over; + template + friend class histogram; + template + friend class iterator_over; friend class ::boost::serialization::access; - template void serialize(Archive &, unsigned); + template + void serialize(Archive&, unsigned); }; /// default static type factory template static_histogram...>> -make_static_histogram(Axis &&... axis) { +make_static_histogram(Axis&&... axis) { return static_histogram...>>( std::forward(axis)...); } @@ -439,7 +460,7 @@ make_static_histogram(Axis &&... axis) { /// static type factory with variable storage type template static_histogram...>, Storage> -make_static_histogram_with(Axis &&... axis) { +make_static_histogram_with(Axis&&... axis) { return static_histogram...>, Storage>( std::forward(axis)...); } diff --git a/include/boost/histogram/storage/adaptive_storage.hpp b/include/boost/histogram/storage/adaptive_storage.hpp index 8239eb0c..9deb401c 100644 --- a/include/boost/histogram/storage/adaptive_storage.hpp +++ b/include/boost/histogram/storage/adaptive_storage.hpp @@ -43,9 +43,10 @@ namespace detail { using mp_int = ::boost::multiprecision::cpp_int; using wcount = ::boost::histogram::weight_counter; -template inline T *alloc(std::size_t s) { +template +inline T* alloc(std::size_t s) { #ifdef BOOST_HISTOGRAM_TRACE_ALLOCS - boost::core::typeinfo const &ti = BOOST_CORE_TYPEID(T); + boost::core::typeinfo const& ti = BOOST_CORE_TYPEID(T); std::cerr << "alloc " << boost::core::demangled_name(ti) << "[" << s << "]" << std::endl; #endif @@ -53,13 +54,13 @@ template inline T *alloc(std::size_t s) { } class array_base { -public: + public: explicit array_base(const std::size_t s) : size(s) {} array_base() = default; - array_base(const array_base &) = default; - array_base &operator=(const array_base &) = default; - array_base(array_base &&rhs) : size(rhs.size) { rhs.size = 0; } - array_base &operator=(array_base &&rhs) { + array_base(const array_base&) = default; + array_base& operator=(const array_base&) = default; + array_base(array_base&& rhs) : size(rhs.size) { rhs.size = 0; } + array_base& operator=(array_base&& rhs) { if (this != &rhs) { size = rhs.size; rhs.size = 0; @@ -69,16 +70,17 @@ public: std::size_t size = 0; }; -template class array : public array_base { -public: +template +class array : public array_base { + public: explicit array(const std::size_t s) : array_base(s), ptr(alloc(s)) { std::fill(begin(), end(), T(0)); } array() = default; - array(const array &rhs) : array_base(rhs), ptr(alloc(rhs.size)) { + array(const array& rhs) : array_base(rhs), ptr(alloc(rhs.size)) { std::copy(rhs.begin(), rhs.end(), begin()); } - array &operator=(const array &rhs) { + array& operator=(const array& rhs) { if (this != &rhs) { if (size != rhs.size) { size = rhs.size; @@ -88,10 +90,10 @@ public: } return *this; } - array(array &&rhs) : array_base(std::move(rhs)), ptr(std::move(rhs.ptr)) { + array(array&& rhs) : array_base(std::move(rhs)), ptr(std::move(rhs.ptr)) { rhs.size = 0; } - array &operator=(array &&rhs) { + array& operator=(array&& rhs) { if (this != &rhs) { size = rhs.size; ptr = std::move(rhs.ptr); @@ -102,48 +104,69 @@ public: // copy only up to nmax elements template - array(const array &rhs, + array(const array& rhs, std::size_t nmax = std::numeric_limits::max()) : array_base(rhs), ptr(alloc(rhs.size)) { std::copy(rhs.begin(), rhs.begin() + std::min(nmax, size), begin()); } - T &operator[](const std::size_t i) { return ptr[i]; } - const T &operator[](const std::size_t i) const { return ptr[i]; } + T& operator[](const std::size_t i) { return ptr[i]; } + const T& operator[](const std::size_t i) const { return ptr[i]; } - T *begin() { return ptr.get(); } - T *end() { return ptr.get() + size; } - const T *begin() const { return ptr.get(); } - const T *end() const { return ptr.get() + size; } + T* begin() { return ptr.get(); } + T* end() { return ptr.get() + size; } + const T* begin() const { return ptr.get(); } + const T* end() const { return ptr.get() + size; } -private: + private: std::unique_ptr ptr; }; -template <> class array : public array_base { -public: +template <> +class array : public array_base { + public: using array_base::array_base; }; -using any_array = - variant, array, array, array, - array, array, array>; +using any_array = variant, + array, + array, + array, + array, + array, + array>; -template struct next_type; -template <> struct next_type { using type = uint16_t; }; -template <> struct next_type { using type = uint32_t; }; -template <> struct next_type { using type = uint64_t; }; -template <> struct next_type { using type = mp_int; }; -template using next = typename next_type::type; +template +struct next_type; +template <> +struct next_type { + using type = uint16_t; +}; +template <> +struct next_type { + using type = uint32_t; +}; +template <> +struct next_type { + using type = uint64_t; +}; +template <> +struct next_type { + using type = mp_int; +}; +template +using next = typename next_type::type; -template inline bool safe_increase(T &t) { +template +inline bool safe_increase(T& t) { if (t == std::numeric_limits::max()) return false; ++t; return true; } -template inline bool safe_assign(T &t, const U &u) { +template +inline bool safe_assign(T& t, const U& u) { if (std::numeric_limits::max() < std::numeric_limits::max() && std::numeric_limits::max() < u) return false; @@ -151,7 +174,8 @@ template inline bool safe_assign(T &t, const U &u) { return true; } -template inline bool safe_radd(T &t, const U &u) { +template +inline bool safe_radd(T& t, const U& u) { if (static_cast(std::numeric_limits::max() - t) < u) return false; t += static_cast(u); @@ -159,7 +183,8 @@ template inline bool safe_radd(T &t, const U &u) { } // float rounding is a mess, the equal sign is necessary here -template inline bool safe_radd(T &t, const double u) { +template +inline bool safe_radd(T& t, const double u) { if ((std::numeric_limits::max() - t) <= u) return false; t += u; @@ -167,41 +192,45 @@ template inline bool safe_radd(T &t, const double u) { } struct size_visitor : public static_visitor { - template std::size_t operator()(const Array &b) const { + template + std::size_t operator()(const Array& b) const { return b.size; } }; -template struct assign_visitor : public static_visitor { - any_array &lhs_any; +template +struct assign_visitor : public static_visitor { + any_array& lhs_any; const std::size_t idx; - const RHS &rhs; - assign_visitor(any_array &a, const std::size_t i, const RHS &x) + const RHS& rhs; + assign_visitor(any_array& a, const std::size_t i, const RHS& x) : lhs_any(a), idx(i), rhs(x) {} - template void operator()(array &lhs) const { + template + void operator()(array& lhs) const { if (!safe_assign(lhs[idx], rhs)) { lhs_any = array>(lhs, idx); operator()(get>>(lhs_any)); } } - void operator()(array &lhs) const { + void operator()(array& lhs) const { lhs_any = array(lhs.size); operator()(get>(lhs_any)); } - void operator()(array &lhs) const { lhs[idx].assign(rhs); } + void operator()(array& lhs) const { lhs[idx].assign(rhs); } - void operator()(array &lhs) const { lhs[idx] = rhs; } + void operator()(array& lhs) const { lhs[idx] = rhs; } }; struct increase_visitor : public static_visitor { - any_array &lhs_any; + any_array& lhs_any; const std::size_t idx; - increase_visitor(any_array &a, const std::size_t i) : lhs_any(a), idx(i) {} + increase_visitor(any_array& a, const std::size_t i) : lhs_any(a), idx(i) {} - template void operator()(array &lhs) const { + template + void operator()(array& lhs) const { if (!safe_increase(lhs[idx])) { array> a = lhs; ++a[idx]; @@ -209,147 +238,158 @@ struct increase_visitor : public static_visitor { } } - void operator()(array &lhs) const { + void operator()(array& lhs) const { array a(lhs.size); ++a[idx]; lhs_any = std::move(a); } - void operator()(array &lhs) const { ++lhs[idx]; } + void operator()(array& lhs) const { ++lhs[idx]; } - void operator()(array &lhs) const { ++lhs[idx]; } + void operator()(array& lhs) const { ++lhs[idx]; } }; struct bin_visitor : public static_visitor { const std::size_t idx; bin_visitor(const std::size_t i) : idx(i) {} - template wcount operator()(const Array &b) const { + template + wcount operator()(const Array& b) const { return wcount(static_cast(b[idx])); } - wcount operator()(const array & /*b*/) const { return wcount(0.0); } + wcount operator()(const array& /*b*/) const { return wcount(0.0); } - wcount operator()(const array &b) const { return b[idx]; } + wcount operator()(const array& b) const { return b[idx]; } }; -template struct radd_visitor : public static_visitor { - any_array &lhs_any; +template +struct radd_visitor : public static_visitor { + any_array& lhs_any; const std::size_t idx; - const RHS &rhs; - radd_visitor(any_array &l, const std::size_t i, const RHS &r) + const RHS& rhs; + radd_visitor(any_array& l, const std::size_t i, const RHS& r) : lhs_any(l), idx(i), rhs(r) {} - template void operator()(array &lhs) const { + template + void operator()(array& lhs) const { if (!safe_radd(lhs[idx], rhs)) { lhs_any = array>(lhs); operator()(get>>(lhs_any)); } } - void operator()(array &lhs) const { + void operator()(array& lhs) const { if (rhs != 0) { lhs_any = array(lhs.size); operator()(get>(lhs_any)); } } - void operator()(array &lhs) const { + void operator()(array& lhs) const { lhs[idx] += static_cast(rhs); } - void operator()(array &lhs) const { lhs[idx] += rhs; } + void operator()(array& lhs) const { lhs[idx] += rhs; } }; -template <> struct radd_visitor : public static_visitor { - any_array &lhs_any; +template <> +struct radd_visitor : public static_visitor { + any_array& lhs_any; const std::size_t idx; - const mp_int &rhs; - radd_visitor(any_array &l, const std::size_t i, const mp_int &r) + const mp_int& rhs; + radd_visitor(any_array& l, const std::size_t i, const mp_int& r) : lhs_any(l), idx(i), rhs(r) {} - template void operator()(array &lhs) const { + template + void operator()(array& lhs) const { if (!safe_radd(lhs[idx], rhs)) { lhs_any = array>(lhs); operator()(get>>(lhs_any)); } } - void operator()(array &lhs) const { + void operator()(array& lhs) const { if (rhs != 0) { lhs_any = array(lhs.size); operator()(get>(lhs_any)); } } - void operator()(array &lhs) const { lhs[idx] += rhs; } + void operator()(array& lhs) const { lhs[idx] += rhs; } - void operator()(array &lhs) const { + void operator()(array& lhs) const { lhs[idx] += static_cast(rhs); } }; -template <> struct radd_visitor : public static_visitor { - any_array &lhs_any; +template <> +struct radd_visitor : public static_visitor { + any_array& lhs_any; const std::size_t idx; - const wcount &rhs; - radd_visitor(any_array &l, const std::size_t i, const wcount &r) + const wcount& rhs; + radd_visitor(any_array& l, const std::size_t i, const wcount& r) : lhs_any(l), idx(i), rhs(r) {} - template void operator()(array &lhs) const { + template + void operator()(array& lhs) const { lhs_any = array(lhs); operator()(get>(lhs_any)); } - void operator()(array &lhs) const { + void operator()(array& lhs) const { lhs_any = array(lhs.size); operator()(get>(lhs_any)); } - void operator()(array &lhs) const { lhs[idx] += rhs; } + void operator()(array& lhs) const { lhs[idx] += rhs; } }; -template <> struct radd_visitor> : public static_visitor { - any_array &lhs_any; +template <> +struct radd_visitor> : public static_visitor { + any_array& lhs_any; const std::size_t idx; const weight rhs; - radd_visitor(any_array &l, const std::size_t i, const double w) + radd_visitor(any_array& l, const std::size_t i, const double w) : lhs_any(l), idx(i), rhs{w} {} - template void operator()(array &lhs) const { + template + void operator()(array& lhs) const { lhs_any = array(lhs); operator()(get>(lhs_any)); } - void operator()(array &lhs) const { + void operator()(array& lhs) const { lhs_any = array(lhs.size); operator()(get>(lhs_any)); } - void operator()(array &lhs) const { lhs[idx] += rhs; } + void operator()(array& lhs) const { lhs[idx] += rhs; } }; // precondition: both arrays must have same size and may not be identical struct radd_array_visitor : public static_visitor { - any_array &lhs_any; - radd_array_visitor(any_array &l) : lhs_any(l) {} - template void operator()(const array &rhs) const { + any_array& lhs_any; + radd_array_visitor(any_array& l) : lhs_any(l) {} + template + void operator()(const array& rhs) const { for (std::size_t i = 0; i < rhs.size; ++i) apply_visitor(radd_visitor(lhs_any, i, rhs[i]), lhs_any); } - void operator()(const array &) const {} + void operator()(const array&) const {} }; struct rmul_visitor : public static_visitor { - any_array &lhs_any; + any_array& lhs_any; const double x; - rmul_visitor(any_array &l, const double v) : lhs_any(l), x(v) {} - template void operator()(array &lhs) const { + rmul_visitor(any_array& l, const double v) : lhs_any(l), x(v) {} + template + void operator()(array& lhs) const { lhs_any = array(lhs); operator()(get>(lhs_any)); } - void operator()(array &) const {} - void operator()(array &lhs) const { + void operator()(array&) const {} + void operator()(array& lhs) const { for (std::size_t i = 0; i != lhs.size; ++i) lhs[i] *= x; } @@ -357,25 +397,25 @@ struct rmul_visitor : public static_visitor { struct bicmp_visitor : public static_visitor { template - bool operator()(const array &b1, const array &b2) const { + bool operator()(const array& b1, const array& b2) const { if (b1.size != b2.size) return false; return std::equal(b1.begin(), b1.end(), b2.begin()); } template - bool operator()(const array &b1, const array &b2) const { + bool operator()(const array& b1, const array& b2) const { if (b1.size != b2.size) return false; - return std::all_of(b1.begin(), b1.end(), [](const T &t) { return t == 0; }); + return std::all_of(b1.begin(), b1.end(), [](const T& t) { return t == 0; }); } template - bool operator()(const array &b1, const array &b2) const { + bool operator()(const array& b1, const array& b2) const { return operator()(b2, b1); } - bool operator()(const array &b1, const array &b2) const { + bool operator()(const array& b1, const array& b2) const { return b1.size == b2.size; } }; @@ -385,20 +425,20 @@ struct bicmp_visitor : public static_visitor { class adaptive_storage { using buffer_type = detail::any_array; -public: + public: using element_type = detail::wcount; using const_reference = element_type; explicit adaptive_storage(std::size_t s) : buffer_(detail::array(s)) {} adaptive_storage() = default; - adaptive_storage(const adaptive_storage &) = default; - adaptive_storage &operator=(const adaptive_storage &) = default; - adaptive_storage(adaptive_storage &&) = default; - adaptive_storage &operator=(adaptive_storage &&) = default; + adaptive_storage(const adaptive_storage&) = default; + adaptive_storage& operator=(const adaptive_storage&) = default; + adaptive_storage(adaptive_storage&&) = default; + adaptive_storage& operator=(adaptive_storage&&) = default; template - explicit adaptive_storage(const RHS &rhs) + explicit adaptive_storage(const RHS& rhs) : buffer_(detail::array(rhs.size())) { using T = typename RHS::element_type; for (std::size_t i = 0, n = rhs.size(); i < n; ++i) { @@ -406,7 +446,8 @@ public: } } - template adaptive_storage &operator=(const RHS &rhs) { + template + adaptive_storage& operator=(const RHS& rhs) { // no check for self-assign needed, default operator above is better match const auto n = rhs.size(); if (size() != n) { @@ -421,7 +462,7 @@ public: // used in unit tests template - explicit adaptive_storage(const detail::array &a) : buffer_(a) {} + explicit adaptive_storage(const detail::array& a) : buffer_(a) {} std::size_t size() const { return apply_visitor(detail::size_visitor(), buffer_); @@ -431,7 +472,7 @@ public: apply_visitor(detail::increase_visitor(buffer_, i), buffer_); } - void add(std::size_t i, const element_type &x) { + void add(std::size_t i, const element_type& x) { if (x.variance() == x.value()) { apply_visitor(detail::radd_visitor(buffer_, i, x.value()), buffer_); @@ -440,11 +481,13 @@ public: } } - template void add(std::size_t i, const T &t) { + template + void add(std::size_t i, const T& t) { apply_visitor(detail::radd_visitor(buffer_, i, t), buffer_); } - template void add(std::size_t i, const detail::weight &w) { + template + void add(std::size_t i, const detail::weight& w) { apply_visitor( detail::radd_visitor>(buffer_, i, w.value), buffer_); @@ -454,12 +497,12 @@ public: return apply_visitor(detail::bin_visitor(i), buffer_); } - bool operator==(const adaptive_storage &rhs) const { + bool operator==(const adaptive_storage& rhs) const { return apply_visitor(detail::bicmp_visitor(), buffer_, rhs.buffer_); } // precondition: storages have same size - adaptive_storage &operator+=(const adaptive_storage &rhs) { + adaptive_storage& operator+=(const adaptive_storage& rhs) { if (this == &rhs) { for (std::size_t i = 0, n = size(); i < n; ++i) { add(i, rhs[i]); // may lose precision @@ -471,7 +514,8 @@ public: } // precondition: storages have same size - template adaptive_storage &operator+=(const RHS &rhs) { + template + adaptive_storage& operator+=(const RHS& rhs) { for (std::size_t i = 0, n = size(); i < n; ++i) apply_visitor( detail::radd_visitor(buffer_, i, rhs[i]), @@ -479,17 +523,19 @@ public: return *this; } - template adaptive_storage &operator*=(const T &x) { + template + adaptive_storage& operator*=(const T& x) { apply_visitor(detail::rmul_visitor(buffer_, x), buffer_); return *this; } -private: + private: buffer_type buffer_; friend class ::boost::python::access; friend class ::boost::serialization::access; - template void serialize(Archive &, unsigned); + template + void serialize(Archive&, unsigned); }; } // namespace histogram diff --git a/include/boost/histogram/storage/array_storage.hpp b/include/boost/histogram/storage/array_storage.hpp index 310b7f34..e2839dbc 100644 --- a/include/boost/histogram/storage/array_storage.hpp +++ b/include/boost/histogram/storage/array_storage.hpp @@ -22,10 +22,11 @@ class access; namespace boost { namespace histogram { -template class array_storage { -public: +template +class array_storage { + public: using element_type = T; - using const_reference = const T &; + using const_reference = const T&; explicit array_storage(std::size_t s) : size_(s), array_(new element_type[s]) { @@ -33,22 +34,22 @@ public: } array_storage() = default; - array_storage(const array_storage &other) + array_storage(const array_storage& other) : size_(other.size()), array_(new element_type[other.size()]) { std::copy(other.array_.get(), other.array_.get() + size_, array_.get()); } - array_storage &operator=(const array_storage &other) { + array_storage& operator=(const array_storage& other) { if (this != &other) { reset(other.size()); std::copy(other.array_.get(), other.array_.get() + size_, array_.get()); } return *this; } - array_storage(array_storage &&other) { + array_storage(array_storage&& other) { std::swap(size_, other.size_); std::swap(array_, other.array_); } - array_storage &operator=(array_storage &&other) { + array_storage& operator=(array_storage&& other) { if (this != &other) { std::swap(size_, other.size_); std::swap(array_, other.array_); @@ -57,14 +58,14 @@ public: } template > - explicit array_storage(const S &other) { + explicit array_storage(const S& other) { reset(other.size()); for (std::size_t i = 0; i < size_; ++i) array_[i] = static_cast(other[i]); } template > - array_storage &operator=(const S &other) { + array_storage& operator=(const S& other) { reset(other.size()); for (std::size_t i = 0; i < size_; ++i) array_[i] = static_cast(other[i]); @@ -75,32 +76,35 @@ public: void increase(std::size_t i) noexcept { ++array_[i]; } - template void add(std::size_t i, const U &x) noexcept { + template + void add(std::size_t i, const U& x) noexcept { array_[i] += x; } const_reference operator[](std::size_t i) const noexcept { return array_[i]; } template - bool operator==(const array_storage &rhs) const noexcept { + bool operator==(const array_storage& rhs) const noexcept { if (size_ != rhs.size_) return false; return std::equal(array_.get(), array_.get() + size_, rhs.array_.get()); } - template array_storage &operator+=(const S &rhs) noexcept { + template + array_storage& operator+=(const S& rhs) noexcept { for (std::size_t i = 0; i < size_; ++i) add(i, rhs[i]); return *this; } - template array_storage &operator*=(const U &x) noexcept { + template + array_storage& operator*=(const U& x) noexcept { for (std::size_t i = 0; i < size_; ++i) array_[i] *= x; return *this; } -private: + private: std::size_t size_ = 0; std::unique_ptr array_; @@ -109,10 +113,12 @@ private: array_.reset(new element_type[size]); } - template friend class array_storage; + template + friend class array_storage; friend class ::boost::serialization::access; - template void serialize(Archive &, unsigned); + template + void serialize(Archive&, unsigned); }; } // namespace histogram diff --git a/include/boost/histogram/storage/operators.hpp b/include/boost/histogram/storage/operators.hpp index 271ffdfe..dd4b140d 100644 --- a/include/boost/histogram/storage/operators.hpp +++ b/include/boost/histogram/storage/operators.hpp @@ -12,9 +12,11 @@ namespace boost { namespace histogram { -template , +template , typename = detail::requires_storage> -bool operator==(const S1 &s1, const S2 &s2) noexcept { +bool operator==(const S1& s1, const S2& s2) noexcept { if (s1.size() != s2.size()) return false; for (std::size_t i = 0, n = s1.size(); i < n; ++i) @@ -23,9 +25,11 @@ bool operator==(const S1 &s1, const S2 &s2) noexcept { return true; } -template , +template , typename = detail::requires_storage> -bool operator!=(const S1 &s1, const S2 &s2) noexcept { +bool operator!=(const S1& s1, const S2& s2) noexcept { return !operator==(s1, s2); } diff --git a/include/boost/histogram/storage/weight_counter.hpp b/include/boost/histogram/storage/weight_counter.hpp index 534abc87..d4e052fe 100644 --- a/include/boost/histogram/storage/weight_counter.hpp +++ b/include/boost/histogram/storage/weight_counter.hpp @@ -19,145 +19,152 @@ class access; namespace histogram { /// Double counter which holds a sum of weights and a sum of squared weights -template class weight_counter { -public: +template +class weight_counter { + public: /// Beware: For performance reasons counters are not initialized weight_counter() = default; - weight_counter(const weight_counter &) = default; - weight_counter(weight_counter &&) = default; - weight_counter &operator=(const weight_counter &) = default; - weight_counter &operator=(weight_counter &&) = default; + weight_counter(const weight_counter&) = default; + weight_counter(weight_counter&&) = default; + weight_counter& operator=(const weight_counter&) = default; + weight_counter& operator=(weight_counter&&) = default; - weight_counter(const RealType &value, const RealType &variance) noexcept + weight_counter(const RealType& value, const RealType& variance) noexcept : w(value), w2(variance) {} - explicit weight_counter(const RealType &value) noexcept : w(value), + explicit weight_counter(const RealType& value) noexcept : w(value), w2(value) {} - weight_counter &operator++() { + weight_counter& operator++() { ++w; ++w2; return *this; } // TODO: explain why this is needed - weight_counter &operator+=(const RealType &x) { + weight_counter& operator+=(const RealType& x) { w += x; w2 += x; return *this; } template - weight_counter &operator+=(const weight_counter &rhs) { + weight_counter& operator+=(const weight_counter& rhs) { w += static_cast(rhs.w); w2 += static_cast(rhs.w2); return *this; } template - weight_counter &operator+=(const detail::weight &rhs) { + weight_counter& operator+=(const detail::weight& rhs) { const auto x = static_cast(rhs.value); w += x; w2 += x * x; return *this; } - weight_counter &operator*=(const RealType &x) { + weight_counter& operator*=(const RealType& x) { w *= x; w2 *= x * x; return *this; } - bool operator==(const weight_counter &rhs) const noexcept { + bool operator==(const weight_counter& rhs) const noexcept { return w == rhs.w && w2 == rhs.w2; } - bool operator!=(const weight_counter &rhs) const noexcept { + bool operator!=(const weight_counter& rhs) const noexcept { return !operator==(rhs); } template - bool operator==(const weight_counter &rhs) const noexcept { + bool operator==(const weight_counter& rhs) const noexcept { return w == rhs.w && w2 == rhs.w2; } template - bool operator!=(const weight_counter &rhs) const noexcept { + bool operator!=(const weight_counter& rhs) const noexcept { return !operator==(rhs); } - const RealType &value() const noexcept { return w; } - const RealType &variance() const noexcept { return w2; } + const RealType& value() const noexcept { return w; } + const RealType& variance() const noexcept { return w2; } // conversion - template explicit weight_counter(const T &t) { operator=(t); } - template weight_counter &operator=(const T &x) { + template + explicit weight_counter(const T& t) { + operator=(t); + } + template + weight_counter& operator=(const T& x) { w = w2 = static_cast(x); return *this; } // lossy conversion must be explicit - template explicit operator T() const { + template + explicit operator T() const { return static_cast(w); } -private: + private: friend class ::boost::serialization::access; - template void serialize(Archive &, unsigned /* version */); + template + void serialize(Archive&, unsigned /* version */); RealType w, w2; }; template -bool operator==(const weight_counter &w, const U &u) { +bool operator==(const weight_counter& w, const U& u) { return w.value() == w.variance() && w.value() == static_cast(u); } template -bool operator==(const T &t, const weight_counter &w) { +bool operator==(const T& t, const weight_counter& w) { return operator==(w, t); } template -bool operator!=(const weight_counter &w, const U &u) { +bool operator!=(const weight_counter& w, const U& u) { return !operator==(w, u); } template -bool operator!=(const T &t, const weight_counter &w) { +bool operator!=(const T& t, const weight_counter& w) { return operator!=(w, t); } template -weight_counter operator+(const weight_counter &a, - const weight_counter &b) noexcept { +weight_counter operator+(const weight_counter& a, + const weight_counter& b) noexcept { weight_counter c = a; return c += b; } template -weight_counter &&operator+(weight_counter &&a, - const weight_counter &b) noexcept { +weight_counter&& operator+(weight_counter&& a, + const weight_counter& b) noexcept { a += b; return std::move(a); } template -weight_counter &&operator+(const weight_counter &a, - weight_counter &&b) noexcept { +weight_counter&& operator+(const weight_counter& a, + weight_counter&& b) noexcept { return operator+(std::move(b), a); } template -weight_counter operator+(const weight_counter &a, const T &b) noexcept { +weight_counter operator+(const weight_counter& a, const T& b) noexcept { auto r = a; return r += b; } template -weight_counter operator+(const T &a, const weight_counter &b) noexcept { +weight_counter operator+(const T& a, const weight_counter& b) noexcept { auto r = b; return r += a; } diff --git a/test/adaptive_storage_test.cpp b/test/adaptive_storage_test.cpp index f34933ea..4affe50a 100644 --- a/test/adaptive_storage_test.cpp +++ b/test/adaptive_storage_test.cpp @@ -18,18 +18,21 @@ using namespace boost::histogram; -template adaptive_storage prepare(std::size_t n) { +template +adaptive_storage prepare(std::size_t n) { auto a = detail::array(n); return adaptive_storage(a); } -template adaptive_storage prepare(std::size_t n, const T x) { +template +adaptive_storage prepare(std::size_t n, const T x) { auto a = detail::array(n); a[0] = x; return adaptive_storage(a); } -template void copy_impl() { +template +void copy_impl() { const auto b = prepare(1); auto a(b); BOOST_TEST(a == b); @@ -47,7 +50,8 @@ template void copy_impl() { } #ifdef HAVE_SERIALIZATION -template void serialization_impl() { +template +void serialization_impl() { const auto a = prepare(1, T(1)); std::ostringstream os; std::string buf; @@ -67,7 +71,8 @@ template void serialization_impl() { BOOST_TEST(a == b); } -template <> void serialization_impl() { +template <> +void serialization_impl() { adaptive_storage a(std::size_t(1)); std::ostringstream os; std::string buf; @@ -88,7 +93,8 @@ template <> void serialization_impl() { } #endif -template void equal_impl() { +template +void equal_impl() { adaptive_storage a(std::size_t(1)); auto b = prepare(1, T(0)); BOOST_TEST_EQ(a[0].value(), 0.0); @@ -104,7 +110,8 @@ template void equal_impl() { BOOST_TEST(!(c == d)); } -template <> void equal_impl() { +template <> +void equal_impl() { adaptive_storage a(std::size_t(1)); auto b = prepare(1, 0); auto c = prepare(2, 0); @@ -125,7 +132,8 @@ template <> void equal_impl() { BOOST_TEST(!(d == a)); } -template void increase_and_grow_impl() { +template +void increase_and_grow_impl() { auto tmax = std::numeric_limits::max(); auto s = prepare(2, tmax); auto n = s; @@ -145,7 +153,8 @@ template void increase_and_grow_impl() { BOOST_TEST_EQ(n2[1].value(), 0.0); } -template <> void increase_and_grow_impl() { +template <> +void increase_and_grow_impl() { adaptive_storage s(std::size_t(2)); BOOST_TEST_EQ(s[0].value(), 0); BOOST_TEST_EQ(s[1].value(), 0); @@ -154,7 +163,8 @@ template <> void increase_and_grow_impl() { BOOST_TEST_EQ(s[1].value(), 0); } -template void convert_array_storage_impl() { +template +void convert_array_storage_impl() { const auto aref = prepare(1, T(0)); array_storage s(std::size_t(1)); s.increase(0); @@ -213,7 +223,8 @@ template void convert_array_storage_impl() { BOOST_TEST(h == u); } -template <> void convert_array_storage_impl() { +template <> +void convert_array_storage_impl() { const auto aref = adaptive_storage(std::size_t(1)); BOOST_TEST_EQ(aref[0].value(), 0.0); array_storage s(std::size_t(1)); @@ -238,7 +249,8 @@ template <> void convert_array_storage_impl() { BOOST_TEST(!(d == t)); } -template void add_impl() { +template +void add_impl() { auto a = prepare(2); auto b = prepare(2); if (std::is_same::value) { @@ -255,7 +267,8 @@ template void add_impl() { } } -template void add_impl_all_rhs() { +template +void add_impl_all_rhs() { add_impl(); add_impl(); add_impl(); diff --git a/test/axis_test.cpp b/test/axis_test.cpp index e3a791dd..bf9f18ad 100644 --- a/test/axis_test.cpp +++ b/test/axis_test.cpp @@ -21,7 +21,7 @@ using namespace boost::histogram; #define BOOST_TEST_IS_CLOSE(a, b, eps) BOOST_TEST(std::abs(a - b) < eps) template -void test_axis_iterator(const Axis &a, int begin, int end) { +void test_axis_iterator(const Axis& a, int begin, int end) { for (auto bin : a) { BOOST_TEST_EQ(bin.idx(), begin); BOOST_TEST_EQ(bin, a[begin]); @@ -36,7 +36,6 @@ void test_axis_iterator(const Axis &a, int begin, int end) { } int main() { - // bad_ctors { BOOST_TEST_THROWS(axis::regular<>(0, 0, 1), std::logic_error); @@ -294,7 +293,7 @@ int main() { axes.push_back(axis::category({a, b}, "category2")); axes.push_back(axis::integer<>(-1, 1, "integer", axis::uoflow::off)); std::ostringstream os; - for (const auto &a : axes) { + for (const auto& a : axes) { os << a << "\n"; } const std::string ref = @@ -319,7 +318,7 @@ int main() { axes.push_back(axis::variable<>{-1, 0, 1}); axes.push_back(axis::category<>{A, B, C}); axes.push_back(axis::integer<>{-1, 1}); - for (const auto &a : axes) { + for (const auto& a : axes) { BOOST_TEST(!(a == axis::any_std())); BOOST_TEST_EQ(a, a); } @@ -332,7 +331,7 @@ int main() { std::string a = "A", b = "B"; axis::any_std x = axis::category({a, b}, "category"); BOOST_TEST_THROWS(x.index(1.5), std::runtime_error); - const auto &cx = axis::cast>(x); + const auto& cx = axis::cast>(x); BOOST_TEST_EQ(cx.index(b), 1); } diff --git a/test/detail_test.cpp b/test/detail_test.cpp index a5721352..703cda55 100644 --- a/test/detail_test.cpp +++ b/test/detail_test.cpp @@ -29,21 +29,25 @@ using i2 = mp11::mp_int<2>; using i3 = mp11::mp_int<3>; namespace std { // never add to std, we only do it to get ADL working -template ostream &operator<<(ostream &os, const vector &v) { +template +ostream& operator<<(ostream& os, const vector& v) { os << "[ "; - for (const auto &x : v) + for (const auto& x : v) os << x << " "; os << "]"; return os; } struct ostreamer { - ostream &os; - template void operator()(const T &t) const { os << t << " "; } + ostream& os; + template + void operator()(const T& t) const { + os << t << " "; + } }; template -ostream &operator<<(ostream &os, const tuple &t) { +ostream& operator<<(ostream& os, const tuple& t) { os << "[ "; ::boost::mp11::tuple_for_each(t, ostreamer{os}); os << "]"; @@ -135,16 +139,16 @@ int main() { struct no_methods {}; struct value_method { - const double &value() const; + const double& value() const; }; struct variance_method { - const double &variance() const; + const double& variance() const; }; struct value_and_variance_methods { - const double &value() const; - const double &variance() const; + const double& value() const; + const double& variance() const; }; BOOST_TEST_EQ(has_variance_support(), false); @@ -158,19 +162,19 @@ int main() { using result1 = classify_container; BOOST_TEST_TRAIT_TRUE((std::is_same)); - using result1a = classify_container; + using result1a = classify_container; BOOST_TEST_TRAIT_TRUE((std::is_same)); using result2 = classify_container>; BOOST_TEST_TRAIT_TRUE((std::is_same)); - using result2a = classify_container &>; + using result2a = classify_container&>; BOOST_TEST_TRAIT_TRUE((std::is_same)); using result3 = classify_container>; BOOST_TEST_TRAIT_TRUE((std::is_same)); - using result3a = classify_container &>; + using result3a = classify_container&>; BOOST_TEST_TRAIT_TRUE((std::is_same)); using result4 = classify_container; @@ -190,10 +194,10 @@ int main() { { using T1 = int; using T2 = const int; - using T3 = const int &; + using T3 = const int&; using T4 = volatile int; using T5 = volatile const int; - using T6 = volatile const int &; + using T6 = volatile const int&; BOOST_TEST_TRAIT_TRUE((std::is_same, int>)); BOOST_TEST_TRAIT_TRUE((std::is_same, int>)); BOOST_TEST_TRAIT_TRUE((std::is_same, int>)); @@ -207,11 +211,10 @@ int main() { BOOST_TEST_TRAIT_TRUE((std::is_same, long>)); BOOST_TEST_TRAIT_TRUE( (std::is_same, const long>)); - BOOST_TEST_TRAIT_TRUE((std::is_same, long &>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, long&>)); BOOST_TEST_TRAIT_TRUE( - (std::is_same, const long &>)); - BOOST_TEST_TRAIT_TRUE( - (std::is_same, long &&>)); + (std::is_same, const long&>)); + BOOST_TEST_TRAIT_TRUE((std::is_same, long&&>)); } // is_common_base @@ -227,9 +230,9 @@ int main() { // mp_set_union { using L1 = mp11::mp_list; - using L2 = mp11::mp_list; + using L2 = mp11::mp_list; using result = mp_set_union; - using expected = mp11::mp_list; + using expected = mp11::mp_list; BOOST_TEST_TRAIT_TRUE((std::is_same)); } diff --git a/test/histogram_test.cpp b/test/histogram_test.cpp index 9ef138ad..1ded757a 100644 --- a/test/histogram_test.cpp +++ b/test/histogram_test.cpp @@ -33,13 +33,13 @@ using namespace boost::histogram::literals; // to get _c suffix namespace mp11 = boost::mp11; template -auto make_histogram(static_tag, Axes &&... axes) +auto make_histogram(static_tag, Axes&&... axes) -> decltype(make_static_histogram_with(std::forward(axes)...)) { return make_static_histogram_with(std::forward(axes)...); } template -auto make_histogram(dynamic_tag, Axes &&... axes) +auto make_histogram(dynamic_tag, Axes&&... axes) -> decltype(make_dynamic_histogram_with(std::forward(axes)...)) { return make_dynamic_histogram_with(std::forward(axes)...); } @@ -48,19 +48,21 @@ int expected_moved_from_dim(static_tag, int static_value) { return static_value; } -int expected_moved_from_dim(dynamic_tag, int) { return 0; } +int expected_moved_from_dim(dynamic_tag, int) { + return 0; +} template -typename Histogram::element_type sum(const Histogram &h) { +typename Histogram::element_type sum(const Histogram& h) { return std::accumulate(h.begin(), h.end(), typename Histogram::element_type(0)); } template -void pass_histogram(boost::histogram::histogram &) {} - -template void run_tests() { +void pass_histogram(boost::histogram::histogram&) {} +template +void run_tests() { // init_1 { auto h = @@ -578,10 +580,11 @@ template void run_tests() { Type(), axis::regular<>(3, -1, 1, "r"), axis::integer<>(0, 2, "i")); std::ostringstream os; os << a; - BOOST_TEST_EQ(os.str(), "histogram(" - "\n regular(3, -1, 1, label='r')," - "\n integer(0, 2, label='i')," - "\n)"); + BOOST_TEST_EQ(os.str(), + "histogram(" + "\n regular(3, -1, 1, label='r')," + "\n integer(0, 2, label='i')," + "\n)"); } // histogram_reset @@ -701,7 +704,7 @@ template void run_tests() { // custom axis { struct custom_axis : public axis::integer<> { - using value_type = const char *; // type that is fed to the axis + using value_type = const char*; // type that is fed to the axis using integer::integer; // inherit ctors of base @@ -727,7 +730,7 @@ template void run_tests() { // histogram iterator 1D { auto h = make_histogram(Type(), axis::integer<>(0, 3)); - const auto &a = h.axis(); + const auto& a = h.axis(); h(weight(2), 0); h(1); h(1); @@ -763,8 +766,8 @@ template void run_tests() { auto h = make_histogram( Type(), axis::integer<>(0, 1), axis::integer<>(2, 4, "", axis::uoflow::off)); - const auto &a0 = h.axis(0_c); - const auto &a1 = h.axis(1_c); + const auto& a0 = h.axis(0_c); + const auto& a1 = h.axis(1_c); h(weight(2), 0, 2); h(-1, 2); h(1, 3); @@ -890,8 +893,8 @@ template void run_tests() { } } -template void run_mixed_tests() { - +template +void run_mixed_tests() { // compare { auto a = make_histogram(T1{}, axis::regular<>{3, 0, 3}, @@ -936,7 +939,6 @@ template void run_mixed_tests() { } int main() { - // common interface run_tests(); run_tests(); @@ -1000,7 +1002,7 @@ int main() { // histogram iterator { auto h = make_dynamic_histogram(axis::integer<>(0, 3)); - const auto &a = h.axis(); + const auto& a = h.axis(); h(weight(2), 0); h(1); h(1); diff --git a/test/speed_cpp.cpp b/test/speed_cpp.cpp index 9519eeeb..75f8e101 100644 --- a/test/speed_cpp.cpp +++ b/test/speed_cpp.cpp @@ -31,7 +31,8 @@ std::unique_ptr random_array(unsigned n, int type) { return r; } -template void ignore( const T& ) { } +template +void ignore(const T&) {} double baseline(unsigned n) { auto r = random_array(n, 0); @@ -49,7 +50,8 @@ double baseline(unsigned n) { return best; } -template double compare_1d(unsigned n, int distrib) { +template +double compare_1d(unsigned n, int distrib) { auto r = random_array(n, distrib); auto best = std::numeric_limits::max(); @@ -65,14 +67,15 @@ template double compare_1d(unsigned n, int distrib) { return best; } -template double compare_2d(unsigned n, int distrib) { +template +double compare_2d(unsigned n, int distrib) { auto r = random_array(n, distrib); auto best = std::numeric_limits::max(); for (unsigned k = 0; k < 20; ++k) { auto h = Histogram(axis::regular<>(100, 0, 1), axis::regular<>(100, 0, 1)); auto t = clock(); - for (unsigned i = 0; i < n/2; ++i) + for (unsigned i = 0; i < n / 2; ++i) h(r[2 * i], r[2 * i + 1]); t = clock() - t; best = std::min(best, double(t) / CLOCKS_PER_SEC); @@ -81,7 +84,8 @@ template double compare_2d(unsigned n, int distrib) { return best; } -template double compare_3d(unsigned n, int distrib) { +template +double compare_3d(unsigned n, int distrib) { auto r = random_array(n, distrib); auto best = std::numeric_limits::max(); @@ -89,7 +93,7 @@ template double compare_3d(unsigned n, int distrib) { auto h = Histogram(axis::regular<>(100, 0, 1), axis::regular<>(100, 0, 1), axis::regular<>(100, 0, 1)); auto t = clock(); - for (unsigned i = 0; i < n/3; ++i) + for (unsigned i = 0; i < n / 3; ++i) h(r[3 * i], r[3 * i + 1], r[3 * i + 2]); t = clock() - t; best = std::min(best, double(t) / CLOCKS_PER_SEC); @@ -98,7 +102,8 @@ template double compare_3d(unsigned n, int distrib) { return best; } -template double compare_6d(unsigned n, int distrib) { +template +double compare_6d(unsigned n, int distrib) { auto r = random_array(n, distrib); auto best = std::numeric_limits::max(); @@ -108,9 +113,9 @@ template double compare_6d(unsigned n, int distrib) { axis::regular<>(10, 0, 1), axis::regular<>(10, 0, 1)); auto t = clock(); - for (unsigned i = 0; i < n/6; ++i) { - h(r[6 * i], r[6 * i + 1], r[6 * i + 2], - r[6 * i + 3], r[6 * i + 4], r[6 * i + 5]); + for (unsigned i = 0; i < n / 6; ++i) { + h(r[6 * i], r[6 * i + 1], r[6 * i + 2], r[6 * i + 3], r[6 * i + 4], + r[6 * i + 5]); } t = clock() - t; best = std::min(best, double(t) / CLOCKS_PER_SEC); @@ -131,19 +136,19 @@ int main() { else printf("normal distribution\n"); printf("hs_ss %.3f\n", - compare_1d>, - array_storage>>( + compare_1d< + static_histogram>, array_storage>>( nfill, itype)); printf("hs_sd %.3f\n", - compare_1d>, - adaptive_storage>>(nfill, itype)); + compare_1d< + static_histogram>, adaptive_storage>>( + nfill, itype)); printf("hd_ss %.3f\n", - compare_1d>>( + compare_1d>>( nfill, itype)); printf("hd_sd %.3f\n", - compare_1d>( - nfill, itype)); + compare_1d>(nfill, + itype)); } printf("2D\n"); @@ -152,21 +157,20 @@ int main() { printf("uniform distribution\n"); else printf("normal distribution\n"); - printf("hs_ss %.3f\n", - compare_2d, axis::regular<>>, - array_storage>>(nfill, itype)); - printf("hs_sd %.3f\n", - compare_2d, axis::regular<>>, - adaptive_storage>>(nfill, itype)); + printf( + "hs_ss %.3f\n", + compare_2d, axis::regular<>>, + array_storage>>(nfill, itype)); + printf( + "hs_sd %.3f\n", + compare_2d, axis::regular<>>, + adaptive_storage>>(nfill, itype)); printf("hd_ss %.3f\n", - compare_2d>>( + compare_2d>>( nfill, itype)); printf("hd_sd %.3f\n", - compare_2d>( - nfill, itype)); + compare_2d>(nfill, + itype)); } printf("3D\n"); @@ -184,12 +188,11 @@ int main() { mp_list, axis::regular<>, axis::regular<>>, adaptive_storage>>(nfill, itype)); printf("hd_ss %.3f\n", - compare_3d>>( + compare_3d>>( nfill, itype)); printf("hd_sd %.3f\n", - compare_3d>( - nfill, itype)); + compare_3d>(nfill, + itype)); } printf("6D\n"); @@ -201,19 +204,18 @@ int main() { printf("hs_ss %.3f\n", compare_6d, axis::regular<>, axis::regular<>, - axis::regular<>, axis::regular<>, axis::regular<>>, + axis::regular<>, axis::regular<>, axis::regular<>>, array_storage>>(nfill, itype)); printf("hs_sd %.3f\n", compare_6d, axis::regular<>, axis::regular<>, - axis::regular<>, axis::regular<>, axis::regular<>>, + axis::regular<>, axis::regular<>, axis::regular<>>, adaptive_storage>>(nfill, itype)); printf("hd_ss %.3f\n", - compare_6d>>( + compare_6d>>( nfill, itype)); printf("hd_sd %.3f\n", - compare_6d>( - nfill, itype)); + compare_6d>(nfill, + itype)); } } diff --git a/test/speed_gsl.cpp b/test/speed_gsl.cpp index f6bb0357..c31c75dd 100644 --- a/test/speed_gsl.cpp +++ b/test/speed_gsl.cpp @@ -34,7 +34,7 @@ void compare_1d(unsigned n, int distrib) { double best = std::numeric_limits::max(); for (unsigned k = 0; k < 20; ++k) { - gsl_histogram *h = gsl_histogram_alloc(100); + gsl_histogram* h = gsl_histogram_alloc(100); gsl_histogram_set_ranges_uniform(h, 0, 1); auto t = clock(); for (unsigned i = 0; i < n; ++i) @@ -51,10 +51,10 @@ void compare_2d(unsigned n, int distrib) { double best = std::numeric_limits::max(); for (unsigned k = 0; k < 20; ++k) { - gsl_histogram2d *h = gsl_histogram2d_alloc(100, 100); + gsl_histogram2d* h = gsl_histogram2d_alloc(100, 100); gsl_histogram2d_set_ranges_uniform(h, 0, 1, 0, 1); auto t = clock(); - for (unsigned i = 0; i < n/2; ++i) + for (unsigned i = 0; i < n / 2; ++i) gsl_histogram2d_increment(h, r[2 * i], r[2 * i + 1]); t = clock() - t; best = std::min(best, double(t) / CLOCKS_PER_SEC); @@ -63,7 +63,7 @@ void compare_2d(unsigned n, int distrib) { printf("gsl %.3f\n", best); } -int main(int argc, char **argv) { +int main(int argc, char** argv) { constexpr unsigned nfill = 6000000; printf("1D\n"); printf("uniform distribution\n"); diff --git a/test/speed_root.cpp b/test/speed_root.cpp index 06e3a424..8a24bc28 100644 --- a/test/speed_root.cpp +++ b/test/speed_root.cpp @@ -53,7 +53,7 @@ void compare_2d(unsigned n, int distrib) { for (unsigned k = 0; k < 20; ++k) { TH2I hroot("", "", 100, 0, 1, 100, 0, 1); auto t = clock(); - for (unsigned i = 0; i < n/2; ++i) + for (unsigned i = 0; i < n / 2; ++i) hroot.Fill(r[2 * i], r[2 * i + 1]); t = clock() - t; best_root = std::min(best_root, double(t) / CLOCKS_PER_SEC); @@ -68,7 +68,7 @@ void compare_3d(unsigned n, int distrib) { for (unsigned k = 0; k < 20; ++k) { TH3I hroot("", "", 100, 0, 1, 100, 0, 1, 100, 0, 1); auto t = clock(); - for (unsigned i = 0; i < n/3; ++i) + for (unsigned i = 0; i < n / 3; ++i) hroot.Fill(r[3 * i], r[3 * i + 1], r[3 * i + 2]); t = clock() - t; best_root = std::min(best_root, double(t) / CLOCKS_PER_SEC); @@ -87,7 +87,7 @@ void compare_6d(unsigned n, int distrib) { THnI hroot("", "", 6, &bin.front(), &min.front(), &max.front()); auto t = clock(); - for (unsigned i = 0; i < n/6; ++i) { + for (unsigned i = 0; i < n / 6; ++i) { hroot.Fill(r.get() + 6 * i); } t = clock() - t; @@ -96,7 +96,7 @@ void compare_6d(unsigned n, int distrib) { printf("root %.3f\n", best_root); } -int main(int argc, char **argv) { +int main(int argc, char** argv) { constexpr unsigned nfill = 6000000; printf("1D\n");