mi32/range.h

Go to the documentation of this file.
00001 /**
00002  * \file range.h <mi32/range.h>
00003  * \brief Definitions for RANGE template
00004  *
00005  * \if NODOC
00006  * $Id: range.h_v 1.9 2004/03/31 18:44:39 mju Exp $
00007  *
00008  * $Log: range.h_v $
00009  * Revision 1.9  2004/03/31 18:44:39  mju
00010  * Add GetSize() method.
00011  *
00012  * Revision 1.8  2003/09/15 13:49:56  fileserver!dwilliss
00013  * Doxygen
00014  *
00015  * Revision 1.7  2003/04/09 19:49:19  dwilliss
00016  * Don't use _T in templates.  Mac's ctype.h defines a global _T. Thanks Apple!
00017  *
00018  * Revision 1.6  2001/10/26 21:46:38  mju
00019  * Add Set, SetMinimum and SetMaximum methods.
00020  *
00021  * Revision 1.5  2001/07/18 19:38:01  mju
00022  * Use 'minval' and 'maxval' instead of 'min' and 'max' as those are #defined as macros by Windows headers.
00023  *
00024  * Revision 1.4  2000/07/07 13:39:57  mju
00025  * Don't use limits.h.
00026  *
00027  * Revision 1.3  2000/06/28 20:27:25  mju
00028  * Use limits.h for Unix.
00029  *
00030  * Revision 1.2  2000/06/26 20:16:49  mju
00031  * Add ctor from separate min/max values.
00032  *
00033  * Revision 1.1  2000/06/26 18:06:09  mju
00034  * Initial revision
00035  *
00036  * \endif
00037 **/
00038 
00039 #ifndef  INC_MI32_RANGE_H
00040 #define  INC_MI32_RANGE_H
00041 
00042 #ifndef  INC_MI32_STDDEFNS_H
00043 #include <mi32/stddefns.h>
00044 #endif
00045 
00046 #ifndef  INC_LIMITS
00047 #include <limits>
00048 #define  INC_LIMITS
00049 #endif
00050 
00051 //! Template for storing numeric range.
00052 //! Designed to work with signed integer and floating-point data types, unsigned types are not supported.
00053 template <class _CT> class RANGE {
00054    public:
00055 
00056       //! Default constructor, initialize to invalid (empty) range.
00057       RANGE (
00058          ) {
00059          SetInvalid();
00060          }
00061 
00062       //! Construct range with specified minimum and maximum values.
00063       RANGE (
00064          _CT minval,
00065          _CT maxval
00066          ): m_min(minval), m_max(maxval)
00067          { }
00068 
00069       //! Construct range to include only specified value.
00070       explicit RANGE (
00071          _CT value
00072          ) : m_min(value), m_max(value)
00073          { }
00074 
00075       //! Equality operator.
00076       bool operator== (
00077          const RANGE& rhs
00078          ) const {
00079          return (m_min == rhs.m_min && m_max == rhs.m_max);
00080          }
00081 
00082       //! Inequality operator.
00083       bool operator!= (
00084          const RANGE& rhs
00085          ) const {
00086          return (!(*this == rhs));
00087          }
00088 
00089       //! Determine if range contains specified value.
00090       bool Contains (
00091          _CT value
00092          ) const {
00093          return (value >= m_min && value <= m_max);
00094          }
00095 
00096       //! Determine if range containes specified range.
00097       bool Contains (
00098          const RANGE& rhs
00099          ) const {
00100          return (rhs.m_min >= m_min && rhs.m_max <= m_max);
00101          }
00102 
00103       //! Expand range by specified amount.
00104       void Expand (
00105          _CT value
00106          ) {
00107          m_min -= value;
00108          m_max += value;
00109          return;
00110          }
00111 
00112       //! Extend range to include specified value.
00113       void Extend (
00114          _CT value
00115          ) {
00116          if (value < m_min) m_min = value;
00117          if (value > m_max) m_max = value;
00118          return;
00119          }
00120 
00121       //! Extend range to include specified range.
00122       void Extend (
00123          _CT minval,
00124          _CT maxval
00125          ) {
00126          if (minval < m_min) m_min = minval;
00127          if (maxval > m_max) m_max = maxval;
00128          return;
00129          }
00130 
00131       //! Extend range to include specified range.
00132       void Extend (
00133          const RANGE& rhs
00134          ) {
00135          if (rhs.m_min < m_min) m_min = rhs.m_min;
00136          if (rhs.m_max > m_max) m_max = rhs.m_max;
00137          return;
00138          }
00139 
00140       //! Get center of range.
00141       _CT GetCenter (
00142          ) const {
00143          return ((m_min + m_max) / 2);
00144          }
00145 
00146       //! Get maximum value in range.
00147       _CT GetMaximum (
00148          ) const {
00149          return (m_max);
00150          }
00151 
00152       //! Get minimum value in range.
00153       _CT GetMinimum (
00154          ) const {
00155          return (m_min);
00156          }
00157 
00158       //! Get 'size' of range.
00159       _CT GetSize (
00160          ) const { return (m_max - m_min); }
00161 
00162       //! Intersect range with another range.
00163       void Intersect (
00164          const RANGE& rhs
00165          ) {
00166          if (rhs.IsValid()) {
00167             if (rhs.m_min > m_min) m_min = rhs.m_min;
00168             if (rhs.m_max < m_max) m_max = rhs.m_max;
00169             }
00170          else {
00171             *this = rhs;
00172             }
00173          return;
00174          }
00175 
00176       //! Determine if range is valid.
00177       bool IsValid (
00178          ) const {
00179          return (m_min <= m_max);
00180          }
00181 
00182       //! Determine if range overlaps another range.
00183       bool Overlaps (
00184          const RANGE& rhs
00185          ) const {
00186          return (!(rhs.m_max < m_min || rhs.m_max > m_min));
00187          }
00188 
00189       //! Set range to be "invalid" (empty).
00190       void SetInvalid (
00191          ) {
00192          m_min = std::numeric_limits<_CT>::max();
00193          m_max = -m_min;
00194          return;
00195          }
00196 
00197       //! Set minimum and maximum values.
00198       void Set (
00199          _CT minval,
00200          _CT maxval
00201          ) { m_min = minval; m_max = maxval; }
00202 
00203       //! Set maximum value.
00204       void SetMaximum (
00205          _CT val
00206          ) { m_max = val; }
00207 
00208       //! Set minimum value.
00209       void SetMinimum (
00210          _CT val
00211          ) { m_min = val; }
00212 
00213    private:
00214       #ifndef GENERATING_DOXYGEN_OUTPUT
00215       _CT m_min;                             //!< Minimum value in range
00216       _CT m_max;                             //!< Maximum value in range
00217       #endif // GENERATING_DOXYGEN_OUTPUT
00218 
00219    };
00220 
00221 typedef RANGE<INT16> INT16_RANGE;
00222 typedef RANGE<INT32> INT32_RANGE;
00223 typedef RANGE<float> FLOAT_RANGE;
00224 typedef RANGE<double> DOUBLE_RANGE;
00225 
00226 #endif   //!< INC_MI32_RANGE_H

Generated on Thu Aug 12 06:19:03 2004 for TNTsdk by doxygen 1.3.4-20031026