C++ bindings for cgul_vector
More...
#include <cgul_vector_cxx.h>
Public Types | |
typedef cgul_vector__compare_t | compare_t |
typedef cgul_vector__next_in_range_t | next_in_range_t |
typedef int(* | fold_t) (void *value, void *data) |
Public Member Functions | |
cgul_vector_cxx () | |
cgul_vector_cxx (cgul_vector_t rhs) | |
virtual | ~cgul_vector_cxx () |
virtual void | free_values () |
virtual int | is_empty () const |
virtual void | assign (unsigned long int index, const void *ptr) |
virtual void | insert (unsigned long int index, const void *ptr) |
virtual void | erase (unsigned long int index) |
virtual void | erase_unordered (unsigned long int index) |
virtual void * | at (unsigned long int index) |
virtual void *& | operator[] (unsigned long int index) |
virtual void | push_back (const void *ptr) |
virtual void * | get_back () const |
virtual void * | pop_back () |
virtual void | sort (compare_t f) |
virtual void | shuffle (next_in_range_t f, void *prng) |
virtual void | clear () |
virtual void ** | get_array () const |
virtual void ** | take_array () |
virtual unsigned long int | get_size () const |
virtual void | trim () |
virtual size_t | get_minimum_capacity () const |
virtual void | set_minimum_capacity (size_t minimum_capacity) |
virtual void | swap (cgul_vector_cxx &rhs) |
virtual void | foldl (fold_t f, void *data) |
virtual void | foldr (fold_t f, void *data) |
virtual cgul_vector_t | get_obj () const |
virtual cgul_vector_t | take_obj () |
virtual void | set_obj (cgul_vector_t rhs) |
This class provides the C++ bindings for C cgul_vector
objects. The main purpose of this class is to convert the C-style function calls and exception handling in cgul_vector
into C++-style function calls and exception handling.
This typedef is the interface for the comparison function used by sort()
.
This type definition defines the interface to a pseudo-random number generator (PRNG) which shuffle()
uses to generate pseudo-random numbers that are used to shuffle the elements of the vector. The quality of the shuffle depends entirely upon the quality of the pseudo-random numbers returned.
Functions that implement this interface will be passed the same PRNG prng
that is passed into the shuffle function. The return value should be a pseudo-random integer in the half-open interval [0, range_max)
. If an error occurs when this function is called, the client should propagate the error via cex
.
Note that this interface closely resembles the signature of the cgul_random_*__next_in_range()
functions. So closely in fact, that it is usually not necessary to write a special callback to implement this function. Instead, this interface can be satisfied by casting cgul_random_*__next_in_range()
to have next_in_range_t
function type and pass in your PRNG object as prng
.
typedef int(* cgul_vector_cxx::fold_t) (void *value, void *data) |
|
inline |
Create a new cgul_vector_cxx
object. If memory cannot be allocated, an exception is thrown.
References cgul_vector__new().
Referenced by set_obj().
|
inline |
Create a new cgul_vector_cxx
object by wrapping an existing cgul_vector
object.
[in] | rhs | right-hand side |
|
inlinevirtual |
This method frees all internally allocated memory. This method does not free the elements stored in the vector. The caller allocated those elements so the caller is responsible for freeing them.
References cgul_vector__delete().
|
inlinevirtual |
This method calls free()
on all the values in the vector. 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
because it otherwise invalidates the vector.
References cgul_vector__free_values().
|
inlinevirtual |
Return true if the vector is empty.
References cgul_vector__is_empty().
|
inlinevirtual |
Assign ptr
to the slot specified by index
. If index
is out of bounds, an exception is thrown. Indexing is zero-based.
[in] | index | index into the vector |
[in] | ptr | pointer to assign |
References cgul_vector__assign().
|
inlinevirtual |
Insert ptr
at the slot specified by index
shifting other pointers to the right as necessary. If index
is out of bounds or if memory cannot be allocated, an exception is thrown. Indexing is zero-based.
It is legal to insert at the end of the vector such that index
is one beyond the last element. Typically, you will use push_back()
for inserting at the end however.
[in] | index | index into the vector |
[in] | ptr | pointer to insert |
References cgul_vector__insert().
|
inlinevirtual |
Erase the element at the slot specified by index
shifting other pointers to the left as necessary. If index
is out of bounds, an exception is thrown. Indexing is zero-based.
If it is not necessary to keep the elements in the underlying array in order, it is generally more efficient to call erase_unordered()
.
[in] | index | index into the vector |
References cgul_vector__erase().
|
inlinevirtual |
Erase the element at the slot specified by index
replacing it with the element at the back of the underlying array. If index
is out of bounds, an exception is thrown. Indexing is zero-based.
If it is necessary to keep the elements in the underlying array in order, erase()
should be used instead of this method.
[in] | index | index into the vector |
References cgul_vector__erase_unordered().
|
inlinevirtual |
This method returns the pointer at the slot specified by index
. If index
is out of bounds, an exception is thrown. Indexing is zero-based.
Unlike std::vector::at()
this method does not return a reference to the pointer. It just returns the pointer. This is much safer because the caller does not have to worry about the reference that is returned being invalidated when the vector is resized because of an insert operation.
Furthermore, the underlying implementation of this method is written in C which cannot return references because they are not part of the language. Thus, to be consistent, this method returns the same value as the underlying C implementation.
[in] | index | index into the vector |
index
References cgul_vector__at().
|
inlinevirtual |
This method returns a reference to the slot specified by index
making it possible to assign directly to the return value. This behavior is similiar to how std::vector::operator[]()
behaves.
The reference that is returned is only valid until the next insert operation so, if possible, use at()
instead of this method to avoid having your pointers invalidated unexpectedly.
As with std::vector::operator[]()
, this method does not bounds check index
. Thus, if index
is out of bounds, behavior is undefined. If bounds checking is desired, use at()
instead.
Indexing is zero-based.
[in] | index | index into the vector |
index
References cgul_vector__get_array().
|
inlinevirtual |
Append ptr
to the end of the vector. If memory cannot be allocated, an exception will be thrown.
[in] | ptr | pointer to add to the back of the vector |
References cgul_vector__push_back().
|
inlinevirtual |
Returns the pointer held in the last element of the vector without removing it from the vector. If there are no more elements, NULL
is returned. If you want to remove the last element from the vector, see pop_back()
.
References cgul_vector__get_back().
|
inlinevirtual |
Return and remove the pointer held in the last element of the vector. If there are no more elements, NULL
is returned. If you want to inspect the last element without removing it, see get_back()
.
References cgul_vector__pop_back().
|
inlinevirtual |
Sort the elements of the vector using the comparison function f
.
[in] | f | comparison function |
References cgul_vector__sort().
|
inlinevirtual |
Shuffle the elements of the vector using the function f
to generate the pseudo-random numbers that will be used to shuffle the vector. The PRNG prng
will be passed through to the PRNG callback function f
without modification. If an error occurs, an exception is thrown.
The following is an an example of how to use this method:
cgul_vector_cxx v; cgul_random_lfg32_cxx prng;
v.shuffle((cgul_vector_cxx::next_in_range_t) cgul_random_lfg32__next_in_range, prng.get_obj());
[in] | f | PRNG callback |
[in] | prng | PRNG |
References cgul_vector__shuffle().
|
inlinevirtual |
Remove all elements from the vector.
References cgul_vector__clear().
|
inlinevirtual |
Get the internal array. The client must not free the pointer that is returned because it is still owned by the vector. The pointer returned is only valid until the next insert operation.
References cgul_vector__get_array().
|
inlinevirtual |
Take ownership of the internal array. The client is responsible for calling free()
on the pointer returned.
Just prior to taking ownership, the internal state of the vector is reset. If an error occurs when attempting to reset the vector, the vector is left unaltered, NULL
is returned, and an exception is thrown.
References cgul_vector__take_array().
|
inlinevirtual |
Return the size of the vector. This is a count of the total number of elements stored in the vector.
References cgul_vector__get_size().
|
inlinevirtual |
When you are through adding elements, you can trim any extra allocated space by calling this method.
References cgul_vector__trim().
|
inlinevirtual |
Get the minimum capacity. The default value is 128
.
References cgul_vector__get_minimum_capacity().
|
inlinevirtual |
Set the minimum capacity. The default value is 128
. Decreasing the capacity will decrease the amount of space used and (typically) also decrease performance. If an error occurs, an exception is thrown.
[in] | minimum_capacity | minimum capacity |
References cgul_vector__set_minimum_capacity().
|
inlinevirtual |
Swap the underlying data for this object and rhs
. For large vectors, this should be much faster than trying to do the same thing using push_back()
.
[in] | rhs | right-hand side |
References cgul_vector__swap().
|
inlinevirtual |
This method performs a left fold of the vector with the combining function f
. f
is called once for each value in the vector starting at the front of the vector and iterating forward to the end of the vector.
The first parameter passed into f
is the current value. The second parameter passed into f
is the client data which is where the result of the fold should be accumulated.
f
must return true after each iteration in order for iteration to continue.
[in] | f | combining function |
[in] | data | client data passed to f |
References at(), and get_size().
|
inlinevirtual |
This method performs a right fold of the vector with the combining function f
. f
is called once for each value in the vector starting at the back of the vector and iterating backward to the front of the vector.
The first parameter passed into f
is the current value. The second parameter passed into f
is the client data which is where the result of the fold should be accumulated.
f
must return true after each iteration in order for iteration to continue.
[in] | f | combining function |
[in] | data | client data passed to f |
References at(), and get_size().
|
inlinevirtual |
Get the underlying cgul_vector
object.
|
inlinevirtual |
Take the underlying cgul_vector
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_vector__delete()
.
Referenced by cgul_heap_cxx::set_obj().
|
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.
[in] | rhs | right-hand side |
References cgul_vector__delete(), and cgul_vector_cxx().