binary heap More...
#include "cgul_common.h"
#include "cgul_exception.h"
#include "cgul_heap_primitives.h"
#include "cgul_vector.h"
Typedefs | |
typedef typedefCGUL_BEGIN_C struct cgul_heap * | cgul_heap_t |
Functions | |
CGUL_EXPORT cgul_heap_t | cgul_heap__new (cgul_exception_t *cex, cgul_heap_primitives__compare_t cf) |
CGUL_EXPORT cgul_heap_t | cgul_heap__new_from_vector (cgul_exception_t *cex, cgul_vector_t v, cgul_heap_primitives__compare_t cf) |
CGUL_EXPORT void | cgul_heap__delete (cgul_heap_t heap) |
CGUL_EXPORT void | cgul_heap__free_values (cgul_heap_t heap) |
CGUL_EXPORT int | cgul_heap__is_empty (cgul_exception_t *cex, cgul_heap_t heap) |
CGUL_EXPORT void | cgul_heap__push (cgul_exception_t *cex, cgul_heap_t heap, void *value) |
CGUL_EXPORT void * | cgul_heap__pop (cgul_exception_t *cex, cgul_heap_t heap) |
CGUL_EXPORT void * | cgul_heap__top (cgul_exception_t *cex, cgul_heap_t heap) |
CGUL_EXPORT unsigned long int | cgul_heap__get_size (cgul_exception_t *cex, cgul_heap_t heap) |
CGUL_EXPORT void | cgul_heap__swap (cgul_exception_t *cex, cgul_heap_t heap1, cgul_heap_t heap2) |
Binary heap implementation. This class can be used to trivially implement a priority queue. By default, this class implements a max-heap but can be used as a min-heap simply by negating the output of the comparison function.
typedef typedefCGUL_BEGIN_C struct cgul_heap* cgul_heap_t |
Opaque pointer to a cgul_heap
instance.
CGUL_EXPORT cgul_heap_t cgul_heap__new | ( | cgul_exception_t * | cex, |
cgul_heap_primitives__compare_t | cf | ||
) |
Create a new cgul_heap
object that maintains heap order using the comparison function cf
. The caller is responsible for freeing the object by calling cgul_heap__delete()
. If memory cannot be allocated, NULL
is returned, and an exception is thrown.
cgul_heap
is by default a max-heap, but it can be used as a min-heap simply by negating the output of the comparison function. [in,out] | cex | c-style exception |
[in] | cf | comparison function |
cgul_heap
instance CGUL_EXPORT cgul_heap_t cgul_heap__new_from_vector | ( | cgul_exception_t * | cex, |
cgul_vector_t | v, | ||
cgul_heap_primitives__compare_t | cf | ||
) |
Create a new cgul_heap
object from the vector v
. The new heap maintains heap order using the comparison function cf
. The caller is responsible for freeing the object by calling cgul_heap__delete()
. This class takes ownership of the vector so that calling cgul_heap__delete()
also deletes the vector. If memory cannot be allocated, NULL
is returned, and an exception is thrown.
cgul_heap
is by default a max-heap, but it can be used as a min-heap simply by negating the output of the comparison function. [in,out] | cex | c-style exception |
[in] | v | vector |
[in] | cf | comparison function |
cgul_heap
instance Referenced by cgul_heap_cxx::set_obj().
CGUL_EXPORT void cgul_heap__delete | ( | cgul_heap_t | heap | ) |
This method frees all internally allocated memory. This method does not free the elements stored in the heap. The caller allocated those elements so the caller is responsible for freeing them. Do not try to access heap
after calling this method.
[in] | heap | cgul_heap instance |
Referenced by cgul_heap_cxx::set_obj(), and cgul_heap_cxx::~cgul_heap_cxx().
CGUL_EXPORT void cgul_heap__free_values | ( | cgul_heap_t | heap | ) |
This method calls free()
on all the values in the heap heap
. Because this is such a common operation, it is an exception to the rule that cgul containers never free values. This method should only ever be called immediately before calling cgul_heap__delete()
because it otherwise invalidates the heap. Because this function is basically an extension of cgul_heap__delete()
, it does not accept a cgul_exception
object as an input parameter.
[in] | heap | cgul_heap instance |
Referenced by cgul_heap_cxx::free_values().
CGUL_EXPORT int cgul_heap__is_empty | ( | cgul_exception_t * | cex, |
cgul_heap_t | heap | ||
) |
Return true if the heap heap
is empty.
[in] | cex | c-style exception |
[in] | heap | cgul_heap instance |
heap
is empty Referenced by cgul_heap_cxx::is_empty().
CGUL_EXPORT void cgul_heap__push | ( | cgul_exception_t * | cex, |
cgul_heap_t | heap, | ||
void * | value | ||
) |
Push the value value
onto the heap heap
maintaining heap order using the some comparison function that was passed into the heap constructor. If an error occurs, an exception is thrown.
[in,out] | cex | c-style exception |
[in] | heap | cgul_heap instance |
[in] | value | to push onto the heap |
Referenced by cgul_heap_cxx::push().
CGUL_EXPORT void* cgul_heap__pop | ( | cgul_exception_t * | cex, |
cgul_heap_t | heap | ||
) |
Pop the top value from the heap heap
maintaining heap order using the some comparison function that was passed into the heap constructor. If the heap is empty, NULL
is returned.
[in] | cex | c-style exception |
[in] | heap | cgul_heap instance |
Referenced by cgul_heap_cxx::pop().
CGUL_EXPORT void* cgul_heap__top | ( | cgul_exception_t * | cex, |
cgul_heap_t | heap | ||
) |
Return the top value from the heap heap
without removing it from the heap. If the heap is empty, NULL
is returned.
[in] | cex | c-style exception |
[in] | heap | cgul_heap instance |
Referenced by cgul_heap_cxx::top().
CGUL_EXPORT unsigned long int cgul_heap__get_size | ( | cgul_exception_t * | cex, |
cgul_heap_t | heap | ||
) |
Return the size of the heap heap
. This is a count of the total number of elements stored in the heap.
[in] | cex | c-style exception |
[in] | heap | cgul_heap instance |
Referenced by cgul_heap_cxx::get_size().
CGUL_EXPORT void cgul_heap__swap | ( | cgul_exception_t * | cex, |
cgul_heap_t | heap1, | ||
cgul_heap_t | heap2 | ||
) |
Swap the underlying data for heap1
and heap2
. For large heaps, this should be much faster than trying to do the same thing using removes and inserts.
[in] | cex | c-style exception |
[in] | heap1 | first cgul_heap instance |
[in] | heap2 | second cgul_heap instance |
Referenced by cgul_heap_cxx::swap().