Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

cached_function.h

Go to the documentation of this file.
00001 // -*- mode: c++; -*-
00002 //
00003 //The Biomolecule Toolkit (BTK) is a C++ library for use in the
00004 //modeling, analysis, and design of biological macromolecules.
00005 //Copyright (C) 2004, Christopher Saunders <ctsa@users.sourceforge.net>
00006 //
00007 //This program is free software; you can redistribute it and/or modify
00008 //it under the terms of the GNU Lesser General Public License as published
00009 //by the Free Software Foundation; either version 2.1 of the License, or (at
00010 //your option) any later version.
00011 //
00012 //This program is distributed in the hope that it will be useful,  but
00013 //WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 //Lesser General Public License for more details.
00016 //
00017 //You should have received a copy of the GNU Lesser General Public License
00018 //along with this program; if not, write to the Free Software Foundation,
00019 //Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021 
00027 #ifndef BTK_CACHED_FUNCTION_H
00028 #define BTK_CACHED_FUNCTION_H
00029 
00030 #include <functional>
00031 
00032 
00033 namespace BTK {
00034 
00035 
00046 template <typename T, typename ArgType>
00047 struct CachedUnaryFunction : public std::unary_function<T,ArgType> {
00048 
00057   template <typename UnaryFunction>
00058   CachedUnaryFunction(UnaryFunction& func,
00059                       ArgType min_arg,
00060                       ArgType max_arg,
00061                       ArgType sample_spacing);
00062 
00063   ~CachedUnaryFunction() { delete [] _func_val; }
00064 
00068   T operator()(ArgType arg);
00069 
00070 
00071 private:
00072   ArgType _min_arg;
00073   ArgType _max_arg;
00074   ArgType _sample_spacing;
00075   T* _func_val;
00076 };
00077 
00078 
00079 } // Namespace BTK
00080 
00081 
00082 
00083 
00085 
00086 
00087 #include <cassert>
00088 
00089 
00090 template <typename T, typename ArgType>
00091 template <typename UnaryFunction>
00092 BTK::CachedUnaryFunction<T,ArgType>::
00093 CachedUnaryFunction(UnaryFunction& func,
00094                     ArgType min_arg,
00095                     ArgType max_arg,
00096                     ArgType sample_spacing)
00097     : _min_arg(min_arg), _max_arg(max_arg), _sample_spacing(sample_spacing)  {
00098   if(_min_arg>_max_arg) std::swap(_min_arg,_max_arg);
00099   ArgType range = _max_arg - _min_arg;
00100   unsigned sample_count = static_cast<unsigned>(range/_sample_spacing)+2;
00101   _max_arg = ((sample_count-1)*_sample_spacing)+_min_arg;
00102 
00103   _func_val = new T[sample_count];
00104   for(unsigned i=0;i<sample_count;++i){
00105     _func_val[i] = func(_min_arg+i*_sample_spacing);
00106   }
00107 }
00108 
00109 
00110 
00111 template <typename T, typename ArgType>
00112 T
00113 BTK::CachedUnaryFunction<T,ArgType>::
00114 operator()(ArgType arg) {
00115   assert( arg >= _min_arg && arg <= _max_arg );
00116   ArgType exact_bin_val = (arg-_min_arg)/_sample_spacing;
00117   unsigned low_bin = static_cast<unsigned>(exact_bin_val);
00118   T low_val = _func_val[low_bin];
00119   ArgType bin_error =
00120     (exact_bin_val-static_cast<ArgType>(low_bin));
00121   return low_val + bin_error*(_func_val[low_bin+1]-low_val);
00122 }
00123 
00124 
00125 
00126 #endif
00127 
00128 

Generated on Wed Apr 14 00:43:16 2004 for BTK by doxygen 1.3.6