proj home

Files   Classes   Functions   Hierarchy  

cirbuffarr< T > Class Template Reference

A Circular Buffer Array. More...

#include <cirbuffarr.h>

Inheritance diagram for cirbuffarr< T >:
Inheritance graph
Collaboration diagram for cirbuffarr< T >:
Collaboration graph

List of all members.

Public Member Functions

void operator++ ()
 Move the current pointer forward.
void operator-- ()
 Move the current pointer back.
 cirbuffarr (uintc nstates_, uintc W_)
 Hold n states, each with W T objects.
 ~cirbuffarr ()
 Free memory.
Toperator[] (uintc k)
 Access the states, 0 is the most recent.
T const * operator[] (uintc k) const
 Access the states, 0 is the most recent.
template<typename Z >
void operator= (Z const &x)
 Copy array x to the current state.
template<typename Z >
void copyto (Z &x, uintc i=0) const
 Copy the i'th state to array x.
template<typename Z >
void push (Z const &x)
 Push array x into the front.
void dup ()
 Duplicate the current state.
ostreamprint (ostream &os) const
 Print the circular buffer from first to last states.

Public Attributes

uintc nstates
 The number of states the circular buffer has.
uintc W
 The number of elements of each state.

Detailed Description

template<typename T>
class cirbuffarr< T >

A Circular Buffer Array.

The client pushes the new state into the current position by using push or they can directly write to the memory area themselves and increment with ++ afterwards.

popping is done by the -- operator. There is no deletion only a change in the pointers. The memory is allocated upfront and released when the object dies.

The copy routines allow you to save a state in another memory location, and restore it. Hence this class can be used in another way.

Definition at line 29 of file cirbuffarr.h.


Constructor & Destructor Documentation

template<typename T >
cirbuffarr< T >::cirbuffarr ( uintc  nstates_,
uintc  W_ 
) [inline]

Hold n states, each with W T objects.

Definition at line 162 of file cirbuffarr.h.

References cirbuffarr< T >::nstates, and cirbuffarr< T >::W.

00163   : nstates(nstates_), W(W_)
00164 {
00165   assert(nstates>0);
00166   current=0;
00167   state = new T[nstates*W];
00168 }

template<typename T >
cirbuffarr< T >::~cirbuffarr (  )  [inline]

Free memory.

Definition at line 171 of file cirbuffarr.h.

00172 {
00173   delete[] state;
00174   state = 0;
00175 }


Member Function Documentation

template<typename T >
template<typename Z >
void cirbuffarr< T >::copyto ( Z &  x,
uintc  i = 0 
) const [inline]

Copy the i'th state to array x.

Definition at line 137 of file cirbuffarr.h.

References cirbuffarr< T >::operator[](), and cirbuffarr< T >::W.

Referenced by cirbuffarrtest01(), and cirbuffarrtest02().

00138 {
00139   T const * c = operator[](i);
00140   
00141   for (uint i=0; i<W; ++i)
00142     x[i] = c[i];
00143 }

template<typename T >
void cirbuffarr< T >::dup (  )  [inline]

Duplicate the current state.

Definition at line 111 of file cirbuffarr.h.

References cirbuffarr< T >::operator[](), and cirbuffarr< T >::push().

Referenced by cirbuffarrtest02().

00112 {
00113   T * prev = operator[](0); //accessState();
00114   push(prev);
00115 }

template<typename T>
void cirbuffarr< T >::operator++ (  )  [inline]

Move the current pointer forward.

Definition at line 47 of file cirbuffarr.h.

Referenced by cirbuffarr< T >::push().

00048     { ++current; current %= nstates; }

template<typename T>
void cirbuffarr< T >::operator-- (  )  [inline]

Move the current pointer back.

Definition at line 50 of file cirbuffarr.h.

00051     { current += (nstates-1); current %= nstates; }

template<typename T >
template<typename Z >
void cirbuffarr< T >::operator= ( Z const &  x  )  [inline]

Copy array x to the current state.

Definition at line 119 of file cirbuffarr.h.

References cirbuffarr< T >::operator[](), and cirbuffarr< T >::W.

00120 {
00121   T * c = operator[](0); //accessState(); 
00122   
00123   for (uint i=0; i<W; ++i)
00124     c[i] = x[i];
00125 }

template<typename T >
T const * cirbuffarr< T >::operator[] ( uintc  k  )  const [inline]

Access the states, 0 is the most recent.

Definition at line 154 of file cirbuffarr.h.

References cirbuffarr< T >::nstates, and cirbuffarr< T >::W.

00155 {
00156   //assert(k<nstates);
00157   assert(nstates!=0);
00158   return state + ((current+(nstates-k))%nstates)*W;
00159 }

template<typename T >
T * cirbuffarr< T >::operator[] ( uintc  k  )  [inline]

Access the states, 0 is the most recent.

Definition at line 146 of file cirbuffarr.h.

References cirbuffarr< T >::nstates, and cirbuffarr< T >::W.

Referenced by cirbuffarr< T >::copyto(), cirbuffarr< T >::dup(), cirbuffarr< T >::operator=(), and cirbuffarr< T >::print().

00147 {
00148   assert(k<nstates);
00149   assert(nstates!=0);
00150   return state + ((current+(nstates-k))%nstates)*W;
00151 }

template<typename T >
ostream & cirbuffarr< T >::print ( ostream os  )  const [inline]

Print the circular buffer from first to last states.

Definition at line 92 of file cirbuffarr.h.

References cirbuffarr< T >::nstates, cirbuffarr< T >::operator[](), and cirbuffarr< T >::W.

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 }

template<typename T >
template<typename Z >
void cirbuffarr< T >::push ( Z const &  x  )  [inline]

Push array x into the front.

Definition at line 129 of file cirbuffarr.h.

References cirbuffarr< T >::operator++().

Referenced by cirbuffarrtest01(), cirbuffarrtest02(), and cirbuffarr< T >::dup().

00130 {
00131   operator ++();
00132   *this = x;
00133 }


Member Data Documentation

template<typename T>
uintc cirbuffarr< T >::nstates
template<typename T>
uintc cirbuffarr< T >::W

The documentation for this class was generated from the following file:

Generated on 27 Nov 2012 for Chelton Evans Source by  doxygen 1.6.1