no double streamer implementation, only use the python one

This commit is contained in:
Hans Dembinski 2016-05-08 20:17:26 -04:00
parent 7a26edb1ee
commit 33993f4c67
4 changed files with 81 additions and 122 deletions

View File

@ -306,7 +306,12 @@ typedef variant<
integer_axis
> 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
}
}

View File

@ -1,11 +1,28 @@
#include <boost/histogram/axis.hpp>
#include <limits>
#include <sstream>
#include <string>
#include <stdexcept>
namespace boost {
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,
const std::string& label,
bool uoflow) :
@ -107,8 +124,9 @@ double
polar_axis::operator[](int idx)
const
{
using namespace boost::math::double_constants;
const double z = double(idx) / bins();
return z * 6.283185307179586 + start_;
return z * two_pi + start_;
}
bool
@ -218,61 +236,57 @@ integer_axis::operator==(const integer_axis& o) const
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 {
std::stringstream line;
line << "category_axis(";
for (int i = 0; i < a.bins(); ++i)
line << a[i] << (i == (a.bins() - 1)? ")" : ", ");
return line.str();
}
os << "regular_axis(" << a.bins() << ", " << a[0] << ", " << a[a.bins()];
if (!a.label().empty())
os << ", label=" << escape(a.label());
if (!a.uoflow())
os << ", uoflow=False";
os << ")";
return os;
}
std::string operator()(const integer_axis& a) const {
std::stringstream line;
line << "integer_axis(";
if (a.bins())
line << a[0] << "," << a[a.bins() - 1];
line << ")";
return line.str();
}
std::ostream& operator<<(std::ostream& os, const polar_axis& a)
{
os << "polar_axis(" << a.bins();
if (a[0] != 0.0)
os << ", " << a[0];
if (!a.label().empty())
os << ", label=" << escape(a.label());
os << ")";
return os;
}
std::string operator()(const polar_axis& a) const {
std::stringstream line;
line << "polar_axis(";
if (!a.label().empty())
line << a.label() << ", ";
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 variable_axis& a)
{
os << "variable_axis(" << a[0];
for (int i = 1; i <= a.bins(); ++i)
os << ", " << a.left(i);
if (!a.label().empty())
os << ", label=" << escape(a.label());
if (!a.uoflow())
os << ", uoflow=False";
os << ")";
return os;
}
std::string operator()(const regular_axis& a) const {
std::stringstream line;
line << "regular_axis[";
if (!a.label().empty())
line << a.label() << ", ";
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 integer_axis& a)
{
os << "integer_axis(" << a[0] << ", " << a[a.bins() - 1];
if (!a.label().empty())
os << ", label=" << escape(a.label());
if (!a.uoflow())
os << ", uoflow=False";
os << ")";
return os;
}
std::string operator()(const variable_axis& a) const {
std::stringstream line;
line << "variable_axis[";
if (!a.label().empty())
line << a.label() << ", ";
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);
std::ostream& operator<<(std::ostream& os, const category_axis& a)
{
os << "category_axis(";
for (int i = 0; i < a.bins(); ++i)
os << escape(a[i]) << (i == (a.bins() - 1)? ")" : ", ");
return os;
}

View File

@ -112,6 +112,14 @@ axis_getitem(const T& t, int 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>
struct has_index_method
{
@ -123,74 +131,6 @@ struct has_index_method
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>
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: low edge of the bin",
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",
python::arg("self"));
cl.def(python::self == python::self);

View File

@ -70,7 +70,7 @@ BOOST_AUTO_TEST_CASE(integer_axis_operators) {
BOOST_CHECK_EQUAL(a, b);
}
BOOST_AUTO_TEST_CASE(stream_operator) {
BOOST_AUTO_TEST_CASE(axis_type_streamable) {
std::vector<double> x;
x += -1, 0, 1;
std::vector<axis_type> axes;