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