TPIE

11a2c2d
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 constexpr memory_size_type memory_usage (memory_size_type size) noexcept
 Return the number of bytes required to create a data structure supporting a given number of elements. More...
 
static constexpr linear_memory_usage memory_usage () noexcept
 
static constexpr memory_size_type memory_fits (memory_size_type memory) noexcept
 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

◆ internal_queue()

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);}

Member Function Documentation

◆ back()

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()];}

◆ clear()

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;}

◆ empty()

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.

99 {return m_first == m_last;}

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

◆ front()

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.

76 {tp_assert(!empty(), "front() on empty queue"); return m_elements[m_first % m_elements.size()];}

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

◆ full()

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();}

◆ memory_coefficient()

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.

◆ memory_fits()

static constexpr memory_size_type tpie::linear_memory_base< internal_queue< T > >::memory_fits ( memory_size_type  memory)
inlinestaticconstexprnoexceptinherited

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 118 of file util.h.

118  {
119  return static_cast<memory_size_type>(
120  floor((memory - child_t::memory_overhead()) / child_t::memory_coefficient()));
121  }

◆ memory_overhead()

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);}

◆ memory_usage()

static constexpr memory_size_type tpie::linear_memory_base< internal_queue< T > >::memory_usage ( memory_size_type  size)
inlinestaticconstexprnoexceptinherited

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 106 of file util.h.

106  {
107  return static_cast<memory_size_type>(
108  floor(static_cast<double>(size) * child_t::memory_coefficient() + child_t::memory_overhead()));
109  }

◆ pop()

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.

93 {++m_first;}

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

◆ push()

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;}

◆ resize()

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

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.

111 { return m_last - m_first;}

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


The documentation for this class was generated from the following file:
tpie::internal_queue::size
size_t size() const
Return the number of elements in the queue.
Definition: internal_queue.h:111
tpie::array::memory_overhead
static constexpr double memory_overhead() noexcept
Return the memory overhead of the structure.
Definition: array.h:405
tp_assert
#define tp_assert(condition, message)
Definition: tpie_assert.h:65
tpie::internal_queue::empty
bool empty() const
Check if the queue is empty.
Definition: internal_queue.h:99
tpie::array::memory_coefficient
static constexpr double memory_coefficient() noexcept
Return the memory coefficient of the structure.
Definition: array.h:398
tpie::array::resize
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:490
tpie::internal_queue::internal_queue
internal_queue(size_t size=0)
Construct a queue.
Definition: internal_queue.h:64
tpie::array::size
size_type size() const
Return the size of the array.
Definition: array.h:531