Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
ulonglong.hh
Go to the documentation of this file.
1#ifndef __ANTARESDAQ__ULONGLONG__
2#define __ANTARESDAQ__ULONGLONG__
3
4#include <istream>
5#include <ostream>
6#include <iomanip>
7#include <limits>
8#include <assert.h>
9
10
11class ulonglong {
12
13 typedef unsigned int uint;
14
15public:
18
19public:
20 /**
21 * Default contructor.
22 */
24 u1(0),
25 u2(0)
26 {}
27
28 /**
29 * Constructor.
30 * \param u1_ most significant value
31 * \param u2_ least significant value
32 */
33 ulonglong(const uint u1_,const uint u2_) :
34 u1(u1_),
35 u2(u2_)
36 {}
37
38 /**
39 * Constructor.
40 * \param u2_ least significant value
41 */
42 ulonglong(const int u2_) :
43 u1(0),
44 u2(u2_)
45 {}
46
47 /**
48 * Constructor.
49 * \param u2_ least significant value
50 */
51 ulonglong(const uint u2_) :
52 u1(0),
53 u2(u2_)
54 {}
55
56 /**
57 * Constructor.
58 * \param d value
59 */
60 ulonglong(const double d) :
61 u1(0),
62 u2(0)
63 {
64 *this = d;
65 }
66
67 /**
68 * Assignment operator
69 */
70 ulonglong& operator=(const int i)
71 {
72 u1 = 0;
73 u2 = (uint) i;
74 return *this;
75 }
76
77 /**
78 * Assignment operator
79 */
81 {
82 u1 = 0;
83 u2 = i;
84 return *this;
85 }
86
87 /**
88 * Assignment operator
89 */
90 ulonglong& operator=(const double d)
91 {
92 if (d < (double) std::numeric_limits<uint>::max()) {
93 u1 = 0;
94 u2 = (uint) d;
95 } else {
96 u1 = (uint) (d / ((double) std::numeric_limits<uint>::max()));
97 u2 = (uint) (d - ((double) u1) * (double) std::numeric_limits<uint>::max());
98 }
99 return *this;
100 }
101
102 /**
103 * cast operator
104 */
105 operator double() const
106 {
107 return ((double) u1) * ((double) std::numeric_limits<uint>::max()) + ((double) u2);
108 }
109
110 /**
111 * prefix decrement
112 */
114 {
115 if (u2 > 0)
116 --u2;
117 else {
118 --u1;
119 u2 = std::numeric_limits<uint>::max();
120 }
121 return *this;
122 }
123
124 /**
125 * prefix increment
126 */
128 {
129 if (u2 < std::numeric_limits<uint>::max())
130 ++u2;
131 else {
132 ++u1;
133 u2 = 0;
134 }
135 return *this;
136 }
137
138 /**
139 * postfix decrement
140 */
142 {
143 ulonglong a(*this);
144 if (u2 > 0)
145 u2--;
146 else {
147 u1--;
148 u2 = std::numeric_limits<uint>::max();
149 }
150 return a;
151 }
152
153 /**
154 * postfix increment
155 */
157 {
158 ulonglong a(*this);
159 if (u2 < std::numeric_limits<uint>::max())
160 u2++;
161 else {
162 u1++;
163 u2 = 0;
164 }
165 return a;
166 }
167
168 ulonglong operator-(const int x)
169 {
170 ulonglong b(*this);
171 b -= x;
172 return b;
173 }
174
175 ulonglong operator+(const int x)
176 {
177 ulonglong b(*this);
178 b += x;
179 return b;
180 }
181
183 {
184 ulonglong a(*this);
185 a.u1 -= b.u1;
186 return (a -= b.u2);
187 }
188
190 {
191 ulonglong a(*this);
192 a.u1 += b.u1;
193 return (a += b.u2);
194 }
195
196 ulonglong& operator+=(const int x)
197 {
198 if (x >= 0)
199 return (*this += (uint) x);
200 else
201 return (*this -= (uint) -x);
202 }
203
204 ulonglong& operator-=(const int x)
205 {
206 if (x >= 0)
207 return (*this -= (uint) x);
208 else
209 return (*this += (uint) -x);
210 }
211
213 {
214 uint l = std::numeric_limits<uint>::max() - u2;
215
216 if (l >= x)
217 u2 += x;
218 else {
219 u1++;
220 u2 = x - l - 1;
221 }
222
223 return *this;
224 }
225
227 {
228 if (u2 >= x)
229 u2 -= x;
230 else {
231 u1--;
232 u2 = (std::numeric_limits<uint>::max() - x) + u2 + 1;
233 }
234 return *this;
235 }
236
238 {
239 u1 -= x.u1;
240 *this -= x.u2;
241 return *this;
242 }
243
245 {
246 *this += x.u2;
247 u1 += x.u1;
248 return *this;
249 }
250
252 {
253 u1 &= x.u1;
254 u2 &= x.u2;
255 return *this;
256 }
257
259 {
260 u1 |= x.u1;
261 u2 |= x.u2;
262 return *this;
263 }
264
266 {
267 u1 ^= x.u1;
268 u2 ^= x.u2;
269 return *this;
270 }
271
272 bool operator==(const ulonglong& b) const
273 {
274 return (u1 == b.u1 && u2 == b.u2);
275 }
276
277 bool operator!=(const ulonglong& b) const
278 {
279 return (u1 != b.u1 || u2 != b.u2);
280 }
281
282 bool operator<(const ulonglong& b) const
283 {
284 if (u1 == b.u1)
285 return (u2 < b.u2);
286 else
287 return (u1 < b.u1);
288 }
289
290 bool operator<=(const ulonglong& b) const
291 {
292 if (u1 == b.u1)
293 return (u2 <= b.u2);
294 else
295 return (u1 <= b.u1);
296 }
297
298 bool operator>(const ulonglong& b) const
299 {
300 if (u1 == b.u1)
301 return (u2 > b.u2);
302 else
303 return (u1 > b.u1);
304 }
305
306 bool operator>=(const ulonglong& b) const {
307 if (u1 == b.u1)
308 return (u2 >= b.u2);
309 else
310 return (u1 >= b.u1);
311 }
312
313 uint operator[](const int i) {
314 assert(i == 0 || i == 1);
315 if (i == 0)
316 return u1;
317 else
318 return u2;
319 }
320
321 uint msw() const { return u1; } //!< most significant value
322 uint lsw() const { return u2; } //!< least significant value
323
324 std::ostream& write(std::ostream& out) const
325 {
326 std::ios::fmtflags oldFlags = out.flags();
327 out << std::hex << std::setfill('0') << std::setw(8) << u1 << std::setw(8) << u2;
328 out.flags(oldFlags);
329 return out;
330 }
331
332 std::istream& read(std::istream& in)
333 {
334 in >> u1 >> u2;
335 return in;
336 }
337
338 /**
339 * distance between 2 values
340 * \param a first value
341 * \param b second value
342 * \return distance
343 */
344 static ulonglong distance(const ulonglong& a, const ulonglong& b)
345 {
346 ulonglong value;
347
348 value.u1 = std::max(a.u1,b.u1) - std::min(a.u1,b.u1);
349 value.u2 = std::max(a.u2,b.u2) - std::min(a.u2,b.u2);
350
351 if ((a.u1 < b.u1 && a.u2 > b.u2) ||
352 (a.u1 > b.u1 && a.u2 < b.u2)) {
353 --value.u1;
354 value.u2 = std::numeric_limits<uint>::max() - value.u2 + 1;
355 }
356
357 return value;
358 }
359};
360
361
362inline ulonglong operator&(const ulonglong& a, const ulonglong& b)
363{
364 ulonglong c(a);
365 return c&=b;
366}
367
368inline ulonglong operator|(const ulonglong& a, const ulonglong& b)
369{
370 ulonglong c(a);
371 return c|=b;
372}
373
374inline ulonglong operator^(const ulonglong& a, const ulonglong& b)
375{
376 ulonglong c(a);
377 return c^=b;
378}
379
380
381inline std::ostream& operator<<(std::ostream& out, const ulonglong& a)
382{
383 return a.write(out);
384}
385
386
387inline std::istream& operator>>(std::istream& in, ulonglong& a)
388{
389 return a.read(in);
390}
391
392#endif
393
ulonglong & operator&=(const ulonglong &x)
Definition ulonglong.hh:251
ulonglong & operator=(const double d)
Assignment operator.
Definition ulonglong.hh:90
ulonglong(const int u2_)
Constructor.
Definition ulonglong.hh:42
ulonglong & operator^=(const ulonglong &x)
Definition ulonglong.hh:265
ulonglong & operator--()
prefix decrement
Definition ulonglong.hh:113
bool operator>=(const ulonglong &b) const
Definition ulonglong.hh:306
ulonglong & operator-=(const ulonglong &x)
Definition ulonglong.hh:237
ulonglong(const uint u2_)
Constructor.
Definition ulonglong.hh:51
uint operator[](const int i)
Definition ulonglong.hh:313
ulonglong & operator-=(const uint x)
Definition ulonglong.hh:226
bool operator<=(const ulonglong &b) const
Definition ulonglong.hh:290
uint msw() const
most significant value
Definition ulonglong.hh:321
ulonglong & operator++()
prefix increment
Definition ulonglong.hh:127
std::istream & read(std::istream &in)
Definition ulonglong.hh:332
ulonglong operator-(const int x)
Definition ulonglong.hh:168
ulonglong(const uint u1_, const uint u2_)
Constructor.
Definition ulonglong.hh:33
bool operator<(const ulonglong &b) const
Definition ulonglong.hh:282
ulonglong & operator+=(const int x)
Definition ulonglong.hh:196
ulonglong & operator=(const uint i)
Assignment operator.
Definition ulonglong.hh:80
ulonglong operator-(const ulonglong &b)
Definition ulonglong.hh:182
ulonglong operator++(int)
postfix increment
Definition ulonglong.hh:156
static ulonglong distance(const ulonglong &a, const ulonglong &b)
distance between 2 values
Definition ulonglong.hh:344
ulonglong & operator+=(const ulonglong &x)
Definition ulonglong.hh:244
bool operator==(const ulonglong &b) const
Definition ulonglong.hh:272
ulonglong & operator-=(const int x)
Definition ulonglong.hh:204
ulonglong operator--(int)
postfix decrement
Definition ulonglong.hh:141
ulonglong & operator|=(const ulonglong &x)
Definition ulonglong.hh:258
ulonglong & operator+=(const uint x)
Definition ulonglong.hh:212
ulonglong(const double d)
Constructor.
Definition ulonglong.hh:60
ulonglong & operator=(const int i)
Assignment operator.
Definition ulonglong.hh:70
ulonglong operator+(const ulonglong &b)
Definition ulonglong.hh:189
bool operator!=(const ulonglong &b) const
Definition ulonglong.hh:277
ulonglong()
Default contructor.
Definition ulonglong.hh:23
uint lsw() const
least significant value
Definition ulonglong.hh:322
unsigned int uint
Definition ulonglong.hh:13
ulonglong operator+(const int x)
Definition ulonglong.hh:175
bool operator>(const ulonglong &b) const
Definition ulonglong.hh:298
std::ostream & write(std::ostream &out) const
Definition ulonglong.hh:324
ulonglong operator|(const ulonglong &a, const ulonglong &b)
Definition ulonglong.hh:368
ulonglong operator^(const ulonglong &a, const ulonglong &b)
Definition ulonglong.hh:374
ulonglong operator&(const ulonglong &a, const ulonglong &b)
Definition ulonglong.hh:362
std::istream & operator>>(std::istream &in, ulonglong &a)
Definition ulonglong.hh:387
std::ostream & operator<<(std::ostream &out, const ulonglong &a)
Definition ulonglong.hh:381