liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2020 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_MULTI_INDEX_HPP
#define BOOST_HISTOGRAM_MULTI_INDEX_HPP
 
#include <algorithm>
#include <boost/histogram/detail/detect.hpp>
#include <boost/histogram/detail/nonmember_container_access.hpp>
#include <boost/histogram/fwd.hpp>
#include <boost/mp11/integer_sequence.hpp>
#include <boost/throw_exception.hpp>
#include <initializer_list>
#include <iterator>
#include <stdexcept>
#include <tuple>
 
namespace boost {
namespace histogram {
 
/** Holder for multiple axis indices.
 
  Adapts external iterable, tuple, or explicit list of indices to the same representation.
 */
template <std::size_t Size>
struct multi_index {
  using value_type = axis::index_type;
  using iterator = value_type*;
  using const_iterator = const value_type*;
 
  static multi_index create(std::size_t s) {
    if (s != size())
      BOOST_THROW_EXCEPTION(std::invalid_argument("size does not match static size"));
    return multi_index(priv_tag{});
  }
 
  template <class... Is>
  multi_index(axis::index_type i, Is... is)
      : multi_index(std::initializer_list<axis::index_type>{
            i, static_cast<axis::index_type>(is)...}) {}
 
  template <class... Is>
  multi_index(const std::tuple<axis::index_type, Is...>& is)
      : multi_index(is, mp11::make_index_sequence<(1 + sizeof...(Is))>{}) {}
 
  template <class Iterable, class = detail::requires_iterable<Iterable>>
  multi_index(const Iterable& is) {
    if (detail::size(is) != size())
      BOOST_THROW_EXCEPTION(std::invalid_argument("no. of axes != no. of indices"));
    using std::begin;
    using std::end;
    std::copy(begin(is), end(is), data_);
  }
 
  iterator begin() noexcept { return data_; }
  iterator end() noexcept { return data_ + size(); }
  const_iterator begin() const noexcept { return data_; }
  const_iterator end() const noexcept { return data_ + size(); }
  static constexpr std::size_t size() noexcept { return Size; }
 
private:
  struct priv_tag {};
 
  multi_index(priv_tag) {}
 
  template <class T, std::size_t... Is>
  multi_index(const T& is, mp11::index_sequence<Is...>)
      : multi_index(static_cast<axis::index_type>(std::get<Is>(is))...) {}
 
  axis::index_type data_[size()];
};
 
template <>
struct multi_index<static_cast<std::size_t>(-1)> {
  using value_type = axis::index_type;
  using iterator = value_type*;
  using const_iterator = const value_type*;
 
  static multi_index create(std::size_t s) { return multi_index(priv_tag{}, s); }
 
  template <class... Is>
  multi_index(axis::index_type i, Is... is)
      : multi_index(std::initializer_list<axis::index_type>{
            i, static_cast<axis::index_type>(is)...}) {}
 
  template <class... Is>
  multi_index(const std::tuple<axis::index_type, Is...>& is)
      : multi_index(is, mp11::make_index_sequence<(1 + sizeof...(Is))>{}) {}
 
  template <class Iterable, class = detail::requires_iterable<Iterable>>
  multi_index(const Iterable& is) : size_(detail::size(is)) {
    using std::begin;
    using std::end;
    std::copy(begin(is), end(is), data_);
  }
 
  iterator begin() noexcept { return data_; }
  iterator end() noexcept { return data_ + size_; }
  const_iterator begin() const noexcept { return data_; }
  const_iterator end() const noexcept { return data_ + size_; }
  std::size_t size() const noexcept { return size_; }
 
private:
  struct priv_tag {};
 
  multi_index(priv_tag, std::size_t s) : size_(s) {}
 
  template <class T, std::size_t... Ns>
  multi_index(const T& is, mp11::index_sequence<Ns...>)
      : multi_index(static_cast<axis::index_type>(std::get<Ns>(is))...) {}
 
  std::size_t size_ = 0;
  static constexpr std::size_t max_size_ = BOOST_HISTOGRAM_DETAIL_AXES_LIMIT;
  axis::index_type data_[max_size_];
};
 
} // namespace histogram
} // namespace boost
 
#endif