00001 #ifndef REGION_H 00002 #define REGION_H 00003 00004 #include <algorithm> 00005 00006 namespace storage 00007 { 00008 00009 class Region 00010 { 00011 public: 00012 Region() : s(0), l(0) {}; 00013 Region( unsigned long start, unsigned long len ) : s(start), l(len) {}; 00014 Region( const Region& x ) 00015 { *this = x; } 00016 Region& operator=(const Region& r) 00017 { s = r.start(); l = r.len(); return( *this ); } 00018 bool doIntersect( const Region& r ) const 00019 { return( r.start() <= end() && r.end() >= start() ); } 00020 Region intersect( const Region& r ) const 00021 { 00022 Region ret; 00023 if( r.start() <= end() && r.end() >= start() ) 00024 { 00025 unsigned long s = std::max( r.start(), start() ); 00026 unsigned long e = std::min( r.end(), end() ); 00027 ret = Region( s, e-s+1 ); 00028 } 00029 return( ret ); 00030 } 00031 bool inside( const Region& r ) const 00032 { return( start()>=r.start() && end() <= r.end() ); } 00033 bool operator==(const Region& r) const 00034 { return( r.start()==s && r.len()==l ); } 00035 bool operator!=(const Region& r) const 00036 { return( ! (*this==r) ); } 00037 bool operator<(const Region& r) const 00038 { return( s < r.start() ); } 00039 bool operator>(const Region& r) const 00040 { return( s > r.start() ); } 00041 unsigned long start() const { return( s ); } 00042 unsigned long end() const { return( s+l-1 ); } 00043 unsigned long len() const { return( l ); } 00044 protected: 00045 unsigned long s; 00046 unsigned long l; 00047 }; 00048 00049 inline std::ostream& operator<< (std::ostream& s, const Region &p ) 00050 { 00051 s << "[" << p.start() << "," << p.len() << "]"; 00052 return( s ); 00053 } 00054 00055 inline std::istream& operator>> (std::istream& s, Region &p ) 00056 { 00057 unsigned long start, len; 00058 s >> start >> len; 00059 p = Region( start, len ); 00060 return( s ); 00061 } 00062 00063 } 00064 00065 #endif