use integral constants to access axis in histogram statically

This commit is contained in:
Hans Dembinski
2017-04-25 10:10:22 +02:00
parent 00807ca54d
commit a2bc4f428d
5 changed files with 99 additions and 45 deletions

View File

@@ -213,12 +213,6 @@ public:
return axes_[i];
}
/// Return axis \a i (for conformity with histogram<Static, ...> interface)
template <unsigned N = 0> const axis_type &axis() const {
BOOST_ASSERT_MSG(N < dim(), "axis index out of range");
return axes_[N];
}
/// Apply unary functor/function to each axis
template <typename Unary> void for_each_axis(Unary &unary) const {
for (const auto &a : axes_) {

View File

@@ -150,14 +150,24 @@ public:
/// Reset bin counters to zero
void reset() { storage_ = std::move(Storage(storage_.size())); }
template <unsigned N = 0>
/// Get N-th axis
template <unsigned N>
constexpr
typename std::add_const<
typename fusion::result_of::value_at_c<axes_type, N>::type>::type &
axis() const {
axis(std::integral_constant<unsigned, N>) const {
static_assert(N < axes_size::value, "axis index out of range");
return fusion::at_c<N>(axes_);
}
// Get first axis (convenience for 1-d histograms)
constexpr
typename std::add_const<
typename fusion::result_of::value_at_c<axes_type, 0>::type>::type &
axis() const {
return fusion::at_c<0>(axes_);
}
/// Apply unary functor/function to each axis
template <typename Unary> void for_each_axis(Unary &unary) const {
fusion::for_each(axes_, unary);

View File

@@ -0,0 +1,45 @@
// Copyright 2015-2017 Hans Dembinski
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef _BOOST_HISTOGRAM_LITERALS_HPP_
#define _BOOST_HISTOGRAM_LITERALS_HPP_
#include <type_traits>
namespace boost {
namespace histogram {
namespace literals {
namespace detail {
template <char C> struct char2int;
template <> struct char2int<'0'> { static constexpr unsigned value = 0; };
template <> struct char2int<'1'> { static constexpr unsigned value = 1; };
template <> struct char2int<'2'> { static constexpr unsigned value = 2; };
template <> struct char2int<'3'> { static constexpr unsigned value = 3; };
template <> struct char2int<'4'> { static constexpr unsigned value = 4; };
template <> struct char2int<'5'> { static constexpr unsigned value = 5; };
template <> struct char2int<'6'> { static constexpr unsigned value = 6; };
template <> struct char2int<'7'> { static constexpr unsigned value = 7; };
template <> struct char2int<'8'> { static constexpr unsigned value = 8; };
template <> struct char2int<'9'> { static constexpr unsigned value = 9; };
template <unsigned N>
constexpr unsigned parse() { return N; }
template <unsigned N, char First, char... Rest>
constexpr unsigned parse() { return parse<N * 10 + char2int<First>::value, Rest...>(); }
} // NS detail
template <char... Digits>
auto operator"" _c() -> decltype(std::integral_constant<unsigned, detail::parse<0, Digits...>()>())
{
return std::integral_constant<unsigned, detail::parse<0, Digits...>()>();
}
} // NS literals
} // NS histogram
} // NS boost
#endif