/****************************************************************************** File: LUT.H Author: Carlos Moreno Date: March 1999 Description: Template class definitions for Look-Up Table objects (LUT_number to encapsulate LUT'd floating-point multiplications, and LUT_function to encapsulate LUT'd functions evaluation) ******************************************************************************/ #ifndef __LUT_H__ #define __LUT_H__ /***************************************************************************** LUT_number definition Template parameters: TResult: type parameter indicating the data type of the result of the multiplication LBound: Lower bound for the integer operand (multiplier) UBound: Upper bound for the integer operand (multiplier) ScaleFactor: The result of the multiplication is scaled by this factor (in case you want to provide the result as an integer, you can reduce the effect of the rounding error by specifying a high scale factor) ******************************************************************************/ template <int LBound = -128, int UBound = 127, class TResult = double, int ScaleFactor = 1> class LUT_number { public: explicit LUT_number (double val = 0) { lut = lut_array - LBound; for (int i = LBound; i <= UBound; i++) { lut[i] = i * ScaleFactor * val; } } const TResult & operator* (int val) const { return lut[val]; } private: TResult lut_array[UBound - LBound + 1]; TResult * lut; }; template <int LBound, int UBound, class TResult, int ScaleFactor> inline const TResult & operator* (int val, const LUT_number<LBound, UBound, TResult, ScaleFactor> & coeff) { return coeff * val; } /******************************************************************************* LUT_function definition Template parameters: TResult: Type parameter indicating the data type of the return value of the function LBound: Lower bound for the integer argument UBound: Upper bound for the integer argument TArg: Type parameter indicating the data type of the parameter (argument) of the function used to "initialize" the LUT ******************************************************************************/ template <int LBound = -128, int UBound = 127, class TResult = double, class TArg = double> class LUT_function { public: explicit LUT_function (TResult (*f) (TArg), double coeff = 1) { lut = lut_array - LBound; for (int i = LBound; i <= UBound; i++) { lut[i] = f(coeff * i); } } const TResult & operator()(int i) const { return lut[i]; } private: TResult lut_array[UBound - LBound + 1]; TResult * lut; }; #endif

Return to article