设为首页 加入收藏

TOP

c语言实现动态指针数组Dynamic arrays(四)
2015-01-22 21:02:36 来源: 作者: 【 】 浏览:104
Tags:语言 实现 动态 指针 Dynamic arrays
h destination in the array. 【返回值】:如果成功返回位置,否则返回-1 ***************************************************************************************************/ cp_int32 DyArrayFind(DyArray* pArr, DataCmpFunc pCmp,void *pData) { cp_int32 i; //if the input parameter is invalid, return. if(!pArr) { return -1; } //visit each one to find the right one. for(i=0; i m_nSize;i++) { if(pCmp) { if(pCmp(pArr->m_ppData[i],pData) == CP_TRUE) { return i; } }else { //if NO compare funciton, just compare the address. if(pArr->m_ppData[i] == pData) { return i; } } } return -1; } /************************************************************************************************** 【函数名】: DyArrayForEach 【描述】:遍历数组 【参数】: pArr: the array's address. pVisit: the callback function to visit the data. 【返回值】:如果成功返回true,否则false ***************************************************************************************************/ cp_bool DyArrayForEach(DyArray* pArr, DataVisitFunc pVisit) { cp_int32 i; //if the input parameter is invalid, return. if(!pArr || !pVisit) { return CP_FALSE; } //visit each one with the visit function. for(i=0; i m_nSize;i++) { pVisit(pArr->m_ppData[i]); } return CP_TRUE; } /************************************************************************************************** 【函数名】: DyArrayDestroy 【描述】:销毁整个数组 【参数】: pArr: the array's address. 【返回值】:NA ***************************************************************************************************/ void DyArrayDestroy(DyArray* pArr) { cp_int32 i; //if the input parameter is invalid, return. if(!pArr) { return; } //Using destroy function to destroy each element's memory. if(pArr->m_fDestroy) { for(i=0; i m_nSize; i++) { pArr->m_fDestroy(pArr->m_ppData[i]); } } //free the array. FREEFUN(pArr->m_ppData); pArr->m_ppData = NULL; FREEFUN(pArr); pArr = NULL; } /************************************************************************************************** 【函数名】: DyArrayDestroyCustom 【描述】:使用用户函数,小围数组 【参数】: pArr: the array's address. pDataDestroy: user's destroy function. 【返回值】:NA ***************************************************************************************************/ void DyArrayDestroyCustom(DyArray* pArr,DataDestroyFunc pDataDestroy) { cp_int32 i; //if the input parameter is invalid, return. if(!pArr) { return; } //Using destroy function to destroy each element's memory. if(pDataDestroy) { for(i=0; i m_nSize;i++) { pDataDestroy(pArr->m_ppData[i]); } } //free the array. FREEFUN(pArr->m_ppData); pArr->m_ppData = NULL; FREEFUN(pArr); } /************************************************************************************************** 【函数名】: DyArrayExpand 【描述】:扩展数组 【参数】: pArr: the array's address. nNeed: the needed new size. 【返回值】:如果成功返回ture否则返回false ***************************************************************************************************/ cp_bool DyArrayExpand(DyArray* pArr, cp_int32 nNeed) { cp_int32 allocSize = 0; void** data = NULL; //if the input parameter is invalid, return. if(!pArr) { return CP_FALSE; } //if need, expand to 1.5 times of the original size. if((pArr->m_nSize + nNeed) > pArr->m_nAllocSize) { allocSize = pArr->m_nAllocSize + (pArr->m_nAllocSize>>1); data = (void**)REALLOCFUN(pArr->m_ppData, sizeof(void*) * allocSi
首页 上一页 1 2 3 4 5 6 下一页 尾页 4/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C和指针 (pointers on C)――第.. 下一篇C专家编程之编译器日期被破坏程序

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: