1 // Copyright 2015-2018 Hans Dembinski
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)
7 #ifndef BOOST_HISTOGRAM_AXIS_VARIABLE_HPP
8 #define BOOST_HISTOGRAM_AXIS_VARIABLE_HPP
11 #include <boost/assert.hpp>
12 #include <boost/core/nvp.hpp>
13 #include <boost/histogram/axis/interval_view.hpp>
14 #include <boost/histogram/axis/iterator.hpp>
15 #include <boost/histogram/axis/metadata_base.hpp>
16 #include <boost/histogram/axis/option.hpp>
17 #include <boost/histogram/detail/convert_integer.hpp>
18 #include <boost/histogram/detail/detect.hpp>
19 #include <boost/histogram/detail/limits.hpp>
20 #include <boost/histogram/detail/replace_type.hpp>
21 #include <boost/histogram/fwd.hpp>
22 #include <boost/throw_exception.hpp>
28 #include <type_traits>
37 Axis for non-equidistant bins on the real line.
39 Binning is a O(log(N)) operation. If speed matters and the problem domain
40 allows it, prefer a regular axis, possibly with a transform.
42 @tparam Value input value type, must be floating point.
43 @tparam MetaData type to store meta data.
44 @tparam Options see boost::histogram::axis::option (all values allowed).
45 @tparam Allocator allocator to use for dynamic memory management.
47 template <class Value, class MetaData, class Options, class Allocator>
48 class variable : public iterator_mixin<variable<Value, MetaData, Options, Allocator>>,
49 public metadata_base<MetaData> {
50 // these must be private, so that they are not automatically inherited
51 using value_type = Value;
52 using metadata_type = typename metadata_base<MetaData>::metadata_type;
54 detail::replace_default<Options, decltype(option::underflow | option::overflow)>;
55 using allocator_type = Allocator;
56 using vector_type = std::vector<Value, allocator_type>;
59 std::is_floating_point<value_type>::value,
60 "current version of variable axis requires floating point type; "
61 "if you need a variable axis with an integral type, please submit an issue");
64 (!options_type::test(option::circular) && !options_type::test(option::growth)) ||
65 (options_type::test(option::circular) ^ options_type::test(option::growth)),
66 "circular and growth options are mutually exclusive");
69 constexpr variable() = default;
70 explicit variable(allocator_type alloc) : vec_(alloc) {}
72 /** Construct from iterator range of bin edges.
74 * \param begin begin of edge sequence.
75 * \param end end of edge sequence.
76 * \param meta description of the axis.
77 * \param alloc allocator instance to use.
79 template <class It, class = detail::requires_iterator<It>>
80 variable(It begin, It end, metadata_type meta = {}, allocator_type alloc = {})
81 : metadata_base<MetaData>(std::move(meta)), vec_(std::move(alloc)) {
82 if (std::distance(begin, end) < 2)
83 BOOST_THROW_EXCEPTION(std::invalid_argument("bins > 0 required"));
85 vec_.reserve(std::distance(begin, end));
86 vec_.emplace_back(*begin++);
87 bool strictly_ascending = true;
88 while (begin != end) {
89 if (*begin <= vec_.back()) strictly_ascending = false;
90 vec_.emplace_back(*begin++);
92 if (!strictly_ascending)
93 BOOST_THROW_EXCEPTION(
94 std::invalid_argument("input sequence must be strictly ascending"));
97 /** Construct variable axis from iterable range of bin edges.
99 * \param iterable iterable range of bin edges.
100 * \param meta description of the axis.
101 * \param alloc allocator instance to use.
103 template <class U, class = detail::requires_iterable<U>>
104 variable(const U& iterable, metadata_type meta = {}, allocator_type alloc = {})
105 : variable(std::begin(iterable), std::end(iterable), std::move(meta),
108 /** Construct variable axis from initializer list of bin edges.
110 * @param list `std::initializer_list` of bin edges.
111 * @param meta description of the axis.
112 * @param alloc allocator instance to use.
115 variable(std::initializer_list<U> list, metadata_type meta = {},
116 allocator_type alloc = {})
117 : variable(list.begin(), list.end(), std::move(meta), std::move(alloc)) {}
119 /// Constructor used by algorithm::reduce to shrink and rebin (not for users).
120 variable(const variable& src, index_type begin, index_type end, unsigned merge)
121 : metadata_base<MetaData>(src), vec_(src.get_allocator()) {
122 BOOST_ASSERT((end - begin) % merge == 0);
123 if (options_type::test(option::circular) && !(begin == 0 && end == src.size()))
124 BOOST_THROW_EXCEPTION(std::invalid_argument("cannot shrink circular axis"));
125 vec_.reserve((end - begin) / merge);
126 const auto beg = src.vec_.begin();
127 for (index_type i = begin; i <= end; i += merge) vec_.emplace_back(*(beg + i));
130 /// Return index for value argument.
131 index_type index(value_type x) const noexcept {
132 if (options_type::test(option::circular)) {
133 const auto a = vec_[0];
134 const auto b = vec_[size()];
135 x -= std::floor((x - a) / (b - a)) * (b - a);
137 return static_cast<index_type>(std::upper_bound(vec_.begin(), vec_.end(), x) -
141 std::pair<index_type, index_type> update(value_type x) noexcept {
142 const auto i = index(x);
143 if (std::isfinite(x)) {
145 if (i < size()) return std::make_pair(i, 0);
146 const auto d = value(size()) - value(size() - 0.5);
147 x = std::nextafter(x, (std::numeric_limits<value_type>::max)());
148 x = (std::max)(x, vec_.back() + d);
152 const auto d = value(0.5) - value(0);
153 x = (std::min)(x, value(0) - d);
154 vec_.insert(vec_.begin(), x);
157 return {x < 0 ? -1 : size(), 0};
160 /// Return value for fractional index argument.
161 value_type value(real_index_type i) const noexcept {
162 if (options_type::test(option::circular)) {
163 auto shift = std::floor(i / size());
166 const auto k = static_cast<index_type>(std::modf(i, &z));
167 const auto a = vec_[0];
168 const auto b = vec_[size()];
169 return (1.0 - z) * vec_[k] + z * vec_[k + 1] + shift * (b - a);
171 if (i < 0) return detail::lowest<value_type>();
172 if (i == size()) return vec_.back();
173 if (i > size()) return detail::highest<value_type>();
174 const auto k = static_cast<index_type>(i); // precond: i >= 0
175 const real_index_type z = i - k;
176 return (1.0 - z) * vec_[k] + z * vec_[k + 1];
179 /// Return bin for index argument.
180 auto bin(index_type idx) const noexcept { return interval_view<variable>(*this, idx); }
182 /// Returns the number of bins, without over- or underflow.
183 index_type size() const noexcept { return static_cast<index_type>(vec_.size()) - 1; }
185 /// Returns the options.
186 static constexpr unsigned options() noexcept { return options_type::value; }
188 template <class V, class M, class O, class A>
189 bool operator==(const variable<V, M, O, A>& o) const noexcept {
190 const auto& a = vec_;
191 const auto& b = o.vec_;
192 return std::equal(a.begin(), a.end(), b.begin(), b.end()) &&
193 metadata_base<MetaData>::operator==(o);
196 template <class V, class M, class O, class A>
197 bool operator!=(const variable<V, M, O, A>& o) const noexcept {
198 return !operator==(o);
201 /// Return allocator instance.
202 auto get_allocator() const { return vec_.get_allocator(); }
204 template <class Archive>
205 void serialize(Archive& ar, unsigned /* version */) {
206 ar& make_nvp("seq", vec_);
207 ar& make_nvp("meta", this->metadata());
213 template <class V, class M, class O, class A>
214 friend class variable;
217 #if __cpp_deduction_guides >= 201606
220 variable(std::initializer_list<T>)
221 ->variable<detail::convert_integer<T, double>, null_type>;
223 template <class T, class M>
224 variable(std::initializer_list<T>, M)
225 ->variable<detail::convert_integer<T, double>,
226 detail::replace_type<std::decay_t<M>, const char*, std::string>>;
228 template <class Iterable, class = detail::requires_iterable<Iterable>>
231 detail::convert_integer<
232 std::decay_t<decltype(*std::begin(std::declval<Iterable&>()))>, double>,
235 template <class Iterable, class M>
236 variable(Iterable, M)
238 detail::convert_integer<
239 std::decay_t<decltype(*std::begin(std::declval<Iterable&>()))>, double>,
240 detail::replace_type<std::decay_t<M>, const char*, std::string>>;
245 } // namespace histogram