Skip to content

Commit 4cceb2a

Browse files
committed
cpp17: set axis compile-time options via deduction guide (#340)
1 parent 26e708e commit 4cceb2a

File tree

7 files changed

+181
-84
lines changed

7 files changed

+181
-84
lines changed

include/boost/histogram/axis/boolean.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class boolean : public iterator_mixin<boolean<MetaData>>,
4040
public:
4141
/** Construct a boolean axis.
4242
*
43-
* \param meta description of the axis.
43+
* @param meta description of the axis.
4444
*/
4545
explicit boolean(metadata_type meta = {}) : metadata_base(std::move(meta)) {}
4646

include/boost/histogram/axis/category.hpp

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,47 +67,69 @@ class category : public iterator_mixin<category<Value, MetaData, Options, Alloca
6767

6868
/** Construct from iterator range of unique values.
6969
*
70-
* \param begin begin of category range of unique values.
71-
* \param end end of category range of unique values.
72-
* \param meta description of the axis.
73-
* \param alloc allocator instance to use.
70+
* @param begin begin of category range of unique values.
71+
* @param end end of category range of unique values.
72+
* @param meta description of the axis (optional).
73+
* @param options see boost::histogram::axis::option (optional).
74+
* @param alloc allocator instance to use (optional).
7475
*/
7576
template <class It, class = detail::requires_iterator<It>>
76-
category(It begin, It end, metadata_type meta = {}, allocator_type alloc = {})
77+
category(It begin, It end, metadata_type meta = {}, options_type options = {},
78+
allocator_type alloc = {})
7779
: metadata_base(std::move(meta)), vec_(alloc) {
80+
(void)options;
7881
if (std::distance(begin, end) < 0)
7982
BOOST_THROW_EXCEPTION(
8083
std::invalid_argument("end must be reachable by incrementing begin"));
8184
vec_.reserve(std::distance(begin, end));
8285
while (begin != end) vec_.emplace_back(*begin++);
8386
}
8487

88+
// kept for backward compatibility
89+
template <class It, class = detail::requires_iterator<It>>
90+
category(It begin, It end, metadata_type meta, allocator_type alloc)
91+
: category(begin, end, std::move(meta), {}, std::move(alloc)) {}
92+
8593
/** Construct axis from iterable sequence of unique values.
8694
*
87-
* \param iterable sequence of unique values.
88-
* \param meta description of the axis.
89-
* \param alloc allocator instance to use.
95+
* @param iterable sequence of unique values.
96+
* @param meta description of the axis.
97+
* @param options see boost::histogram::axis::option (optional).
98+
* @param alloc allocator instance to use.
9099
*/
91100
template <class C, class = detail::requires_iterable<C>>
92-
category(const C& iterable, metadata_type meta = {}, allocator_type alloc = {})
93-
: category(std::begin(iterable), std::end(iterable), std::move(meta),
101+
category(const C& iterable, metadata_type meta = {}, options_type options = {},
102+
allocator_type alloc = {})
103+
: category(std::begin(iterable), std::end(iterable), std::move(meta), options,
104+
std::move(alloc)) {}
105+
106+
// kept for backward compatibility
107+
template <class C, class = detail::requires_iterable<C>>
108+
category(const C& iterable, metadata_type meta, allocator_type alloc)
109+
: category(std::begin(iterable), std::end(iterable), std::move(meta), {},
94110
std::move(alloc)) {}
95111

96112
/** Construct axis from an initializer list of unique values.
97113
*
98-
* \param list `std::initializer_list` of unique values.
99-
* \param meta description of the axis.
100-
* \param alloc allocator instance to use.
114+
* @param list `std::initializer_list` of unique values.
115+
* @param meta description of the axis.
116+
* @param options see boost::histogram::axis::option (optional).
117+
* @param alloc allocator instance to use.
101118
*/
102119
template <class U>
103120
category(std::initializer_list<U> list, metadata_type meta = {},
104-
allocator_type alloc = {})
105-
: category(list.begin(), list.end(), std::move(meta), std::move(alloc)) {}
121+
options_type options = {}, allocator_type alloc = {})
122+
: category(list.begin(), list.end(), std::move(meta), options, std::move(alloc)) {}
123+
124+
// kept for backward compatibility
125+
template <class U>
126+
category(std::initializer_list<U> list, metadata_type meta, allocator_type alloc)
127+
: category(list.begin(), list.end(), std::move(meta), {}, std::move(alloc)) {}
106128

107129
/// Constructor used by algorithm::reduce to shrink and rebin (not for users).
108130
category(const category& src, index_type begin, index_type end, unsigned merge)
109131
// LCOV_EXCL_START: gcc-8 is missing the delegated ctor for no reason
110-
: category(src.vec_.begin() + begin, src.vec_.begin() + end, src.metadata(),
132+
: category(src.vec_.begin() + begin, src.vec_.begin() + end, src.metadata(), {},
111133
src.get_allocator())
112134
// LCOV_EXCL_STOP
113135
{
@@ -189,12 +211,17 @@ class category : public iterator_mixin<category<Value, MetaData, Options, Alloca
189211

190212
template <class T>
191213
category(std::initializer_list<T>)
192-
->category<detail::replace_cstring<std::decay_t<T>>, null_type>;
214+
-> category<detail::replace_cstring<std::decay_t<T>>, null_type>;
193215

194216
template <class T, class M>
195217
category(std::initializer_list<T>, M)
196-
->category<detail::replace_cstring<std::decay_t<T>>,
197-
detail::replace_cstring<std::decay_t<M>>>;
218+
-> category<detail::replace_cstring<std::decay_t<T>>,
219+
detail::replace_cstring<std::decay_t<M>>>;
220+
221+
template <class T, class M, unsigned B>
222+
category(std::initializer_list<T>, M, const option::bitset<B>&)
223+
-> category<detail::replace_cstring<std::decay_t<T>>,
224+
detail::replace_cstring<std::decay_t<M>>, option::bitset<B>>;
198225

199226
#endif
200227

include/boost/histogram/axis/integer.hpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ namespace axis {
3434
3535
Binning is a O(1) operation. This axis bins faster than a regular axis.
3636
37-
@tparam Value input value type. Must be integer or floating point.
38-
@tparam MetaData type to store meta data.
39-
@tparam Options see boost::histogram::axis::option (all values allowed).
37+
@tparam Value input value type. Must be integer or floating point.
38+
@tparam MetaData type to store meta data.
39+
@tparam Options see boost::histogram::axis::option.
4040
*/
4141
template <class Value, class MetaData, class Options>
4242
class integer : public iterator_mixin<integer<Value, MetaData, Options>>,
@@ -73,14 +73,17 @@ class integer : public iterator_mixin<integer<Value, MetaData, Options>>,
7373

7474
/** Construct over semi-open integer interval [start, stop).
7575
*
76-
* \param start first integer of covered range.
77-
* \param stop one past last integer of covered range.
78-
* \param meta description of the axis.
76+
* @param start first integer of covered range.
77+
* @param stop one past last integer of covered range.
78+
* @param meta description of the axis (optional).
79+
* @param options see boost::histogram::axis::option (optional).
7980
*/
80-
integer(value_type start, value_type stop, metadata_type meta = {})
81+
integer(value_type start, value_type stop, metadata_type meta = {},
82+
options_type options = {})
8183
: metadata_base(std::move(meta))
8284
, size_(static_cast<index_type>(stop - start))
8385
, min_(start) {
86+
(void)options;
8487
if (!(stop >= start))
8588
BOOST_THROW_EXCEPTION(std::invalid_argument("stop >= start required"));
8689
}
@@ -217,6 +220,12 @@ integer(T, T, M)
217220
-> integer<detail::convert_integer<T, index_type>,
218221
detail::replace_type<std::decay_t<M>, const char*, std::string>>;
219222

223+
template <class T, class M, unsigned B>
224+
integer(T, T, M, const option::bitset<B>&)
225+
-> integer<detail::convert_integer<T, index_type>,
226+
detail::replace_type<std::decay_t<M>, const char*, std::string>,
227+
option::bitset<B>>;
228+
220229
#endif
221230

222231
} // namespace axis

include/boost/histogram/axis/option.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ constexpr auto operator-(bitset<B1>, bitset<B2>) {
5757
@tparam Pos position of the bit in the set.
5858
*/
5959
template <unsigned Pos>
60-
struct bit : bitset<(1 << Pos)> {};
60+
using bit = bitset<(1 << Pos)>;
6161

6262
/// All options off.
6363
using none_t = bitset<0>;

include/boost/histogram/axis/regular.hpp

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ step_type<T> step(T t) {
173173
@tparam Value input value type, must be floating point.
174174
@tparam Transform builtin or user-defined transform type.
175175
@tparam MetaData type to store meta data.
176-
@tparam Options see boost::histogram::axis::option (all values allowed).
176+
@tparam Options see boost::histogram::axis::option.
177177
*/
178178
template <class Value, class Transform, class MetaData, class Options>
179179
class regular : public iterator_mixin<regular<Value, Transform, MetaData, Options>>,
@@ -213,14 +213,16 @@ class regular : public iterator_mixin<regular<Value, Transform, MetaData, Option
213213
* @param start low edge of first bin.
214214
* @param stop high edge of last bin.
215215
* @param meta description of the axis (optional).
216+
* @param options see boost::histogram::axis::option (optional).
216217
*/
217218
regular(transform_type trans, unsigned n, value_type start, value_type stop,
218-
metadata_type meta = {})
219+
metadata_type meta = {}, options_type options = {})
219220
: transform_type(std::move(trans))
220221
, metadata_base(std::move(meta))
221222
, size_(static_cast<index_type>(n))
222223
, min_(this->forward(detail::get_scale(start)))
223224
, delta_(this->forward(detail::get_scale(stop)) - min_) {
225+
(void)options;
224226
if (size() == 0) BOOST_THROW_EXCEPTION(std::invalid_argument("bins > 0 required"));
225227
if (!std::isfinite(min_) || !std::isfinite(delta_))
226228
BOOST_THROW_EXCEPTION(
@@ -235,46 +237,51 @@ class regular : public iterator_mixin<regular<Value, Transform, MetaData, Option
235237
* @param start low edge of first bin.
236238
* @param stop high edge of last bin.
237239
* @param meta description of the axis (optional).
240+
* @param options see boost::histogram::axis::option (optional).
238241
*/
239-
regular(unsigned n, value_type start, value_type stop, metadata_type meta = {})
240-
: regular({}, n, start, stop, std::move(meta)) {}
242+
regular(unsigned n, value_type start, value_type stop, metadata_type meta = {},
243+
options_type options = {})
244+
: regular({}, n, start, stop, std::move(meta), options) {}
241245

242246
/** Construct bins with the given step size over real transformed range
243247
* [start, stop).
244248
*
245-
* @param trans transform instance to use.
246-
* @param step width of a single bin.
247-
* @param start low edge of first bin.
248-
* @param stop upper limit of high edge of last bin (see below).
249-
* @param meta description of the axis (optional).
249+
* @param trans transform instance to use.
250+
* @param step width of a single bin.
251+
* @param start low edge of first bin.
252+
* @param stop upper limit of high edge of last bin (see below).
253+
* @param meta description of the axis (optional).
254+
* @param options see boost::histogram::axis::option (optional).
250255
*
251256
* The axis computes the number of bins as n = abs(stop - start) / step,
252257
* rounded down. This means that stop is an upper limit to the actual value
253258
* (start + n * step).
254259
*/
255260
template <class T>
256261
regular(transform_type trans, step_type<T> step, value_type start, value_type stop,
257-
metadata_type meta = {})
262+
metadata_type meta = {}, options_type options = {})
258263
: regular(trans, static_cast<index_type>(std::abs(stop - start) / step.value),
259264
start,
260265
start + static_cast<index_type>(std::abs(stop - start) / step.value) *
261266
step.value,
262-
std::move(meta)) {}
267+
std::move(meta), options) {}
263268

264269
/** Construct bins with the given step size over real range [start, stop).
265270
*
266-
* @param step width of a single bin.
267-
* @param start low edge of first bin.
268-
* @param stop upper limit of high edge of last bin (see below).
269-
* @param meta description of the axis (optional).
271+
* @param step width of a single bin.
272+
* @param start low edge of first bin.
273+
* @param stop upper limit of high edge of last bin (see below).
274+
* @param meta description of the axis (optional).
275+
* @param options see boost::histogram::axis::option (optional).
270276
*
271277
* The axis computes the number of bins as n = abs(stop - start) / step,
272278
* rounded down. This means that stop is an upper limit to the actual value
273279
* (start + n * step).
274280
*/
275281
template <class T>
276-
regular(step_type<T> step, value_type start, value_type stop, metadata_type meta = {})
277-
: regular({}, step, start, stop, std::move(meta)) {}
282+
regular(step_type<T> step, value_type start, value_type stop, metadata_type meta = {},
283+
options_type options = {})
284+
: regular({}, step, start, stop, std::move(meta), options) {}
278285

279286
/// Constructor used by algorithm::reduce to shrink and rebin (not for users).
280287
regular(const regular& src, index_type begin, index_type end, unsigned merge)
@@ -397,20 +404,28 @@ class regular : public iterator_mixin<regular<Value, Transform, MetaData, Option
397404

398405
template <class T>
399406
regular(unsigned, T, T)
400-
->regular<detail::convert_integer<T, double>, transform::id, null_type>;
407+
-> regular<detail::convert_integer<T, double>, transform::id, null_type>;
401408

402409
template <class T, class M>
403-
regular(unsigned, T, T, M)
404-
->regular<detail::convert_integer<T, double>, transform::id,
405-
detail::replace_cstring<std::decay_t<M>>>;
410+
regular(unsigned, T, T, M) -> regular<detail::convert_integer<T, double>, transform::id,
411+
detail::replace_cstring<std::decay_t<M>>>;
412+
413+
template <class T, class M, unsigned B>
414+
regular(unsigned, T, T, M, const option::bitset<B>&)
415+
-> regular<detail::convert_integer<T, double>, transform::id,
416+
detail::replace_cstring<std::decay_t<M>>, option::bitset<B>>;
406417

407418
template <class Tr, class T, class = detail::requires_transform<Tr, T>>
408-
regular(Tr, unsigned, T, T)->regular<detail::convert_integer<T, double>, Tr, null_type>;
419+
regular(Tr, unsigned, T, T) -> regular<detail::convert_integer<T, double>, Tr, null_type>;
409420

410421
template <class Tr, class T, class M>
411-
regular(Tr, unsigned, T, T, M)
412-
->regular<detail::convert_integer<T, double>, Tr,
413-
detail::replace_cstring<std::decay_t<M>>>;
422+
regular(Tr, unsigned, T, T, M) -> regular<detail::convert_integer<T, double>, Tr,
423+
detail::replace_cstring<std::decay_t<M>>>;
424+
425+
template <class Tr, class T, class M, unsigned B>
426+
regular(Tr, unsigned, T, T, M, const option::bitset<B>&)
427+
-> regular<detail::convert_integer<T, double>, Tr,
428+
detail::replace_cstring<std::decay_t<M>>, option::bitset<B>>;
414429

415430
#endif
416431

0 commit comments

Comments
 (0)