histogram.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. // Copyright 2015-2018 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_HISTOGRAM_HISTOGRAM_HPP
  7. #define BOOST_HISTOGRAM_HISTOGRAM_HPP
  8. #include <boost/histogram/detail/accumulator_traits.hpp>
  9. #include <boost/histogram/detail/argument_traits.hpp>
  10. #include <boost/histogram/detail/at.hpp>
  11. #include <boost/histogram/detail/axes.hpp>
  12. #include <boost/histogram/detail/common_type.hpp>
  13. #include <boost/histogram/detail/fill.hpp>
  14. #include <boost/histogram/detail/fill_n.hpp>
  15. #include <boost/histogram/detail/mutex_base.hpp>
  16. #include <boost/histogram/detail/non_member_container_access.hpp>
  17. #include <boost/histogram/detail/span.hpp>
  18. #include <boost/histogram/fwd.hpp>
  19. #include <boost/histogram/sample.hpp>
  20. #include <boost/histogram/storage_adaptor.hpp>
  21. #include <boost/histogram/unsafe_access.hpp>
  22. #include <boost/histogram/weight.hpp>
  23. #include <boost/mp11/integral.hpp>
  24. #include <boost/mp11/list.hpp>
  25. #include <boost/mp11/tuple.hpp>
  26. #include <boost/throw_exception.hpp>
  27. #include <mutex>
  28. #include <stdexcept>
  29. #include <tuple>
  30. #include <type_traits>
  31. #include <utility>
  32. #include <vector>
  33. namespace boost {
  34. namespace histogram {
  35. /** Central class of the histogram library.
  36. Histogram uses the call operator to insert data, like the
  37. [Boost.Accumulators](https://www.boost.org/doc/libs/develop/doc/html/accumulators.html).
  38. Use factory functions (see
  39. [make_histogram.hpp](histogram/reference.html#header.boost.histogram.make_histogram_hpp)
  40. and
  41. [make_profile.hpp](histogram/reference.html#header.boost.histogram.make_profile_hpp)) to
  42. conveniently create histograms rather than calling the ctors directly.
  43. Use the [indexed](boost/histogram/indexed.html) range generator to iterate over filled
  44. histograms, which is convenient and faster than hand-written loops for multi-dimensional
  45. histograms.
  46. @tparam Axes std::tuple of axis types OR std::vector of an axis type or axis::variant
  47. @tparam Storage class that implements the storage interface
  48. */
  49. template <class Axes, class Storage>
  50. class histogram : detail::mutex_base<Axes, Storage> {
  51. static_assert(std::is_same<std::decay_t<Storage>, Storage>::value,
  52. "Storage may not be a reference or const or volatile");
  53. static_assert(mp11::mp_size<Axes>::value > 0, "at least one axis required");
  54. public:
  55. using axes_type = Axes;
  56. using storage_type = Storage;
  57. using value_type = typename storage_type::value_type;
  58. // typedefs for boost::range_iterator
  59. using iterator = typename storage_type::iterator;
  60. using const_iterator = typename storage_type::const_iterator;
  61. private:
  62. using mutex_base = typename detail::mutex_base<axes_type, storage_type>;
  63. public:
  64. histogram() = default;
  65. template <class A, class S>
  66. explicit histogram(histogram<A, S>&& rhs)
  67. : storage_(std::move(unsafe_access::storage(rhs)))
  68. , offset_(unsafe_access::offset(rhs)) {
  69. detail::axes_assign(axes_, std::move(unsafe_access::axes(rhs)));
  70. detail::throw_if_axes_is_too_large(axes_);
  71. }
  72. template <class A, class S>
  73. explicit histogram(const histogram<A, S>& rhs)
  74. : storage_(unsafe_access::storage(rhs)), offset_(unsafe_access::offset(rhs)) {
  75. detail::axes_assign(axes_, unsafe_access::axes(rhs));
  76. detail::throw_if_axes_is_too_large(axes_);
  77. }
  78. template <class A, class S>
  79. histogram& operator=(histogram<A, S>&& rhs) {
  80. detail::axes_assign(axes_, std::move(unsafe_access::axes(rhs)));
  81. detail::throw_if_axes_is_too_large(axes_);
  82. storage_ = std::move(unsafe_access::storage(rhs));
  83. offset_ = unsafe_access::offset(rhs);
  84. return *this;
  85. }
  86. template <class A, class S>
  87. histogram& operator=(const histogram<A, S>& rhs) {
  88. detail::axes_assign(axes_, unsafe_access::axes(rhs));
  89. detail::throw_if_axes_is_too_large(axes_);
  90. storage_ = unsafe_access::storage(rhs);
  91. offset_ = unsafe_access::offset(rhs);
  92. return *this;
  93. }
  94. template <class A, class = detail::requires_axes<A>>
  95. histogram(A&& a, Storage s)
  96. : axes_(std::forward<A>(a))
  97. , storage_(std::move(s))
  98. , offset_(detail::offset(axes_)) {
  99. detail::throw_if_axes_is_too_large(axes_);
  100. storage_.reset(detail::bincount(axes_));
  101. }
  102. explicit histogram(Axes axes) : histogram(axes, storage_type()) {}
  103. template <class... As, class = detail::requires_axes<std::tuple<std::decay_t<As>...>>>
  104. explicit histogram(As&&... as)
  105. : histogram(std::tuple<std::decay_t<As>...>(std::forward<As>(as)...),
  106. storage_type()) {}
  107. /// Number of axes (dimensions).
  108. constexpr unsigned rank() const noexcept { return detail::axes_rank(axes_); }
  109. /// Total number of bins (including underflow/overflow).
  110. std::size_t size() const noexcept { return storage_.size(); }
  111. /// Reset all bins to default initialized values.
  112. void reset() { storage_.reset(size()); }
  113. /// Get N-th axis using a compile-time number.
  114. /// This version is more efficient than the one accepting a run-time number.
  115. template <unsigned N = 0>
  116. decltype(auto) axis(std::integral_constant<unsigned, N> = {}) const {
  117. detail::axis_index_is_valid(axes_, N);
  118. return detail::axis_get<N>(axes_);
  119. }
  120. /// Get N-th axis with run-time number.
  121. /// Prefer the version that accepts a compile-time number, if you can use it.
  122. decltype(auto) axis(unsigned i) const {
  123. detail::axis_index_is_valid(axes_, i);
  124. return detail::axis_get(axes_, i);
  125. }
  126. /// Apply unary functor/function to each axis.
  127. template <class Unary>
  128. auto for_each_axis(Unary&& unary) const {
  129. return detail::for_each_axis(axes_, std::forward<Unary>(unary));
  130. }
  131. /** Fill histogram with values, an optional weight, and/or a sample.
  132. Arguments are passed in order to the axis objects. Passing an argument type that is
  133. not convertible to the value type accepted by the axis or passing the wrong number
  134. of arguments causes a throw of `std::invalid_argument`.
  135. __Optional weight__
  136. An optional weight can be passed as the first or last argument
  137. with the [weight](boost/histogram/weight.html) helper function. Compilation fails if
  138. the storage elements do not support weights.
  139. __Samples__
  140. If the storage elements accept samples, pass them with the sample helper function
  141. in addition to the axis arguments, which can be the first or last argument. The
  142. [sample](boost/histogram/sample.html) helper function can pass one or more arguments
  143. to the storage element. If samples and weights are used together, they can be passed
  144. in any order at the beginning or end of the argument list.
  145. __Axis with multiple arguments__
  146. If the histogram contains an axis which accepts a `std::tuple` of arguments, the
  147. arguments for that axis need to passed as a `std::tuple`, for example,
  148. `std::make_tuple(1.2, 2.3)`. If the histogram contains only this axis and no other,
  149. the arguments can be passed directly.
  150. */
  151. template <class Arg0, class... Args>
  152. std::enable_if_t<(detail::is_tuple<Arg0>::value == false || sizeof...(Args) > 0),
  153. iterator>
  154. operator()(const Arg0& arg0, const Args&... args) {
  155. return operator()(std::forward_as_tuple(arg0, args...));
  156. }
  157. /// Fill histogram with values, an optional weight, and/or a sample from a `std::tuple`.
  158. template <class... Ts>
  159. iterator operator()(const std::tuple<Ts...>& args) {
  160. using arg_traits = detail::argument_traits<std::decay_t<Ts>...>;
  161. using acc_traits = detail::accumulator_traits<value_type>;
  162. constexpr bool weight_valid =
  163. arg_traits::wpos::value == -1 || acc_traits::wsupport::value;
  164. static_assert(weight_valid, "error: accumulator does not support weights");
  165. detail::sample_args_passed_vs_expected<typename arg_traits::sargs,
  166. typename acc_traits::args>();
  167. constexpr bool sample_valid =
  168. std::is_convertible<typename arg_traits::sargs, typename acc_traits::args>::value;
  169. std::lock_guard<typename mutex_base::type> guard{mutex_base::get()};
  170. return detail::fill(mp11::mp_bool<(weight_valid && sample_valid)>{}, arg_traits{},
  171. offset_, storage_, axes_, args);
  172. }
  173. /** Fill histogram with several values at once.
  174. The argument must be an iterable with a size that matches the
  175. rank of the histogram. The element of an iterable may be 1) a value or 2) an iterable
  176. with contiguous storage over values or 3) a variant of 1) and 2). Sub-iterables must
  177. have the same length.
  178. Values are passed to the corresponding histogram axis in order. If a single value is
  179. passed together with an iterable of values, the single value is treated like an
  180. iterable with matching length of copies of this value.
  181. If the histogram has only one axis, an iterable of values may be passed directly.
  182. @param args iterable as explained in the long description.
  183. */
  184. template <class Iterable, class = detail::requires_iterable<Iterable>>
  185. void fill(const Iterable& args) {
  186. using acc_traits = detail::accumulator_traits<value_type>;
  187. constexpr unsigned n_sample_args_expected =
  188. std::tuple_size<typename acc_traits::args>::value;
  189. static_assert(n_sample_args_expected == 0,
  190. "sample argument is missing but required by accumulator");
  191. std::lock_guard<typename mutex_base::type> guard{mutex_base::get()};
  192. detail::fill_n(mp11::mp_bool<(n_sample_args_expected == 0)>{}, offset_, storage_,
  193. axes_, detail::make_span(args));
  194. }
  195. /** Fill histogram with several values and weights at once.
  196. @param args iterable of values.
  197. @param weights single weight or an iterable of weights.
  198. */
  199. template <class Iterable, class T, class = detail::requires_iterable<Iterable>>
  200. void fill(const Iterable& args, const weight_type<T>& weights) {
  201. using acc_traits = detail::accumulator_traits<value_type>;
  202. constexpr bool weight_valid = acc_traits::wsupport::value;
  203. static_assert(weight_valid, "error: accumulator does not support weights");
  204. detail::sample_args_passed_vs_expected<std::tuple<>, typename acc_traits::args>();
  205. constexpr bool sample_valid =
  206. std::is_convertible<std::tuple<>, typename acc_traits::args>::value;
  207. std::lock_guard<typename mutex_base::type> guard{mutex_base::get()};
  208. detail::fill_n(mp11::mp_bool<(weight_valid && sample_valid)>{}, offset_, storage_,
  209. axes_, detail::make_span(args),
  210. weight(detail::to_ptr_size(weights.value)));
  211. }
  212. /** Fill histogram with several values and weights at once.
  213. @param weights single weight or an iterable of weights.
  214. @param args iterable of values.
  215. */
  216. template <class Iterable, class T, class = detail::requires_iterable<Iterable>>
  217. void fill(const weight_type<T>& weights, const Iterable& args) {
  218. fill(args, weights);
  219. }
  220. /** Fill histogram with several values and samples at once.
  221. @param args iterable of values.
  222. @param samples single sample or an iterable of samples.
  223. */
  224. template <class Iterable, class... Ts, class = detail::requires_iterable<Iterable>>
  225. void fill(const Iterable& args, const sample_type<std::tuple<Ts...>>& samples) {
  226. using acc_traits = detail::accumulator_traits<value_type>;
  227. using sample_args_passed =
  228. std::tuple<decltype(*detail::to_ptr_size(std::declval<Ts>()).first)...>;
  229. detail::sample_args_passed_vs_expected<sample_args_passed,
  230. typename acc_traits::args>();
  231. std::lock_guard<typename mutex_base::type> guard{mutex_base::get()};
  232. mp11::tuple_apply(
  233. [&](const auto&... sargs) {
  234. constexpr bool sample_valid =
  235. std::is_convertible<sample_args_passed, typename acc_traits::args>::value;
  236. detail::fill_n(mp11::mp_bool<(sample_valid)>{}, offset_, storage_, axes_,
  237. detail::make_span(args), detail::to_ptr_size(sargs)...);
  238. },
  239. samples.value);
  240. }
  241. /** Fill histogram with several values and samples at once.
  242. @param samples single sample or an iterable of samples.
  243. @param args iterable of values.
  244. */
  245. template <class Iterable, class T, class = detail::requires_iterable<Iterable>>
  246. void fill(const sample_type<T>& samples, const Iterable& args) {
  247. fill(args, samples);
  248. }
  249. template <class Iterable, class T, class... Ts,
  250. class = detail::requires_iterable<Iterable>>
  251. void fill(const Iterable& args, const weight_type<T>& weights,
  252. const sample_type<std::tuple<Ts...>>& samples) {
  253. using acc_traits = detail::accumulator_traits<value_type>;
  254. using sample_args_passed =
  255. std::tuple<decltype(*detail::to_ptr_size(std::declval<Ts>()).first)...>;
  256. detail::sample_args_passed_vs_expected<sample_args_passed,
  257. typename acc_traits::args>();
  258. std::lock_guard<typename mutex_base::type> guard{mutex_base::get()};
  259. mp11::tuple_apply(
  260. [&](const auto&... sargs) {
  261. constexpr bool weight_valid = acc_traits::wsupport::value;
  262. static_assert(weight_valid, "error: accumulator does not support weights");
  263. constexpr bool sample_valid =
  264. std::is_convertible<sample_args_passed, typename acc_traits::args>::value;
  265. detail::fill_n(mp11::mp_bool<(weight_valid && sample_valid)>{}, offset_,
  266. storage_, axes_, detail::make_span(args),
  267. weight(detail::to_ptr_size(weights.value)),
  268. detail::to_ptr_size(sargs)...);
  269. },
  270. samples.value);
  271. }
  272. template <class Iterable, class T, class U, class = detail::requires_iterable<Iterable>>
  273. void fill(const sample_type<T>& samples, const weight_type<U>& weights,
  274. const Iterable& args) {
  275. fill(args, weights, samples);
  276. }
  277. template <class Iterable, class T, class U, class = detail::requires_iterable<Iterable>>
  278. void fill(const weight_type<T>& weights, const sample_type<U>& samples,
  279. const Iterable& args) {
  280. fill(args, weights, samples);
  281. }
  282. template <class Iterable, class T, class U, class = detail::requires_iterable<Iterable>>
  283. void fill(const Iterable& args, const sample_type<T>& samples,
  284. const weight_type<U>& weights) {
  285. fill(args, weights, samples);
  286. }
  287. /** Access cell value at integral indices.
  288. You can pass indices as individual arguments, as a std::tuple of integers, or as an
  289. interable range of integers. Passing the wrong number of arguments causes a throw of
  290. std::invalid_argument. Passing an index which is out of bounds causes a throw of
  291. std::out_of_range.
  292. @param i index of first axis.
  293. @param is indices of second, third, ... axes.
  294. @returns reference to cell value.
  295. */
  296. template <class... Indices>
  297. decltype(auto) at(axis::index_type i, Indices... is) {
  298. return at(std::forward_as_tuple(i, is...));
  299. }
  300. /// Access cell value at integral indices (read-only).
  301. template <class... Indices>
  302. decltype(auto) at(axis::index_type i, Indices... is) const {
  303. return at(std::forward_as_tuple(i, is...));
  304. }
  305. /// Access cell value at integral indices stored in `std::tuple`.
  306. template <class... Indices>
  307. decltype(auto) at(const std::tuple<Indices...>& is) {
  308. if (rank() != sizeof...(Indices))
  309. BOOST_THROW_EXCEPTION(
  310. std::invalid_argument("number of arguments != histogram rank"));
  311. const auto idx = detail::at(axes_, is);
  312. if (!is_valid(idx))
  313. BOOST_THROW_EXCEPTION(std::out_of_range("at least one index out of bounds"));
  314. BOOST_ASSERT(idx < storage_.size());
  315. return storage_[idx];
  316. }
  317. /// Access cell value at integral indices stored in `std::tuple` (read-only).
  318. template <typename... Indices>
  319. decltype(auto) at(const std::tuple<Indices...>& is) const {
  320. if (rank() != sizeof...(Indices))
  321. BOOST_THROW_EXCEPTION(
  322. std::invalid_argument("number of arguments != histogram rank"));
  323. const auto idx = detail::at(axes_, is);
  324. if (!is_valid(idx))
  325. BOOST_THROW_EXCEPTION(std::out_of_range("at least one index out of bounds"));
  326. BOOST_ASSERT(idx < storage_.size());
  327. return storage_[idx];
  328. }
  329. /// Access cell value at integral indices stored in iterable.
  330. template <class Iterable, class = detail::requires_iterable<Iterable>>
  331. decltype(auto) at(const Iterable& is) {
  332. if (rank() != detail::axes_rank(is))
  333. BOOST_THROW_EXCEPTION(
  334. std::invalid_argument("number of arguments != histogram rank"));
  335. const auto idx = detail::at(axes_, is);
  336. if (!is_valid(idx))
  337. BOOST_THROW_EXCEPTION(std::out_of_range("at least one index out of bounds"));
  338. BOOST_ASSERT(idx < storage_.size());
  339. return storage_[idx];
  340. }
  341. /// Access cell value at integral indices stored in iterable (read-only).
  342. template <class Iterable, class = detail::requires_iterable<Iterable>>
  343. decltype(auto) at(const Iterable& is) const {
  344. if (rank() != detail::axes_rank(is))
  345. BOOST_THROW_EXCEPTION(
  346. std::invalid_argument("number of arguments != histogram rank"));
  347. const auto idx = detail::at(axes_, is);
  348. if (!is_valid(idx))
  349. BOOST_THROW_EXCEPTION(std::out_of_range("at least one index out of bounds"));
  350. BOOST_ASSERT(idx < storage_.size());
  351. return storage_[idx];
  352. }
  353. /// Access value at index (number for rank = 1, else `std::tuple` or iterable).
  354. template <class Indices>
  355. decltype(auto) operator[](const Indices& is) {
  356. return at(is);
  357. }
  358. /// Access value at index (read-only).
  359. template <class Indices>
  360. decltype(auto) operator[](const Indices& is) const {
  361. return at(is);
  362. }
  363. /// Equality operator, tests equality for all axes and the storage.
  364. template <class A, class S>
  365. bool operator==(const histogram<A, S>& rhs) const noexcept {
  366. // testing offset is redundant, but offers fast return if it fails
  367. return offset_ == unsafe_access::offset(rhs) &&
  368. detail::axes_equal(axes_, unsafe_access::axes(rhs)) &&
  369. storage_ == unsafe_access::storage(rhs);
  370. }
  371. /// Negation of the equality operator.
  372. template <class A, class S>
  373. bool operator!=(const histogram<A, S>& rhs) const noexcept {
  374. return !operator==(rhs);
  375. }
  376. /// Add values of another histogram.
  377. template <class A, class S>
  378. std::enable_if_t<
  379. detail::has_operator_radd<value_type, typename histogram<A, S>::value_type>::value,
  380. histogram&>
  381. operator+=(const histogram<A, S>& rhs) {
  382. if (!detail::axes_equal(axes_, unsafe_access::axes(rhs)))
  383. BOOST_THROW_EXCEPTION(std::invalid_argument("axes of histograms differ"));
  384. auto rit = unsafe_access::storage(rhs).begin();
  385. for (auto&& x : storage_) x += *rit++;
  386. return *this;
  387. }
  388. /// Subtract values of another histogram.
  389. template <class A, class S>
  390. std::enable_if_t<
  391. detail::has_operator_rsub<value_type, typename histogram<A, S>::value_type>::value,
  392. histogram&>
  393. operator-=(const histogram<A, S>& rhs) {
  394. if (!detail::axes_equal(axes_, unsafe_access::axes(rhs)))
  395. BOOST_THROW_EXCEPTION(std::invalid_argument("axes of histograms differ"));
  396. auto rit = unsafe_access::storage(rhs).begin();
  397. for (auto&& x : storage_) x -= *rit++;
  398. return *this;
  399. }
  400. /// Multiply by values of another histogram.
  401. template <class A, class S>
  402. std::enable_if_t<
  403. detail::has_operator_rmul<value_type, typename histogram<A, S>::value_type>::value,
  404. histogram&>
  405. operator*=(const histogram<A, S>& rhs) {
  406. if (!detail::axes_equal(axes_, unsafe_access::axes(rhs)))
  407. BOOST_THROW_EXCEPTION(std::invalid_argument("axes of histograms differ"));
  408. auto rit = unsafe_access::storage(rhs).begin();
  409. for (auto&& x : storage_) x *= *rit++;
  410. return *this;
  411. }
  412. /// Divide by values of another histogram.
  413. template <class A, class S>
  414. std::enable_if_t<
  415. detail::has_operator_rdiv<value_type, typename histogram<A, S>::value_type>::value,
  416. histogram&>
  417. operator/=(const histogram<A, S>& rhs) {
  418. if (!detail::axes_equal(axes_, unsafe_access::axes(rhs)))
  419. BOOST_THROW_EXCEPTION(std::invalid_argument("axes of histograms differ"));
  420. auto rit = unsafe_access::storage(rhs).begin();
  421. for (auto&& x : storage_) x /= *rit++;
  422. return *this;
  423. }
  424. /// Multiply all values with a scalar.
  425. template <class V = value_type>
  426. std::enable_if_t<(detail::has_operator_rmul<V, double>::value &&
  427. detail::has_operator_rmul<storage_type, double>::value == true),
  428. histogram&>
  429. operator*=(const double x) {
  430. // use special implementation of scaling if available
  431. storage_ *= x;
  432. return *this;
  433. }
  434. /// Multiply all values with a scalar.
  435. template <class V = value_type>
  436. std::enable_if_t<(detail::has_operator_rmul<V, double>::value &&
  437. detail::has_operator_rmul<storage_type, double>::value == false),
  438. histogram&>
  439. operator*=(const double x) {
  440. // generic implementation of scaling
  441. for (auto&& si : storage_) si *= x;
  442. return *this;
  443. }
  444. /// Divide all values by a scalar.
  445. template <class V = value_type>
  446. std::enable_if_t<detail::has_operator_rmul<V, double>::value, histogram&> operator/=(
  447. const double x) {
  448. return operator*=(1.0 / x);
  449. }
  450. /// Return value iterator to the beginning of the histogram.
  451. iterator begin() noexcept { return storage_.begin(); }
  452. /// Return value iterator to the end in the histogram.
  453. iterator end() noexcept { return storage_.end(); }
  454. /// Return value iterator to the beginning of the histogram (read-only).
  455. const_iterator begin() const noexcept { return storage_.begin(); }
  456. /// Return value iterator to the end in the histogram (read-only).
  457. const_iterator end() const noexcept { return storage_.end(); }
  458. /// Return value iterator to the beginning of the histogram (read-only).
  459. const_iterator cbegin() const noexcept { return begin(); }
  460. /// Return value iterator to the end in the histogram (read-only).
  461. const_iterator cend() const noexcept { return end(); }
  462. template <class Archive>
  463. void serialize(Archive& ar, unsigned /* version */) {
  464. detail::axes_serialize(ar, axes_);
  465. ar& make_nvp("storage", storage_);
  466. if (Archive::is_loading::value) {
  467. offset_ = detail::offset(axes_);
  468. detail::throw_if_axes_is_too_large(axes_);
  469. }
  470. }
  471. private:
  472. axes_type axes_;
  473. storage_type storage_;
  474. std::size_t offset_ = 0;
  475. friend struct unsafe_access;
  476. };
  477. /**
  478. Pairwise add cells of two histograms and return histogram with the sum.
  479. The returned histogram type is the most efficient and safest one constructible from the
  480. inputs, if they are not the same type. If one histogram has a tuple axis, the result has
  481. a tuple axis. The chosen storage is the one with the larger dynamic range.
  482. */
  483. template <class A1, class S1, class A2, class S2>
  484. auto operator+(const histogram<A1, S1>& a, const histogram<A2, S2>& b) {
  485. auto r = histogram<detail::common_axes<A1, A2>, detail::common_storage<S1, S2>>(a);
  486. return r += b;
  487. }
  488. /** Pairwise multiply cells of two histograms and return histogram with the product.
  489. For notes on the returned histogram type, see operator+.
  490. */
  491. template <class A1, class S1, class A2, class S2>
  492. auto operator*(const histogram<A1, S1>& a, const histogram<A2, S2>& b) {
  493. auto r = histogram<detail::common_axes<A1, A2>, detail::common_storage<S1, S2>>(a);
  494. return r *= b;
  495. }
  496. /** Pairwise subtract cells of two histograms and return histogram with the difference.
  497. For notes on the returned histogram type, see operator+.
  498. */
  499. template <class A1, class S1, class A2, class S2>
  500. auto operator-(const histogram<A1, S1>& a, const histogram<A2, S2>& b) {
  501. auto r = histogram<detail::common_axes<A1, A2>, detail::common_storage<S1, S2>>(a);
  502. return r -= b;
  503. }
  504. /** Pairwise divide cells of two histograms and return histogram with the quotient.
  505. For notes on the returned histogram type, see operator+.
  506. */
  507. template <class A1, class S1, class A2, class S2>
  508. auto operator/(const histogram<A1, S1>& a, const histogram<A2, S2>& b) {
  509. auto r = histogram<detail::common_axes<A1, A2>, detail::common_storage<S1, S2>>(a);
  510. return r /= b;
  511. }
  512. /** Multiply all cells of the histogram by a number and return a new histogram.
  513. If the original histogram has integer cells, the result has double cells.
  514. */
  515. template <class A, class S>
  516. auto operator*(const histogram<A, S>& h, double x) {
  517. auto r = histogram<A, detail::common_storage<S, dense_storage<double>>>(h);
  518. return r *= x;
  519. }
  520. /** Multiply all cells of the histogram by a number and return a new histogram.
  521. If the original histogram has integer cells, the result has double cells.
  522. */
  523. template <class A, class S>
  524. auto operator*(double x, const histogram<A, S>& h) {
  525. return h * x;
  526. }
  527. /** Divide all cells of the histogram by a number and return a new histogram.
  528. If the original histogram has integer cells, the result has double cells.
  529. */
  530. template <class A, class S>
  531. auto operator/(const histogram<A, S>& h, double x) {
  532. return h * (1.0 / x);
  533. }
  534. #if __cpp_deduction_guides >= 201606
  535. template <class... Axes, class = detail::requires_axes<std::tuple<std::decay_t<Axes>...>>>
  536. histogram(Axes...)->histogram<std::tuple<std::decay_t<Axes>...>>;
  537. template <class... Axes, class S, class = detail::requires_storage_or_adaptible<S>>
  538. histogram(std::tuple<Axes...>, S)
  539. ->histogram<std::tuple<Axes...>, std::conditional_t<detail::is_adaptible<S>::value,
  540. storage_adaptor<S>, S>>;
  541. template <class Iterable, class = detail::requires_iterable<Iterable>,
  542. class = detail::requires_any_axis<typename Iterable::value_type>>
  543. histogram(Iterable)->histogram<std::vector<typename Iterable::value_type>>;
  544. template <class Iterable, class S, class = detail::requires_iterable<Iterable>,
  545. class = detail::requires_any_axis<typename Iterable::value_type>,
  546. class = detail::requires_storage_or_adaptible<S>>
  547. histogram(Iterable, S)
  548. ->histogram<
  549. std::vector<typename Iterable::value_type>,
  550. std::conditional_t<detail::is_adaptible<S>::value, storage_adaptor<S>, S>>;
  551. #endif
  552. } // namespace histogram
  553. } // namespace boost
  554. #endif