|
PE(DyArray);
//if the input parameter is invalid, return.
if(!pArr)
{
return NULL;
}
//malloc memory for dynamic array and iniatilize it.
pArr->m_fDestroy = pDataDestroy;
pArr->m_ppData = (void *)f_malloc(sizeof(void *)*MIN_PRE_ALLOCATE_SIZE);
if(!pArr->m_ppData)
{
free(pArr);
return NULL;
}
pArr->m_nAllocSize = MIN_PRE_ALLOCATE_SIZE;
return pArr;
}
/**************************************************************************************************
【函数名】: DyArrayGetByIndex
【描述】:获取数组元素by index
【参数】:
pArr: the array's address.
nIndex: the element's position.
ppData: out parameter, to record the element's pointer.
【返回值】: true or false.
***************************************************************************************************/
cp_bool DyArrayGetByIndex(DyArray* pArr, cp_int32 nIndex, void** ppData)
{
//if the input parameter is invalid, return.
if(!pArr || nIndex < 0 || !ppData || nIndex >= pArr->m_nSize)
{
*ppData = NULL;
return CP_FALSE;
}
*ppData = pArr->m_ppData[nIndex];//get the related element.
return CP_TRUE;
}
/**************************************************************************************************
【函数名】: DyArrayGetFirst
【描述】:获取数组第一个元素
【参数】:
pArr: the array's address.
ppData: out parameter, to record the element's pointer.
【返回值】: true or false.
***************************************************************************************************/
cp_bool DyArrayGetFirst(DyArray* pArr,void** ppData)
{
return DyArrayGetByIndex(pArr,0,ppData) ? CP_TRUE : CP_FALSE;
}
/**************************************************************************************************
【函数名】: DyArrayGetLast
【描述】: 获取数组最后一个元素
【参数】:
pArr: the array's address.
ppData: out parameter, to record the element's pointer.
【返回值】: true or false.
***************************************************************************************************/
cp_bool DyArrayGetLast(DyArray* pArr,void** ppData)
{
return DyArrayGetByIndex(pArr,pArr->m_nSize-1,ppData) ? CP_TRUE : CP_FALSE;
}
/**************************************************************************************************
【函数名】:DyArraySetByIndex
【描述】: 设置数组元素by index
【参数】:
pArr: the array's address.
nIndex: the element's position.
pData: the element's pointer.
【返回值】: true or false.
***************************************************************************************************/
cp_bool DyArraySetByIndex(DyArray* pArr, cp_int32 nIndex, void* pData)
{
//if the input parameter is invalid, return.
if(!pArr || nIndex < 0)
{
return CP_FALSE;
}
pArr->m_ppData[nIndex] = pData;//find the related position and set its value.
return CP_TRUE;
}
/**************************************************************************************************
【函数名】: DyArrayLength
【描述】:获取数组的长度
【参数】:
pArr: the array's address.
【返回值】: 数组长度.
***************************************************************************************************/
cp_int32 DyArrayLength(DyArray* pArr)
{
return pArr ? pArr->m_nSize : -1;
}
/**************************************************************************************************
【函数名】: DyArrayFind
【描述】:查找数组中的指定元素
【参数】:
pArr: the array's address.
pCmp: the callback function to compare the data.
pData: the searc |