clang-format

This commit is contained in:
Hans Dembinski 2018-03-10 22:51:54 +01:00
parent 460b417338
commit 5a546b0971
14 changed files with 179 additions and 177 deletions

View File

@ -154,8 +154,12 @@ public:
const_iterator begin() const { return const_iterator(*this, 0); } const_iterator begin() const { return const_iterator(*this, 0); }
const_iterator end() const { return const_iterator(*this, size()); } const_iterator end() const { return const_iterator(*this, size()); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(*this, size()); } const_reverse_iterator rbegin() const {
const_reverse_iterator rend() const { return const_reverse_iterator(*this, 0); } return const_reverse_iterator(*this, size());
}
const_reverse_iterator rend() const {
return const_reverse_iterator(*this, 0);
}
private: private:
friend class ::boost::serialization::access; friend class ::boost::serialization::access;

View File

@ -10,8 +10,8 @@
#include <algorithm> #include <algorithm>
#include <boost/bimap.hpp> #include <boost/bimap.hpp>
#include <boost/histogram/axis/iterator.hpp> #include <boost/histogram/axis/iterator.hpp>
#include <boost/histogram/interval.hpp>
#include <boost/histogram/detail/meta.hpp> #include <boost/histogram/detail/meta.hpp>
#include <boost/histogram/interval.hpp>
#include <boost/math/constants/constants.hpp> #include <boost/math/constants/constants.hpp>
#include <boost/utility/string_view.hpp> #include <boost/utility/string_view.hpp>
#include <cmath> #include <cmath>
@ -37,8 +37,7 @@ namespace axis {
enum class uoflow { off = false, on = true }; enum class uoflow { off = false, on = true };
/// Base class for all axes, uses CRTP. /// Base class for all axes, uses CRTP.
template <typename Derived> template <typename Derived> class axis_base {
class axis_base {
public: public:
using const_iterator = iterator_over<Derived>; using const_iterator = iterator_over<Derived>;
using const_reverse_iterator = reverse_iterator_over<Derived>; using const_reverse_iterator = reverse_iterator_over<Derived>;
@ -54,10 +53,18 @@ public:
/// Change the label of an axis. /// Change the label of an axis.
void label(string_view label) { label_.assign(label.begin(), label.end()); } void label(string_view label) { label_.assign(label.begin(), label.end()); }
const_iterator begin() const noexcept { return const_iterator(*static_cast<const Derived*>(this), 0); } const_iterator begin() const noexcept {
const_iterator end() const noexcept { return const_iterator(*static_cast<const Derived*>(this), size()); } return const_iterator(*static_cast<const Derived *>(this), 0);
const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(*static_cast<const Derived*>(this), size()); } }
const_reverse_iterator rend() const noexcept { return const_reverse_iterator(*static_cast<const Derived*>(this), 0); } const_iterator end() const noexcept {
return const_iterator(*static_cast<const Derived *>(this), size());
}
const_reverse_iterator rbegin() const noexcept {
return const_reverse_iterator(*static_cast<const Derived *>(this), size());
}
const_reverse_iterator rend() const noexcept {
return const_reverse_iterator(*static_cast<const Derived *>(this), 0);
}
protected: protected:
axis_base(unsigned n, string_view label) axis_base(unsigned n, string_view label)
@ -96,9 +103,9 @@ private:
}; };
/// Base class for axes with overflow/underflow bins, uses CRTP. /// Base class for axes with overflow/underflow bins, uses CRTP.
template <typename Derived> template <typename Derived> class axis_base_uoflow : public axis_base<Derived> {
class axis_base_uoflow : public axis_base<Derived> {
using base_type = axis_base<Derived>; using base_type = axis_base<Derived>;
public: public:
/// Returns the number of bins, including overflow/underflow. /// Returns the number of bins, including overflow/underflow.
inline int shape() const noexcept { return shape_; } inline int shape() const noexcept { return shape_; }
@ -189,8 +196,10 @@ private:
*/ */
// private inheritance from Transform wastes no space if it is stateless // private inheritance from Transform wastes no space if it is stateless
template <typename RealType, typename Transform> template <typename RealType, typename Transform>
class regular : public axis_base_uoflow<regular<RealType, Transform>>, Transform { class regular : public axis_base_uoflow<regular<RealType, Transform>>,
Transform {
using base_type = axis_base_uoflow<regular<RealType, Transform>>; using base_type = axis_base_uoflow<regular<RealType, Transform>>;
public: public:
using value_type = RealType; using value_type = RealType;
using bin_type = interval<value_type>; using bin_type = interval<value_type>;
@ -208,8 +217,7 @@ public:
string_view label = {}, string_view label = {},
enum uoflow uo = ::boost::histogram::axis::uoflow::on, enum uoflow uo = ::boost::histogram::axis::uoflow::on,
Transform trans = Transform()) Transform trans = Transform())
: base_type(n, label, uo), Transform(trans), : base_type(n, label, uo), Transform(trans), min_(trans.forward(lower)),
min_(trans.forward(lower)),
delta_((trans.forward(upper) - trans.forward(lower)) / n) { delta_((trans.forward(upper) - trans.forward(lower)) / n) {
if (!(lower < upper)) { if (!(lower < upper)) {
throw std::logic_error("lower < upper required"); throw std::logic_error("lower < upper required");
@ -228,7 +236,9 @@ public:
inline int index(value_type x) const noexcept { inline int index(value_type x) const noexcept {
// Optimized code // Optimized code
const value_type z = (this->forward(x) - min_) / delta_; const value_type z = (this->forward(x) - min_) / delta_;
return z >= 0.0 ? (z > base_type::size() ? base_type::size() : static_cast<int>(z)) : -1; return z >= 0.0 ? (z > base_type::size() ? base_type::size()
: static_cast<int>(z))
: -1;
} }
/// Returns the starting edge of the bin. /// Returns the starting edge of the bin.
@ -267,8 +277,10 @@ private:
* perimeter value. Therefore, there are no overflow/underflow * perimeter value. Therefore, there are no overflow/underflow
* bins for this axis. Binning is a O(1) operation. * bins for this axis. Binning is a O(1) operation.
*/ */
template <typename RealType> class circular : public axis_base<circular<RealType>> { template <typename RealType>
class circular : public axis_base<circular<RealType>> {
using base_type = axis_base<circular<RealType>>; using base_type = axis_base<circular<RealType>>;
public: public:
using value_type = RealType; using value_type = RealType;
using bin_type = interval<value_type>; using bin_type = interval<value_type>;
@ -294,7 +306,8 @@ public:
/// Returns the bin index for the passed argument. /// Returns the bin index for the passed argument.
inline int index(value_type x) const noexcept { inline int index(value_type x) const noexcept {
const value_type z = (x - phase_) / perimeter_; const value_type z = (x - phase_) / perimeter_;
const int i = static_cast<int>(std::floor(z * base_type::size())) % base_type::size(); const int i =
static_cast<int>(std::floor(z * base_type::size())) % base_type::size();
return i + (i < 0) * base_type::size(); return i + (i < 0) * base_type::size();
} }
@ -327,8 +340,10 @@ private:
* Binning is a O(log(N)) operation. If speed matters and the problem * Binning is a O(log(N)) operation. If speed matters and the problem
* domain allows it, prefer a regular axis, possibly with a transform. * domain allows it, prefer a regular axis, possibly with a transform.
*/ */
template <typename RealType> class variable : public axis_base_uoflow<variable<RealType>> { template <typename RealType>
class variable : public axis_base_uoflow<variable<RealType>> {
using base_type = axis_base_uoflow<variable<RealType>>; using base_type = axis_base_uoflow<variable<RealType>>;
public: public:
using value_type = RealType; using value_type = RealType;
using bin_type = interval<value_type>; using bin_type = interval<value_type>;
@ -341,8 +356,7 @@ public:
*/ */
variable(std::initializer_list<value_type> x, string_view label = {}, variable(std::initializer_list<value_type> x, string_view label = {},
enum uoflow uo = ::boost::histogram::axis::uoflow::on) enum uoflow uo = ::boost::histogram::axis::uoflow::on)
: base_type(x.size() - 1, label, uo), : base_type(x.size() - 1, label, uo), x_(new value_type[x.size()]) {
x_(new value_type[x.size()]) {
if (x.size() < 2) { if (x.size() < 2) {
throw std::logic_error("at least two values required"); throw std::logic_error("at least two values required");
} }
@ -377,7 +391,8 @@ public:
/// Returns the bin index for the passed argument. /// Returns the bin index for the passed argument.
inline int index(value_type x) const noexcept { inline int index(value_type x) const noexcept {
return std::upper_bound(x_.get(), x_.get() + base_type::size() + 1, x) - x_.get() - 1; return std::upper_bound(x_.get(), x_.get() + base_type::size() + 1, x) -
x_.get() - 1;
} }
/// Returns the starting edge of the bin. /// Returns the starting edge of the bin.
@ -413,8 +428,10 @@ private:
* Binning is a O(1) operation. This axis operates * Binning is a O(1) operation. This axis operates
* faster than a regular. * faster than a regular.
*/ */
template <typename IntType> class integer : public axis_base_uoflow<integer<IntType>> { template <typename IntType>
class integer : public axis_base_uoflow<integer<IntType>> {
using base_type = axis_base_uoflow<integer<IntType>>; using base_type = axis_base_uoflow<integer<IntType>>;
public: public:
using value_type = IntType; using value_type = IntType;
using bin_type = interval<value_type>; using bin_type = interval<value_type>;
@ -481,6 +498,7 @@ private:
template <typename T> class category : public axis_base<category<T>> { template <typename T> class category : public axis_base<category<T>> {
using map_type = bimap<T, int>; using map_type = bimap<T, int>;
using base_type = axis_base<category<T>>; using base_type = axis_base<category<T>>;
public: public:
using value_type = T; using value_type = T;
using bin_type = T; using bin_type = T;
@ -511,7 +529,8 @@ public:
throw std::logic_error("sequence is empty"); throw std::logic_error("sequence is empty");
} }
template <typename Iterator, typename = ::boost::histogram::detail::is_iterator<Iterator>> template <typename Iterator,
typename = ::boost::histogram::detail::is_iterator<Iterator>>
category(Iterator begin, Iterator end, string_view label = {}) category(Iterator begin, Iterator end, string_view label = {})
: base_type(std::distance(begin, end), label), map_(new map_type()) { : base_type(std::distance(begin, end), label), map_(new map_type()) {
int index = 0; int index = 0;

View File

@ -15,8 +15,7 @@ namespace axis {
template <typename Axis> template <typename Axis>
class iterator_over class iterator_over
: public iterator_facade<iterator_over<Axis>, : public iterator_facade<iterator_over<Axis>, typename Axis::bin_type,
typename Axis::bin_type,
random_access_traversal_tag, random_access_traversal_tag,
typename Axis::bin_type> { typename Axis::bin_type> {
public: public:
@ -39,30 +38,28 @@ protected:
bool equal(const iterator_over &other) const noexcept { bool equal(const iterator_over &other) const noexcept {
return &axis_ == &other.axis_ && idx_ == other.idx_; return &axis_ == &other.axis_ && idx_ == other.idx_;
} }
typename Axis::bin_type dereference() const { typename Axis::bin_type dereference() const { return axis_[idx_]; }
return axis_[idx_];
}
friend class ::boost::iterator_core_access; friend class ::boost::iterator_core_access;
const Axis& axis_; const Axis &axis_;
int idx_; int idx_;
}; };
template <typename Axis> template <typename Axis>
class reverse_iterator_over class reverse_iterator_over
: public iterator_facade<reverse_iterator_over<Axis>, : public iterator_facade<
typename Axis::bin_type, reverse_iterator_over<Axis>, typename Axis::bin_type,
random_access_traversal_tag, random_access_traversal_tag, typename Axis::bin_type> {
typename Axis::bin_type> {
public: public:
explicit reverse_iterator_over(const Axis &axis, int idx) : axis_(axis), idx_(idx) {} explicit reverse_iterator_over(const Axis &axis, int idx)
: axis_(axis), idx_(idx) {}
reverse_iterator_over(const reverse_iterator_over &) = default; reverse_iterator_over(const reverse_iterator_over &) = default;
reverse_iterator_over &operator=(const reverse_iterator_over &) = default; reverse_iterator_over &operator=(const reverse_iterator_over &) = default;
operator bool() const noexcept { return idx_ > 0; } operator bool() const noexcept { return idx_ > 0; }
explicit operator int() const noexcept { return idx_; } explicit operator int() const noexcept { return idx_; }
int idx() const noexcept { return idx_-1; } int idx() const noexcept { return idx_ - 1; }
protected: protected:
void increment() noexcept { --idx_; } void increment() noexcept { --idx_; }
@ -74,12 +71,10 @@ protected:
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_; return &axis_ == &other.axis_ && idx_ == other.idx_;
} }
typename Axis::bin_type dereference() const { typename Axis::bin_type dereference() const { return axis_[idx_ - 1]; }
return axis_[idx_-1];
}
friend class ::boost::iterator_core_access; friend class ::boost::iterator_core_access;
const Axis& axis_; const Axis &axis_;
int idx_; int idx_;
}; };

View File

@ -36,8 +36,8 @@ template <typename T, typename = decltype(std::declval<T &>().size(),
struct requires_storage {}; struct requires_storage {};
template <typename T> struct has_variance_support { template <typename T> struct has_variance_support {
template <typename U, typename = decltype(std::declval<U&>().value(), template <typename U, typename = decltype(std::declval<U &>().value(),
std::declval<U&>().variance())> std::declval<U &>().variance())>
struct SFINAE {}; struct SFINAE {};
template <typename U> static std::true_type Test(SFINAE<U> *); template <typename U> static std::true_type Test(SFINAE<U> *);
template <typename U> static std::false_type Test(...); template <typename U> static std::false_type Test(...);
@ -55,11 +55,10 @@ template <typename T, typename = decltype(std::begin(std::declval<T &>()),
std::end(std::declval<T &>()))> std::end(std::declval<T &>()))>
struct is_sequence {}; struct is_sequence {};
template <typename MainVector, typename AuxVector> struct union_ : template <typename MainVector, typename AuxVector>
mpl::copy_if<AuxVector, struct union_
mpl::not_<mpl::contains<MainVector, mpl::_1>>, : mpl::copy_if<AuxVector, mpl::not_<mpl::contains<MainVector, mpl::_1>>,
mpl::back_inserter<MainVector>> mpl::back_inserter<MainVector>> {};
{};
template <typename MainVector, typename AuxVector> template <typename MainVector, typename AuxVector>
using union_t = typename union_<MainVector, AuxVector>::type; using union_t = typename union_<MainVector, AuxVector>::type;

View File

@ -17,16 +17,16 @@
#include <boost/histogram/detail/utility.hpp> #include <boost/histogram/detail/utility.hpp>
#include <boost/histogram/histogram_fwd.hpp> #include <boost/histogram/histogram_fwd.hpp>
#include <boost/histogram/iterator.hpp> #include <boost/histogram/iterator.hpp>
#include <boost/histogram/storage/operators.hpp>
#include <boost/histogram/storage/array_storage.hpp> #include <boost/histogram/storage/array_storage.hpp>
#include <boost/histogram/storage/operators.hpp>
#include <boost/histogram/storage/weight_counter.hpp> #include <boost/histogram/storage/weight_counter.hpp>
#include <boost/mpl/bool.hpp> #include <boost/mpl/bool.hpp>
#include <boost/mpl/count_if.hpp> #include <boost/mpl/count_if.hpp>
#include <boost/mpl/find_if.hpp> #include <boost/mpl/deref.hpp>
#include <boost/mpl/empty.hpp> #include <boost/mpl/empty.hpp>
#include <boost/mpl/find_if.hpp>
#include <boost/mpl/int.hpp> #include <boost/mpl/int.hpp>
#include <boost/mpl/vector.hpp> #include <boost/mpl/vector.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/type_index.hpp> #include <boost/type_index.hpp>
#include <cstddef> #include <cstddef>
#include <iterator> #include <iterator>
@ -124,14 +124,12 @@ public:
return *this; return *this;
} }
template <typename T> template <typename T> histogram &operator*=(const T &rhs) {
histogram &operator*=(const T& rhs) {
storage_ *= rhs; storage_ *= rhs;
return *this; return *this;
} }
template <typename T> template <typename T> histogram &operator/=(const T &rhs) {
histogram &operator/=(const T& rhs) {
storage_ *= 1.0 / rhs; storage_ *= 1.0 / rhs;
return *this; return *this;
} }
@ -145,7 +143,7 @@ public:
"more than one weight argument is not allowed"); "more than one weight argument is not allowed");
static_assert(n_sample::value <= 1, static_assert(n_sample::value <= 1,
"more than one sample argument is not allowed"); "more than one sample argument is not allowed");
if (dim() != sizeof...(args) - n_weight::value - n_sample::value) if (dim() != sizeof...(args)-n_weight::value - n_sample::value)
throw std::invalid_argument( throw std::invalid_argument(
"fill arguments does not match histogram dimension"); "fill arguments does not match histogram dimension");
fill_impl(mpl::bool_<n_weight::value>(), mpl::bool_<n_sample::value>(), fill_impl(mpl::bool_<n_weight::value>(), mpl::bool_<n_sample::value>(),
@ -265,9 +263,7 @@ public:
return reduce_impl(b); return reduce_impl(b);
} }
bin_iterator begin() const noexcept { bin_iterator begin() const noexcept { return bin_iterator(*this, storage_); }
return bin_iterator(*this, storage_);
}
bin_iterator end() const noexcept { return bin_iterator(storage_); } bin_iterator end() const noexcept { return bin_iterator(storage_); }
@ -293,9 +289,8 @@ private:
template <typename... Args> template <typename... Args>
inline void fill_impl(mpl::true_, mpl::false_, const Args &... args) { inline void fill_impl(mpl::true_, mpl::false_, const Args &... args) {
std::size_t idx = 0, stride = 1; std::size_t idx = 0, stride = 1;
typename mpl::deref< typename mpl::deref<typename mpl::find_if<
typename mpl::find_if<mpl::vector<Args...>, detail::is_weight<mpl::_>>::type mpl::vector<Args...>, detail::is_weight<mpl::_>>::type>::type w;
>::type w;
wxlin<0>(idx, stride, w, args...); wxlin<0>(idx, stride, w, args...);
if (stride) { if (stride) {
storage_.add(idx, w); storage_.add(idx, w);
@ -355,12 +350,11 @@ private:
} }
}; };
template <unsigned D> template <unsigned D> inline void xlin(std::size_t &, std::size_t &) const {}
inline void xlin(std::size_t &, std::size_t &) const {}
template <unsigned D, typename First, typename... Rest> template <unsigned D, typename First, typename... Rest>
inline void xlin(std::size_t &idx, std::size_t &stride, inline void xlin(std::size_t &idx, std::size_t &stride, const First &first,
const First &first, const Rest &... rest) const { const Rest &... rest) const {
apply_visitor(xlin_visitor<First>{idx, stride, first}, axes_[D]); apply_visitor(xlin_visitor<First>{idx, stride, first}, axes_[D]);
xlin<D + 1>(idx, stride, rest...); xlin<D + 1>(idx, stride, rest...);
} }
@ -371,15 +365,16 @@ private:
// enable_if needed, because gcc thinks the overloads are ambiguous // enable_if needed, because gcc thinks the overloads are ambiguous
template <unsigned D, typename Weight, typename First, typename... Rest> template <unsigned D, typename Weight, typename First, typename... Rest>
inline typename std::enable_if<!(detail::is_weight<First>::value)>::type inline typename std::enable_if<!(detail::is_weight<First>::value)>::type
wxlin(std::size_t &idx, std::size_t &stride, Weight &w, wxlin(std::size_t &idx, std::size_t &stride, Weight &w, const First &first,
const First &first, const Rest &... rest) const { const Rest &... rest) const {
apply_visitor(xlin_visitor<First>{idx, stride, first}, axes_[D]); apply_visitor(xlin_visitor<First>{idx, stride, first}, axes_[D]);
wxlin<D + 1>(idx, stride, w, rest...); wxlin<D + 1>(idx, stride, w, rest...);
} }
template <unsigned D, typename Weight, typename T, typename... Rest> template <unsigned D, typename Weight, typename T, typename... Rest>
inline void wxlin(std::size_t &idx, std::size_t &stride, Weight &w, inline void wxlin(std::size_t &idx, std::size_t &stride, Weight &w,
const detail::weight_t<T> &first, const Rest &... rest) const { const detail::weight_t<T> &first,
const Rest &... rest) const {
w = first; w = first;
wxlin<D>(idx, stride, w, rest...); wxlin<D>(idx, stride, w, rest...);
} }
@ -437,12 +432,12 @@ make_dynamic_histogram(Axes &&... axes) {
template <typename... Axes> template <typename... Axes>
histogram<dynamic_tag, detail::union_t<axis::builtins, mpl::vector<Axes...>>, histogram<dynamic_tag, detail::union_t<axis::builtins, mpl::vector<Axes...>>,
array_storage<weight_counter<double>>> array_storage<weight_counter<double>>>
make_dynamic_weighted_histogram(Axes &&... axes) { make_dynamic_weighted_histogram(Axes &&... axes) {
return histogram<dynamic_tag, return histogram<dynamic_tag,
detail::union_t<axis::builtins, mpl::vector<Axes...>>, detail::union_t<axis::builtins, mpl::vector<Axes...>>,
array_storage<weight_counter<double>> array_storage<weight_counter<double>>>(
>(std::forward<Axes>(axes)...); std::forward<Axes>(axes)...);
} }
template <typename Storage, typename... Axes> template <typename Storage, typename... Axes>
@ -455,8 +450,8 @@ make_dynamic_histogram_with(Axes &&... axes) {
} }
template <typename Iterator, typename = detail::is_iterator<Iterator>> template <typename Iterator, typename = detail::is_iterator<Iterator>>
histogram<dynamic_tag, detail::union_t<axis::builtins, histogram<dynamic_tag,
typename Iterator::value_type::types>> detail::union_t<axis::builtins, typename Iterator::value_type::types>>
make_dynamic_histogram(Iterator begin, Iterator end) { make_dynamic_histogram(Iterator begin, Iterator end) {
return histogram< return histogram<
dynamic_tag, dynamic_tag,
@ -465,23 +460,21 @@ make_dynamic_histogram(Iterator begin, Iterator end) {
} }
template <typename Iterator, typename = detail::is_iterator<Iterator>> template <typename Iterator, typename = detail::is_iterator<Iterator>>
histogram<dynamic_tag, detail::union_t<axis::builtins, histogram<dynamic_tag,
typename Iterator::value_type::types>, detail::union_t<axis::builtins, typename Iterator::value_type::types>,
array_storage<weight_counter<double>>> array_storage<weight_counter<double>>>
make_dynamic_weighted_histogram(Iterator begin, Iterator end) { make_dynamic_weighted_histogram(Iterator begin, Iterator end) {
return histogram< return histogram<
dynamic_tag, dynamic_tag,
detail::union_t<axis::builtins, typename Iterator::value_type::types>, detail::union_t<axis::builtins, typename Iterator::value_type::types>,
array_storage<weight_counter<double>>>( array_storage<weight_counter<double>>>(begin, end);
begin, end);
} }
template <typename Storage, typename Iterator, template <typename Storage, typename Iterator,
typename = detail::is_iterator<Iterator>> typename = detail::is_iterator<Iterator>>
histogram< histogram<dynamic_tag,
dynamic_tag, detail::union_t<axis::builtins, typename Iterator::value_type::types>,
detail::union_t<axis::builtins, typename Iterator::value_type::types>, Storage>
Storage>
make_dynamic_histogram_with(Iterator begin, Iterator end) { make_dynamic_histogram_with(Iterator begin, Iterator end) {
return histogram< return histogram<
dynamic_tag, dynamic_tag,

View File

@ -9,8 +9,8 @@
#include <boost/mpl/bool.hpp> #include <boost/mpl/bool.hpp>
#include <boost/mpl/vector.hpp> #include <boost/mpl/vector.hpp>
#include <type_traits>
#include <string> #include <string>
#include <type_traits>
namespace boost { namespace boost {
namespace histogram { namespace histogram {
@ -60,8 +60,8 @@ using static_histogram = histogram<static_tag, Axes, Storage>;
namespace detail { namespace detail {
template <typename T> struct weight_t { template <typename T> struct weight_t {
T value; T value;
operator const T&() const { return value; } operator const T &() const { return value; }
}; };
template <typename T> struct is_weight : std::false_type {}; template <typename T> struct is_weight : std::false_type {};
template <typename T> struct is_weight<weight_t<T>> : std::true_type {}; template <typename T> struct is_weight<weight_t<T>> : std::true_type {};

View File

@ -67,9 +67,9 @@ protected:
template <typename Storage> template <typename Storage>
class bin_iterator_over class bin_iterator_over
: public iterator_facade< : public iterator_facade<bin_iterator_over<Storage>,
bin_iterator_over<Storage>, typename Storage::bin_type, typename Storage::bin_type, forward_traversal_tag,
forward_traversal_tag, typename Storage::bin_type>, typename Storage::bin_type>,
public detail::multi_index { public detail::multi_index {
public: public:

View File

@ -28,9 +28,9 @@
#include <boost/histogram/storage/operators.hpp> #include <boost/histogram/storage/operators.hpp>
#include <boost/mpl/bool.hpp> #include <boost/mpl/bool.hpp>
#include <boost/mpl/count_if.hpp> #include <boost/mpl/count_if.hpp>
#include <boost/mpl/find_if.hpp>
#include <boost/mpl/deref.hpp> #include <boost/mpl/deref.hpp>
#include <boost/mpl/empty.hpp> #include <boost/mpl/empty.hpp>
#include <boost/mpl/find_if.hpp>
#include <boost/mpl/int.hpp> #include <boost/mpl/int.hpp>
#include <boost/mpl/vector.hpp> #include <boost/mpl/vector.hpp>
#include <type_traits> #include <type_traits>
@ -193,7 +193,7 @@ public:
template <int N> template <int N>
typename std::add_const< typename std::add_const<
typename fusion::result_of::value_at_c<axes_type, N>::type>::type & typename fusion::result_of::value_at_c<axes_type, N>::type>::type &
axis(mpl::int_<N>) const { axis(mpl::int_<N>) const {
static_assert(N < axes_size::value, "axis index out of range"); static_assert(N < axes_size::value, "axis index out of range");
return fusion::at_c<N>(axes_); return fusion::at_c<N>(axes_);
} }
@ -201,7 +201,7 @@ public:
/// Get N-th axis /// Get N-th axis
template <int N> template <int N>
typename fusion::result_of::value_at_c<axes_type, N>::type & typename fusion::result_of::value_at_c<axes_type, N>::type &
axis(mpl::int_<N>) { axis(mpl::int_<N>) {
static_assert(N < axes_size::value, "axis index out of range"); static_assert(N < axes_size::value, "axis index out of range");
return fusion::at_c<N>(axes_); return fusion::at_c<N>(axes_);
} }
@ -226,12 +226,13 @@ public:
/// Returns a lower-dimensional histogram /// Returns a lower-dimensional histogram
template <int N, typename... Rest> template <int N, typename... Rest>
auto reduce_to(mpl::int_<N>, Rest...) const -> histogram< auto reduce_to(mpl::int_<N>, Rest...) const -> histogram<
static_tag, detail::axes_select_t<Axes, mpl::vector<mpl::int_<N>, Rest...>>, static_tag,
detail::axes_select_t<Axes, mpl::vector<mpl::int_<N>, Rest...>>,
Storage> { Storage> {
using HR = using HR = histogram<
histogram<static_tag, static_tag,
detail::axes_select_t<Axes, mpl::vector<mpl::int_<N>, Rest...>>, detail::axes_select_t<Axes, mpl::vector<mpl::int_<N>, Rest...>>,
Storage>; Storage>;
typename HR::axes_type axes; typename HR::axes_type axes;
detail::axes_assign_subset<mpl::vector<mpl::int_<N>, Rest...>>(axes, axes_); detail::axes_assign_subset<mpl::vector<mpl::int_<N>, Rest...>>(axes, axes_);
auto hr = HR(std::move(axes)); auto hr = HR(std::move(axes));
@ -241,9 +242,7 @@ public:
return hr; return hr;
} }
bin_iterator begin() const noexcept { bin_iterator begin() const noexcept { return bin_iterator(*this, storage_); }
return bin_iterator(*this, storage_);
}
bin_iterator end() const noexcept { return bin_iterator(storage_); } bin_iterator end() const noexcept { return bin_iterator(storage_); }
@ -269,9 +268,8 @@ private:
template <typename... Args> template <typename... Args>
inline void fill_impl(mpl::true_, mpl::false_, const Args &... args) { inline void fill_impl(mpl::true_, mpl::false_, const Args &... args) {
std::size_t idx = 0, stride = 1; std::size_t idx = 0, stride = 1;
typename mpl::deref< typename mpl::deref<typename mpl::find_if<
typename mpl::find_if<mpl::vector<Args...>, detail::is_weight<mpl::_>>::type mpl::vector<Args...>, detail::is_weight<mpl::_>>::type>::type w;
>::type w;
wxlin<0>(idx, stride, w, args...); wxlin<0>(idx, stride, w, args...);
if (stride) if (stride)
storage_.add(idx, w); storage_.add(idx, w);
@ -297,12 +295,11 @@ private:
lin<D + 1>(idx, stride, rest...); lin<D + 1>(idx, stride, rest...);
} }
template <unsigned D> template <unsigned D> inline void xlin(std::size_t &, std::size_t &) const {}
inline void xlin(std::size_t &, std::size_t &) const {}
template <unsigned D, typename First, typename... Rest> template <unsigned D, typename First, typename... Rest>
inline void xlin(std::size_t &idx, std::size_t &stride, inline void xlin(std::size_t &idx, std::size_t &stride, const First &first,
const First &first, const Rest &... rest) const { const Rest &... rest) const {
detail::xlin(idx, stride, fusion::at_c<D>(axes_), first); detail::xlin(idx, stride, fusion::at_c<D>(axes_), first);
xlin<D + 1>(idx, stride, rest...); xlin<D + 1>(idx, stride, rest...);
} }
@ -313,15 +310,16 @@ private:
// enable_if needed, because gcc thinks the overloads are ambiguous // enable_if needed, because gcc thinks the overloads are ambiguous
template <unsigned D, typename Weight, typename First, typename... Rest> template <unsigned D, typename Weight, typename First, typename... Rest>
inline typename std::enable_if<!(detail::is_weight<First>::value)>::type inline typename std::enable_if<!(detail::is_weight<First>::value)>::type
wxlin(std::size_t &idx, std::size_t &stride, Weight &w, wxlin(std::size_t &idx, std::size_t &stride, Weight &w, const First &first,
const First &first, const Rest &... rest) const { const Rest &... rest) const {
detail::xlin(idx, stride, fusion::at_c<D>(axes_), first); detail::xlin(idx, stride, fusion::at_c<D>(axes_), first);
wxlin<D + 1>(idx, stride, w, rest...); wxlin<D + 1>(idx, stride, w, rest...);
} }
template <unsigned D, typename Weight, typename T, typename... Rest> template <unsigned D, typename Weight, typename T, typename... Rest>
inline void wxlin(std::size_t &idx, std::size_t &stride, Weight &w, inline void wxlin(std::size_t &idx, std::size_t &stride, Weight &w,
const detail::weight_t<T> &first, const Rest &... rest) const { const detail::weight_t<T> &first,
const Rest &... rest) const {
w = first; w = first;
wxlin<D>(idx, stride, w, rest...); wxlin<D>(idx, stride, w, rest...);
} }
@ -358,13 +356,14 @@ make_static_histogram(Axis &&... axis) {
} }
template <typename... Axis> template <typename... Axis>
inline histogram<static_tag, mpl::vector<Axis...>, array_storage<weight_counter<double>>> inline histogram<static_tag, mpl::vector<Axis...>,
array_storage<weight_counter<double>>>
make_static_weighted_histogram(Axis &&... axis) { make_static_weighted_histogram(Axis &&... axis) {
using h = histogram<static_tag, mpl::vector<Axis...>, array_storage<weight_counter<double>>>; using h = histogram<static_tag, mpl::vector<Axis...>,
array_storage<weight_counter<double>>>;
return h(typename h::axes_type(std::forward<Axis>(axis)...)); return h(typename h::axes_type(std::forward<Axis>(axis)...));
} }
/// static type factory with variable storage type /// static type factory with variable storage type
template <typename Storage, typename... Axis> template <typename Storage, typename... Axis>
inline histogram<static_tag, mpl::vector<Axis...>, Storage> inline histogram<static_tag, mpl::vector<Axis...>, Storage>

View File

@ -231,9 +231,7 @@ struct bin_visitor : public static_visitor<wcount> {
wcount operator()(const array<void> & /*b*/) const { return wcount(0.0); } wcount operator()(const array<void> & /*b*/) const { return wcount(0.0); }
wcount operator()(const array<wcount> &b) const { wcount operator()(const array<wcount> &b) const { return b[idx]; }
return b[idx];
}
}; };
template <typename RHS> struct radd_visitor : public static_visitor<void> { template <typename RHS> struct radd_visitor : public static_visitor<void> {
@ -261,9 +259,7 @@ template <typename RHS> struct radd_visitor : public static_visitor<void> {
lhs[idx] += static_cast<mp_int>(rhs); lhs[idx] += static_cast<mp_int>(rhs);
} }
void operator()(array<wcount> &lhs) const { void operator()(array<wcount> &lhs) const { lhs[idx] += rhs; }
lhs[idx] += rhs;
}
}; };
template <> struct radd_visitor<mp_int> : public static_visitor<void> { template <> struct radd_visitor<mp_int> : public static_visitor<void> {
@ -287,9 +283,7 @@ template <> struct radd_visitor<mp_int> : public static_visitor<void> {
} }
} }
void operator()(array<mp_int> &lhs) const { void operator()(array<mp_int> &lhs) const { lhs[idx] += rhs; }
lhs[idx] += rhs;
}
void operator()(array<wcount> &lhs) const { void operator()(array<wcount> &lhs) const {
lhs[idx] += static_cast<double>(rhs); lhs[idx] += static_cast<double>(rhs);
@ -316,7 +310,8 @@ template <> struct radd_visitor<wcount> : public static_visitor<void> {
void operator()(array<wcount> &lhs) const { lhs[idx] += rhs; } void operator()(array<wcount> &lhs) const { lhs[idx] += rhs; }
}; };
template <> struct radd_visitor<weight_t<double>> : public static_visitor<void> { template <>
struct radd_visitor<weight_t<double>> : public static_visitor<void> {
any_array &lhs_any; any_array &lhs_any;
const std::size_t idx; const std::size_t idx;
const weight_t<double> rhs; const weight_t<double> rhs;
@ -408,8 +403,7 @@ public:
: buffer_(detail::array<void>(rhs.size())) { : buffer_(detail::array<void>(rhs.size())) {
using T = typename RHS::bin_type; using T = typename RHS::bin_type;
for (std::size_t i = 0, n = rhs.size(); i < n; ++i) { for (std::size_t i = 0, n = rhs.size(); i < n; ++i) {
apply_visitor(detail::assign_visitor<T>(buffer_, i, rhs[i]), apply_visitor(detail::assign_visitor<T>(buffer_, i, rhs[i]), buffer_);
buffer_);
} }
} }
@ -421,8 +415,7 @@ public:
} }
using T = typename RHS::bin_type; using T = typename RHS::bin_type;
for (std::size_t i = 0; i < n; ++i) { for (std::size_t i = 0; i < n; ++i) {
apply_visitor(detail::assign_visitor<T>(buffer_, i, rhs[i]), apply_visitor(detail::assign_visitor<T>(buffer_, i, rhs[i]), buffer_);
buffer_);
} }
return *this; return *this;
} }
@ -435,9 +428,10 @@ public:
apply_visitor(detail::increase_visitor(buffer_, i), buffer_); apply_visitor(detail::increase_visitor(buffer_, i), buffer_);
} }
void add(std::size_t i, const bin_type& x) { void add(std::size_t i, const bin_type &x) {
if (x.has_trivial_variance()) { if (x.has_trivial_variance()) {
apply_visitor(detail::radd_visitor<double>(buffer_, i, x.value()), buffer_); apply_visitor(detail::radd_visitor<double>(buffer_, i, x.value()),
buffer_);
} else { } else {
apply_visitor(detail::radd_visitor<bin_type>(buffer_, i, x), buffer_); apply_visitor(detail::radd_visitor<bin_type>(buffer_, i, x), buffer_);
} }
@ -448,8 +442,9 @@ public:
} }
template <typename T> void add(std::size_t i, const detail::weight_t<T> &w) { template <typename T> void add(std::size_t i, const detail::weight_t<T> &w) {
apply_visitor(detail::radd_visitor<detail::weight_t<double>>(buffer_, i, w.value), apply_visitor(
buffer_); detail::radd_visitor<detail::weight_t<double>>(buffer_, i, w.value),
buffer_);
} }
bin_type operator[](std::size_t i) const { bin_type operator[](std::size_t i) const {
@ -475,13 +470,13 @@ public:
// precondition: storages have same size // precondition: storages have same size
template <typename RHS> adaptive_storage &operator+=(const RHS &rhs) { template <typename RHS> adaptive_storage &operator+=(const RHS &rhs) {
for (auto i = 0ul, n = size(); i < n; ++i) for (auto i = 0ul, n = size(); i < n; ++i)
apply_visitor(detail::radd_visitor<typename RHS::bin_type>( apply_visitor(
buffer_, i, rhs[i]), detail::radd_visitor<typename RHS::bin_type>(buffer_, i, rhs[i]),
buffer_); buffer_);
return *this; return *this;
} }
template <typename T> adaptive_storage &operator*=(const T& x) { template <typename T> adaptive_storage &operator*=(const T &x) {
apply_visitor(detail::rmul_visitor(buffer_, x), buffer_); apply_visitor(detail::rmul_visitor(buffer_, x), buffer_);
return *this; return *this;
} }

View File

@ -26,15 +26,13 @@ template <typename T> class array_storage {
public: public:
using bin_type = T; using bin_type = T;
explicit array_storage(std::size_t s) : explicit array_storage(std::size_t s) : size_(s), array_(new bin_type[s]) {
size_(s), array_(new bin_type[s])
{
std::fill(array_.get(), array_.get() + s, bin_type(0)); std::fill(array_.get(), array_.get() + s, bin_type(0));
} }
array_storage() = default; array_storage() = default;
array_storage(const array_storage &other) : array_storage(const array_storage &other)
size_(other.size()), array_(new bin_type[other.size()]) { : size_(other.size()), array_(new bin_type[other.size()]) {
std::copy(other.array_.get(), other.array_.get() + size_, array_.get()); 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) {
@ -79,16 +77,13 @@ public:
array_[i] += x; array_[i] += x;
} }
const bin_type& operator[](std::size_t i) const noexcept { const bin_type &operator[](std::size_t i) const noexcept { return array_[i]; }
return array_[i];
}
template <typename U> template <typename U>
bool operator==(const array_storage<U> &rhs) const noexcept { bool operator==(const array_storage<U> &rhs) const noexcept {
if (size_ != rhs.size_) if (size_ != rhs.size_)
return false; return false;
return std::equal(array_.get(), array_.get() + size_, return std::equal(array_.get(), array_.get() + size_, rhs.array_.get());
rhs.array_.get());
} }
template <typename S> array_storage &operator+=(const S &rhs) noexcept { template <typename S> array_storage &operator+=(const S &rhs) noexcept {

View File

@ -28,7 +28,8 @@ public:
weight_counter &operator=(const weight_counter &) = default; weight_counter &operator=(const weight_counter &) = default;
weight_counter &operator=(weight_counter &&) = default; weight_counter &operator=(weight_counter &&) = default;
weight_counter(const RealType& value, const RealType& variance) : w(value), w2(variance) {} weight_counter(const RealType &value, const RealType &variance)
: w(value), w2(variance) {}
weight_counter &operator++() { weight_counter &operator++() {
++w; ++w;
@ -71,11 +72,13 @@ public:
return !operator==(rhs); return !operator==(rhs);
} }
template <typename T> bool operator==(const weight_counter<T> &rhs) const noexcept { template <typename T>
bool operator==(const weight_counter<T> &rhs) const noexcept {
return w == rhs.w && w2 == rhs.w2; return w == rhs.w && w2 == rhs.w2;
} }
template <typename T> bool operator!=(const weight_counter<T> &rhs) const noexcept { template <typename T>
bool operator!=(const weight_counter<T> &rhs) const noexcept {
return !operator==(rhs); return !operator==(rhs);
} }
@ -87,21 +90,24 @@ public:
return !operator==(rhs); return !operator==(rhs);
} }
const RealType& value() const noexcept { return w; } const RealType &value() const noexcept { return w; }
const RealType& variance() const noexcept { return w2; } const RealType &variance() const noexcept { return w2; }
bool has_trivial_variance() const noexcept { return w == w2; } bool has_trivial_variance() const noexcept { return w == w2; }
// conversion // conversion
template <typename T> explicit weight_counter(const T &t) : w(static_cast<T>(t)), w2(w) {} template <typename T>
explicit weight_counter(const T &t) : w(static_cast<T>(t)), w2(w) {}
explicit operator RealType() const { explicit operator RealType() const {
if (!has_trivial_variance()) if (!has_trivial_variance())
throw std::logic_error("cannot convert weight_counter to RealType, value and variance differ"); throw std::logic_error("cannot convert weight_counter to RealType, value "
"and variance differ");
return w; return w;
} }
template <typename T> explicit operator T() const { template <typename T> explicit operator T() const {
if (!has_trivial_variance()) if (!has_trivial_variance())
throw std::logic_error("cannot convert weight_counter to RealType, value and variance differ"); throw std::logic_error("cannot convert weight_counter to RealType, value "
"and variance differ");
return w; return w;
} }
template <typename T> weight_counter &operator=(const T &x) { template <typename T> weight_counter &operator=(const T &x) {

View File

@ -6,17 +6,17 @@
#include <boost/core/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>
#include <boost/histogram/detail/axis_visitor.hpp> #include <boost/histogram/detail/axis_visitor.hpp>
#include <boost/histogram/detail/utility.hpp>
#include <boost/histogram/detail/cat.hpp> #include <boost/histogram/detail/cat.hpp>
#include <boost/histogram/detail/meta.hpp>
#include <boost/histogram/detail/utility.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/variant.hpp> #include <boost/variant.hpp>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector>
#include <boost/histogram/detail/meta.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/vector.hpp>
#include <type_traits> #include <type_traits>
#include <vector>
using namespace boost::mpl; using namespace boost::mpl;
using namespace boost::histogram::detail; using namespace boost::histogram::detail;
@ -93,9 +93,7 @@ int main() {
} }
// cat // cat
{ { BOOST_TEST_EQ(cat("foo", 1, "bar"), std::string("foo1bar")); }
BOOST_TEST_EQ(cat("foo", 1, "bar"), std::string("foo1bar"));
}
// unique_sorted // unique_sorted
{ {
@ -109,10 +107,10 @@ int main() {
// union // union
{ {
typedef vector<int, unsigned, char> main_vector; typedef vector<int, unsigned, char> main_vector;
typedef vector<unsigned, void*> aux_vector; typedef vector<unsigned, void *> aux_vector;
using result = union_t<main_vector, aux_vector>; using result = union_t<main_vector, aux_vector>;
typedef vector<int, unsigned, char, void*> expected; typedef vector<int, unsigned, char, void *> expected;
BOOST_MPL_ASSERT((equal<result, expected, std::is_same<_, _>>)); BOOST_MPL_ASSERT((equal<result, expected, std::is_same<_, _>>));
} }
@ -121,26 +119,25 @@ int main() {
struct no_methods {}; struct no_methods {};
struct value_method { struct value_method {
const double& value() const; const double &value() const;
}; };
struct variance_method { struct variance_method {
const double& variance() const; const double &variance() const;
}; };
struct value_and_variance_methods { struct value_and_variance_methods {
const double& value() const; const double &value() const;
const double& variance() const; const double &variance() const;
}; };
BOOST_TEST_EQ(typename has_variance_support<no_methods>::type(), BOOST_TEST_EQ(typename has_variance_support<no_methods>::type(), false);
false); BOOST_TEST_EQ(typename has_variance_support<value_method>::type(), false);
BOOST_TEST_EQ(typename has_variance_support<value_method>::type(),
false);
BOOST_TEST_EQ(typename has_variance_support<variance_method>::type(), BOOST_TEST_EQ(typename has_variance_support<variance_method>::type(),
false); false);
BOOST_TEST_EQ(typename has_variance_support<value_and_variance_methods>::type(), BOOST_TEST_EQ(
true); typename has_variance_support<value_and_variance_methods>::type(),
true);
} }
return boost::report_errors(); return boost::report_errors();

View File

@ -4,6 +4,7 @@
// (See accompanying file LICENSE_1_0.txt // (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt) // or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <array>
#include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_oarchive.hpp>
#include <boost/core/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>
@ -19,7 +20,6 @@
#include <limits> #include <limits>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <array>
using namespace boost::histogram; using namespace boost::histogram;
using namespace boost::histogram::literals; // to get _c suffix using namespace boost::histogram::literals; // to get _c suffix
@ -852,8 +852,8 @@ int main() {
// using iterator ranges // using iterator ranges
{ {
auto h = make_dynamic_histogram(axis::integer<>(0, 2), auto h =
axis::integer<>(2, 4)); make_dynamic_histogram(axis::integer<>(0, 2), axis::integer<>(2, 4));
auto v = std::vector<int>(2); auto v = std::vector<int>(2);
auto i = std::array<int, 2>(); auto i = std::array<int, 2>();

View File

@ -34,7 +34,7 @@ int main() {
w += weight(2); w += weight(2);
BOOST_TEST_EQ(w.value(), 3); BOOST_TEST_EQ(w.value(), 3);
BOOST_TEST_EQ(w.variance(), 5); BOOST_TEST_EQ(w.variance(), 5);
} }
{ {
@ -43,7 +43,7 @@ int main() {
wcount u(0); wcount u(0);
u += weight(1); u += weight(1);
u *= 2; u *= 2;
BOOST_TEST_EQ(u, wcount(2, 4)); BOOST_TEST_EQ(u, wcount(2, 4));
wcount v(0); wcount v(0);
v += weight(2); v += weight(2);