A 100% typesafe generic vector for C.
This module contains macros, types and functions for MX_VECTOR whic is a a 100% typesafe generic vector object for C. The underlying type must be bit-copy-correct which means that a binary copy retains proper functioning and takes over ownership of any internal buffers.
The interface of MX_VECTOR is done through macros and generic type-indenpendent functions perform the vector operations. In contrast to C++ vector types, the code size when using MX_VECTOR is constant regardless of how many vector types are instantiated.
The correct operation of MX_VECTOR depends on some things that not in the C standard:
MX_VECTOR objects themselves are bit-copy-correct which means that memory containing a MX_VECTOR can be safely realloc'd and MX_VECTOR can be used inside another MXVECTOR.
Here is a simple example for doubles but the same concept applies to all bit-copy-correct types.
size_t i; MX_VECTOR(double) dvect; mx_vector(&dvect); mx_vector_resize(&dvect, 100); for (i=0; i < mx_vector_size(&dvect); i++) dvect.data[i] = i; mx_vector_free(&dvect);NOTE: All of the mx_vector* functions are in fact macros that may evaluate the arguments (the vector) more than once. This is especially true for the vector itself (the first argument) and any pointer arguments. Efforts have been made to evaluate other arguments only once. So modifiying arguments in the macro like this:
mx_vector_resize(&dvect, n++);will work as expected. However code like this:
mx_vector_resize(vectptr++, n);will not work as expected. (../include/deds/vector.h)
Part of the deds library.
#define MX_VECTOR(type)
A 100% typesafe generic vector for C
This macro functions as a type delaration for a vector of user-defined type. (../include/deds/vector.h)
#define MX_VECTOR_ITERATOR(type)
An iterator for a MX_VECTOR
This macro functions as a type declaration for an iterator for a similarly typed MX_VECTOR. (../include/deds/vector.h)
#define mx_vector(mxvect)
Construct a MX_VECTOR
This macro constructs a MX_VECTOR object. This must be called first before any other mx_vector_* functions are called.
MX_VECTOR is correctly initialized with a 0 bit pattern. So static MX_VECTOR are correctly initialized and dont necessarily need a call to the constructor. (../include/deds/vector.h)
#define mx_vector_free(mxvect)
Destruct a MX_VECTOR
This macro destroys an MX_VECTOR object by releasing any memory that has been allocated. It should be the last call to any mx_vector_* functions. Access of the MX_VECTOR object after destruction should not be done. (../include/deds/vector.h)
#define mx_vector_at(mxvect, mxn)
Return an element from a MX_VECTOR at a specific index
This macro returns elements from a MX_VECTOR at a specific index. It is safer than accessing the underlying pointer because it wont allow access outside the correct range for the vector. (../include/deds/vector.h)
#define mx_vector_back(mxvect)
Return the last element from a MX_VECTOR
This macro returns the last element from a MX_VECTOR. (../include/deds/vector.h)
#define mx_vector_begin(mxvect)
Pointer to first element
This macro returns a pointer to the first data element in the MX_VECTOR object. If the MX_VECTOR contains no data elements then 0 is returned. (../include/deds/vector.h)
#define mx_vector_capacity(mxvect)
Capacity
This macro returns the capacity of an MX_VECTOR object. This is the number of elements that the vector can contain without reqriring any internal memory management. (../include/deds/vector.h)
#define mx_vector_clear(mxvect)
Clear
This macro deletes all the objects from a MX_VECTOR (../include/deds/vector.h)
#define mx_vector_empty(mxvect)
Empty
This macro deletes all the objects from a MX_VECTOR (../include/deds/vector.h)
#define mx_vector_end(mxvect)
Pointer one-past last element
This macro returns a pointer one-past the last data element in the MX_VECTOR object. If the MX_VECTOR contains no data elements then 0 is returned. (../include/deds/vector.h)
#define mx_vector_erase(mxvect, mxiter)
Erase an element
This macro removes a data element from an MX_VECTOR object. If there are data elements at indicies higher than those removed then they are are moved to lower indicies. (../include/deds/vector.h)
#define mx_vector_push_back(mxvect, mxdat)
Push an element onto the back of the MX_VECTOR
This macro makes a bitwise copy of a single data element and appends the copy onto an existing MX_VECTOR object. The size of the vector is expanded to hold the new data elements. (../include/deds/vector.h)
#define mx_vector_size(mxvect)
Number of elements
This macro returns the size of an MX_VECTOR object. This is the number of data elements that can be safely accessed. (../include/deds/vector.h)
#define mx_vector_resize(mxvect, n)
Resize
This macro resizes a MX_VECTOR object. If the MX_VECTOR does not have sufficient capacity to hold the new number of elements then new space is reserved using mx_vector_reserve(). (../include/deds/vector.h)
#define mx_vector_reserve(mxvect, n)
Set capacity
This macro reserves space in a MX_VECTOR object for later copying or appending of data elements. If enough space is reserved the insertion or appending of elements does not require memory management (realloc of internal buffer) and will be fast. If enough space is already reserved this macro does nothing. (../include/deds/vector.h)
#define mx_vector_extend(mxvect, n)
Increase capacity
This macro reserves space for additional data elements in a MX_VECTOR object. If enough space is reserved then future insertion or appending may not require memory management (realloc of internal buffer) and will therefore be fast. If enough space is already reserved this macro does nothing. (../include/deds/vector.h)
#define mx_vector_contract(mxvect)
Reduce capacity to minimum
This macro reduces the memory used by a MX_VECTOR object to the minimum required to safely contain the data elements contained. This function can cause memory management (realloc of internal buffer) to occur. (../include/deds/vector.h)
#define mx_vector_append1(mxvect, dat)
Append 1 element (bitwise copy)
This macro makes a bitwise copy of a single data element and appends the copy onto an existing MX_VECTOR object. The size of the vector is expanded to hold the new data elements. (../include/deds/vector.h)
#define mx_vector_append(mxvect, dat, n)
Append a number of elements (bitwise copy)
This macro makes a bitwise copy of a number of data elements and appends the copies onto an existing MX_VECTOR object. More than one data element can be appended at once. If necessary the size of the vector is expanded to hold the new data elements. (../include/deds/vector.h)
#define mx_vector_insert1(mxvect, dat, ind)
Insert 1 element (bitwise copy)
This macro makes a bitwise copy of one data element and inserts the copy into an existing MX_VECTOR object. If necessary the size of the vector is expanded to hold the new data element. (../include/deds/vector.h)
#define mx_vector_insert(mxvect, dat, n, ind)
Insert a number of elements (bitwise copy)
This macro makes a bitwise copy of a number of data elements and inserts the copies into an existing MX_VECTOR object at sepcific indicies. More than one data element can be inserted at once. If necessary the size of the vector is expanded to hold the new data elements. (../include/deds/vector.h)
#define mx_vector_remove_last(mxvect)
Remove last element
This macro removes only the last object from a vector. The size of the MX_VECTOR is reduced by one. (../include/deds/vector.h)
size_t mx__vector_makeroom(MX__VECTOR_DATA* vect, const size_t num, const size_t place, const size_t sizeofdata)
During appending we expand vectors by doubling thier size with a minimum of 16 but a different value might be better in some applications. Can a reasonable API be made for this? (../include/deds/vector/makeroom.c)
Generated by MXDOC 2.2 on Sun Feb 4 15:16:26 2007