LibCDS
vector.h File Reference

The dynamically growable array. More...

Go to the source code of this file.

Data Structures

struct  Vector
 The implementation for dynamically growable array. More...
 

Typedefs

typedef struct _VectorData VectorData
 VectorData is the data type for the container private information. More...
 

Functions

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...
 

Detailed Description

The dynamically growable array.

Definition in file vector.h.

Typedef Documentation

typedef struct _VectorData VectorData

VectorData is the data type for the container private information.

Definition at line 11 of file vector.h.

Function Documentation

int32_t VectorInit ( Vector **  ppObj,
int32_t  iCap 
)

The constructor for Vector.

Parameters
ppObjThe double pointer to the to be constructed vector
iCapThe designated initial capacity
Return values
SUCC
ERR_NOMEMInsufficient 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
ppObjThe 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
selfThe pointer to the Vector structure
itemThe designated item
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NOMEMInsufficient 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
selfThe pointer to the Vector structure
itemThe designated item
iIdxThe designated index
Return values
SUCC
ERR_NOINITUninitialized container
ERR_IDXIllegal 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
selfThe pointer to the Vector structure
Return values
SUCC
ERR_NOINITUninitialized container
ERR_IDXEmpty 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
selfThe pointer to the Vector structure
iIdxThe designated index
Return values
SUCC
ERR_NOINITUninitialized container
ERR_IDXIllegal 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
selfThe pointer to the Vector structure
itemThe designated item
iIdxThe designated index
Return values
SUCC
ERR_NOINITUninitialized container
ERR_IDXIllegal 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
selfThe pointer to the Vector structure
pItemThe pointer to the returned item
iIdxThe designated index
Return values
SUCC
ERR_NOINITUninitialized container
ERR_IDXIllegal index
ERR_GETInvalid 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
selfThe pointer to the Vector structure
iCapThe designated capacity
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NOMEMInsufficient memory for vector extension
Note
The designated capacity should greater than zero.
int32_t VectorSize ( Vector self)

Return the number of stored items.

Parameters
selfThe pointer to the Vector structure
Returns
The number of stored items
Return values
ERR_NOINITUninitialized container
int32_t VectorCapacity ( Vector self)

Return the container capacity.

Parameters
selfThe pointer to the Vector structure
Returns
The container capacity
Return values
ERR_NOINITUninitialized container
int32_t VectorSort ( Vector self,
int32_t(*)(const void *, const void *)  pFunc 
)

Sort the items via the designated item comparison method.

Parameters
selfThe pointer to the Vector structure
pFuncThe function pointer to the custom method
Return values
SUCC
ERR_NOINITUninitialized 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
selfThe pointer to the Vector structure
bResetThe knob to reset the iteration
pItemThe pointer to the returned item
Return values
SUCC
ENDAt the tail end of the vector
ERR_NOINITUninitialized container
ERR_GETInvalid 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
selfThe pointer to the Vector structure
bResetThe knob to reset the iteration
pItemThe pointer to the returned item
Return values
SUCC
ENDAt the tail end of the vector
ERR_NOINITUninitialized container
ERR_GETInvalid parameter to store returned item
int32_t VectorSetDestroy ( Vector self,
void(*)(Item)  pFunc 
)

Set the custom item resource clean method.

Parameters
selfThe pointer to the Vector structure
pFuncThe function pointer to the custom method
Return values
SUCC
ERR_NOINITUninitialized container