TabFunct.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 #ifndef __TABFUNCT__
00022 #define __TABFUNCT__
00023 
00024 #include "DataTypes.hxx"
00025 #include "Assert.hxx"
00026 #include <vector>
00027 
00028 using std::vector;
00029 
00030 namespace CLAM
00031 {
00043 template <class OriginalFunction> class TabFunct : public OriginalFunction
00044 {
00045 private:
00046         TabFunct() {};
00047 public:
00048         TData operator() (const TData arg) {
00049 
00050                 CLAM_DEBUG_ASSERT(arg>=mLowerBound && arg<=mUpperBound, "Tablulated functor argument out of bound");
00051                 
00052                 int index = int((arg-mLowerBound) / mIncr);
00053 
00054                 CLAM_DEBUG_ASSERT(index<=mTableSize-2, "Bad index calculation");
00055 
00056                 TData x1 = mLowerBound+mIncr*index;
00057                 TData yIncr = mTable[index+1]-mTable[index];
00058 
00059                 return mTable[index] + ((arg-x1) * yIncr) * mInvIncr;
00060         }
00061 
00062 // constructor
00063         TabFunct(const unsigned tableSize, const TData lowerBound, const TData upperBound) :
00064                 mLowerBound(lowerBound),
00065                 mUpperBound(upperBound),
00066                 mTableSize(tableSize)
00067 
00068         {
00069                 CLAM_ASSERT(tableSize>=2, "Tabulating a function with less than 2 points.");
00070                 CLAM_ASSERT(lowerBound < upperBound, "No interval left to calculate values");
00071 
00072                 CalculateTable();
00073         }
00074 
00075 private:
00076         
00077         void CalculateTable()
00078         {
00079                 mTable.reserve(mTableSize);
00080                 
00081                 mIncr = (mUpperBound - mLowerBound) / (mTableSize-1);
00082                 mInvIncr = 1/mIncr;
00083 
00084                 TData arg = mLowerBound;
00085 
00086                 for (int i=0; i<=mTableSize-1; arg+=mIncr, i++)
00087                         mTable[i] = OriginalFunction::operator() (arg);
00088 
00089         }
00090         // internal table
00091         vector<TData>      mTable;
00092         TData              mLowerBound;
00093         TData              mUpperBound;
00094         unsigned           mTableSize;
00095         TData              mIncr, mInvIncr;
00096 
00097 };
00098 
00099 } //namespace
00100 
00101 #endif // TabFunct.hxx
00102 
Generated by  doxygen 1.6.3