Jpp
JRange.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JRANGE__
2 #define __JTOOLS__JRANGE__
3 
4 #include <cmath>
5 #include <functional>
6 
7 #include "JLang/JClass.hh"
8 #include "JLang/JBool.hh"
9 #include "JLang/JEquals.hh"
10 #include "JTools/JPair.hh"
11 #include "JMath/JMath.hh"
12 #include "JMath/JLimits.hh"
13 
14 
15 /**
16  * \file
17  *
18  * Auxiliary class to define a range between two values.
19  * \author mdejong
20  */
21 namespace JTOOLS {}
22 namespace JPP { using namespace JTOOLS; }
23 
24 namespace JTOOLS {
25 
26  using JLANG::JEquals;
27  using JMATH::JMath;
28 
29 
30  /**
31  * Range of values.
32  */
33  template<class T, class JComparator_t = std::less<T> >
34  class JRange :
35  public JPair<T,T>,
36  public JEquals< JRange<T> >,
37  public JMath < JRange<T> >
38  {
39  public:
40 
43 
44 
45  /**
46  * Default constructor.
47  * This range corresponds to the maximal possible range.
48  */
49  JRange() :
50  JPair<T,T>(getMinimum(), getMaximum())
51  {}
52 
53 
54  /**
55  * Constructor.
56  *
57  * \param x lower limit
58  * \param y upper limit
59  */
61  argument_type y) :
62  JPair<T,T>(x, y)
63  {}
64 
65 
66  /**
67  * Constructor.
68  *
69  * \param x lower and upper limit
70  */
72  JPair<T,T>(x, x)
73  {}
74 
75 
76  /**
77  * Constructor.\n
78  * The arguments could be values or iterators.
79  *
80  * \param first first
81  * \param second second
82  */
83  template<class R>
85  JPair<T,T>()
86  {
88  }
89 
90 
91  /**
92  * Constructor.\n
93  * Set range according to minimal and maximal value in input data.
94  *
95  * \param __begin begin of data
96  * \param __end end of data
97  * \param member pointer to data member
98  */
99  template<class iterator_type, class value_type, class result_type>
100  JRange(iterator_type __begin, iterator_type __end, result_type value_type::*member) :
101  JPair<T,T>()
102  {
103  setRange(__begin, __end, member);
104  }
105 
106 
107  /**
108  * Constructor.\n
109  * Set range according to minimal and maximal value in input data.
110  *
111  * \param __begin begin of data
112  * \param __end end of data
113  * \param function pointer to member method
114  */
115  template<class iterator_type, class value_type, class result_type>
116  JRange(iterator_type __begin, iterator_type __end, result_type (value_type::*function)() const) :
117  JPair<T,T>()
118  {
119  setRange(__begin, __end, function);
120  }
121 
122 
123  /**
124  * Get range.
125  *
126  * \return range
127  */
128  const range_type& getRange() const
129  {
130  return static_cast<const range_type&>(*this);
131  }
132 
133 
134  /**
135  * Set range.
136  *
137  * \param range range
138  */
139  void setRange(const range_type& range)
140  {
141  static_cast<range_type&>(*this) = range;
142  }
143 
144 
145  /**
146  * Set lower and upper limit.
147  *
148  * \param x lower limit
149  * \param y upper limit
150  */
152  {
153  this->first = x;
154  this->second = y;
155  }
156 
157 
158  /**
159  * Set range.\n
160  * The arguments could be values or iterators.
161  *
162  * \param first first
163  * \param second second
164  */
165  template<class R>
166  void setRange(R first, R second)
167  {
168  using namespace JLANG;
169 
171  }
172 
173 
174  /**
175  * Set lower and upper limit according to minimal and maximal value in input data, respectively.
176  *
177  * \param __begin begin of data
178  * \param __end end of data
179  * \param member pointer to data member
180  */
181  template<class iterator_type, class value_type, class result_type>
182  void setRange(iterator_type __begin, iterator_type __end, result_type value_type::*member)
183  {
185 
186  for (iterator_type i = __begin; i != __end; ++i) {
187  include((*i).*member);
188  }
189  }
190 
191 
192  /**
193  * Set lower and upper limit according to minimal and maximal value in input data, respectively.
194  *
195  * \param __begin begin of data
196  * \param __end end of data
197  * \param function pointer to member method
198  */
199  template<class iterator_type, class value_type, class result_type>
200  void setRange(iterator_type __begin, iterator_type __end, result_type (value_type::*function)() const)
201  {
203 
204  for (iterator_type i = __begin; i != __end; ++i) {
205  include(((*i).*function)());
206  }
207  }
208 
209 
210  /**
211  * Get lower limit.
212  *
213  * \return lower limit
214  */
215  T getLowerLimit() const
216  {
217  return this->first;
218  }
219 
220 
221  /**
222  * Get upper limit.
223  *
224  * \return upper limit
225  */
226  T getUpperLimit() const
227  {
228  return this->second;
229  }
230 
231 
232  /**
233  * Set lower limit.
234  *
235  * \param x lower limit
236  */
237  void setLowerLimit(argument_type x)
238  {
239  this->first = x;
240  }
241 
242 
243  /**
244  * Set upper limit.
245  *
246  * \param y upper limit
247  */
249  {
250  this->second = y;
251  }
252 
253 
254  /**
255  * Fix lower limit.
256  *
257  * The range is shifted to the given lower limit.
258  *
259  * \param x lower limit
260  */
262  {
263  this->second += x - this->first;
264  this->first = x;
265  }
266 
267 
268  /**
269  * Fix upper limit.
270  *
271  * The range is shifted to the given upper limit.
272  *
273  * \param y upper limit
274  */
276  {
277  this->first += y - this->second;
278  this->second = y;
279  }
280 
281 
282  /**
283  * Equal method.
284  *
285  * \param range range
286  * \result true if this module range equal to given module range; else false
287  */
288  inline bool equals(const range_type& range) const
289  {
290  return (!this->compare(this->getLowerLimit(), range.getLowerLimit()) &&
291  !this->compare(range.getLowerLimit(), this->getLowerLimit()) &&
292  !this->compare(this->getUpperLimit(), range.getUpperLimit()) &&
293  !this->compare(range.getUpperLimit(), this->getUpperLimit()));
294  }
295 
296 
297  /**
298  * Get length (difference between upper and lower limit).
299  *
300  * \return length
301  */
302  T getLength() const
303  {
304  return getUpperLimit() - getLowerLimit();
305  }
306 
307 
308  /**
309  * Check validity of range.
310  *
311  * \return true if lower limit less than or equal to upper limit; else false
312  */
313  bool is_valid() const
314  {
315  return !compare(getUpperLimit(), getLowerLimit());
316  }
317 
318 
319  /**
320  * Test whether value is inside range.
321  *
322  * \param x value
323  * \return true if lower limit <= value <= upper limit; else false
324  */
325  bool operator()(argument_type x) const
326  {
327  return (!compare(x, getLowerLimit()) &&
328  !compare(getUpperLimit(), x));
329  }
330 
331 
332  /**
333  * Constrain value to range.\n
334  * This method returns the original value if it is in this range, else
335  * lower limit if value < lower limit or upper limit if value > upper limit.
336  *
337  * \param x value
338  * \return lower limit <= x <= upper limit
339  */
341  {
342  if (compare(x, getLowerLimit())) { return getLowerLimit(); }
343  if (compare(getUpperLimit(), x)) { return getUpperLimit(); }
344 
345  return x;
346  }
347 
348 
349  /**
350  * Modulo value with respect to range.\n
351  *
352  * \param x value
353  * \return lower limit <= x <= upper limit
354  */
355  T mod(argument_type x) const
356  {
357  if (compare(x, getLowerLimit()))
358  return x + getLength() * floor((getUpperLimit() - x) / getLength());
359  else if (compare(getUpperLimit(), x))
360  return x - getLength() * floor((x - getLowerLimit()) / getLength());
361  else
362  return x;
363  }
364 
365 
366  /**
367  * Test overlap with given range.\n
368  * The result is equivalent to join(range).is_valid().
369  *
370  * \param range range
371  * \return true if there is a non-zero overlap; else false
372  */
373  bool overlap(const range_type& range) const
374  {
375  return (compare(getLowerLimit(), range.getUpperLimit()) &&
376  compare(range.getLowerLimit(), getUpperLimit()));
377  }
378 
379 
380  /**
381  * Include given value to range.\n
382  * The new lower limit is the minimim of the original lower limit and given value and\n
383  * the new upper limit is the maximum of the original upper limit and given value;
384  *
385  * \param x value
386  * \return range
387  */
389  {
390  if (compare(x, getLowerLimit())) { setLowerLimit(x); }
391  if (compare(getUpperLimit(), x)) { setUpperLimit(x); }
392 
393  return *this;
394  }
395 
396 
397  /**
398  * Join ranges.\n
399  * The new lower limit is the maximim of the two lower limits and\n
400  * the new upper limit is the minimum of the two upper limits.\n
401  * This operation results in an equal or smaller range and
402  * may result in an unphysical range (i.e. lower limit > upper limit).
403  *
404  * \param range range
405  */
406  range_type& join(const range_type& range)
407  {
408  if (compare(getLowerLimit(), range.getLowerLimit())) { setLowerLimit(range.getLowerLimit()); }
409  if (compare(range.getUpperLimit(), getUpperLimit())) { setUpperLimit(range.getUpperLimit()); }
410 
411  return *this;
412  }
413 
414 
415  /**
416  * Combine ranges.\n
417  * The new lower limit is the minimim of the two lower limits and\n
418  * the new upper limit is the maximum of the two upper limits.\n
419  * This operation results in an equal or larger range.
420  *
421  * \param range range
422  */
424  {
425  if (compare(range.getLowerLimit(), getLowerLimit())) { setLowerLimit(range.getLowerLimit()); }
426  if (compare(getUpperLimit(), range.getUpperLimit())) { setUpperLimit(range.getUpperLimit()); }
427 
428  return *this;
429  }
430 
431 
432  /**
433  * Add offset.
434  *
435  * \param x offset
436  */
438  {
439  this->first += x;
440  this->second += x;
441 
442  return *this;
443  }
444 
445 
446  /**
447  * Subtract offset.
448  *
449  * \param x offset
450  */
452  {
453  this->first -= x;
454  this->second -= x;
455 
456  return *this;
457  }
458 
459 
460  /**
461  * Add offsets.\n
462  * The new lower limit is the sum of the two lower limits and\n
463  * the new upper limit is the sum of the two upper limits.
464  *
465  * \param range offset
466  */
467  range_type& add(const range_type& range)
468  {
469  this->first += range.getLowerLimit();
470  this->second += range.getUpperLimit();
471 
472  return *this;
473  }
474 
475 
476  /**
477  * Subtract offsets.\n
478  * The new lower limit is the difference of the two lower limits and\n
479  * the new upper limit is the difference of the two upper limits.
480  *
481  * \param range offset
482  */
483  range_type& sub(const range_type& range)
484  {
485  this->first -= range.getLowerLimit();
486  this->second -= range.getUpperLimit();
487 
488  return *this;
489  }
490 
491 
492  /**
493  * Multiply range.
494  *
495  * \param factor factor
496  */
497  range_type& mul(const double factor)
498  {
499  this->first *= factor;
500  this->second *= factor;
501 
502  return *this;
503  }
504 
505 
506  /**
507  * Divide range.
508  *
509  * \param factor factor
510  */
511  range_type& div(const double factor)
512  {
513  this->first /= factor;
514  this->second /= factor;
515 
516  return *this;
517  }
518 
519 
520  /**
521  * Get expected number of occurances of given rate within this interval.
522  *
523  * \param R rate
524  * \return expectation value
525  */
526  T getN(const double R) const
527  {
528  return R * (getUpperLimit() - getLowerLimit());
529  }
530 
531 
532  /**
533  * Get minimum possible value.
534  *
535  * \return minimum possible value
536  */
537  static T getMinimum()
538  {
539  return JMATH::JLimits<T>::min();
540  }
541 
542 
543  /**
544  * Get maximum possible value.
545  *
546  * \return maximum possible value
547  */
548  static T getMaximum()
549  {
550  return JMATH::JLimits<T>::max();
551  }
552 
553 
554  /**
555  * Default range.
556  * This range corresponds to an unphysical range.
557  */
559 
560 
561  /**
562  * Function object.
563  *
564  * \param first first argument
565  * \param second second argument
566  * \return true if first < second; else false
567  */
568  JComparator_t compare;
569 
570  protected:
571  /**
572  * Set range.
573  *
574  * \param first first
575  * \param second second
576  * \param option false
577  */
578  template<class R>
579  void setRange(R first, R second, const JLANG::JBool<false>& option)
580  {
582  }
583 
584 
585  /**
586  * Set range.
587  *
588  * \param first first
589  * \param second second
590  * \param option true
591  */
592  template<class R>
593  void setRange(R first, R second, const JLANG::JBool<true>& option)
594  {
596 
597  for (R i = first; i != second; ++i) {
598  include(*i);
599  }
600  }
601  };
602 
603 
604  /**
605  * Default range.
606  * This range corresponds to an unphysical range.
607  */
608  template<class T, class JComparator_t>
611 
612 
613  /**
614  * Add ranges.\n
615  * The new lower limit is the sum of the two lower limits and\n
616  * the new upper limit is the sum of the two upper limits.
617  *
618  * \param first first range
619  * \param second second range
620  * \result range
621  */
622  template<class T, class JComparator_t>
624  {
626  }
627 
628 
629  /**
630  * Subtract ranges.\n
631  * The new lower limit is the difference of the two lower limits and
632  * the new upper limit is the difference of the two upper limits.
633  *
634  * \param first first range
635  * \param second second range
636  * \result range
637  */
638  template<class T, class JComparator_t>
640  {
642  }
643 
644 
645  /**
646  * Test overlap between ranges.
647  *
648  * \param first first range
649  * \param second second range
650  * \return true if there is a non-zero overlap; else false
651  */
652  template<class T, class JComparator_t>
654  {
655  return first.overlap(second);
656  }
657 
658 
659  /**
660  * Join ranges.\n
661  * The new lower limit is the maximim of the two lower limits and\n
662  * the new upper limit is the minimum of the two upper limits.\n
663  * This operation results in an equal or smaller range and
664  * may result in an unphysical range (i.e. lower limit > upper limit).
665  *
666  * \param first first range
667  * \param second second range
668  * \result range
669  */
670  template<class T, class JComparator_t>
672  {
674  }
675 
676 
677  /**
678  * Combine ranges.\n
679  * The new lower limit is the minimim of the two lower limits and\n
680  * the new upper limit is the maximum of the two upper limits.\n
681  * This operation results in an equal or larger range.
682  *
683  * \param first first range
684  * \param second second range
685  * \result range
686  */
687  template<class T, class JComparator_t>
689  {
691  }
692 
693 
694  /**
695  * Auxiliary method to create range of values.
696  *
697  * \param x lower limit
698  * \param y upper limit
699  * \return range
700  */
701  template<class T>
702  inline JRange<T> make_range(T x, T y)
703  {
704  return JRange<T>(x,y);
705  }
706 }
707 
708 #endif
JTOOLS::JRange::getLowerLimit
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:215
JTOOLS::JRange::include
range_type include(argument_type x)
Include given value to range.
Definition: JRange.hh:388
JTOOLS::JRange::getUpperLimit
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:226
operator-
Vec operator-(const Vec &a, const Vec &b)
Subtract two vectors.
Definition: Vec.hh:346
JBool.hh
JLimits.hh
JTOOLS::JRange::getRange
const range_type & getRange() const
Get range.
Definition: JRange.hh:128
JTOOLS::JRange::fixUpperLimit
void fixUpperLimit(argument_type y)
Fix upper limit.
Definition: JRange.hh:275
JTOOLS::JRange::setRange
void setRange(R first, R second, const JLANG::JBool< false > &option)
Set range.
Definition: JRange.hh:579
JTOOLS::JPair< T, T >::first
T first
Definition: JPair.hh:128
JLANG::JEquals
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:24
JLANG::is_iterator
Data structure to check whether given data type is an iterator.
Definition: JClass.hh:62
JTOOLS::JRange::setRange
void setRange(const range_type &range)
Set range.
Definition: JRange.hh:139
JTOOLS::JRange::add
range_type & add(argument_type x)
Add offset.
Definition: JRange.hh:437
JTOOLS::JRange::getLength
T getLength() const
Get length (difference between upper and lower limit).
Definition: JRange.hh:302
JTOOLS::JRange::setRange
void setRange(R first, R second, const JLANG::JBool< true > &option)
Set range.
Definition: JRange.hh:593
JLANG::JClass::argument_type
JArgument< T >::argument_type argument_type
Definition: JClass.hh:82
JTOOLS::JRange
Range of values.
Definition: JRange.hh:34
JTOOLS::make_range
JRange< T > make_range(T x, T y)
Auxiliary method to create range of values.
Definition: JRange.hh:702
JTOOLS::JRange::DEFAULT_RANGE
static const JRange< T, JComparator_t > DEFAULT_RANGE
Default range.
Definition: JRange.hh:558
JTOOLS::JRange::mod
T mod(argument_type x) const
Modulo value with respect to range.
Definition: JRange.hh:355
JTOOLS::JRange::mul
range_type & mul(const double factor)
Multiply range.
Definition: JRange.hh:497
JTOOLS::JRange::combine
range_type & combine(const range_type &range)
Combine ranges.
Definition: JRange.hh:423
JMATH::JMath
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:26
JEquals.hh
JTOOLS::JRange::add
range_type & add(const range_type &range)
Add offsets.
Definition: JRange.hh:467
JTOOLS::JRange::getMinimum
static T getMinimum()
Get minimum possible value.
Definition: JRange.hh:537
JTOOLS::JRange::setLowerLimit
void setLowerLimit(argument_type x)
Set lower limit.
Definition: JRange.hh:237
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JTOOLS::JRange::constrain
T constrain(argument_type x) const
Constrain value to range.
Definition: JRange.hh:340
JTOOLS::JRange::getMaximum
static T getMaximum()
Get maximum possible value.
Definition: JRange.hh:548
JTOOLS::JRange::join
range_type & join(const range_type &range)
Join ranges.
Definition: JRange.hh:406
JTOOLS::JRange::JRange
JRange(argument_type x)
Constructor.
Definition: JRange.hh:71
JTOOLS::JRange::fixLowerLimit
void fixLowerLimit(argument_type x)
Fix lower limit.
Definition: JRange.hh:261
JTOOLS::JRange::JRange
JRange(iterator_type __begin, iterator_type __end, result_type(value_type::*function)() const)
Constructor.
Definition: JRange.hh:116
JTOOLS::JRange::operator()
bool operator()(argument_type x) const
Test whether value is inside range.
Definition: JRange.hh:325
JPair.hh
JTOOLS::JRange::setUpperLimit
void setUpperLimit(argument_type y)
Set upper limit.
Definition: JRange.hh:248
JTOOLS::JRange::setRange
void setRange(R first, R second)
Set range.
Definition: JRange.hh:166
JMATH::JLimits::max
TTimeStamp max()
Get maximum possible value.
Definition: JMathSupportkit.hh:33
JTOOLS::JRange::compare
JComparator_t compare
Function object.
Definition: JRange.hh:568
JLANG::JBool
Auxiliary template class for type bool.
Definition: JBool.hh:20
JTOOLS::JRange::is_valid
bool is_valid() const
Check validity of range.
Definition: JRange.hh:313
JTOOLS::JRange::equals
bool equals(const range_type &range) const
Equal method.
Definition: JRange.hh:288
JMath.hh
JTOOLS::JRange::setRange
void setRange(argument_type x, argument_type y)
Set lower and upper limit.
Definition: JRange.hh:151
JTOOLS::JRange::overlap
bool overlap(const range_type &range) const
Test overlap with given range.
Definition: JRange.hh:373
JTOOLS::JRange::JRange
JRange(R first, R second)
Constructor.
Definition: JRange.hh:84
JTOOLS::JRange::argument_type
JLANG::JClass< T >::argument_type argument_type
Definition: JRange.hh:42
JTOOLS::JRange::getN
T getN(const double R) const
Get expected number of occurances of given rate within this interval.
Definition: JRange.hh:526
JTOOLS::JRange::range_type
JRange< T, JComparator_t > range_type
Definition: JRange.hh:41
JTOOLS::JRange::JRange
JRange(argument_type x, argument_type y)
Constructor.
Definition: JRange.hh:60
operator+
Vec operator+(const Vec &a, const Vec &b)
Add two vectors.
Definition: Vec.hh:337
JClass.hh
JTOOLS::JRange::setRange
void setRange(iterator_type __begin, iterator_type __end, result_type(value_type::*function)() const)
Set lower and upper limit according to minimal and maximal value in input data, respectively.
Definition: JRange.hh:200
JTOOLS::JRange::JRange
JRange()
Default constructor.
Definition: JRange.hh:49
JTOOLS::JPair
Template specialisation for a pair of values.
Definition: JPair.hh:28
JTOOLS::JRange::div
range_type & div(const double factor)
Divide range.
Definition: JRange.hh:511
JTOOLS
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
Definition: JAbstractCollection.hh:9
JTOOLS::JPair< T, T >::second
T second
Definition: JPair.hh:129
JLANG
Auxiliary classes and methods for language specific functionality.
Definition: JAbstractClass.hh:10
JTOOLS::JRange::sub
range_type & sub(argument_type x)
Subtract offset.
Definition: JRange.hh:451
JTOOLS::JRange::setRange
void setRange(iterator_type __begin, iterator_type __end, result_type value_type::*member)
Set lower and upper limit according to minimal and maximal value in input data, respectively.
Definition: JRange.hh:182
JTOOLS::JRange::sub
range_type & sub(const range_type &range)
Subtract offsets.
Definition: JRange.hh:483
JMATH::JLimits::min
float min()
Get minimum possible value.
Definition: JLimits.hh:96
JTOOLS::JRange::JRange
JRange(iterator_type __begin, iterator_type __end, result_type value_type::*member)
Constructor.
Definition: JRange.hh:100