Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Tools/Range.h

Go to the documentation of this file.
00001 /**
00002  * @file Range.h
00003  *
00004  * The file defines a template class to represent ranges.
00005  * 
00006  * @author <A href=mailto:roefer@tzi.de>Thomas Röfer</A>
00007  */
00008 
00009 #ifndef __RANGE_H__
00010 #define __RANGE_H__
00011 
00012 /**
00013  * A template class to represent ranges. It also defines the 13 Allen relations
00014  */
00015 template <class T> class Range
00016 {
00017   public:
00018     T min,max;                    /**< The limits of the range. */
00019 
00020     /**
00021      * Constructor.
00022      * Defines an empty range.
00023      */
00024     Range() {min = max = 0;}
00025 
00026     /**
00027      * Constructor.
00028      * @param min The minimum of the range.
00029      * @param max The maximum of the range.
00030      */
00031     Range(T min,T max)
00032       {Range::min = min; Range::max = max;}
00033 
00034     /**
00035      * The function enlarges the range so that a certain value will be part of it.
00036      * @param t The value that will be part of the range.
00037      * @return A reference to the range.
00038      */
00039     Range<T>& add(T t)
00040     {
00041       if(min > t)
00042         min = t;
00043       if(max < t)
00044         max = t;
00045       return *this;
00046     }
00047 
00048     /**
00049      * The function enlarges the range so that the resulting range also contains another one.
00050      * @param r The range that also will be part of the range.
00051      * @return A reference to the range.
00052      */
00053     Range<T>& add(const Range<T>& r)
00054     {
00055       add(r.min);
00056       add(r.max);
00057       return *this;
00058     }
00059 
00060     /**
00061      * The function checks whether a certain value is in the range.
00062      * Note that the function is able to handle circular range, i.e. max < min.
00063      * @param t The value.
00064      * @return Is the value inside the range?
00065      */
00066     bool isInside(T t) const
00067       {return min <= max ? t >= min && t <= max : t >= min || t <= max;}
00068 
00069     /**
00070      * The function limits a certain value to the range.
00071      * Note that the function is not able to handle circular range, i.e. max < min.
00072      * @param t The value that will be "clipped" to the range.
00073      * @return The limited value.
00074      */
00075     T limit(T t) const {return t < min ? min : t > max ? max : t;} //sets a limit for a Range
00076 
00077     /**
00078      * The function limits another range to this range.
00079      * Note that the function is able to handle circular range, i.e. max < min.
00080      * @param r The range that will be "clipped" to this range.
00081      * @return The limited value.
00082      */
00083     Range<T> limit(const Range<T>& r) const {return Range<T>(limit(r.min),limit(r.max));} //sets the limit of a Range
00084 
00085     /**
00086      * The function returns the size of the range.
00087      * @return The difference between the lower limit and the higher limit.
00088      */
00089     T getSize() const {return max - min;}
00090 
00091     /**
00092      * The function returns the center of the range.
00093      * @return The center.
00094      */
00095     T getCenter() const {return (max + min) / 2;}
00096 
00097   //!@name The 13 Allen relations
00098   //!@{
00099     bool operator==(const Range<T>& r) const {return min == r.min && max == r.max;}
00100     bool operator<(const Range<T>& r) const {return max < r.min;}
00101     bool operator>(const Range<T>& r) const {return min > r.max;}
00102     bool meets(const Range<T>& r) const {return max == r.min;}
00103     bool metBy(const Range<T>& r) const {return min == r.max;}
00104     bool overlaps(const Range<T>& r) const {return min < r.min && max < r.max && max > r.min;}
00105     bool overlappedBy(const Range<T>& r) const {return min > r.min && max > r.max && min < r.max;}
00106     bool starts(const Range<T>& r) const {return min == r.min && max < r.max;}
00107     bool startedBy(const Range<T>& r) const {return min == r.min && max > r.max;}
00108     bool finishes(const Range<T>& r) const {return max == r.max && min > r.min;}
00109     bool finishedBy(const Range<T>& r) const {return max == r.max && min < r.min;}
00110     bool during(const Range<T>& r) const {return min > r.min && max < r.max;}
00111     bool contains(const Range<T>& r) const {return min < r.min && max > r.max;}
00112   //!@}
00113 };
00114 
00115 #endif // __RANGE_H__

Generated on Mon Mar 20 22:00:09 2006 for GT2005 by doxygen 1.3.6