Generated on Mon Jun 23 16:24:56 2008 for BIU-1.7.0 by doxygen 1.5.1

src/biu/DistanceEnergyFunction.cc

Go to the documentation of this file.
00001 #include "biu/DistanceEnergyFunction.hh"
00002 
00003 namespace biu
00004 {
00005     
00006     DistanceEnergyFunction::~DistanceEnergyFunction()
00007     {
00008     }
00009 
00010 } // biu
00011 
00012 
00013 #include <algorithm>
00014 
00015 namespace biu
00016 {
00017 
00018     IntervalEnergyFunction::IntervalEnergyFunction( const Alphabet* const alph_)
00019      :  biu::DistanceEnergyFunction(), 
00020         alphabet(alph_),
00021         energyMat(),
00022         intervalMax()
00023     {
00024         
00025     }
00026     
00027     IntervalEnergyFunction::IntervalEnergyFunction( const biu::IntervalEnergyFunction & toCopy)
00028      :  biu::DistanceEnergyFunction(),
00029         alphabet(toCopy.alphabet),
00030         energyMat(),
00031         intervalMax(toCopy.intervalMax)
00032     {
00033         for (size_t i=0; i<toCopy.energyMat.size(); i++) {
00034             energyMat.push_back(new biu::EnergyMatrix(*(toCopy.energyMat[i])));
00035         }
00036         assertbiu(energyMat.size() == intervalMat.size(),
00037                 "different numbers of energy tables and interval boundaries");
00038     }
00039 
00040     
00041     IntervalEnergyFunction::~IntervalEnergyFunction()
00042     {
00043          // remove heap data
00044         for (size_t i=0; i<energyMat.size(); i++) {
00045             delete energyMat[i];
00046         }
00047          // clear vectors
00048         energyMat.clear();
00049         intervalMax.clear();
00050     }
00051     
00052     const Alphabet* const
00053     IntervalEnergyFunction::getAlphabet() const 
00054     {
00055         return alphabet;
00056     }
00057     
00058     double 
00059     IntervalEnergyFunction::getEnergy(  const Alphabet::AlphElem& first, 
00060                                         const Alphabet::AlphElem& second,
00061                                         const double distance ) const 
00062     {
00063         assertbiu( distance < *(intervalMax.rbegin()), "given distance is out of interval bounds");
00064     
00065         return (*energyMat[getInterval(distance)])[alphabet->getIndex(first)][alphabet->getIndex(second)];;
00066     }
00067 
00068     bool 
00069     IntervalEnergyFunction::operator == (const DistanceEnergyFunction& ef2) const
00070     {
00071         if (&ef2 == this)
00072             return true;
00073         
00074         bool retVal = false;
00075         
00076         const IntervalEnergyFunction* ief = dynamic_cast<const IntervalEnergyFunction*>(&ef2);
00077         if (ief != NULL) {
00078             retVal = ief->intervalMax.size() == this->intervalMax.size()
00079                         && ief->energyMat.size() == this->energyMat.size();
00080               // compare all intervals in worst case
00081             for (size_t i=0; retVal && i<intervalMax.size(); i++) {
00082                 retVal =    ief->intervalMax[i] == this->intervalMax[i]
00083                          && *(ief->energyMat[i]) == *(this->energyMat[i]);
00084             }
00085         }
00086         
00087         return retVal;
00088     }
00089     
00090     bool 
00091     IntervalEnergyFunction::operator != (const DistanceEnergyFunction& ef2) const
00092     {
00093         return !(this->operator ==(ef2));
00094     }
00095 
00096     size_t
00097     IntervalEnergyFunction::addInterval(    const biu::EnergyMatrix& energies,
00098                                             const double upperBound )
00099     {
00100         assertbiu( intervalMax.size() == 0 || upperBound > *(intervalMax.rbegin()),
00101                     "given upperBound is NOT greater than the last in use");
00102         assertbiu(  alphabet->getAlphabetSize() == energies.numRows() 
00103                     && alphabet->getAlphabetSize() == energies.numColumns(),
00104                     "energy matrix has the wrong size for this alphabet size");
00105         
00106           // add energy matrix (copy)
00107         energyMat.push_back(new biu::EnergyMatrix(energies));
00108           // add upper bound of the interval
00109         intervalMax.push_back(upperBound);
00110         
00111         return intervalMax.size()-1;
00112     }
00113 
00114     size_t
00115     IntervalEnergyFunction::getIntervalNum(void) const {
00116         return intervalMax.size();
00117     }
00118     
00119     double
00120     IntervalEnergyFunction::getIntervalMax(const size_t index) const {
00121         assertbiu(index < intervalMax.size(), "index out of bound");
00122         return intervalMax[index];
00123     }
00124 
00125     const biu::EnergyMatrix* const
00126     IntervalEnergyFunction::getIntervalMatrix(const size_t index) const {
00127         assertbiu(index < intervalMax.size(), "index out of bound");
00128         return energyMat[index];
00129     }
00130 
00131     size_t
00132     IntervalEnergyFunction::getInterval( double distance) const {
00133         if (intervalMax.size() == 0 || distance > *(intervalMax.rbegin())) {
00134             return UINT_MAX;
00135         }
00136           // find the corresponding index
00137         size_t index = 0;
00138         while (distance > intervalMax[index]) {
00139             index++;
00140         }
00141         return index;
00142     }
00143 
00144 
00145 } // biu