The dynamically growable array.
More...
Go to the source code of this file.
|
struct | Vector |
| The implementation for dynamically growable array. More...
|
|
|
typedef struct _VectorData | VectorData |
| VectorData is the data type for the container private information. More...
|
|
|
int32_t | VectorInit (Vector **ppObj, int32_t iCap) |
| The constructor for Vector. More...
|
|
void | VectorDeinit (Vector **ppObj) |
| The destructor for Vector. More...
|
|
int32_t | VectorPushBack (Vector *self, Item item) |
| Push an item to the tail of the vector. More...
|
|
int32_t | VectorInsert (Vector *self, Item item, int32_t iIdx) |
| Insert an item to the designated index of the vector. More...
|
|
int32_t | VectorPopBack (Vector *self) |
| Pop an item from the tail of the vector. More...
|
|
int32_t | VectorDelete (Vector *self, int32_t iIdx) |
| Delete an item from the designated index of the vector. More...
|
|
int32_t | VectorSet (Vector *self, Item item, int32_t iIdx) |
| Set an item at the designated index of the vector. More...
|
|
int32_t | VectorGet (Vector *self, Item *pItem, int32_t iIdx) |
| Get an item from the designated index of the vector. More...
|
|
int32_t | VectorResize (Vector *self, int32_t iCap) |
| Change the container capacity. More...
|
|
int32_t | VectorSize (Vector *self) |
| Return the number of stored items. More...
|
|
int32_t | VectorCapacity (Vector *self) |
| Return the container capacity. More...
|
|
int32_t | VectorSort (Vector *self, int32_t(*pFunc)(const void *, const void *)) |
| Sort the items via the designated item comparison method. More...
|
|
int32_t | VectorIterate (Vector *self, bool bReset, Item *pItem) |
| Iterate through the vector till the tail end. More...
|
|
int32_t | VectorReverseIterate (Vector *self, bool bReset, Item *pItem) |
| Reversely iterate through the vector till the head end. More...
|
|
int32_t | VectorSetDestroy (Vector *self, void(*pFunc)(Item)) |
| Set the custom item resource clean method. More...
|
|
The dynamically growable array.
Definition in file vector.h.
VectorData is the data type for the container private information.
Definition at line 11 of file vector.h.
int32_t VectorInit |
( |
Vector ** |
ppObj, |
|
|
int32_t |
iCap |
|
) |
| |
The constructor for Vector.
- Parameters
-
ppObj | The double pointer to the to be constructed vector |
iCap | The designated initial capacity |
- Return values
-
SUCC | |
ERR_NOMEM | Insufficient memory for vector construction |
- Note
- Specify iCap to 0 for default initial capacity.
void VectorDeinit |
( |
Vector ** |
ppObj | ) |
|
The destructor for Vector.
If the custom resource clean method is set, it also runs the clean method for all the items.
- Parameters
-
ppObj | The double pointer to the to be destructed vector |
int32_t VectorPushBack |
( |
Vector * |
self, |
|
|
Item |
item |
|
) |
| |
Push an item to the tail of the vector.
This function pushes an item to the tail of the vector with the corresponding vector size extension. If the storage is full, the space reallocation is automatically triggered to store the newly pushed item.
- Parameters
-
self | The pointer to the Vector structure |
item | The designated item |
- Return values
-
SUCC | |
ERR_NOINIT | Uninitialized container |
ERR_NOMEM | Insufficient memory for vector extension |
int32_t VectorInsert |
( |
Vector * |
self, |
|
|
Item |
item, |
|
|
int32_t |
iIdx |
|
) |
| |
Insert an item to the designated index of the vector.
This function inserts an item to the designated index of the vector and shifts the trailing items one position to the tail. Naturally, the vector size is extended. And if the storage is full, the space reallocation is automatically triggered to store the newly inserted item.
- Parameters
-
self | The pointer to the Vector structure |
item | The designated item |
iIdx | The designated index |
- Return values
-
SUCC | |
ERR_NOINIT | Uninitialized container |
ERR_IDX | Illegal index |
- Note
- The designated index should be equal to or smaller than the vector size and should not be negative. If the index is equal to the vector size, the effect is equivalent to push_back().
int32_t VectorPopBack |
( |
Vector * |
self | ) |
|
Pop an item from the tail of the vector.
This function removes an item from the tail of the vector. If the custom resource clean method is set, it also runs the clean method for the removed item. Naturally, the vector size is shrunk.
- Parameters
-
self | The pointer to the Vector structure |
- Return values
-
SUCC | |
ERR_NOINIT | Uninitialized container |
ERR_IDX | Empty vector |
int32_t VectorDelete |
( |
Vector * |
self, |
|
|
int32_t |
iIdx |
|
) |
| |
Delete an item from the designated index of the vector.
This function removes an item from the designated index of the vector and shifts the trailing items one position to the head. If the custom resource clean method is set, it also runs the clean method for the removed item. Naturally, the vector size is shrunk.
- Parameters
-
self | The pointer to the Vector structure |
iIdx | The designated index |
- Return values
-
SUCC | |
ERR_NOINIT | Uninitialized container |
ERR_IDX | Illegal index |
- Note
- The designated index should be smaller than the vector size and should not be negative. If the index is equal to the vector size minus one, the operation is equivalent to pop_back().
int32_t VectorSet |
( |
Vector * |
self, |
|
|
Item |
item, |
|
|
int32_t |
iIdx |
|
) |
| |
Set an item at the designated index of the vector.
This function replaces an item at the designated index of the vector. If the custom resource clean method is set, it also runs the clean method for the replaced item.
- Parameters
-
self | The pointer to the Vector structure |
item | The designated item |
iIdx | The designated index |
- Return values
-
SUCC | |
ERR_NOINIT | Uninitialized container |
ERR_IDX | Illegal index |
- Note
- The designated index should be smaller than the vector size and should not be negative.
int32_t VectorGet |
( |
Vector * |
self, |
|
|
Item * |
pItem, |
|
|
int32_t |
iIdx |
|
) |
| |
Get an item from the designated index of the vector.
This function retrieves an item from the designated index of the vector. If the index is illegal, the error code is returned and the second parameter is also updated with NULL.
- Parameters
-
self | The pointer to the Vector structure |
pItem | The pointer to the returned item |
iIdx | The designated index |
- Return values
-
SUCC | |
ERR_NOINIT | Uninitialized container |
ERR_IDX | Illegal index |
ERR_GET | Invalid parameter to store returned item |
- Note
- The designated index should be smaller than the vector size and should not be negative.
int32_t VectorResize |
( |
Vector * |
self, |
|
|
int32_t |
iCap |
|
) |
| |
Change the container capacity.
This function resizes the storage capacity. If the new capacity is smaller than the old size. The trailing items will be removed. If the custom resource clean method is set, it also runs the clean method for the removed items.
- Parameters
-
self | The pointer to the Vector structure |
iCap | The designated capacity |
- Return values
-
SUCC | |
ERR_NOINIT | Uninitialized container |
ERR_NOMEM | Insufficient memory for vector extension |
- Note
- The designated capacity should greater than zero.
int32_t VectorSize |
( |
Vector * |
self | ) |
|
Return the number of stored items.
- Parameters
-
self | The pointer to the Vector structure |
- Returns
- The number of stored items
- Return values
-
ERR_NOINIT | Uninitialized container |
int32_t VectorCapacity |
( |
Vector * |
self | ) |
|
Return the container capacity.
- Parameters
-
self | The pointer to the Vector structure |
- Returns
- The container capacity
- Return values
-
ERR_NOINIT | Uninitialized container |
int32_t VectorSort |
( |
Vector * |
self, |
|
|
int32_t(*)(const void *, const void *) |
pFunc |
|
) |
| |
Sort the items via the designated item comparison method.
- Parameters
-
self | The pointer to the Vector structure |
pFunc | The function pointer to the custom method |
- Return values
-
SUCC | |
ERR_NOINIT | Uninitialized container |
int32_t VectorIterate |
( |
Vector * |
self, |
|
|
bool |
bReset, |
|
|
Item * |
pItem |
|
) |
| |
Iterate through the vector till the tail end.
Before iterating through the vector, it is necessary to pass bReset := true and pItem := NULL for iterator initialization. After initialization, you can pass bReset := false and pItem := the relevant pointer to get the returned item at each iteration.
- Parameters
-
self | The pointer to the Vector structure |
bReset | The knob to reset the iteration |
pItem | The pointer to the returned item |
- Return values
-
SUCC | |
END | At the tail end of the vector |
ERR_NOINIT | Uninitialized container |
ERR_GET | Invalid parameter to store returned item |
int32_t VectorReverseIterate |
( |
Vector * |
self, |
|
|
bool |
bReset, |
|
|
Item * |
pItem |
|
) |
| |
Reversely iterate through the vector till the head end.
Before reversely iterating through the vector, it is necessary to pass bReset := true and pItem := NULL for iterator initialization. After initialization, you can pass bReset := false and pItem := the relevant pointer to get the returned item at each iteration.
- Parameters
-
self | The pointer to the Vector structure |
bReset | The knob to reset the iteration |
pItem | The pointer to the returned item |
- Return values
-
SUCC | |
END | At the tail end of the vector |
ERR_NOINIT | Uninitialized container |
ERR_GET | Invalid parameter to store returned item |
int32_t VectorSetDestroy |
( |
Vector * |
self, |
|
|
void(*)(Item) |
pFunc |
|
) |
| |
Set the custom item resource clean method.
- Parameters
-
self | The pointer to the Vector structure |
pFunc | The function pointer to the custom method |
- Return values
-
SUCC | |
ERR_NOINIT | Uninitialized container |