proj home

Files   Classes   Functions   Hierarchy  

cirbuffarr.h

Go to the documentation of this file.
00001 #ifndef CIRBUFFARR_H
00002 #define CIRBUFFARR_H
00003 
00004 ;  // hack (compiler difficulties)
00005 #include <cassert>
00006 #include <iostream>
00007 //;  // hack (compiler difficulties)
00008 using namespace std;
00009 
00010 typedef unsigned int uint;
00011 typedef unsigned int const uintc;
00012 
00028 template< typename T >
00029 class cirbuffarr
00030 {
00032   T * state;
00034   uint current;
00035 
00036   cirbuffarr() 
00037     { assert(false); }
00038 public:
00039 
00041   uintc nstates; 
00042 
00044   uintc W;
00045 
00047   void operator ++ ()
00048     { ++current; current %= nstates; }
00050   void operator -- ()
00051     { current += (nstates-1); current %= nstates; }
00052 
00054   cirbuffarr(uintc nstates_, uintc W_);
00056   ~cirbuffarr();
00057 
00059   inline T * operator [] (uintc k);
00061   inline T const * operator [] (uintc k) const;
00062 
00064   template< typename Z >
00065   void operator = ( Z const & x );
00066 
00068   template< typename Z >
00069   void copyto( Z & x, uintc i=0 ) const;
00070 
00072   template< typename Z >
00073   void push( Z const & x );
00074 
00076   void dup();
00077 
00079   ostream & print(ostream & os) const;
00080 
00081 };
00082 
00083 template< typename T >
00084 ostream & operator << (ostream & os, cirbuffarr<T> const & cir)
00085   { return cir.print(os); }
00086 
00087 
00088 // ---------------------------------------------------------
00089 // Implementation
00090 
00091 template< typename T >
00092 ostream & cirbuffarr<T>::print(ostream & os) const
00093 {
00094   T const * s;
00095   uint i;
00096   uint k;
00097   for (i=0; i<nstates; ++i)
00098   {
00099     s = operator[](i); 
00100     os << i << ": ";
00101     for (k=0; k<W; ++k)
00102       os << s[k] << " ";
00103     os << endl;
00104   }
00105 
00106   return os;
00107 }
00108 
00109 
00110 template< typename T >
00111 void cirbuffarr<T>::dup()
00112 {
00113   T * prev = operator[](0); //accessState();
00114   push(prev);
00115 }
00116 
00117 template< typename T >
00118 template< typename Z >
00119 void cirbuffarr<T>::operator = ( Z const & x )
00120 {
00121   T * c = operator[](0); //accessState(); 
00122   
00123   for (uint i=0; i<W; ++i)
00124     c[i] = x[i];
00125 }
00126 
00127 template< typename T >
00128 template< typename Z >
00129 void cirbuffarr<T>::push( Z const & x )
00130 {
00131   operator ++();
00132   *this = x;
00133 }
00134 
00135 template< typename T >
00136 template< typename Z >
00137 void cirbuffarr<T>::copyto( Z & x, uintc i ) const
00138 {
00139   T const * c = operator[](i);
00140   
00141   for (uint i=0; i<W; ++i)
00142     x[i] = c[i];
00143 }
00144 
00145 template< typename T >
00146 T * cirbuffarr<T>::operator [] (uintc k)
00147 {
00148   assert(k<nstates);
00149   assert(nstates!=0);
00150   return state + ((current+(nstates-k))%nstates)*W;
00151 }
00152 
00153 template< typename T >
00154 T const * cirbuffarr<T>::operator [] (uintc k) const
00155 {
00156   //assert(k<nstates);
00157   assert(nstates!=0);
00158   return state + ((current+(nstates-k))%nstates)*W;
00159 }
00160 
00161 template< typename T >
00162 cirbuffarr<T>::cirbuffarr(uintc nstates_, uintc W_)
00163   : nstates(nstates_), W(W_)
00164 {
00165   assert(nstates>0);
00166   current=0;
00167   state = new T[nstates*W];
00168 }
00169 
00170 template< typename T >
00171 cirbuffarr<T>::~cirbuffarr()
00172 {
00173   delete[] state;
00174   state = 0;
00175 }
00176 
00177 
00178 #endif
00179 

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