#include <PriorityQueue.h>
Inheritance diagram for base::PriorityQueue< T >:
Public Types | |
enum | { DEC_ALLOC = 16 } |
If we want to have one allocation to fit on a page (4k), it is advisable to allocate slightly less than 4k for headers. More... | |
Public Member Functions | |
PriorityQueue () | |
Constructor: initialize the heap (size 0). | |
~PriorityQueue () | |
Destructor: update initial size & deallocate memory without calling any destructor. | |
void | push (const T &item) |
Push an item to the queue. | |
const T & | top () const |
Read the top element of the queue. | |
void | pop () |
Remove the top element from the queue. | |
bool | empty () const |
uint32_t | size () const |
Private Member Functions | |
T * | alloc (uint32_t n) |
void | ensureCapacity () |
Ensure capacity for a new element to be added on the heap. | |
Private Attributes | |
uint32_t | capacity |
capacity of the heap | |
uint32_t | qsize |
size of the queue | |
T * | table |
table of items with a heap structure |
std::priority_queue is 20-30% slower. The difference with std::priority_queue is the ordering: here it is from lowest to highest, while in std::priority_queue it is from highest to lowest.
The element must be sortable and have a < comparison operator. Elements are copied and never destroyed. The algorithm is based on heaps. The the Introduction to Algorithms 2nd edition p130 for max-heapify and p140 for insertion. We use min instead of max, so invert the comparisons.
|
If we want to have one allocation to fit on a page (4k), it is advisable to allocate slightly less than 4k for headers.
|
|
Constructor: initialize the heap (size 0).
|
|
Destructor: update initial size & deallocate memory without calling any destructor.
|
|
|
|
|
|
Ensure capacity for a new element to be added on the heap.
|
|
Remove the top element from the queue. Implement min-heapify (see max-heapify p130: Left(i) == l and Right(i) == l+1).
|
|
Push an item to the queue. Implements min-heap insert (p140). The original algorithm uses a swap, but it is to propagate the new item.
|
|
|
|
Read the top element of the queue. It is the minimal one with respect to < |
|
capacity of the heap
|
|
size of the queue
|
|
table of items with a heap structure
|