Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
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 */
21namespace JSUPPORT {}
22namespace JPP { using namespace JSUPPORT; }
23
24namespace JSUPPORT {
25
26 using JTOOLS::JRange;
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>,
44 public JComparable<JLimit, counter_type>
45 {
46
48
49
50 /**
51 * Default constructor.
52 */
55 {}
56
57
58 /**
59 * Constructor.
60 *
61 * \param counter number of entries
62 */
63 JLimit(const counter_type counter) :
64 JRange<counter_type>(JLimit::min(), counter)
65 {}
66
67
68 /**
69 * Constructor.
70 *
71 * \param lower lower limit
72 * \param upper upper limit
73 */
74 JLimit(const counter_type lower, const counter_type upper) :
75 JRange<counter_type>(lower, upper)
76 {}
77
78
79 /**
80 * Get limit.
81 *
82 * \return this limit
83 */
84 const JLimit& getLimit() const
85 {
86 return static_cast<const JLimit&>(*this);
87 }
88
89
90 /**
91 * Get limit.
92 *
93 * \return this limit
94 */
96 {
97 return static_cast<JLimit&>(*this);
98 }
99
100
101 /**
102 * Set limit.
103 *
104 * \param limit limit
105 */
106 void setLimit(const JLimit& limit)
107 {
108 static_cast<JLimit&>(*this) = limit;
109 }
110
111
112 /**
113 * Get minimum counter value.
114 *
115 * \return value
116 */
118 {
119 return 0;
120 }
121
122
123 /**
124 * Get maximum counter value.
125 *
126 * \return value
127 */
129 {
130 return std::numeric_limits<counter_type>::max();
131 }
132
133
134 /**
135 * Compare limit.
136 *
137 * \param limit limit
138 * \return true if this upper limit is less then given upper limit; else false
139 */
140 bool less(const JLimit& limit) const
141 {
142 return getUpperLimit() < limit.getUpperLimit();
143 }
144
145
146 /**
147 * Compare counter.
148 *
149 * \param counter counter
150 * \return true if this upper limit is less than counter; else false
151 */
152 bool less(const counter_type counter) const
153 {
154 return getUpperLimit() < counter;
155 }
156
157
158 /**
159 * Compare counter.
160 *
161 * \param counter counter
162 * \return true if this upper limit is more than counter; else false
163 */
164 bool more(const counter_type counter) const
165 {
166 return getUpperLimit() > counter;
167 }
168
169
170 /**
171 * Read limit from input.
172 *
173 * Note that input <tt>n</tt> will set the begin and end of the iteration to <tt>0</tt> and <tt>n</tt>, respectively;
174 * 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.
175 *
176 * \param in input stream
177 * \param limit limit
178 * \return input stream
179 */
180 friend inline std::istream& operator>>(std::istream& in, JLimit& limit)
181 {
182 using namespace std;
183
184 limit.setLowerLimit(JLimit::min());
185 limit.setUpperLimit(JLimit::max());
186
187 string buffer;
188
189 if (in >> buffer) {
190
191 string::size_type pos = buffer.find(SEPARATOR);
192
193 if (pos != string::npos) {
194
195 buffer[pos] = ' ';
196
197 istringstream is(buffer);
198
199 if (is >> static_cast<range_type&>(limit)) {
200 limit.setUpperLimit(limit.getLowerLimit() + limit.getUpperLimit());
201 }
202
203 } else {
204
205 counter_type value;
206
207 istringstream is(buffer);
208
209 is >> value;
210
211 limit.setUpperLimit(value);
212 }
213 }
214
215 return in;
216 }
217
218
219 static const char SEPARATOR = ':'; //!< separator between values
220 };
221
222
223 /**
224 * Type definition of limit.
225 */
227}
228
229#endif
Type definition for counter for ROOT TTree and auxiliary methods.
Auxiliary class to define a range between two values.
Range of values.
Definition JRange.hh:42
void setUpperLimit(argument_type y)
Set upper limit.
Definition JRange.hh:235
T getLowerLimit() const
Get lower limit.
Definition JRange.hh:202
void setLowerLimit(argument_type x)
Set lower limit.
Definition JRange.hh:224
T getUpperLimit() const
Get upper limit.
Definition JRange.hh:213
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Long64_t counter_type
Type definition for counter.
Support classes and methods for experiment specific I/O.
JLimit JLimit_t
Type definition of limit.
Definition JLimit.hh:226
Template definition of auxiliary base class for comparison of data structures.
Auxiliary class for defining the range of iterations of objects.
Definition JLimit.hh:45
JRange< counter_type > range_type
Definition JLimit.hh:47
static const char SEPARATOR
separator between values
Definition JLimit.hh:219
JLimit(const counter_type lower, const counter_type upper)
Constructor.
Definition JLimit.hh:74
const JLimit & getLimit() const
Get limit.
Definition JLimit.hh:84
JLimit & getLimit()
Get limit.
Definition JLimit.hh:95
static counter_type max()
Get maximum counter value.
Definition JLimit.hh:128
JLimit()
Default constructor.
Definition JLimit.hh:53
friend std::istream & operator>>(std::istream &in, JLimit &limit)
Read limit from input.
Definition JLimit.hh:180
bool more(const counter_type counter) const
Compare counter.
Definition JLimit.hh:164
void setLimit(const JLimit &limit)
Set limit.
Definition JLimit.hh:106
JLimit(const counter_type counter)
Constructor.
Definition JLimit.hh:63
bool less(const JLimit &limit) const
Compare limit.
Definition JLimit.hh:140
static counter_type min()
Get minimum counter value.
Definition JLimit.hh:117
bool less(const counter_type counter) const
Compare counter.
Definition JLimit.hh:152