mirror of
https://github.com/boostorg/histogram.git
synced 2025-05-12 05:31:51 +00:00
no double streamer implementation, only use the python one
This commit is contained in:
parent
7a26edb1ee
commit
33993f4c67
@ -306,7 +306,12 @@ typedef variant<
|
|||||||
integer_axis
|
integer_axis
|
||||||
> axis_type;
|
> axis_type;
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream&, const axis_type&);
|
std::ostream& operator<<(std::ostream&, const regular_axis&);
|
||||||
|
std::ostream& operator<<(std::ostream&, const polar_axis&);
|
||||||
|
std::ostream& operator<<(std::ostream&, const variable_axis&);
|
||||||
|
std::ostream& operator<<(std::ostream&, const category_axis&);
|
||||||
|
std::ostream& operator<<(std::ostream&, const integer_axis&);
|
||||||
|
// axis_type is automatically output-streamable if all its bounded types are
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
116
src/axis.cpp
116
src/axis.cpp
@ -1,11 +1,28 @@
|
|||||||
#include <boost/histogram/axis.hpp>
|
#include <boost/histogram/axis.hpp>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace boost {
|
namespace boost {
|
||||||
namespace histogram {
|
namespace histogram {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
std::string escape(const std::string& s) {
|
||||||
|
std::ostringstream os;
|
||||||
|
os << '\'';
|
||||||
|
for (unsigned i = 0; i < s.size(); ++i) {
|
||||||
|
const char c = s[i];
|
||||||
|
if (c == '\'' && (i == 0 || s[i - 1] != '\\'))
|
||||||
|
os << "\\\'";
|
||||||
|
else
|
||||||
|
os << c;
|
||||||
|
}
|
||||||
|
os << '\'';
|
||||||
|
return os.str();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
axis_base::axis_base(int size,
|
axis_base::axis_base(int size,
|
||||||
const std::string& label,
|
const std::string& label,
|
||||||
bool uoflow) :
|
bool uoflow) :
|
||||||
@ -107,8 +124,9 @@ double
|
|||||||
polar_axis::operator[](int idx)
|
polar_axis::operator[](int idx)
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
|
using namespace boost::math::double_constants;
|
||||||
const double z = double(idx) / bins();
|
const double z = double(idx) / bins();
|
||||||
return z * 6.283185307179586 + start_;
|
return z * two_pi + start_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -218,61 +236,57 @@ integer_axis::operator==(const integer_axis& o) const
|
|||||||
min_ == o.min_;
|
min_ == o.min_;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct stream_visitor : public static_visitor<std::string>
|
std::ostream& operator<<(std::ostream& os, const regular_axis& a)
|
||||||
{
|
{
|
||||||
std::string operator()(const category_axis& a) const {
|
os << "regular_axis(" << a.bins() << ", " << a[0] << ", " << a[a.bins()];
|
||||||
std::stringstream line;
|
if (!a.label().empty())
|
||||||
line << "category_axis(";
|
os << ", label=" << escape(a.label());
|
||||||
for (int i = 0; i < a.bins(); ++i)
|
if (!a.uoflow())
|
||||||
line << a[i] << (i == (a.bins() - 1)? ")" : ", ");
|
os << ", uoflow=False";
|
||||||
return line.str();
|
os << ")";
|
||||||
}
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
std::string operator()(const integer_axis& a) const {
|
std::ostream& operator<<(std::ostream& os, const polar_axis& a)
|
||||||
std::stringstream line;
|
{
|
||||||
line << "integer_axis(";
|
os << "polar_axis(" << a.bins();
|
||||||
if (a.bins())
|
if (a[0] != 0.0)
|
||||||
line << a[0] << "," << a[a.bins() - 1];
|
os << ", " << a[0];
|
||||||
line << ")";
|
if (!a.label().empty())
|
||||||
return line.str();
|
os << ", label=" << escape(a.label());
|
||||||
}
|
os << ")";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
std::string operator()(const polar_axis& a) const {
|
std::ostream& operator<<(std::ostream& os, const variable_axis& a)
|
||||||
std::stringstream line;
|
{
|
||||||
line << "polar_axis(";
|
os << "variable_axis(" << a[0];
|
||||||
if (!a.label().empty())
|
for (int i = 1; i <= a.bins(); ++i)
|
||||||
line << a.label() << ", ";
|
os << ", " << a.left(i);
|
||||||
line << a.bins() << ":";
|
if (!a.label().empty())
|
||||||
for (int i = 0; i <= a.bins(); ++i)
|
os << ", label=" << escape(a.label());
|
||||||
line << " " << a.left(i);
|
if (!a.uoflow())
|
||||||
return line.str();
|
os << ", uoflow=False";
|
||||||
}
|
os << ")";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
std::string operator()(const regular_axis& a) const {
|
std::ostream& operator<<(std::ostream& os, const integer_axis& a)
|
||||||
std::stringstream line;
|
{
|
||||||
line << "regular_axis[";
|
os << "integer_axis(" << a[0] << ", " << a[a.bins() - 1];
|
||||||
if (!a.label().empty())
|
if (!a.label().empty())
|
||||||
line << a.label() << ", ";
|
os << ", label=" << escape(a.label());
|
||||||
line << a.bins() << ":";
|
if (!a.uoflow())
|
||||||
for (int i = 0; i <= a.bins(); ++i)
|
os << ", uoflow=False";
|
||||||
line << " " << a.left(i);
|
os << ")";
|
||||||
return line.str();
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string operator()(const variable_axis& a) const {
|
std::ostream& operator<<(std::ostream& os, const category_axis& a)
|
||||||
std::stringstream line;
|
{
|
||||||
line << "variable_axis[";
|
os << "category_axis(";
|
||||||
if (!a.label().empty())
|
for (int i = 0; i < a.bins(); ++i)
|
||||||
line << a.label() << ", ";
|
os << escape(a[i]) << (i == (a.bins() - 1)? ")" : ", ");
|
||||||
line << a.bins() << ":";
|
|
||||||
for (int i = 0; i <= a.bins(); ++i)
|
|
||||||
line << " " << a.left(i);
|
|
||||||
return line.str();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& os, const axis_type& a) {
|
|
||||||
os << apply_visitor(stream_visitor(), a);
|
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,6 +112,14 @@ axis_getitem(const T& t, int i) {
|
|||||||
return t[i];
|
return t[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
std::string
|
||||||
|
axis_repr(const T& t) {
|
||||||
|
std::ostringstream os;
|
||||||
|
os << t;
|
||||||
|
return os.str();
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct has_index_method
|
struct has_index_method
|
||||||
{
|
{
|
||||||
@ -123,74 +131,6 @@ struct has_index_method
|
|||||||
enum { value = sizeof(test<T>(0)) == sizeof(yes) };
|
enum { value = sizeof(test<T>(0)) == sizeof(yes) };
|
||||||
};
|
};
|
||||||
|
|
||||||
std::string escape(const std::string& s) {
|
|
||||||
std::ostringstream os;
|
|
||||||
os << '\'';
|
|
||||||
for (unsigned i = 0; i < s.size(); ++i) {
|
|
||||||
const char c = s[i];
|
|
||||||
if (c == '\'' && (i == 0 || s[i - 1] != '\\'))
|
|
||||||
os << "\\\'";
|
|
||||||
else
|
|
||||||
os << c;
|
|
||||||
}
|
|
||||||
os << '\'';
|
|
||||||
return os.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string axis_repr(const regular_axis& a) {
|
|
||||||
std::stringstream s;
|
|
||||||
s << "regular_axis(" << a.bins() << ", " << a[0] << ", " << a[a.bins()];
|
|
||||||
if (!a.label().empty())
|
|
||||||
s << ", label=" << escape(a.label());
|
|
||||||
if (!a.uoflow())
|
|
||||||
s << ", uoflow=False";
|
|
||||||
s << ")";
|
|
||||||
return s.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string axis_repr(const polar_axis& a) {
|
|
||||||
std::stringstream s;
|
|
||||||
s << "polar_axis(" << a.bins();
|
|
||||||
if (a[0] != 0.0)
|
|
||||||
s << ", " << a[0];
|
|
||||||
if (!a.label().empty())
|
|
||||||
s << ", label=" << escape(a.label());
|
|
||||||
s << ")";
|
|
||||||
return s.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string axis_repr(const variable_axis& a) {
|
|
||||||
std::stringstream s;
|
|
||||||
s << "variable_axis(" << a[0];
|
|
||||||
for (int i = 1; i <= a.bins(); ++i)
|
|
||||||
s << ", " << a.left(i);
|
|
||||||
if (!a.label().empty())
|
|
||||||
s << ", label=" << escape(a.label());
|
|
||||||
if (!a.uoflow())
|
|
||||||
s << ", uoflow=False";
|
|
||||||
s << ")";
|
|
||||||
return s.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string axis_repr(const category_axis& a) {
|
|
||||||
std::stringstream s;
|
|
||||||
s << "category_axis(";
|
|
||||||
for (int i = 0; i < a.bins(); ++i)
|
|
||||||
s << escape(a[i]) << (i == (a.bins() - 1)? ")" : ", ");
|
|
||||||
return s.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string axis_repr(const integer_axis& a) {
|
|
||||||
std::stringstream s;
|
|
||||||
s << "integer_axis(" << a[0] << ", " << a[a.bins() - 1];
|
|
||||||
if (!a.label().empty())
|
|
||||||
s << ", label=" << escape(a.label());
|
|
||||||
if (!a.uoflow())
|
|
||||||
s << ", uoflow=False";
|
|
||||||
s << ")";
|
|
||||||
return s.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
struct axis_suite : public python::def_visitor<axis_suite<T> > {
|
struct axis_suite : public python::def_visitor<axis_suite<T> > {
|
||||||
|
|
||||||
@ -242,7 +182,7 @@ struct axis_suite : public python::def_visitor<axis_suite<T> > {
|
|||||||
":returns: category mapped to passed bin index" :
|
":returns: category mapped to passed bin index" :
|
||||||
":returns: low edge of the bin",
|
":returns: low edge of the bin",
|
||||||
python::args("self", "index"));
|
python::args("self", "index"));
|
||||||
cl.def("__repr__", (std::string (*)(const T&)) &axis_repr,
|
cl.def("__repr__", axis_repr<T>,
|
||||||
":returns: string representation of this axis",
|
":returns: string representation of this axis",
|
||||||
python::arg("self"));
|
python::arg("self"));
|
||||||
cl.def(python::self == python::self);
|
cl.def(python::self == python::self);
|
||||||
|
@ -70,7 +70,7 @@ BOOST_AUTO_TEST_CASE(integer_axis_operators) {
|
|||||||
BOOST_CHECK_EQUAL(a, b);
|
BOOST_CHECK_EQUAL(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(stream_operator) {
|
BOOST_AUTO_TEST_CASE(axis_type_streamable) {
|
||||||
std::vector<double> x;
|
std::vector<double> x;
|
||||||
x += -1, 0, 1;
|
x += -1, 0, 1;
|
||||||
std::vector<axis_type> axes;
|
std::vector<axis_type> axes;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user