mirror of
https://github.com/boostorg/histogram.git
synced 2025-05-11 13:14:06 +00:00
use new add method and clangformat
This commit is contained in:
parent
b94d21a46f
commit
def7d6b05a
@ -19,15 +19,6 @@ namespace boost {
|
||||
namespace histogram {
|
||||
namespace detail {
|
||||
|
||||
template <typename T> struct supports_weights {
|
||||
template <typename> static std::false_type test(...);
|
||||
|
||||
template <typename C>
|
||||
static decltype(std::declval<C &>().weighted_increase(0, 0.0), std::true_type{}) test(int);
|
||||
|
||||
static bool const value = decltype(test<T>(0))::value;
|
||||
};
|
||||
|
||||
template <typename T, typename = decltype(std::declval<T &>().size(),
|
||||
std::declval<T &>().increase(0),
|
||||
std::declval<T &>().value(0))>
|
||||
|
@ -7,10 +7,10 @@
|
||||
#ifndef _BOOST_HISTOGRAM_DETAIL_UTILITY_HPP_
|
||||
#define _BOOST_HISTOGRAM_DETAIL_UTILITY_HPP_
|
||||
|
||||
#include <boost/call_traits.hpp>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <boost/call_traits.hpp>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
namespace boost {
|
||||
namespace histogram {
|
||||
@ -62,13 +62,12 @@ template <typename A, typename T> struct xlin {
|
||||
struct index_mapper {
|
||||
std::size_t first, second;
|
||||
|
||||
index_mapper(std::vector<unsigned> && nvec, std::vector<bool> && bvec) :
|
||||
first(0), second(0)
|
||||
{
|
||||
index_mapper(std::vector<unsigned> &&nvec, std::vector<bool> &&bvec)
|
||||
: first(0), second(0) {
|
||||
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;
|
||||
@ -78,12 +77,11 @@ struct index_mapper {
|
||||
s1 *= ni;
|
||||
++bi;
|
||||
}
|
||||
std::sort(dims.begin(), dims.end(),
|
||||
[](const dim& a, const dim& b) {
|
||||
if (a.stride1 == b.stride1)
|
||||
return 0;
|
||||
return a.stride1 < b.stride1 ? -1 : 1;
|
||||
});
|
||||
std::sort(dims.begin(), dims.end(), [](const dim &a, const dim &b) {
|
||||
if (a.stride1 == b.stride1)
|
||||
return 0;
|
||||
return a.stride1 < b.stride1 ? -1 : 1;
|
||||
});
|
||||
nfirst = s1;
|
||||
}
|
||||
|
||||
@ -91,18 +89,19 @@ 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;
|
||||
}
|
||||
assert(f == 0);
|
||||
return first < nfirst;
|
||||
}
|
||||
|
||||
private:
|
||||
std::size_t nfirst;
|
||||
struct dim { std::size_t stride1, stride2; };
|
||||
struct dim {
|
||||
std::size_t stride1, stride2;
|
||||
};
|
||||
std::vector<dim> dims;
|
||||
};
|
||||
|
||||
|
@ -8,8 +8,8 @@
|
||||
#define _BOOST_HISTOGRAM_HISTOGRAM_FWD_HPP_
|
||||
|
||||
#include <boost/histogram/storage/adaptive_storage.hpp>
|
||||
#include <type_traits>
|
||||
#include <set>
|
||||
#include <type_traits>
|
||||
|
||||
namespace boost {
|
||||
namespace histogram {
|
||||
@ -24,6 +24,7 @@ class weight {
|
||||
public:
|
||||
explicit weight(double v) : value(v) {}
|
||||
explicit operator double() const { return value; }
|
||||
|
||||
private:
|
||||
double value;
|
||||
};
|
||||
@ -32,14 +33,15 @@ class count {
|
||||
public:
|
||||
explicit count(unsigned v) : value(v) {}
|
||||
explicit operator unsigned() const { return value; }
|
||||
|
||||
private:
|
||||
unsigned value;
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
template <unsigned>
|
||||
struct keep_remove : std::set<unsigned> {
|
||||
keep_remove(const std::initializer_list<unsigned>& l) : std::set<unsigned>(l) {}
|
||||
template <unsigned> struct keep_remove : std::set<unsigned> {
|
||||
keep_remove(const std::initializer_list<unsigned> &l)
|
||||
: std::set<unsigned>(l) {}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
|
@ -15,9 +15,9 @@
|
||||
#include <boost/histogram/detail/meta.hpp>
|
||||
#include <boost/histogram/detail/utility.hpp>
|
||||
#include <boost/histogram/histogram_fwd.hpp>
|
||||
#include <boost/mpl/int.hpp>
|
||||
#include <boost/mpl/count.hpp>
|
||||
#include <boost/mpl/empty.hpp>
|
||||
#include <boost/mpl/int.hpp>
|
||||
#include <boost/mpl/vector.hpp>
|
||||
#include <boost/variant.hpp>
|
||||
#include <cstddef>
|
||||
@ -129,9 +129,11 @@ public:
|
||||
template <typename... Args> void fill(Args... args) noexcept {
|
||||
using n_count = typename mpl::count<mpl::vector<Args...>, count>;
|
||||
using n_weight = typename mpl::count<mpl::vector<Args...>, weight>;
|
||||
static_assert((n_count::value + n_weight::value) <= 1,
|
||||
"arguments may contain at most one instance of type count or weight");
|
||||
BOOST_ASSERT_MSG(sizeof...(args) == (dim() + n_count::value + n_weight::value),
|
||||
static_assert(
|
||||
(n_count::value + n_weight::value) <= 1,
|
||||
"arguments may contain at most one instance of type count or weight");
|
||||
BOOST_ASSERT_MSG(sizeof...(args) ==
|
||||
(dim() + n_count::value + n_weight::value),
|
||||
"number of arguments does not match histogram dimension");
|
||||
fill_impl(mpl::int_<(n_count::value + 2 * n_weight::value)>(), args...);
|
||||
}
|
||||
@ -311,9 +313,10 @@ private:
|
||||
template <template <class, class> class Lin, unsigned D, typename X>
|
||||
inline void apply_lin_x(std::size_t &, std::size_t &, X &) const {}
|
||||
|
||||
template <template <class, class> class Lin, unsigned D, typename X, typename First,
|
||||
typename... Rest>
|
||||
inline typename std::enable_if<!(std::is_same<First, weight>::value || std::is_same<First, count>::value)>::type
|
||||
template <template <class, class> class Lin, unsigned D, typename X,
|
||||
typename First, typename... Rest>
|
||||
inline typename std::enable_if<!(std::is_same<First, weight>::value ||
|
||||
std::is_same<First, count>::value)>::type
|
||||
apply_lin_x(std::size_t &idx, std::size_t &stride, X &x, const First &first,
|
||||
const Rest &... rest) const {
|
||||
apply_visitor(lin_visitor<Lin, First>(idx, stride, first), axes_[D]);
|
||||
@ -345,13 +348,12 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
histogram reduce_impl(std::vector<bool>&& b) const
|
||||
{
|
||||
histogram reduce_impl(std::vector<bool> &&b) const {
|
||||
axes_type axes;
|
||||
std::vector<unsigned> 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 = apply_visitor(detail::shape(), *axes_iter);
|
||||
@ -361,21 +363,22 @@ private:
|
||||
histogram h(axes.begin(), axes.end());
|
||||
detail::index_mapper m(std::move(n), std::move(b));
|
||||
do {
|
||||
h.storage_.increase(m.second, storage_.value(m.first));
|
||||
h.storage_.add(m.second, storage_.value(m.first),
|
||||
storage_.variance(m.first));
|
||||
} while (m.next());
|
||||
return h;
|
||||
}
|
||||
|
||||
friend histogram reduce(const histogram& h, const keep& k) {
|
||||
friend histogram reduce(const histogram &h, const keep &k) {
|
||||
std::vector<bool> b(h.dim(), false);
|
||||
for (const auto& i : k)
|
||||
for (const auto &i : k)
|
||||
b[i] = true;
|
||||
return h.reduce_impl(std::move(b));
|
||||
}
|
||||
|
||||
friend histogram reduce(const histogram& h, const remove& r) {
|
||||
friend histogram reduce(const histogram &h, const remove &r) {
|
||||
std::vector<bool> b(h.dim(), true);
|
||||
for (const auto& i : r)
|
||||
for (const auto &i : r)
|
||||
b[i] = false;
|
||||
return h.reduce_impl(std::move(b));
|
||||
}
|
||||
@ -401,9 +404,10 @@ inline histogram<
|
||||
Dynamic, typename detail::combine<builtin_axes, mpl::vector<Axes...>>::type,
|
||||
Storage>
|
||||
make_dynamic_histogram_with(Axes &&... axes) {
|
||||
return histogram<Dynamic, typename detail::combine<
|
||||
builtin_axes, mpl::vector<Axes...>>::type,
|
||||
Storage>(std::forward<Axes>(axes)...);
|
||||
return histogram<
|
||||
Dynamic,
|
||||
typename detail::combine<builtin_axes, mpl::vector<Axes...>>::type,
|
||||
Storage>(std::forward<Axes>(axes)...);
|
||||
}
|
||||
|
||||
} // namespace histogram
|
||||
|
@ -26,9 +26,9 @@
|
||||
#include <boost/histogram/detail/meta.hpp>
|
||||
#include <boost/histogram/detail/utility.hpp>
|
||||
#include <boost/histogram/histogram_fwd.hpp>
|
||||
#include <boost/mpl/int.hpp>
|
||||
#include <boost/mpl/count.hpp>
|
||||
#include <boost/mpl/empty.hpp>
|
||||
#include <boost/mpl/int.hpp>
|
||||
#include <boost/mpl/vector.hpp>
|
||||
#include <type_traits>
|
||||
|
||||
@ -106,9 +106,11 @@ public:
|
||||
template <typename... Args> void fill(const Args &... args) {
|
||||
using n_count = typename mpl::count<mpl::vector<Args...>, count>;
|
||||
using n_weight = typename mpl::count<mpl::vector<Args...>, weight>;
|
||||
static_assert((n_count::value + n_weight::value) <= 1,
|
||||
"arguments may contain at most one instance of type count or weight");
|
||||
static_assert(sizeof...(args) == (axes_size::value + n_count::value + n_weight::value),
|
||||
static_assert(
|
||||
(n_count::value + n_weight::value) <= 1,
|
||||
"arguments may contain at most one instance of type count or weight");
|
||||
static_assert(sizeof...(args) ==
|
||||
(axes_size::value + n_count::value + n_weight::value),
|
||||
"number of arguments does not match histogram dimension");
|
||||
fill_impl(mpl::int_<(n_count::value + 2 * n_weight::value)>(), args...);
|
||||
}
|
||||
@ -232,7 +234,8 @@ private:
|
||||
|
||||
template <template <class, class> class Lin, unsigned D, typename X,
|
||||
typename First, typename... Rest>
|
||||
inline typename std::enable_if<!(std::is_same<First, weight>::value || std::is_same<First, count>::value)>::type
|
||||
inline typename std::enable_if<!(std::is_same<First, weight>::value ||
|
||||
std::is_same<First, count>::value)>::type
|
||||
apply_lin_x(std::size_t &idx, std::size_t &stride, X &x, const First &first,
|
||||
const Rest &... rest) const {
|
||||
Lin<typename fusion::result_of::value_at_c<axes_type, D>::type,
|
||||
|
@ -11,9 +11,9 @@
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/histogram/detail/meta.hpp>
|
||||
#include <boost/histogram/detail/weight.hpp>
|
||||
#include <boost/mpl/int.hpp>
|
||||
#include <boost/multiprecision/cpp_int.hpp>
|
||||
#include <boost/variant.hpp>
|
||||
#include <boost/mpl/int.hpp>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
|
||||
@ -188,25 +188,22 @@ public:
|
||||
apply_visitor(increase_visitor(i, buffer_), buffer_);
|
||||
}
|
||||
|
||||
template <typename Value>
|
||||
void increase(std::size_t i, const Value& n) {
|
||||
apply_visitor(
|
||||
add_visitor<Value>(i, n, buffer_),
|
||||
buffer_);
|
||||
template <typename Value> void increase(std::size_t i, const Value &n) {
|
||||
apply_visitor(add_visitor<Value>(i, n, buffer_), buffer_);
|
||||
}
|
||||
|
||||
void weighted_increase(std::size_t i, value_type weight) {
|
||||
apply_visitor(wincrease_visitor(i, weight, buffer_), buffer_);
|
||||
}
|
||||
|
||||
void add(std::size_t i, const value_type& val, const value_type& var) {
|
||||
void add(std::size_t i, const value_type &val, const value_type &var) {
|
||||
if (val == var) {
|
||||
apply_visitor(add_visitor<value_type>(i, val, buffer_), buffer_);
|
||||
} else {
|
||||
if (!boost::get<array<weight>>(&buffer_)) {
|
||||
apply_visitor(wincrease_visitor(0, 0.0, buffer_), buffer_);
|
||||
}
|
||||
auto& b = boost::get<array<weight>>(buffer_);
|
||||
auto &b = boost::get<array<weight>>(buffer_);
|
||||
b[i].w += val;
|
||||
b[i].w2 += var;
|
||||
}
|
||||
|
@ -57,8 +57,7 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
template <typename S>
|
||||
container_storage &operator=(const S &other) {
|
||||
template <typename S> container_storage &operator=(const S &other) {
|
||||
detail::init(container_, other.size());
|
||||
for (std::size_t i = 0; i < container_.size(); ++i) {
|
||||
container_[i] = other.value(i);
|
||||
@ -68,10 +67,11 @@ public:
|
||||
|
||||
std::size_t size() const { return container_.size(); }
|
||||
void increase(std::size_t i) { ++(container_[i]); }
|
||||
template <typename Value>
|
||||
void increase(std::size_t i, const Value& n) { container_[i] += n; }
|
||||
template <typename Value> void increase(std::size_t i, const Value &n) {
|
||||
container_[i] += n;
|
||||
}
|
||||
|
||||
void add(std::size_t i, const value_type& val, const value_type& /* var */) {
|
||||
void add(std::size_t i, const value_type &val, const value_type & /* var */) {
|
||||
container_[i] += val;
|
||||
}
|
||||
|
||||
|
@ -347,7 +347,7 @@ int main() {
|
||||
adaptive_storage<> c(1);
|
||||
c.weighted_increase(0, a.value(0));
|
||||
BOOST_TEST_EQ(c.value(0), x);
|
||||
BOOST_TEST_EQ(c.variance(0), x*x);
|
||||
BOOST_TEST_EQ(c.variance(0), x * x);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -603,7 +603,8 @@ int main() {
|
||||
|
||||
// reduce
|
||||
{
|
||||
auto h1 = histogram<Dynamic, builtin_axes>(integer_axis(0, 1), integer_axis(2, 3));
|
||||
auto h1 = histogram<Dynamic, builtin_axes>(integer_axis(0, 1),
|
||||
integer_axis(2, 3));
|
||||
h1.fill(0, 2);
|
||||
h1.fill(0, 3);
|
||||
h1.fill(1, 2);
|
||||
|
Loading…
x
Reference in New Issue
Block a user