proj home

Files   Classes   Functions   Hierarchy  

Public Member Functions | Public Attributes | List of all members
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

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_ 
)

Hold n states, each with W T objects.

Definition at line 162 of file cirbuffarr.h.

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

: nstates(nstates_), W(W_)
{
assert(nstates>0);
current=0;
state = new T[nstates*W];
}
template<typename T >
cirbuffarr< T >::~cirbuffarr ( )

Free memory.

Definition at line 171 of file cirbuffarr.h.

{
delete[] state;
state = 0;
}

Member Function Documentation

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

Copy the i'th state to array x.

Definition at line 137 of file cirbuffarr.h.

Referenced by cirbuffarrtest01(), and cirbuffarrtest02().

{
T const * c = operator[](i);
for (uint i=0; i<W; ++i)
x[i] = c[i];
}
template<typename T >
void cirbuffarr< T >::dup ( )

Duplicate the current state.

Definition at line 111 of file cirbuffarr.h.

Referenced by cirbuffarrtest02().

{
T * prev = operator[](0); //accessState();
push(prev);
}
template<typename T>
void cirbuffarr< T >::operator++ ( )
inline

Move the current pointer forward.

Definition at line 47 of file cirbuffarr.h.

{ ++current; current %= nstates; }
template<typename T>
void cirbuffarr< T >::operator-- ( )
inline

Move the current pointer back.

Definition at line 50 of file cirbuffarr.h.

{ current += (nstates-1); current %= nstates; }
template<typename T >
template<typename Z >
void cirbuffarr< T >::operator= ( Z const &  x)

Copy array x to the current state.

Definition at line 119 of file cirbuffarr.h.

{
T * c = operator[](0); //accessState();
for (uint i=0; i<W; ++i)
c[i] = x[i];
}
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.

{
assert(k<nstates);
assert(nstates!=0);
return state + ((current+(nstates-k))%nstates)*W;
}
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.

{
//assert(k<nstates);
assert(nstates!=0);
return state + ((current+(nstates-k))%nstates)*W;
}
template<typename T >
ostream & cirbuffarr< T >::print ( ostream os) const

Print the circular buffer from first to last states.

Definition at line 92 of file cirbuffarr.h.

{
T const * s;
uint i;
uint k;
for (i=0; i<nstates; ++i)
{
s = operator[](i);
os << i << ": ";
for (k=0; k<W; ++k)
os << s[k] << " ";
os << endl;
}
return os;
}
template<typename T >
template<typename Z >
void cirbuffarr< T >::push ( Z const &  x)

Push array x into the front.

Definition at line 129 of file cirbuffarr.h.

References operator++().

Referenced by cirbuffarrtest01(), and cirbuffarrtest02().

{
*this = x;
}

Member Data Documentation

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

The number of states the circular buffer has.

Definition at line 41 of file cirbuffarr.h.

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

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

The number of elements of each state.

Definition at line 44 of file cirbuffarr.h.

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


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