Anaphe Home Page Reference Documentation

Main Page     Namespaces     Classes     Source Code    

Even_Partition.cpp

Go to the documentation of this file.
00001 #include "HTL/Even_Partition.h"
00002 
00003 
00004 //
00005 // Let's get lazy and use the preprocessor:
00006 //
00007 //#define TEMPLATE___   template< class T_Point >
00008 //#define CLASS___      Even_Partition< T_Point >
00009 #define TEMPLATE___     
00010 #define CLASS___        Even_Partition
00011 #define T_Point         double
00012 
00013 
00014 TEMPLATE___
00015 CLASS___::Even_Partition( Size a_bin_count,
00016         T_Point a_leftmost_point, T_Point a_rightmost_point, 
00017         End_Point_Convention a_convention ) 
00018 #ifdef HTL_USE_EXCEPTIONS
00019 throw (HTLExceptions::WrongLimits,HTLExceptions::WrongBins)
00020 #endif //HTL_USE_EXCEPTIONS
00021         : I_Partition()
00022 {
00023   if (a_leftmost_point < a_rightmost_point) {
00024     if   (a_bin_count > 0) {
00025       count_ = a_bin_count;
00026       lower_point_ = a_leftmost_point;
00027       upper_point_ = a_rightmost_point;
00028       bin_width_ = (upper_point_ - lower_point_) / count_;
00029       end_point_convention_ = a_convention;
00030       //ENSURE( (bin_width_ > 0) && (count_ > 0) );
00031       if (!( (bin_width_ > 0) && (count_ > 0) ))
00032         HTL_ERR("(bin_width_ > 0) && (count_ > 0)");
00033     } else {
00034       HTL_ERR("The number of bins is not greater than 0 ");
00035 #ifdef HTL_USE_EXCEPTIONS
00036       throw HTLExceptions::WrongBins();      
00037 #endif //HTL_USE_EXCEPTIONS
00038     }
00039   } else {
00040     HTL_ERR("The histogram low limit is greater than the high limit");
00041 #ifdef HTL_USE_EXCEPTIONS
00042     throw HTLExceptions::WrongLimits();
00043 #endif //HTL_USE_EXCEPTIONS
00044   }
00045 }
00046 
00047 
00048 TEMPLATE___
00049 CLASS___::Even_Partition( I_Partition &another ) 
00050 #ifdef HTL_USE_EXCEPTIONS
00051 throw (HTLExceptions::IncompatiblePartition)
00052 #endif //HTL_USE_EXCEPTIONS
00053         : count_( another.bin_count() )
00054         , lower_point_( T_Point( another.i_lower_point() ) )
00055         , upper_point_( T_Point( another.i_upper_point() ) )
00056         , end_point_convention_( another.end_point_convention() )
00057         // , bin_width_( T_Point( another.i_bin_width(0) ) )
00058 {
00059   bin_width_ = (upper_point_ - lower_point_) / count_;
00060 
00061   // Let's check if the actual type of `another' is Even_Partition or
00062   // *Uneven_Partition. To do this, we just compare all of the bins
00063   // widths to the would-be bin width `bin_width_':
00064   //
00065   for( Index i=1; i<count_; i++ ) {
00066     if (!(bin_width_ == T_Point( another.i_bin_width(i)))) {
00067       HTL_ERR("*Uneven_Partition arg can't match an Even_Partition");
00068 #ifdef HTL_USE_EXCEPTIONS
00069       throw HTLExceptions::IncompatiblePartition();
00070 #endif //HTL_USE_EXCEPTIONS
00071     }
00072   }
00073 }
00074 
00075 
00076 /*TEMPLATE___
00077 T_Point CLASS___::lower_point( Index i ) const
00078 {
00079         REQUIRE( (i >= 0) && (i < count_) );
00080         return( lower_point() + i*bin_width(i) );
00081 }
00082 
00083 TEMPLATE___
00084 T_Point CLASS___::upper_point( Index i ) const
00085 {
00086         REQUIRE( (i >= 0) && (i < count_) );
00087         return( lower_point() + (i+1)*bin_width(i) );
00088 }
00089 */
00090 
00091 
00092 TEMPLATE___
00093 void CLASS___::map_index1( T_Point a_point, Index &an_index, 
00094         Extra_Index &an_extra_index )
00095 {
00096         // Note: the returned value of `an_index' must not be relied on 
00097         // when `a_mapped_index' is out of range.
00098 
00099         // Update extra index:
00100         //
00101         // 1. Simple case:
00102         //      a_point not in [lower_point; upper_point]
00103         //
00104         if( a_point < lower_point() ) {
00105                 // Simple case: a_point < lower_point:
00106                 an_extra_index = H_UNDERFLOW;
00107                 return;
00108 
00109         } else if( a_point > upper_point() ) {
00110                 // Simple case: a_point > upper_point:
00111                 an_extra_index = H_OVERFLOW;
00112                 return;
00113         }
00114 
00115         //ENSURE( a_point >= lower_point() && a_point <= upper_point() );
00116         if (!( a_point >= lower_point() && a_point <= upper_point() )) {
00117           HTL_ERR("a_point >= lower_point() && a_point <= upper_point()");
00118           return;
00119         }
00120 
00121 
00122         // From now on, the point `a_point' MAY be in range.
00123         // We are not this sure because of end point convention.
00124         //
00125         // Find the in range index:
00126         //
00127         an_index = Index( (a_point - lower_point()) / bin_width_ );
00128         //ENSURE( an_index >= 0 && an_index <= count_ );
00129         if (!( an_index >= 0 && an_index <= count_ )) {
00130           HTL_ERR("( an_index >= 0 && an_index <= count_ )");
00131           return;
00132         }
00133 
00134         // 2. Now check whether the point `a_point' maps existing boundaries,
00135         // i.e. check whether `a_point' equals lower_point(i), i=0..count_.
00136         // If it does, then apply end-point convention:
00137         //      . RIGHT_OPEN [a;b[: nothing to do.
00138         //      . LEFT_OPEN  ]a;b]: correct index is an_index-1.
00139         //
00140         if( end_point_convention_ == LEFT_OPEN ) {
00141                 if(     (upper_point() == a_point) ||
00142                         (lower_point(an_index) == a_point) )
00143                 {
00144                         // The 1st test is NECESSARY for the rightmost point.
00145                         // It tests the case when: a_point == rightmost_point =>
00146                         // rightmost_point then belongs to the last in-range bin
00147                         // <=> an_index -- == bin_count()-1
00148                         an_index --;
00149                 }
00150         }
00151 
00152         if( an_index < 0 ) {
00153                 // Case when: a_point == leftmost_point =>
00154                 // lower_point then belongs to the H_UNDERFLOW bin:
00155                 an_extra_index = H_UNDERFLOW;
00156                 return;
00157 
00158         } else if( an_index >= bin_count() ) {
00159                 // Case when: a_point == rightmost_point and the end-point
00160                 // convention is RIGHT_OPEN => rightmost_point then belongs
00161                 // to the H_OVERFLOW bin:
00162                 an_extra_index = H_OVERFLOW;
00163                 return;
00164         }
00165 
00166         // Now we are sure that the mapped index is in range:
00167         //
00168         an_extra_index = H_IN_RANGE;
00169 
00170         //ENSURE( (an_index >= 0) && (an_index < count_) );
00171         if (! (an_index >= 0) && (an_index < count_))
00172           HTL_ERR("(an_index >= 0) && (an_index < count_)");
00173         // Valid in-range bin index.
00174 }
00175 
00176 
00177 
00178 //
00179 // Thanks preprocessor! Now unuse it:
00180 //
00181 #undef double
00182 #undef CLASS___
00183 #undef TEMPLATE___
00184 
00185 


Anaphe documentation generated by Doxygen (www.doxygen.org)