TPIE

2362a60
tpie::internal_priority_queue< T, comp_t > Class Template Reference

Standard binary internal heap. More...

#include <tpie/internal_priority_queue.h>

Inherits tpie::linear_memory_base< internal_priority_queue< T, comp_t > >.

Public Types

typedef memory_size_type size_type
 

Public Member Functions

 internal_priority_queue (size_type max_size, comp_t c=comp_t(), memory_bucket_ref bucket=memory_bucket_ref())
 Construct a priority queue. More...
 
template<typename IT >
 internal_priority_queue (size_type max_size, const IT &start, const IT &end, comp_t c=comp_t(), memory_bucket_ref bucket=memory_bucket_ref())
 Construct a priority queue with given elements. More...
 
template<typename IT >
void insert (const IT &start, const IT &end)
 Insert some elements and run make_heap. More...
 
void unsafe_push (const T &v)
 Insert an element into the priority queue, possibly destroying ordering information. More...
 
void unsafe_push (T &&v)
 
void make_safe ()
 Make the priority queue safe after a sequence of calls to unsafe_push. More...
 
bool empty () const
 Is the queue empty? More...
 
size_type size () const
 Returns the size of the queue. More...
 
void push (const T &v)
 Insert an element into the priority queue. More...
 
void push (T &&v)
 
void pop ()
 Remove the minimum element from heap. More...
 
void pop_and_push (const T &v)
 Remove the minimum element and insert a new element. More...
 
void pop_and_push (T &&v)
 
const T & top () const
 Return the minimum element. More...
 
T & top ()
 
tpie::array< T > & get_array ()
 Return the underlying array. More...
 
void clear ()
 Clear the structure of all elements. More...
 
void resize (size_t s)
 Resize priority queue to given size. 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, typename comp_t = std::less<T>>
class tpie::internal_priority_queue< T, comp_t >

Standard binary internal heap.

Author
Lars Hvam Petersen, Jakob Truelsen

Definition at line 37 of file internal_priority_queue.h.

Constructor & Destructor Documentation

template<typename T, typename comp_t = std::less<T>>
tpie::internal_priority_queue< T, comp_t >::internal_priority_queue ( size_type  max_size,
comp_t  c = comp_t(),
memory_bucket_ref  bucket = memory_bucket_ref() 
)
inline

Construct a priority queue.

Parameters
max_sizeMaximum size of queue.

Definition at line 45 of file internal_priority_queue.h.

47  : pq(max_size, bucket), sz(0), comp(c) {}
template<typename T, typename comp_t = std::less<T>>
template<typename IT >
tpie::internal_priority_queue< T, comp_t >::internal_priority_queue ( size_type  max_size,
const IT &  start,
const IT &  end,
comp_t  c = comp_t(),
memory_bucket_ref  bucket = memory_bucket_ref() 
)
inline

Construct a priority queue with given elements.

Definition at line 53 of file internal_priority_queue.h.

56  : pq(max_size, bucket), sz(0), comp(c) {
57  insert(start, end);
58  }
void insert(const IT &start, const IT &end)
Insert some elements and run make_heap.

Member Function Documentation

template<typename T, typename comp_t = std::less<T>>
void tpie::internal_priority_queue< T, comp_t >::clear ( )
inline

Clear the structure of all elements.

Definition at line 182 of file internal_priority_queue.h.

182 {sz=0;}
template<typename T, typename comp_t = std::less<T>>
bool tpie::internal_priority_queue< T, comp_t >::empty ( ) const
inline

Is the queue empty?

Returns
True if the queue is empty.

Definition at line 96 of file internal_priority_queue.h.

Referenced by tpie::internal_priority_queue< tpie::ami::heap_ptr< REC >, comp >::pop(), and tpie::internal_priority_queue< tpie::ami::heap_ptr< REC >, comp >::pop_and_push().

96 {return sz == 0;}
template<typename T, typename comp_t = std::less<T>>
tpie::array<T>& tpie::internal_priority_queue< T, comp_t >::get_array ( )
inline

Return the underlying array.

Make sure you know what you are doing.

Returns
The underlying array.

Definition at line 175 of file internal_priority_queue.h.

175  {
176  return pq;
177  }
template<typename T, typename comp_t = std::less<T>>
template<typename IT >
void tpie::internal_priority_queue< T, comp_t >::insert ( const IT &  start,
const IT &  end 
)
inline

Insert some elements and run make_heap.

Definition at line 64 of file internal_priority_queue.h.

Referenced by tpie::internal_priority_queue< tpie::ami::heap_ptr< REC >, comp >::internal_priority_queue().

64  {
65  std::copy(start, end, pq.find(sz));
66  sz += (end - start);
67  make_safe();
68  }
iterator find(size_t idx)
Return an iterator to the i'th element of the array.
Definition: array.h:167
void make_safe()
Make the priority queue safe after a sequence of calls to unsafe_push.
template<typename T, typename comp_t = std::less<T>>
void tpie::internal_priority_queue< T, comp_t >::make_safe ( )
inline

Make the priority queue safe after a sequence of calls to unsafe_push.

Definition at line 88 of file internal_priority_queue.h.

Referenced by tpie::internal_priority_queue< tpie::ami::heap_ptr< REC >, comp >::insert().

88  {
89  std::make_heap(pq.begin(), pq.find(sz), comp);
90  }
iterator find(size_t idx)
Return an iterator to the i'th element of the array.
Definition: array.h:167
iterator begin()
Return an iterator to the beginning of the array.
Definition: array.h:307
template<typename T, typename comp_t = std::less<T>>
static double tpie::internal_priority_queue< T, comp_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 158 of file internal_priority_queue.h.

158  {
160  }
static double memory_coefficient()
Return the memory coefficient of the structure.
Definition: array.h:393
static memory_size_type tpie::linear_memory_base< internal_priority_queue< T, comp_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, typename comp_t = std::less<T>>
static double tpie::internal_priority_queue< T, comp_t >::memory_overhead ( )
inlinestatic

Return the memory overhead of the structure.

See also
memory_coefficient()
Returns
The memory overhead.

Definition at line 166 of file internal_priority_queue.h.

166  {
168  }
internal_priority_queue(size_type max_size, comp_t c=comp_t(), memory_bucket_ref bucket=memory_bucket_ref())
Construct a priority 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_priority_queue< T, comp_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, typename comp_t = std::less<T>>
void tpie::internal_priority_queue< T, comp_t >::pop ( )
inline

Remove the minimum element from heap.

Definition at line 124 of file internal_priority_queue.h.

124  {
125  assert(!empty());
126  std::pop_heap(pq.begin(), pq.find(sz), comp);
127  --sz;
128  }
iterator find(size_t idx)
Return an iterator to the i'th element of the array.
Definition: array.h:167
bool empty() const
Is the queue empty?
iterator begin()
Return an iterator to the beginning of the array.
Definition: array.h:307
template<typename T, typename comp_t = std::less<T>>
void tpie::internal_priority_queue< T, comp_t >::pop_and_push ( const T &  v)
inline

Remove the minimum element and insert a new element.

Definition at line 133 of file internal_priority_queue.h.

133  {
134  assert(!empty());
135  pq[0] = v;
136  pop_and_push_heap(pq.begin(), pq.find(sz), comp);
137  }
void pop_and_push_heap(T a, T b, C lt)
Restore heap invariants after the first element has been replaced by some other element.
Definition: util.h:119
iterator find(size_t idx)
Return an iterator to the i'th element of the array.
Definition: array.h:167
bool empty() const
Is the queue empty?
iterator begin()
Return an iterator to the beginning of the array.
Definition: array.h:307
template<typename T, typename comp_t = std::less<T>>
void tpie::internal_priority_queue< T, comp_t >::push ( const T &  v)
inline

Insert an element into the priority queue.

Parameters
vThe element that should be inserted.

Definition at line 109 of file internal_priority_queue.h.

109  {
110  assert(size() < pq.size());
111  pq[sz++] = v;
112  std::push_heap(pq.begin(), pq.find(sz), comp);
113  }
size_type size() const
Returns the size of the queue.
size_type size() const
Return the size of the array.
Definition: array.h:526
template<typename T, typename comp_t = std::less<T>>
void tpie::internal_priority_queue< T, comp_t >::resize ( size_t  s)
inline

Resize priority queue to given size.

Parameters
sNew size of priority queue.

Definition at line 188 of file internal_priority_queue.h.

188 {sz=0; pq.resize(s);}
void resize(size_t size, const T &elm)
Change the size of the array.
Definition: array.h:485
template<typename T, typename comp_t = std::less<T>>
size_type tpie::internal_priority_queue< T, comp_t >::size ( ) const
inline

Returns the size of the queue.

Returns
Queue size.

Definition at line 102 of file internal_priority_queue.h.

Referenced by tpie::internal_priority_queue< tpie::ami::heap_ptr< REC >, comp >::push().

102 {return sz;}
template<typename T, typename comp_t = std::less<T>>
const T& tpie::internal_priority_queue< T, comp_t >::top ( ) const
inline

Return the minimum element.

Returns
The minimum element.

Definition at line 150 of file internal_priority_queue.h.

150 {return pq[0];}
template<typename T, typename comp_t = std::less<T>>
void tpie::internal_priority_queue< T, comp_t >::unsafe_push ( const T &  v)
inline

Insert an element into the priority queue, possibly destroying ordering information.

Parameters
vThe element that should be inserted.

Definition at line 76 of file internal_priority_queue.h.

76  {
77  pq[sz++] = v;
78  }

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