Simple C Variable Size Vector
 All Data Structures Files Functions Variables Typedefs Pages
Data Structures | Typedefs | Functions
vector.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  vector
 The vector data structre. More...
 

Typedefs

typedef struct vector Vector
 

Functions

VectorNewVector (unsigned long int elementSize)
 Create a new vector. More...
 
int VectorAppendCopy (Vector *vec, void *element)
 Inserts a new element at the end of the vector. More...
 
int VectorAllocateOne (Vector *vec)
 Allocates space for at least one more element. More...
 
void * VectorGetElement (Vector *vec, int position)
 Gets the element at informed position. More...
 
VectorDeleteVector (Vector *vec)
 Delete the vector and its elements. More...
 

Typedef Documentation

typedef struct vector Vector

Definition at line 37 of file vector.h.

Function Documentation

Vector* NewVector ( unsigned long int  elementSize)

Create a new vector.

Parameters
elementSizeThe size of one element of the vector.
Returns
A new vector wich each element have size elementSize. A new vector is created, the size of each element is elementSize.
If the Vector allocation fails, NULL is returned.
The best way is to use with sizeof, like:
1 Vector *vec= NewVector( sizeof ( int ) );

Definition at line 23 of file vector.c.

int VectorAppendCopy ( Vector vec,
void *  element 
)

Inserts a new element at the end of the vector.

Parameters
*vecA pointer to the vector.
*elementA pointer to the element wich value will be copied to the vector.
Returns
Returns the the position of the value passed as argument, with the new element at the end of the vector. Inserts a new element at the end of the vector, expanding the vector size if necessary.
If the allocation fails, an error message is send to stderr and exit is called.

Example:

1 int newElementPosition= VectorAppendCopy( vec, &value );

The example above inserts an element at the end of the vector and assigns to newElementPosition its position.

Definition at line 59 of file vector.c.

int VectorAllocateOne ( Vector vec)

Allocates space for at least one more element.

Parameters
*vecA pointer to the vector.
Returns
Returns the position in the vector allocated. May be used with VectorGetElement. Allocate space in the vector for at least one more element, expanding the vector size if necessary. The allocated position is uninitialized.
If the allocation fails, an error message is send to stderr and exit is called.
Its return value may be used with VectorGetElement.
Example:
1 Vector *vec= new Vector(sizeof(int));
2  *( (int*) VectorGetElement(vec, VectorAllocateOne(vec) ) = 2;
The example above assigns 2 to the first position of the vector. The vector holds elements of type "int".

Definition at line 49 of file vector.c.

void* VectorGetElement ( Vector vec,
int  position 
)

Gets the element at informed position.

Parameters
*vecA pointer to the vector.
positionThe wanted vector position
Returns
Returns a pointer to the element of the vector at the position informed. Its return value may be used with VectorGetElement.
Since the pointer to the position is returned, you may assign new values to the position using the returned pointer.
Example 1:
1 Vector *vec= new Vector(sizeof(int));
2  *( (int*) VectorGetElement(vec, VectorAllocateOne(vec) ) = 2;
The example above assigns 2 to the first position of the vector. The vector holds elements of type "int".
To assign values you may deference the pointer and then assign. Or use memcpy, declared at string.h.

Definition at line 67 of file vector.c.

Vector* DeleteVector ( Vector vec)

Delete the vector and its elements.

Parameters
*vecA pointer to the vector.
Returns
NULL is returned Deletes the vector and its elements, deallocating both. Example :
1 Vector *vec= new Vector(sizeof(int));
2 DeleteVector(vec);

Definition at line 82 of file vector.c.