Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:59:31

0001 #ifndef TRange_H
0002 #define TRange_H
0003 
0004 /** Define a range [aMin,aMax] */
0005 
0006 #include <iostream>
0007 #include <utility>
0008 #include <algorithm>
0009 
0010 template <class T>
0011 class TRange : public std::pair<T, T> {
0012 public:
0013   TRange() {}
0014 
0015   TRange(const T& aMin, const T& aMax) : std::pair<T, T>(aMin, aMax) {}
0016 
0017   TRange(const std::pair<T, T>& apair) : std::pair<T, T>(apair) {}
0018 
0019   /// lower edge of range
0020   const T& min() const { return this->first; }
0021 
0022   /// upper edge of range
0023   const T& max() const { return this->second; }
0024 
0025   T mean() const { return (this->first + this->second) / 2.; }
0026 
0027   /// true for empty region. note that region [x,x] is not empty
0028   bool empty() const { return (this->second < this->first); }
0029 
0030   /// check if object is inside region
0031   bool inside(const T& value) const {
0032     if (value < this->first || this->second < value)
0033       return false;
0034     else
0035       return true;
0036   }
0037 
0038   /*
0039   /// check if ranges overlap
0040   bool hasIntersection( const TRange<T> & r) const {
0041     return rangesIntersect(*this,r); 
0042   }
0043 
0044   /// overlaping region
0045   TRange<T> intersection( 
0046       const TRange<T> & r) const {
0047     return rangeIntersection(*this,r); 
0048   }
0049 */
0050 
0051   /// sum of overlaping regions. the overlapping should be checked before.
0052   TRange<T> sum(const TRange<T>& r) const {
0053     if (this->empty())
0054       return r;
0055     else if (r.empty())
0056       return *this;
0057     else
0058       return TRange((min() < r.min()) ? min() : r.min(), (max() < r.max()) ? r.max() : max());
0059   }
0060 
0061   void sort() {
0062     if (empty())
0063       std::swap(this->first, this->second);
0064   }
0065 };
0066 
0067 template <class T>
0068 std::ostream& operator<<(std::ostream& out, const TRange<T>& r) {
0069   return out << "(" << r.min() << "," << r.max() << ")";
0070 }
0071 #endif