CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/CalibTracker/SiPixelConnectivity/interface/TRange.h

Go to the documentation of this file.
00001 #ifndef TRange_H
00002 #define TRange_H
00003 
00006 #include <iostream>
00007 #include <utility>
00008 #include <algorithm>
00009 
00010 
00011 template<class T> class TRange : public std::pair<T,T> {
00012 public:
00013 
00014   TRange() { }
00015 
00016   TRange(const T & aMin, const T & aMax) 
00017       : std::pair<T,T> (aMin,aMax) { }
00018 
00019   TRange(const std::pair<T,T> & apair ) 
00020       : std::pair<T,T> (apair) { }  
00021 
00023   const T & min() const { return this->first; }
00024 
00026   const T & max() const { return this->second; }
00027 
00028   T mean() const { return (this->first+this->second)/2.; }
00029 
00031   bool empty() const { return (this->second < this->first); }
00032 
00034   bool inside(const T & value) const {
00035     if (value < this->first || this->second < value)  return false; 
00036     else  return true;
00037   }
00038 
00039 /*
00041   bool hasIntersection( const TRange<T> & r) const {
00042     return rangesIntersect(*this,r); 
00043   }
00044 
00046   TRange<T> intersection( 
00047       const TRange<T> & r) const {
00048     return rangeIntersection(*this,r); 
00049   }
00050 */
00051 
00053   TRange<T> sum(const TRange<T> & r) const {
00054    if( this->empty()) return r;
00055    else if( r.empty()) return *this;
00056    else return TRange( (min() < r.min()) ? min() : r.min(), 
00057       (max() < r.max()) ? r.max() : max()); 
00058   }
00059 
00060   void sort() { if (empty() ) std::swap(this->first,this->second); }
00061 };
00062 
00063 template <class T> std::ostream & operator<<( 
00064     std::ostream& out, const TRange<T>& r) 
00065 {
00066   return out << "("<<r.min()<<","<<r.max()<<")";
00067 }
00068 #endif