/****************************************************************************** from url: http://www.mochima.com/articles/LUT/LUT.html 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 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 inline const TResult & operator* (int val, const LUT_number & 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 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 { if (iLBound) { return lut[i]; } else return lut[LBound]; } else return lut[UBound]; } private: TResult lut_array[UBound - LBound + 1]; TResult * lut; }; #endif