Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JLimit.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT__JLIMIT__
2 #define __JSUPPORT__JLIMIT__
3 
4 #include <limits>
5 #include <string>
6 #include <istream>
7 #include <ostream>
8 #include <sstream>
9 
10 #include "JROOT/JCounter.hh"
11 
12 #include "JTools/JRange.hh"
13 #include "JLang/JComparable.hh"
14 
15 
16 /**
17  * \file
18  * Auxiliaries for defining the range of iterations of objects.
19  * \author mdejong
20  */
21 namespace JSUPPORT {}
22 namespace JPP { using namespace JSUPPORT; }
23 
24 namespace JSUPPORT {
25 
26  using JTOOLS::JRange;
27  using JLANG::JComparable;
28  using JROOT::counter_type;
29 
30 
31  /**
32  * Auxiliary class for defining the range of iterations of objects.
33  *
34  * Possible input syntax:
35  * <pre>
36  * <begin of iteration>:<number of iterations>
37  * <number of iterations>
38  * </pre>
39  * where the iteration starts at 0.
40  */
41  struct JLimit :
42  public JRange<counter_type>,
43  public JComparable<JLimit, counter_type>
44  {
45 
47 
48 
49  /**
50  * Default constructor.
51  */
52  JLimit() :
54  {}
55 
56 
57  /**
58  * Constructor.
59  *
60  * \param counter end of iteration
61  */
62  JLimit(const counter_type counter) :
63  JRange<counter_type>(JLimit::min(), counter)
64  {}
65 
66 
67  /**
68  * Get limit.
69  *
70  * \return this limit
71  */
72  const JLimit& getLimit() const
73  {
74  return static_cast<const JLimit&>(*this);
75  }
76 
77 
78  /**
79  * Get limit.
80  *
81  * \return this limit
82  */
84  {
85  return static_cast<JLimit&>(*this);
86  }
87 
88 
89  /**
90  * Set limit.
91  *
92  * \param limit limit
93  */
94  void setLimit(const JLimit& limit)
95  {
96  static_cast<JLimit&>(*this) = limit;
97  }
98 
99 
100  /**
101  * Get minimum counter value.
102  *
103  * \return value
104  */
105  static counter_type min()
106  {
107  return 0;
108  }
109 
110 
111  /**
112  * Get maximum counter value.
113  *
114  * \return value
115  */
116  static counter_type max()
117  {
118  return std::numeric_limits<counter_type>::max();
119  }
120 
121 
122  /**
123  * Compare limit.
124  *
125  * \param limit limit
126  * \return true if this upper limit is less then given upper limit; else false
127  */
128  bool less(const JLimit& limit) const
129  {
130  return getUpperLimit() < limit.getUpperLimit();
131  }
132 
133 
134  /**
135  * Compare counter.
136  *
137  * \param counter counter
138  * \return true if this upper limit is less than counter; else false
139  */
140  bool less(const counter_type counter) const
141  {
142  return getUpperLimit() < counter;
143  }
144 
145 
146  /**
147  * Compare counter.
148  *
149  * \param counter counter
150  * \return true if this upper limit is more than counter; else false
151  */
152  bool more(const counter_type counter) const
153  {
154  return getUpperLimit() > counter;
155  }
156 
157 
158  /**
159  * Read limit from input.
160  *
161  * Note that input <tt>n</tt> will set the begin and end of the iteration to <tt>0</tt> and <tt>n</tt>, respectively;
162  * and input <tt>i:n</tt> will set the begin and end of the iteration to <tt>i</tt> and <tt>i+n</tt>, respectively.
163  *
164  * \param in input stream
165  * \param limit limit
166  * \return input stream
167  */
168  friend inline std::istream& operator>>(std::istream& in, JLimit& limit)
169  {
170  using namespace std;
171 
172  limit.setLowerLimit(JLimit::min());
173  limit.setUpperLimit(JLimit::max());
174 
175  string buffer;
176 
177  if (in >> buffer) {
178 
179  string::size_type pos = buffer.find(SEPARATOR);
180 
181  if (pos != string::npos) {
182 
183  buffer[pos] = ' ';
184 
185  istringstream is(buffer);
186 
187  if (is >> static_cast<range_type&>(limit)) {
188  limit.setUpperLimit(limit.getLowerLimit() + limit.getUpperLimit());
189  }
190 
191  } else {
192 
193  counter_type value;
194 
195  istringstream is(buffer);
196 
197  is >> value;
198 
199  limit.setUpperLimit(value);
200  }
201  }
202 
203  return in;
204  }
205 
206 
207  static const char SEPARATOR = ':'; //!< separator between values
208  };
209 
210 
211  /**
212  * Type definition of limit.
213  */
214  typedef JLimit JLimit_t;
215 }
216 
217 #endif
bool less(const JLimit &limit) const
Compare limit.
Definition: JLimit.hh:128
JRange< counter_type > range_type
Definition: JLimit.hh:46
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:180
static counter_type max()
Get maximum counter value.
Definition: JLimit.hh:116
Long64_t counter_type
Type definition for counter.
Definition: JCounter.hh:24
void setUpperLimit(argument_type y)
Set upper limit.
Definition: JRange.hh:213
bool more(const counter_type counter) const
Compare counter.
Definition: JLimit.hh:152
JLimit JLimit_t
Type definition of limit.
Definition: JLimit.hh:214
JLimit()
Default constructor.
Definition: JLimit.hh:52
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:191
static const char SEPARATOR
separator between values
Definition: JLimit.hh:207
friend std::istream & operator>>(std::istream &in, JLimit &limit)
Read limit from input.
Definition: JLimit.hh:168
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:24
Range of values.
Definition: JRange.hh:33
bool less(const counter_type counter) const
Compare counter.
Definition: JLimit.hh:140
Auxiliary class to define a range between two values.
JLimit(const counter_type counter)
Constructor.
Definition: JLimit.hh:62
static counter_type min()
Get minimum counter value.
Definition: JLimit.hh:105
Type definition for counter for ROOT TTree and auxiliary methods.
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:72
void setLimit(const JLimit &limit)
Set limit.
Definition: JLimit.hh:94
JLimit & getLimit()
Get limit.
Definition: JLimit.hh:83
void setLowerLimit(argument_type x)
Set lower limit.
Definition: JRange.hh:202