casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ExpCache.h
Go to the documentation of this file.
00001 //# ExpCache.cc: Implementation of ExpCache (tabulated exponential) class
00002 //# Copyright (C) 1997,1998,1999,2000,2001,2002,2003
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id$
00027 
00028 #if !defined(EXP_CACHE_H)
00029 #define EXP_CACHE_H
00030 
00031 #include <stdlib.h>
00032 #include <math.h>
00033 
00034 namespace casa
00035 {
00036 template<class T> class ExpCache
00037 {
00038 public:
00039   ExpCache() {EStep=0; ETable=NULL;Size=0;};
00040   ExpCache(int n, T Step) {EStep=Size=0;ETable=NULL;Build(n,Step);};
00041   ~ExpCache() {if (ETable) free(ETable);};
00042 
00043   inline void build(int n, T Step)
00044   {
00045     if (ETable) free(ETable);
00046 
00047     ETable=(T *)malloc(sizeof(T)*n);
00048     Size = n;
00049     EStep = Step;
00050 
00051     for (int i=0;i<n;i++) ETable[i]=exp(-i*Step);
00052   }
00053   inline T operator()(T arg) 
00054   {
00055     int N=(int)(-arg/EStep); 
00056 
00057     //    return (fabs(N)>=Size)?0:((ETable[N]-ETable[N+1])*arg + ETable[N]);
00058 
00059     return (abs(N)>=Size)?0:ETable[N];
00060   }
00061 private:
00062   T EStep;
00063   T *ETable;
00064   int Size;
00065 };
00066 }; 
00067 #endif