TPIE

11a2c2d
Priority queue

TPIE provides a very efficient external memory priority queue based off Sanders, Fast Priority Queues for Cached Memory. It is implemented in the file priority_queue.h and exists in the tpie::ami namespace.

It is conceptually compatible with std::priority_queue, except it operates by default as a min-heap rather than a max-heap, as is common in research literature.

One may push() and pop() elements, and top() will always return the least element pushed that has not yet been popped. size() will return the number of elements pushed that have not been popped, and empty() returns size() == 0.

You must set a memory limit for the memory manager using tpie::memory_manager::set_limit() before using the priority queue, as its initialization depends on knowing how much memory is available.

Example pq.cpp:

#include <tpie/priority_queue.h> // for tpie::priority_queue
#include <tpie/tpie_assert.h> // for tp_assert macro
#include <tpie/tpie.h> // for tpie_init
void pq_test() {
for (int i = 10; i > 0; --i) {
pq.push(i);
}
tp_assert(pq.size() == 10, "Incorrect size");
for (int i = 1; i <= 10; ++i) {
tp_assert(pq.top() == i, "Incorrect element");
pq.pop();
}
tp_assert(pq.empty(), "Incorrect size");
}
int main() {
pq_test();
return 0;
}

The above code initializes TPIE, sets the memory limit to 1 GB, initializes a priority queue, pushes 10 elements onto it, verifies that they are returned in the correct order.

Place the code file in your tpie project folder, and on Linux, compile with:

$ g++ -I. -Ibuild -c pq.cpp -Wall -Wextra -O3
$ g++ pq.o -o pq build/tpie/libtpie.a -lboost_thread-mt -lboost_filesystem-mt -lboost_system-mt
tpie::resource_manager::set_limit
void set_limit(size_t new_limit)
Update the resource limit.
tpie_assert.h
tpie::get_memory_manager
TPIE_EXPORT memory_manager & get_memory_manager()
Return a reference to the memory manager.
priority_queue.h
External memory priority queue implementation.
tpie::priority_queue
External memory priority queue implementation. The top of the queue is the least element in the speci...
Definition: priority_queue.h:78
tpie::priority_queue::empty
bool empty() const
Return true if queue is empty otherwise false.
tpie::tpie_init
TPIE_EXPORT void tpie_init(flags< subsystem > subsystems=ALL)
Initialize the given subsystems of TPIE.
tpie.h
tp_assert
#define tp_assert(condition, message)
Definition: tpie_assert.h:65
tpie::priority_queue::pop
void pop()
Remove the top element from the priority queue.
tpie::priority_queue::size
stream_size_type size() const
Returns the size of the queue.
tpie::priority_queue::top
const T & top()
See what's on the top of the priority queue.
tpie::priority_queue::push
void push(const T &x)
Insert an element into the priority queue.
tpie::tpie_finish
TPIE_EXPORT void tpie_finish(flags< subsystem > subsystems=ALL)
Deinitialize the given subsystems of TPIE.
tpie
Definition: access_type.h:26