Go to the source code of this file.
| Vector* NewVector |
( |
unsigned long int |
elementSize | ) |
|
Create a new vector.
- Parameters
-
| elementSize | The 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
-
| *vec | A pointer to the vector. |
| *element | A 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
-
| *vec | A 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
-
| *vec | A pointer to the vector. |
| position | The 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.
Delete the vector and its elements.
- Parameters
-
| *vec | A pointer to the vector. |
- Returns
- NULL is returned Deletes the vector and its elements, deallocating both. Example :
1 Vector *vec= new Vector(sizeof(int));
Definition at line 82 of file vector.c.