cgul_heap_cxx Class Reference

C++ bindings for cgul_heap More...

#include <cgul_heap_cxx.h>

Collaboration diagram for cgul_heap_cxx:
Collaboration graph

Public Member Functions

 cgul_heap_cxx (cgul_heap_primitives_cxx::compare_t cf)
 
 cgul_heap_cxx (cgul_vector_cxx &v, cgul_heap_primitives_cxx::compare_t cf)
 
 cgul_heap_cxx (cgul_heap_t rhs)
 
virtual ~cgul_heap_cxx ()
 
virtual void free_values ()
 
virtual int is_empty () const
 
virtual void push (void *value)
 
virtual void * pop ()
 
virtual void * top ()
 
virtual unsigned long int get_size () const
 
virtual void swap (cgul_heap_cxx &rhs)
 
virtual cgul_heap_t get_obj () const
 
virtual cgul_heap_t take_obj ()
 
virtual void set_obj (cgul_heap_t rhs)
 

Detailed Description

This class provides the C++ bindings for C cgul_heap objects. The main purpose of this class is to convert the C-style function calls and exception handling in cgul_heap into C++-style function calls and exception handling.

See also
cgul_heap_t

Constructor & Destructor Documentation

§ cgul_heap_cxx() [1/3]

cgul_heap_cxx::cgul_heap_cxx ( cgul_heap_primitives_cxx::compare_t  cf)
inline

Create a new cgul_heap_cxx object that maintains heap order using the comparison function cf. The caller is responsible for freeing the object by calling delete on the object. If memory cannot be allocated, an exception is thrown.

Note
cgul_heap_cxx is by default a max-heap, but it can be used as a min-heap simply by negating the output of the comparison function.
Parameters
[in]cfcomparison function
Returns
new cgul_heap_cxx instance

§ cgul_heap_cxx() [2/3]

cgul_heap_cxx::cgul_heap_cxx ( cgul_vector_cxx v,
cgul_heap_primitives_cxx::compare_t  cf 
)
inline

Create a new cgul_heap_cxx 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 delete on the object. This class takes ownership of the underlying cgul_vector so that deleting the object also deletes the underlying vector. If memory cannot be allocated, an exception is thrown.

Note
cgul_heap_cxx is by default a max-heap, but it can be used as a min-heap simply by negating the output of the comparison function.
Note
The asymptotic complexity of this method is O(n) which is more efficient than creating an empty heap and inserting n elements which would be O(n log n).
Parameters
[in]vvector
[in]cfcomparison function

§ cgul_heap_cxx() [3/3]

cgul_heap_cxx::cgul_heap_cxx ( cgul_heap_t  rhs)
inline

Create a new cgul_heap_cxx object by wrapping an existing cgul_heap object.

Parameters
[in]rhsright-hand side

§ ~cgul_heap_cxx()

virtual cgul_heap_cxx::~cgul_heap_cxx ( )
inlinevirtual

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.

See also
free_values()

References cgul_heap__delete().

Member Function Documentation

§ free_values()

virtual void cgul_heap_cxx::free_values ( )
inlinevirtual

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 delete on the object because it otherwise invalidates the heap.

References cgul_heap__free_values().

§ is_empty()

virtual int cgul_heap_cxx::is_empty ( ) const
inlinevirtual

Return true if the heap is empty.

Returns
whether the heap is empty

References cgul_heap__is_empty().

§ push()

virtual void cgul_heap_cxx::push ( void *  value)
inlinevirtual

Push the value value onto the heap maintaining heap order using the some comparison function that was passed into the heap constructor. If an error occurs, an exception is thrown.

Parameters
[in]valueto push onto the heap

References cgul_heap__push().

§ pop()

virtual void* cgul_heap_cxx::pop ( )
inlinevirtual

Pop the top value from the heap maintaining heap order using the some comparison function that was passed into the heap constructor. If the heap is empty, NULL is returned.

Returns
top value

References cgul_heap__pop().

§ top()

virtual void* cgul_heap_cxx::top ( )
inlinevirtual

Return the top value from the heap without removing it from the heap. If the heap is empty, NULL is returned.

Returns
top value

References cgul_heap__top().

§ get_size()

virtual unsigned long int cgul_heap_cxx::get_size ( ) const
inlinevirtual

Return the size of the heap. This is a count of the total number of elements stored in the heap.

Returns
size of the heap

References cgul_heap__get_size().

§ swap()

virtual void cgul_heap_cxx::swap ( cgul_heap_cxx rhs)
inlinevirtual

Swap the underlying data for this object and rhs. For large heaps, this should be much faster than trying to do the same thing using removes and inserts.

Parameters
[in]rhsright-hand side

References cgul_heap__swap().

§ get_obj()

virtual cgul_heap_t cgul_heap_cxx::get_obj ( ) const
inlinevirtual

Get the underlying cgul_heap object.

Returns
underlying object

§ take_obj()

virtual cgul_heap_t cgul_heap_cxx::take_obj ( )
inlinevirtual

Take the underlying cgul_heap object. This means the underlying object will not be deleted when the wrapper goes out of scope. Also, because you have taken the underlying object, no other methods should be called on this wrapper's instance. Lastly, after taking the underlying object, it is the caller's responsibility to delete the underlying object by calling cgul_heap__delete().

Returns
underlying object

§ set_obj()

virtual void cgul_heap_cxx::set_obj ( cgul_heap_t  rhs)
inlinevirtual

Set the new underlying object to rhs. This causes the old underlying object to be deleted which invalidates any outstanding pointers to or iterators for the old underlying object.

This instance takes ownership of rhs which means rhs will be automatically deleted when the C++ wrapper is deleted. To prevent automatic deletion of rhs, call take_obj() when the C++ wrapper is no longer needed.

Parameters
[in]rhsright-hand side

References cgul_heap__delete(), cgul_heap__new_from_vector(), and cgul_vector_cxx::take_obj().


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