put axis in extra namespace, put python axis objects in axis submodule

This commit is contained in:
Hans Dembinski
2017-06-04 11:53:15 +02:00
parent 6ec89079e0
commit ef4898ddc2
15 changed files with 486 additions and 463 deletions

View File

@@ -36,6 +36,7 @@ class access;
namespace boost {
namespace histogram {
namespace axis {
template <typename Value> struct bin {
int idx;
@@ -220,11 +221,11 @@ template <typename Value> struct sqrt {
*/
template <typename RealType = double,
template <class> class Transform = transform::identity>
class regular_axis : public axis_base<true>,
boost::operators<regular_axis<RealType, Transform>> {
class regular : public axis_base<true>,
boost::operators<regular<RealType, Transform>> {
public:
using value_type = RealType;
using const_iterator = axis_iterator<regular_axis>;
using const_iterator = axis_iterator<regular>;
/** Construct axis with n bins over range [min, max).
*
@@ -234,7 +235,7 @@ public:
* \param label description of the axis.
* \param uoflow whether to add under-/overflow bins.
*/
regular_axis(unsigned n, value_type min, value_type max,
regular(unsigned n, value_type min, value_type max,
const std::string &label = std::string(), bool uoflow = true)
: axis_base<true>(n, label, uoflow),
min_(Transform<value_type>::forward(min)),
@@ -244,11 +245,11 @@ public:
}
}
regular_axis() = default;
regular_axis(const regular_axis &) = default;
regular_axis &operator=(const regular_axis &) = default;
regular_axis(regular_axis &&) = default;
regular_axis &operator=(regular_axis &&) = default;
regular() = default;
regular(const regular &) = default;
regular &operator=(const regular &) = default;
regular(regular &&) = default;
regular &operator=(regular &&) = default;
/// Returns the bin index for the passed argument.
inline int index(value_type x) const noexcept {
@@ -272,7 +273,7 @@ public:
z * (min_ + delta_ * bins()));
}
bool operator==(const regular_axis &o) const {
bool operator==(const regular &o) const {
return axis_base<true>::operator==(o) && min_ == o.min_ &&
delta_ == o.delta_;
}
@@ -299,11 +300,11 @@ private:
* bins for this axis. Binning is a O(1) operation.
*/
template <typename RealType = double>
class circular_axis : public axis_base<false>,
boost::operators<regular_axis<RealType>> {
class circular : public axis_base<false>,
boost::operators<regular<RealType>> {
public:
using value_type = RealType;
using const_iterator = axis_iterator<circular_axis>;
using const_iterator = axis_iterator<circular>;
/** Constructor for n bins with an optional offset.
*
@@ -312,16 +313,16 @@ public:
* \param perimeter range after which value wraps around.
* \param label description of the axis.
*/
explicit circular_axis(unsigned n, value_type phase = 0.0,
explicit circular(unsigned n, value_type phase = 0.0,
value_type perimeter = math::double_constants::two_pi,
const std::string &label = std::string())
: axis_base<false>(n, label), phase_(phase), perimeter_(perimeter) {}
circular_axis() = default;
circular_axis(const circular_axis &) = default;
circular_axis &operator=(const circular_axis &) = default;
circular_axis(circular_axis &&) = default;
circular_axis &operator=(circular_axis &&) = default;
circular() = default;
circular(const circular &) = default;
circular &operator=(const circular &) = default;
circular(circular &&) = default;
circular &operator=(circular &&) = default;
/// Returns the bin index for the passed argument.
inline int index(value_type x) const noexcept {
@@ -336,7 +337,7 @@ public:
return z * perimeter_ + phase_;
}
bool operator==(const circular_axis &o) const {
bool operator==(const circular &o) const {
return axis_base<false>::operator==(o) && phase_ == o.phase_ &&
perimeter_ == o.perimeter_;
}
@@ -358,14 +359,14 @@ private:
/** An axis for real-valued data and bins of varying width.
*
* Binning is a O(log(N)) operation. If speed matters
* and the problem domain allows it, prefer a regular_axis.
* and the problem domain allows it, prefer a regular.
*/
template <typename RealType = double>
class variable_axis : public axis_base<true>,
boost::operators<variable_axis<RealType>> {
class variable : public axis_base<true>,
boost::operators<variable<RealType>> {
public:
using value_type = RealType;
using const_iterator = axis_iterator<variable_axis>;
using const_iterator = axis_iterator<variable>;
/** Construct an axis from bin edges.
*
@@ -373,7 +374,7 @@ public:
* \param label description of the axis.
* \param uoflow whether to add under-/overflow bins.
*/
variable_axis(const std::initializer_list<value_type> &x,
variable(const std::initializer_list<value_type> &x,
const std::string &label = std::string(), bool uoflow = true)
: axis_base<true>(x.size() - 1, label, uoflow),
x_(new value_type[x.size()]) {
@@ -385,7 +386,7 @@ public:
}
template <typename Iterator>
variable_axis(Iterator begin, Iterator end,
variable(Iterator begin, Iterator end,
const std::string &label = std::string(), bool uoflow = true)
: axis_base<true>(std::distance(begin, end) - 1, label, uoflow),
x_(new value_type[std::distance(begin, end)]) {
@@ -393,12 +394,12 @@ public:
std::sort(x_.get(), x_.get() + bins() + 1);
}
variable_axis() = default;
variable_axis(const variable_axis &o)
variable() = default;
variable(const variable &o)
: axis_base<true>(o), x_(new value_type[bins() + 1]) {
std::copy(o.x_.get(), o.x_.get() + bins() + 1, x_.get());
}
variable_axis &operator=(const variable_axis &o) {
variable &operator=(const variable &o) {
if (this != &o) {
axis_base<true>::operator=(o);
x_.reset(new value_type[bins() + 1]);
@@ -406,8 +407,8 @@ public:
}
return *this;
}
variable_axis(variable_axis &&) = default;
variable_axis &operator=(variable_axis &&) = default;
variable(variable &&) = default;
variable &operator=(variable &&) = default;
/// Returns the bin index for the passed argument.
inline int index(value_type x) const noexcept {
@@ -425,7 +426,7 @@ public:
return x_[idx];
}
bool operator==(const variable_axis &o) const {
bool operator==(const variable &o) const {
if (!axis_base<true>::operator==(o)) {
return false;
}
@@ -450,19 +451,19 @@ private:
/** An axis for a contiguous range of integers.
*
* Binning is a O(1) operation. This axis operates
* faster than a regular_axis.
* faster than a regular.
*/
class integer_axis : public axis_base<true>, boost::operators<integer_axis> {
class integer : public axis_base<true>, boost::operators<integer> {
public:
using value_type = int;
using const_iterator = axis_iterator<integer_axis>;
using const_iterator = axis_iterator<integer>;
/** Construct axis over integer range [min, max].
*
* \param min smallest integer of the covered range.
* \param max largest integer of the covered range.
*/
integer_axis(value_type min, value_type max,
integer(value_type min, value_type max,
const std::string &label = std::string(), bool uoflow = true)
: axis_base<true>(max + 1 - min, label, uoflow), min_(min) {
if (min > max) {
@@ -470,11 +471,11 @@ public:
}
}
integer_axis() = default;
integer_axis(const integer_axis &) = default;
integer_axis &operator=(const integer_axis &) = default;
integer_axis(integer_axis &&) = default;
integer_axis &operator=(integer_axis &&) = default;
integer() = default;
integer(const integer &) = default;
integer &operator=(const integer &) = default;
integer(integer &&) = default;
integer &operator=(integer &&) = default;
/// Returns the bin index for the passed argument.
inline int index(value_type x) const noexcept {
@@ -485,7 +486,7 @@ public:
/// Returns the integer that is mapped to the bin index.
value_type operator[](int idx) const { return min_ + idx; }
bool operator==(const integer_axis &o) const {
bool operator==(const integer &o) const {
return axis_base<true>::operator==(o) && min_ == o.min_;
}
@@ -511,13 +512,13 @@ private:
* There are no underflow/overflow bins for this axis.
* Binning is a O(1) operation.
*/
class category_axis : public axis_base<false>, boost::operators<category_axis> {
class category : public axis_base<false>, boost::operators<category> {
public:
using value_type = BOOST_HISTOGRAM_STRING_VIEW;
using const_iterator = axis_iterator<category_axis>;
using const_iterator = axis_iterator<category>;
template <typename Iterator>
category_axis(Iterator begin, Iterator end,
category(Iterator begin, Iterator end,
const std::string &label = std::string())
: axis_base<false>(std::distance(begin, end), label),
ptr_(new std::string[bins()]) {
@@ -528,16 +529,16 @@ public:
*
* \param categories sequence of labeled categories.
*/
category_axis(const std::initializer_list<std::string> &categories,
category(const std::initializer_list<std::string> &categories,
const std::string &label = std::string())
: category_axis(categories.begin(), categories.end(), label) {}
: category(categories.begin(), categories.end(), label) {}
category_axis() = default;
category() = default;
category_axis(const category_axis &other)
: category_axis(other.ptr_.get(), other.ptr_.get() + other.bins(),
category(const category &other)
: category(other.ptr_.get(), other.ptr_.get() + other.bins(),
other.label()) {}
category_axis &operator=(const category_axis &other) {
category &operator=(const category &other) {
if (this != &other) {
axis_base<false>::operator=(other);
ptr_.reset(new std::string[other.bins()]);
@@ -546,10 +547,10 @@ public:
return *this;
}
category_axis(category_axis &&other)
category(category &&other)
: axis_base<false>(std::move(other)), ptr_(std::move(other.ptr_)) {}
category_axis &operator=(category_axis &&other) {
category &operator=(category &&other) {
if (this != &other) {
axis_base<false>::operator=(std::move(other));
ptr_ = std::move(other.ptr_);
@@ -571,7 +572,7 @@ public:
return ptr_.get()[idx];
}
bool operator==(const category_axis &other) const {
bool operator==(const category &other) const {
return axis_base<false>::operator==(other) &&
std::equal(ptr_.get(), ptr_.get() + bins(), other.ptr_.get());
}
@@ -587,10 +588,14 @@ private:
template <class Archive> void serialize(Archive &, unsigned);
};
using builtin_axes = mpl::vector<regular_axis<double>, regular_axis<float>,
circular_axis<double>, circular_axis<float>,
variable_axis<double>, variable_axis<float>,
integer_axis, category_axis>::type;
} // namespace axis
using builtin_axes = mpl::vector<axis::regular<double>,
axis::circular<double>,
axis::variable<double>,
axis::integer,
axis::category>;
} // namespace histogram
} // namespace boost

View File

@@ -14,11 +14,12 @@
namespace boost {
namespace histogram {
namespace axis {
template <typename RealType>
inline std::ostream &operator<<(std::ostream &os,
const regular_axis<RealType> &a) {
os << "regular_axis(" << a.bins() << ", " << a[0] << ", " << a[a.bins()];
const regular<RealType> &a) {
os << "regular(" << a.bins() << ", " << a[0] << ", " << a[a.bins()];
if (!a.label().empty()) {
os << ", label=";
detail::escape(os, a.label());
@@ -32,8 +33,8 @@ inline std::ostream &operator<<(std::ostream &os,
template <typename RealType>
inline std::ostream &operator<<(std::ostream &os,
const circular_axis<RealType> &a) {
os << "circular_axis(" << a.bins();
const circular<RealType> &a) {
os << "circular(" << a.bins();
if (a.phase() != 0.0) {
os << ", phase=" << a.phase();
}
@@ -50,8 +51,8 @@ inline std::ostream &operator<<(std::ostream &os,
template <typename RealType>
inline std::ostream &operator<<(std::ostream &os,
const variable_axis<RealType> &a) {
os << "variable_axis(" << a[0];
const variable<RealType> &a) {
os << "variable(" << a[0];
for (int i = 1; i <= a.bins(); ++i) {
os << ", " << a[i];
}
@@ -66,8 +67,8 @@ inline std::ostream &operator<<(std::ostream &os,
return os;
}
inline std::ostream &operator<<(std::ostream &os, const integer_axis &a) {
os << "integer_axis(" << a[0] << ", " << a[a.bins() - 1];
inline std::ostream &operator<<(std::ostream &os, const integer &a) {
os << "integer(" << a[0] << ", " << a[a.bins() - 1];
if (!a.label().empty()) {
os << ", label=";
detail::escape(os, a.label());
@@ -79,8 +80,8 @@ inline std::ostream &operator<<(std::ostream &os, const integer_axis &a) {
return os;
}
inline std::ostream &operator<<(std::ostream &os, const category_axis &a) {
os << "category_axis(";
inline std::ostream &operator<<(std::ostream &os, const category &a) {
os << "category(";
for (int i = 0; i < a.bins(); ++i) {
detail::escape(os, a[i]);
os << (i == (a.bins() - 1) ? "" : ", ");
@@ -92,6 +93,8 @@ inline std::ostream &operator<<(std::ostream &os, const category_axis &a) {
os << ")";
return os;
}
} // namespace axis
} // namespace histogram
} // namespace boost

View File

@@ -117,6 +117,8 @@ void adaptive_storage<Allocator>::serialize(Archive &ar,
}
}
namespace axis {
template <class Archive>
void axis_base<false>::serialize(Archive &ar, unsigned /* version */) {
ar &size_;
@@ -132,7 +134,7 @@ void axis_base<true>::serialize(Archive &ar, unsigned /* version */) {
template <typename RealType, template <class> class Transform>
template <class Archive>
void regular_axis<RealType, Transform>::serialize(Archive &ar,
void regular<RealType, Transform>::serialize(Archive &ar,
unsigned /* version */) {
ar &boost::serialization::base_object<axis_base<true>>(*this);
ar &min_;
@@ -141,7 +143,7 @@ void regular_axis<RealType, Transform>::serialize(Archive &ar,
template <typename RealType>
template <class Archive>
void circular_axis<RealType>::serialize(Archive &ar, unsigned /* version */) {
void circular<RealType>::serialize(Archive &ar, unsigned /* version */) {
ar &boost::serialization::base_object<axis_base<false>>(*this);
ar &phase_;
ar &perimeter_;
@@ -149,7 +151,7 @@ void circular_axis<RealType>::serialize(Archive &ar, unsigned /* version */) {
template <typename RealType>
template <class Archive>
void variable_axis<RealType>::serialize(Archive &ar, unsigned /* version */) {
void variable<RealType>::serialize(Archive &ar, unsigned /* version */) {
ar &boost::serialization::base_object<axis_base<true>>(*this);
if (Archive::is_loading::value) {
x_.reset(new RealType[bins() + 1]);
@@ -158,13 +160,13 @@ void variable_axis<RealType>::serialize(Archive &ar, unsigned /* version */) {
}
template <class Archive>
void integer_axis::serialize(Archive &ar, unsigned /* version */) {
void integer::serialize(Archive &ar, unsigned /* version */) {
ar &boost::serialization::base_object<axis_base<true>>(*this);
ar &min_;
}
template <class Archive>
void category_axis::serialize(Archive &ar, unsigned /* version */) {
void category::serialize(Archive &ar, unsigned /* version */) {
ar &boost::serialization::base_object<axis_base<false>>(*this);
if (Archive::is_loading::value) {
ptr_.reset(new std::string[bins()]);
@@ -172,6 +174,8 @@ void category_axis::serialize(Archive &ar, unsigned /* version */) {
ar &boost::serialization::make_array(ptr_.get(), bins());
}
} // namespace axis
template <class A, class S>
template <class Archive>
void histogram<Static, A, S>::serialize(Archive &ar, unsigned /* version */) {