CircularBuffer< T > Class Template Reference

A circular buffer containing a maximum of N entries of type T. More...

#include <CircularBuffer.h>

List of all members.

Public Member Functions

 CircularBuffer ()
 CircularBuffer (uint nEntriesInit)
void clear ()
 Initializes the buffer to an empty state.
void resize (uint nEntriesInit)
 Resizes the number of entries in the circular buffer.
uint size ()
 
Returns:
max number of entries that can be stored in the buffer

bool empty ()
 
Returns:
true if the buffer is empty

bool spaceAvailable ()
 
Returns:
true if there is space available in the buffer

uint nFreeSpace ()
 
Returns:
number of free entries available in the buffer

uint getFirstPos ()
 
Returns:
the position of the first entry in the buffer

uint getLastPos ()
 
Returns:
the position of the last entry in the buffer

T & reference_back ()
 
Returns:
reference to the last entry in the buffer

void push_back (T &newEntry)
 Adds an entry to the end of the buffer.
T & reference_front ()
 
Returns:
reference to the first entry in the buffer

void pop_front ()
 Removes the first entry from the buffer.
T & operator[] (uint pos)
 Returns the entry at a specified position in the circular buffer.
bool isReadOnly ()
 
Returns:
true if the buffer is readonly

void setReadOnly ()
 Sets the buffer to a readonly state.

Private Member Functions

void init (uint nEntriesInit)
 Initializes the buffer.

Private Attributes

std::vector< T > buffer
 Vector used to represent the contents of the circular buffer.
uint nEntries
 Number of entries in the buffer.
uint nFreeEntries
 Number of free entries in the buffer.
uint firstPos
 Position of the current, first entry in the circular buffer.
bool readOnly
 If true, no additional new entries can be added to the buffer.


Detailed Description

template<class T>
class CircularBuffer< T >

A circular buffer containing a maximum of N entries of type T.

The max size of the buffer must be set before the buffer can be used.

Definition at line 34 of file CircularBuffer.h.


Constructor & Destructor Documentation

template<class T>
CircularBuffer< T >::CircularBuffer (  )  [inline, explicit]

Definition at line 76 of file CircularBuffer.h.

00077     {
00078         init(0);
00079     }

template<class T>
CircularBuffer< T >::CircularBuffer ( uint  nEntriesInit  )  [inline, explicit]

Definition at line 81 of file CircularBuffer.h.

00082     {
00083         init(nEntriesInit);
00084     }


Member Function Documentation

template<class T>
void CircularBuffer< T >::init ( uint  nEntriesInit  )  [inline, private]

Initializes the buffer.

Parameters:
nEntriesInit size of the buffer

Definition at line 68 of file CircularBuffer.h.

Referenced by CircularBuffer< std::pair< PageId, EntryT > >::CircularBuffer(), and CircularBuffer< std::pair< PageId, EntryT > >::resize().

00069     {
00070         nEntries = nEntriesInit;
00071         buffer.resize(nEntries);
00072         clear();
00073     }

template<class T>
void CircularBuffer< T >::clear (  )  [inline]

Initializes the buffer to an empty state.

Definition at line 89 of file CircularBuffer.h.

Referenced by CircularBuffer< std::pair< PageId, EntryT > >::init(), SegPageEntryIter< EntryT >::mapRange(), LcsRowScanExecStream::open(), and LbmGeneratorExecStream::open().

00090     {
00091         firstPos = 0;
00092         nFreeEntries = nEntries;
00093         readOnly = false;
00094     }

template<class T>
void CircularBuffer< T >::resize ( uint  nEntriesInit  )  [inline]

Resizes the number of entries in the circular buffer.

The buffer positions are also reset back to their initial state, so any existing entries stored in the buffer are ignored.

Parameters:
nEntriesInit new number of entries

Definition at line 103 of file CircularBuffer.h.

Referenced by LcsRowScanExecStream::LcsRowScanExecStream(), LbmGeneratorExecStream::prepare(), and SegPageEntryIter< EntryT >::resize().

00104     {
00105         init(nEntriesInit);
00106     }

template<class T>
uint CircularBuffer< T >::size (  )  [inline]

Returns:
max number of entries that can be stored in the buffer

Definition at line 111 of file CircularBuffer.h.

Referenced by SegPageEntryIter< EntryT >::setPrefetchSource().

00112     {
00113         return nEntries;
00114     }

template<class T>
bool CircularBuffer< T >::empty (  )  [inline]

Returns:
true if the buffer is empty

Definition at line 119 of file CircularBuffer.h.

Referenced by CircularBuffer< std::pair< PageId, EntryT > >::getFirstPos(), SegPageEntryIter< EntryT >::operator++(), CircularBuffer< std::pair< PageId, EntryT > >::pop_front(), SegPageEntryIter< EntryT >::prefetchPages(), CircularBuffer< std::pair< PageId, EntryT > >::reference_back(), and CircularBuffer< std::pair< PageId, EntryT > >::reference_front().

00120     {
00121         return (nFreeEntries == nEntries);
00122     }

template<class T>
bool CircularBuffer< T >::spaceAvailable (  )  [inline]

Returns:
true if there is space available in the buffer

Definition at line 127 of file CircularBuffer.h.

Referenced by LcsRowScanExecStream::fillRidRunBuffer(), and SegPageEntryIter< EntryT >::prefetchPages().

00128     {
00129         return (nFreeEntries != 0);
00130     }

template<class T>
uint CircularBuffer< T >::nFreeSpace (  )  [inline]

Returns:
number of free entries available in the buffer

Definition at line 135 of file CircularBuffer.h.

Referenced by LcsRowScanExecStream::execute().

00136     {
00137         return nFreeEntries;
00138     }

template<class T>
uint CircularBuffer< T >::getFirstPos (  )  [inline]

Returns:
the position of the first entry in the buffer

Definition at line 143 of file CircularBuffer.h.

00144     {
00145         assert(!empty());
00146         return firstPos;
00147     }

template<class T>
uint CircularBuffer< T >::getLastPos (  )  [inline]

Returns:
the position of the last entry in the buffer

Definition at line 152 of file CircularBuffer.h.

Referenced by SegPageEntryIter< EntryT >::prefetchPages(), and CircularBuffer< std::pair< PageId, EntryT > >::reference_back().

00153     {
00154         return nEntries - nFreeEntries + firstPos - 1;
00155     }

template<class T>
T& CircularBuffer< T >::reference_back (  )  [inline]

Returns:
reference to the last entry in the buffer

Definition at line 160 of file CircularBuffer.h.

Referenced by SegPageEntryIter< EntryT >::prefetchPages().

00161     {
00162         assert(!empty());
00163         uint lastEntry = getLastPos() % nEntries;
00164         return buffer[lastEntry];
00165     }

template<class T>
void CircularBuffer< T >::push_back ( T &  newEntry  )  [inline]

Adds an entry to the end of the buffer.

Definition at line 170 of file CircularBuffer.h.

Referenced by LbmGeneratorExecStream::execute(), LcsRowScanExecStream::fillRidRunBuffer(), and SegPageEntryIter< EntryT >::prefetchPages().

00171     {
00172         assert(!readOnly);
00173         assert(nFreeEntries > 0);
00174         uint freeEntry = (nEntries - nFreeEntries + firstPos) % nEntries;
00175         buffer[freeEntry] = newEntry;
00176         nFreeEntries--;
00177     }

template<class T>
T& CircularBuffer< T >::reference_front (  )  [inline]

Returns:
reference to the first entry in the buffer

Definition at line 182 of file CircularBuffer.h.

Referenced by SegPageEntryIter< LcsRid >::operator *(), and SegPageEntryIter< EntryT >::operator++().

00183     {
00184         assert(!empty());
00185         return buffer[firstPos % nEntries];
00186     }

template<class T>
void CircularBuffer< T >::pop_front (  )  [inline]

Removes the first entry from the buffer.

Definition at line 191 of file CircularBuffer.h.

Referenced by SegPageEntryIter< EntryT >::operator++().

00192     {
00193         assert(!empty());
00194         firstPos++;
00195         nFreeEntries++;
00196     }

template<class T>
T& CircularBuffer< T >::operator[] ( uint  pos  )  [inline]

Returns the entry at a specified position in the circular buffer.

Parameters:
pos the position
Returns:
reference to the entry

Definition at line 205 of file CircularBuffer.h.

00206     {
00207         return buffer[pos % nEntries];
00208     }

template<class T>
bool CircularBuffer< T >::isReadOnly (  )  [inline]

Returns:
true if the buffer is readonly

Definition at line 213 of file CircularBuffer.h.

00214     {
00215         return readOnly;
00216     }

template<class T>
void CircularBuffer< T >::setReadOnly (  )  [inline]

Sets the buffer to a readonly state.

Definition at line 221 of file CircularBuffer.h.

Referenced by LcsRowScanExecStream::fillRidRunBuffer().

00222     {
00223         readOnly = true;
00224     }


Member Data Documentation

template<class T>
std::vector<T> CircularBuffer< T >::buffer [private]

Vector used to represent the contents of the circular buffer.

Definition at line 39 of file CircularBuffer.h.

Referenced by CircularBuffer< std::pair< PageId, EntryT > >::init(), CircularBuffer< std::pair< PageId, EntryT > >::operator[](), CircularBuffer< std::pair< PageId, EntryT > >::push_back(), CircularBuffer< std::pair< PageId, EntryT > >::reference_back(), and CircularBuffer< std::pair< PageId, EntryT > >::reference_front().

template<class T>
uint CircularBuffer< T >::nEntries [private]

Number of entries in the buffer.

Definition at line 44 of file CircularBuffer.h.

Referenced by CircularBuffer< std::pair< PageId, EntryT > >::clear(), CircularBuffer< std::pair< PageId, EntryT > >::empty(), CircularBuffer< std::pair< PageId, EntryT > >::getLastPos(), CircularBuffer< std::pair< PageId, EntryT > >::init(), CircularBuffer< std::pair< PageId, EntryT > >::operator[](), CircularBuffer< std::pair< PageId, EntryT > >::push_back(), CircularBuffer< std::pair< PageId, EntryT > >::reference_back(), CircularBuffer< std::pair< PageId, EntryT > >::reference_front(), and CircularBuffer< std::pair< PageId, EntryT > >::size().

template<class T>
uint CircularBuffer< T >::nFreeEntries [private]

Number of free entries in the buffer.

Definition at line 49 of file CircularBuffer.h.

Referenced by CircularBuffer< std::pair< PageId, EntryT > >::clear(), CircularBuffer< std::pair< PageId, EntryT > >::empty(), CircularBuffer< std::pair< PageId, EntryT > >::getLastPos(), CircularBuffer< std::pair< PageId, EntryT > >::nFreeSpace(), CircularBuffer< std::pair< PageId, EntryT > >::pop_front(), CircularBuffer< std::pair< PageId, EntryT > >::push_back(), and CircularBuffer< std::pair< PageId, EntryT > >::spaceAvailable().

template<class T>
uint CircularBuffer< T >::firstPos [private]

Position of the current, first entry in the circular buffer.

Position always increases, even after positions in the circular buffer are recycled.

Definition at line 56 of file CircularBuffer.h.

Referenced by CircularBuffer< std::pair< PageId, EntryT > >::clear(), CircularBuffer< std::pair< PageId, EntryT > >::getFirstPos(), CircularBuffer< std::pair< PageId, EntryT > >::getLastPos(), CircularBuffer< std::pair< PageId, EntryT > >::pop_front(), CircularBuffer< std::pair< PageId, EntryT > >::push_back(), and CircularBuffer< std::pair< PageId, EntryT > >::reference_front().

template<class T>
bool CircularBuffer< T >::readOnly [private]

If true, no additional new entries can be added to the buffer.

Definition at line 61 of file CircularBuffer.h.

Referenced by CircularBuffer< std::pair< PageId, EntryT > >::clear(), CircularBuffer< std::pair< PageId, EntryT > >::isReadOnly(), CircularBuffer< std::pair< PageId, EntryT > >::push_back(), and CircularBuffer< std::pair< PageId, EntryT > >::setReadOnly().


The documentation for this class was generated from the following file:
Generated on Mon Jun 22 04:00:28 2009 for Fennel by  doxygen 1.5.1