TPIE

2362a60
tpie::internal_queue< T > Class Template Reference

A generic internal circular queue. More...

#include <tpie/internal_queue.h>

Inherits tpie::linear_memory_base< internal_queue< T > >.

Public Member Functions

 internal_queue (size_t size=0)
 Construct a queue. More...
 
void resize (size_t size=0)
 Resize the queue; all data is lost. More...
 
const T & front () const
 Return the item that has been in the queue for the longest time. More...
 
const T & back () const
 Return the last item pushed to the queue. More...
 
void push (T val)
 Add an element to the front of the queue. More...
 
void pop ()
 Remove an element from the back of the queue. More...
 
bool empty () const
 Check if the queue is empty. More...
 
bool full () const
 Check if the queue is empty. More...
 
size_t size () const
 Return the number of elements in the queue. More...
 
void clear ()
 Clear the queue of all elements. More...
 

Static Public Member Functions

static double memory_coefficient ()
 Return the memory coefficient of the structure. More...
 
static double memory_overhead ()
 Return the memory overhead of the structure. More...
 
static memory_size_type memory_usage (memory_size_type size)
 Return the number of bytes required to create a data structure supporting a given number of elements. More...
 
static memory_size_type memory_fits (memory_size_type memory)
 Return the maximum number of elements that can be contained in in the structure when it is allowed to fill a given number of bytes. More...
 

Detailed Description

template<typename T>
class tpie::internal_queue< T >

A generic internal circular queue.

The queue supports a fixed number of unpopped elements between calls to clear. The number of elements is given as an argument to the constructor or to resize.

Template Parameters
TThe type of items stored in the queue

Definition at line 42 of file internal_queue.h.

Constructor & Destructor Documentation

template<typename T>
tpie::internal_queue< T >::internal_queue ( size_t  size = 0)
inline

Construct a queue.

Parameters
sizeThe number of pushes supported between calls to clear and resize.

Definition at line 64 of file internal_queue.h.

64 : m_first(0), m_last(0) {m_elements.resize(size);}
size_t size() const
Return the number of elements in the queue.
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:485

Member Function Documentation

template<typename T>
const T& tpie::internal_queue< T >::back ( ) const
inline

Return the last item pushed to the queue.

Definition at line 81 of file internal_queue.h.

81 {return m_elements[(m_last-1) % m_elements.size()];}
size_type size() const
Return the size of the array.
Definition: array.h:526
template<typename T>
void tpie::internal_queue< T >::clear ( )
inline

Clear the queue of all elements.

After this call, the queue again supports the number of pushes passed to the constructor or resize.

Definition at line 118 of file internal_queue.h.

118 {m_first = m_last =0;}
template<typename T>
bool tpie::internal_queue< T >::empty ( ) const
inline

Check if the queue is empty.

Returns
true if the queue is empty, otherwise false.

Definition at line 99 of file internal_queue.h.

Referenced by tpie::internal_queue< item_type >::front().

99 {return m_first == m_last;}
template<typename T>
const T& tpie::internal_queue< T >::front ( ) const
inline

Return the item that has been in the queue for the longest time.

Definition at line 76 of file internal_queue.h.

Referenced by tpie::pipelining::parallel_bits::producer< T1, T2 >::end().

76 {tp_assert(!empty(), "front() on empty queue"); return m_elements[m_first % m_elements.size()];}
bool empty() const
Check if the queue is empty.
size_type size() const
Return the size of the array.
Definition: array.h:526
#define tp_assert(condition, message)
Definition: tpie_assert.h:48
template<typename T>
bool tpie::internal_queue< T >::full ( ) const
inline

Check if the queue is empty.

Returns
true if the queue is empty, otherwise false.

Definition at line 105 of file internal_queue.h.

105 {return m_last - m_first == m_elements.size();}
size_type size() const
Return the size of the array.
Definition: array.h:526
template<typename T>
static double tpie::internal_queue< T >::memory_coefficient ( )
inlinestatic

Return the memory coefficient of the structure.

Allocating a structure with n elements will use at most $ \lfloor \mathrm{memory\_coefficient} \cdot n + \mathrm{memory\_overhead} \rfloor $ bytes. This does not include memory overhead incurred if the structure is allocated using new.

Returns
The memory coefficient of the structure.

Definition at line 50 of file internal_queue.h.

static double memory_coefficient()
Return the memory coefficient of the structure.
Definition: array.h:393
static memory_size_type tpie::linear_memory_base< internal_queue< T > >::memory_fits ( memory_size_type  memory)
inlinestaticinherited

Return the maximum number of elements that can be contained in in the structure when it is allowed to fill a given number of bytes.

Parameters
memoryThe number of bytes the structure is allowed to occupy
Returns
The number of elements that will fit in the structure

Definition at line 93 of file util.h.

93  {
94  return static_cast<memory_size_type>(
95  floor((memory - child_t::memory_overhead()) / child_t::memory_coefficient()));
96  }
template<typename T>
static double tpie::internal_queue< T >::memory_overhead ( )
inlinestatic

Return the memory overhead of the structure.

See also
memory_coefficient()
Returns
The memory overhead.

Definition at line 56 of file internal_queue.h.

56 {return array<T>::memory_overhead() - sizeof(array<T>) + sizeof(internal_queue);}
internal_queue(size_t size=0)
Construct a queue.
static double memory_overhead()
Return the memory overhead of the structure.
Definition: array.h:400
static memory_size_type tpie::linear_memory_base< internal_queue< T > >::memory_usage ( memory_size_type  size)
inlinestaticinherited

Return the number of bytes required to create a data structure supporting a given number of elements.

Parameters
sizeThe number of elements to support
Returns
The amount of memory required in bytes

Definition at line 81 of file util.h.

81  {
82  return static_cast<memory_size_type>(
83  floor(static_cast<double>(size) * child_t::memory_coefficient() + child_t::memory_overhead()));
84  }
template<typename T>
void tpie::internal_queue< T >::pop ( )
inline

Remove an element from the back of the queue.

Definition at line 93 of file internal_queue.h.

Referenced by tpie::pipelining::parallel_bits::producer< T1, T2 >::end().

93 {++m_first;}
template<typename T>
void tpie::internal_queue< T >::push ( val)
inline

Add an element to the front of the queue.

Parameters
valThe element to add.

Definition at line 88 of file internal_queue.h.

88 {m_elements[m_last++ % m_elements.size()] = val;}
size_type size() const
Return the size of the array.
Definition: array.h:526
template<typename T>
void tpie::internal_queue< T >::resize ( size_t  size = 0)
inline

Resize the queue; all data is lost.

Parameters
sizeThe number of elements to contain.

Definition at line 71 of file internal_queue.h.

71 {m_elements.resize(size); m_first = m_last = 0;}
size_t size() const
Return the number of elements in the queue.
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:485
template<typename T>
size_t tpie::internal_queue< T >::size ( ) const
inline

Return the number of elements in the queue.

Returns
The number of elements in the queue.

Definition at line 111 of file internal_queue.h.

Referenced by tpie::internal_queue< item_type >::internal_queue(), and tpie::internal_queue< item_type >::resize().

111 { return m_last - m_first;}

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