#ifndef __JSUPPORT__JLIMIT__ #define __JSUPPORT__JLIMIT__ #include #include #include #include #include #include "JROOT/JCounter.hh" #include "JTools/JRange.hh" #include "JLang/JComparable.hh" /** * \file * Auxiliaries for defining the range of iterations of objects. * \author mdejong */ namespace JSUPPORT {} namespace JPP { using namespace JSUPPORT; } namespace JSUPPORT { using JTOOLS::JRange; using JLANG::JComparable; using JROOT::counter_type; /** * Auxiliary class for defining the range of iterations of objects. * * Possible input syntax: *
   *     \:\
   *     \
   * 
* where the iteration starts at 0. */ struct JLimit : public JRange, public JComparable, public JComparable { typedef JRange range_type; /** * Default constructor. */ JLimit() : JRange(JLimit::min(), JLimit::max()) {} /** * Constructor. * * \param counter number of entries */ JLimit(const counter_type counter) : JRange(JLimit::min(), counter) {} /** * Constructor. * * \param lower lower limit * \param upper upper limit */ JLimit(const counter_type lower, const counter_type upper) : JRange(lower, upper) {} /** * Get limit. * * \return this limit */ const JLimit& getLimit() const { return static_cast(*this); } /** * Get limit. * * \return this limit */ JLimit& getLimit() { return static_cast(*this); } /** * Set limit. * * \param limit limit */ void setLimit(const JLimit& limit) { static_cast(*this) = limit; } /** * Get minimum counter value. * * \return value */ static counter_type min() { return 0; } /** * Get maximum counter value. * * \return value */ static counter_type max() { return std::numeric_limits::max(); } /** * Compare limit. * * \param limit limit * \return true if this upper limit is less then given upper limit; else false */ bool less(const JLimit& limit) const { return getUpperLimit() < limit.getUpperLimit(); } /** * Compare counter. * * \param counter counter * \return true if this upper limit is less than counter; else false */ bool less(const counter_type counter) const { return getUpperLimit() < counter; } /** * Compare counter. * * \param counter counter * \return true if this upper limit is more than counter; else false */ bool more(const counter_type counter) const { return getUpperLimit() > counter; } /** * Read limit from input. * * Note that input n will set the begin and end of the iteration to 0 and n, respectively; * and input i:n will set the begin and end of the iteration to i and i+n, respectively. * * \param in input stream * \param limit limit * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JLimit& limit) { using namespace std; limit.setLowerLimit(JLimit::min()); limit.setUpperLimit(JLimit::max()); string buffer; if (in >> buffer) { string::size_type pos = buffer.find(SEPARATOR); if (pos != string::npos) { buffer[pos] = ' '; istringstream is(buffer); if (is >> static_cast(limit)) { limit.setUpperLimit(limit.getLowerLimit() + limit.getUpperLimit()); } } else { counter_type value; istringstream is(buffer); is >> value; limit.setUpperLimit(value); } } return in; } static const char SEPARATOR = ':'; //!< separator between values }; /** * Type definition of limit. */ typedef JLimit JLimit_t; } #endif