设为首页 加入收藏

TOP

c语言实现动态指针数组Dynamic arrays(六)
2015-01-22 21:02:36 来源: 作者: 【 】 浏览:103
Tags:语言 实现 动态 指针 Dynamic arrays
**************************************************************************************************/ cp_bool DyArrayDelete(DyArray* pArr, cp_int32 nIndex) { cp_int32 i; //if the input parameter is invalid, return. if(!pArr) { return CP_FALSE; } //destroy the element with destroy function. if(pArr->m_fDestroy) { pArr->m_fDestroy(pArr->m_ppData[nIndex]); } //move the elements after 'nIndex' to the previous one. for(i = nIndex; (i+1) < pArr->m_nSize; i++) { pArr->m_ppData[i] = pArr->m_ppData[i+1]; } //set the last one to null. pArr->m_ppData[i] = NULL; pArr->m_nSize--; //if need ,shrink the size. DyArrayShrink(pArr); return CP_TRUE; } /************************************************************************************************** 【函数名】: DyArrayDeleteEx 【描述】:删除元素by index (扩展) 【参数】: pArr: the array's address. nIndex: the position in the array to delete useless data. nEdn: 【返回值】:ture 或者false ***************************************************************************************************/ cp_bool DyArrayDeleteEx(DyArray* pArr, cp_int32 nBegin,cp_int32 nEnd) { cp_int32 i,nLen = 0; //if the input parameter is invalid, return. //if(!pArr && nBegin>nEnd && nBegin<0 && nBegin>=pArr->m_nSize && nEnd<0 && nEnd>=pArr->m_nSize) if(!pArr) { return CP_FALSE; } //destroy the element with destroy function. if(pArr->m_fDestroy) { for(i=nBegin; i<=nEnd; i++) { pArr->m_fDestroy(pArr->m_ppData[i]); } } //move the elements after 'nIndex' to the previous one. nLen = nEnd - nBegin + 1; for(i = nBegin; (i+nLen) < pArr->m_nSize; i++) { pArr->m_ppData[i] = pArr->m_ppData[i+nLen]; } //set the last one to null. //pArr->m_ppData[i] = NULL; pArr->m_nSize -= nLen; //if need ,shrink the size. DyArrayShrink(pArr); return CP_TRUE; } /************************************************************************************************** 【函数名】: DyArrayReset 【描述】:清除数组数据,缩减数组到原始大小 【参数】: pArr: the array's address. 【返回值】: none. ***************************************************************************************************/ void DyArrayReset(DyArray* pArr) { cp_int32 i; void** pData = NULL; //if the input parameter is invalid, return. if(!pArr) { return; } //reset all the elements with destroy function. if(pArr->m_fDestroy) { for(i=0; i m_nSize;i++) { pArr->m_fDestroy(pArr->m_ppData[i]); } } pArr->m_nSize = 0; //if need, shrink the size. if(pArr->m_nAllocSize > MIN_PRE_ALLOCATE_SIZE) { pData = (void**)REALLOCFUN(pArr->m_ppData, sizeof(void*) * MIN_PRE_ALLOCATE_SIZE); if(pData != NULL) { pArr->m_ppData = pData; pArr->m_nAllocSize = MIN_PRE_ALLOCATE_SIZE; } if(pArr->m_ppData) { MEMSETFUN(pArr->m_ppData,0,sizeof(void*)*pArr->m_nAllocSize); } } } /************************************************************************************************** 【函数名】: DyArrayClear 【描述】:清除数组数据,不缩减数组到原始大小 【参数】: pArr: the array's address. 【返回值】:NA ***************************************************************************************************/ void DyArrayClear(DyArray* pArr) { cp_int32 i; void** pData = NULL; //if the input parameter is invalid, return. if(!pArr) { return; } //reset all the elements with destroy function. if(pArr->m_fDestroy) { for(i=0; i m_nSize;i++) { pArr->m_fDestroy(pArr->m_ppData[i]); } } pArr->m_nSize = 0; } /************************************************************************************************** 【函数名】: DyArrayResetCustom 【描述】:清除数组使用用户函数,缩减数组到原始大小
首页 上一页 3 4 5 6 下一页 尾页 6/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C和指针 (pointers on C)――第.. 下一篇C专家编程之编译器日期被破坏程序

评论

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