proj home

Files   Classes   Functions   Hierarchy  

funcstate.h

Go to the documentation of this file.
00001 #ifndef FUNCSTATE_H
00002 #define FUNCSTATE_H
00003 
00004 #include <cassert>
00005 #include <deque>
00006 using namespace std;
00007 
00008 #include <typedefs.h>
00009 
00017 template< typename X >
00018 class funcstate
00019 {
00020 public:
00021 
00023   uint counter;
00024 
00026   uint dim;
00027   
00029   X* xi;
00030 
00032   funcstate()
00033     : counter(0), dim(0), xi(0), mem(false) {}
00034 
00036   funcstate(uintc dim_, boolc mem_=true);
00037 
00039   virtual X const operator()() = 0;
00040  
00042   virtual ~funcstate();
00043 
00045   bool mem;
00046 
00048   void set_position(X* xi_);
00050   void set_positionvalue(X* xi_);
00052   X const fnvalue()
00053     { return xi[dim]; }
00054 };
00055 
00056 
00060 template< typename X >
00061 class funchistory
00062 {
00063 public:
00064 
00066   deque< X* > xik;
00067 
00069   funcstate<X> & fs;
00070 
00072   funchistory(funcstate<X> & fs_)
00073     : fs(fs_) {}
00075   ~funchistory();
00076 
00078   X* operator [] (uintc i)
00079     { assert(i<xik.size()); return xik[i]; }
00080 
00082   void push();
00083 
00086   void pop();
00087 
00089   void restore();
00090 
00092   void del_back();
00093 
00094 };
00095 
00096 
00097 // template wrappers to create inherited classes of funcstate.
00098 
00099 //---------------------------------------------------------
00100 //  Implementation
00101 
00102 template< typename X >
00103 funcstate<X>::funcstate(uintc dim_, boolc mem_)
00104   : counter(0), dim(dim_), mem(mem_)
00105 {
00106   if (mem==true)
00107   {
00108     assert(dim!=0);
00109     xi = new X[dim+1];
00110   }
00111 }
00112 
00113 template< typename X >
00114 void funcstate<X>::set_position(X* xi_)
00115 {
00116   for (uint i=0; i<dim; ++i)
00117     xi[i] = xi_[i];
00118 }
00119 
00120 template< typename X >
00121 void funcstate<X>::set_positionvalue(X* xi_)
00122 {
00123   for (uint i=0; i<=dim; ++i)
00124     xi[i] = xi_[i];
00125 }
00126 
00127 template< typename X >
00128 funcstate<X>::~funcstate()
00129 {
00130   if (mem)
00131     delete[] xi;
00132   xi=0;  
00133 }
00134 
00135 template< typename X >
00136 void funchistory<X>::push()
00137 {
00138   X* x= new X[fs.dim+1];
00139   for (uint i=0; i<=fs.dim; ++i)
00140     x[i] = fs.xi[i];
00141 
00142   xik.push_front(x);
00143 }
00144   
00145 template< typename X >
00146 void funchistory<X>::pop()
00147 {
00148   assert( xik.empty()==false);
00149   if (xik.empty())
00150     return;
00151 
00152   X * z = xik[0];
00153   fs.set_positionvalue( z );
00154   delete[] z;
00155   xik.pop_front();
00156 }
00157 
00158 template< typename X >
00159 void funchistory<X>::restore()
00160 {
00161   assert( xik.empty()==false);
00162   if (xik.empty())
00163     return;
00164 
00165   X * z = xik[0];
00166   fs.set_positionvalue( z );
00167 }
00168 
00169 template< typename X >
00170 void funchistory<X>::del_back()
00171 {
00172   if (xik.empty())
00173     return;
00174 
00175   X * z = xik.back();
00176   xik.pop_back();
00177   delete[] z; 
00178 }
00179 
00180 template< typename X >
00181 funchistory<X>::~funchistory()
00182 {
00183   for (uint i=0; i<xik.size(); ++i)
00184     delete[] xik[i];
00185   xik.clear();
00186 }
00187 
00188 #endif
00189 
00190 

Generated on Fri Mar 4 00:49:28 2011 for Chelton Evans Source by  doxygen 1.5.8