Line data Source code
1 : //# ExpCache.cc: Implementation of ExpCache (tabulated exponential) class 2 : //# Copyright (C) 1997,1998,1999,2000,2001,2002,2003 3 : //# Associated Universities, Inc. Washington DC, USA. 4 : //# 5 : //# This library is free software; you can redistribute it and/or modify it 6 : //# under the terms of the GNU Library General Public License as published by 7 : //# the Free Software Foundation; either version 2 of the License, or (at your 8 : //# option) any later version. 9 : //# 10 : //# This library is distributed in the hope that it will be useful, but WITHOUT 11 : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 : //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 13 : //# License for more details. 14 : //# 15 : //# You should have received a copy of the GNU Library General Public License 16 : //# along with this library; if not, write to the Free Software Foundation, 17 : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 18 : //# 19 : //# Correspondence concerning AIPS++ should be addressed as follows: 20 : //# Internet email: casa-feedback@nrao.edu. 21 : //# Postal address: AIPS++ Project Office 22 : //# National Radio Astronomy Observatory 23 : //# 520 Edgemont Road 24 : //# Charlottesville, VA 22903-2475 USA 25 : //# 26 : //# $Id$ 27 : 28 : #if !defined(EXP_CACHE_H) 29 : #define EXP_CACHE_H 30 : 31 : #include <stdlib.h> 32 : #include <math.h> 33 : 34 : namespace casa 35 : { 36 : template<class T> class ExpCache 37 : { 38 : public: 39 28 : ExpCache() {EStep=0; ETable=NULL;Size=0;}; 40 : ExpCache(int n, T Step) {EStep=Size=0;ETable=NULL;Build(n,Step);}; 41 28 : ~ExpCache() {if (ETable) free(ETable);}; 42 : 43 : inline void build(int n, T Step) 44 : { 45 : if (ETable) free(ETable); 46 : 47 : ETable=(T *)malloc(sizeof(T)*n); 48 : Size = n; 49 : EStep = Step; 50 : 51 : for (int i=0;i<n;i++) ETable[i]=exp(-i*Step); 52 : } 53 0 : inline T operator()(T arg) 54 : { 55 0 : int N=(int)(-arg/EStep); 56 : 57 : // return (fabs(N)>=Size)?0:((ETable[N]-ETable[N+1])*arg + ETable[N]); 58 : 59 0 : return (abs(N)>=Size)?0:ETable[N]; 60 : } 61 : private: 62 : T EStep; 63 : T *ETable; 64 : int Size; 65 : }; 66 : }; 67 : #endif