基本原理:事先准备好一个固定长度的数组。如果长度不够的时候,realloc一块区域。另外:在数组元素减少的情况下,需要缩减数组长度。
主要接口:
cp_bool DyArrayAppend(DyArray* pArr, void* pData)//加数据到数组末尾 cp_bool DyArrayExpand(DyArray* pArr, cp_int32 nNeed)//扩展数组 cp_bool DyArrayDelete(DyArray* pArr, cp_int32 nIndex)//删除元素by index cp_bool DyArrayShrink(DyArray* pArr)//缩减数组
源代码如下:(iOS平台的c实现)
// // dyArray.h // dataStruct // // Created by hherima on 14-7-28. // Copyright (c) 2014年 . All rights reserved. // #ifndef dataStruct_dyArray_h #define dataStruct_dyArray_h #includeenum { CP_FALSE = 0, CP_TRUE = !CP_FALSE }; typedef unsigned char cp_bool; typedef signed int cp_int32; typedef void (*DataDestroyFunc)(void *); typedef cp_bool (*DataCmpFunc)(void *,void *); typedef void (*DataVisitFunc)(void *); #define F_MALLOC_TYPE(s) (s*)f_malloc(sizeof(s)) #define FREEFUN free #define MIN_PRE_ALLOCATE_SIZE 10 //The initial size of the dynamic array. #define MEMSETFUN memset #define REALLOCFUN realloc #define MALLOCFUN malloc struct DynamicArray { void **m_ppData; //the address of the allocated array. cp_int32 m_nAllocSize; //the allocated array size. cp_int32 m_nSize; //the used size of the array. DataDestroyFunc m_fDestroy; //the callback function to destroy one data. DataCmpFunc m_fCmp; //the callback function to compare one data. DataVisitFunc m_fVisit; //the callback function to visit each data. }; typedef struct DynamicArray DyArray; DyArray* DyArrayCreate(DataDestroyFunc pDataDestroy); cp_bool DyArrayInsert(DyArray* pArr, cp_int32 nIndex, void* pData); cp_bool DyArrayPrepend(DyArray* pArr, void* pData); cp_bool DyArrayAppend(DyArray* pArr, void* pData); cp_bool DyArrayDelete(DyArray* pArr, cp_int32 nIndex); cp_bool DyArrayDeleteEx(DyArray* pArr, cp_int32 nBegin,cp_int32 nEnd); cp_bool DyArrayGetByIndex(DyArray* pArr, cp_int32 nIndex, void** ppData); cp_bool DyArrayGetFirst(DyArray* pArr,void** ppData); cp_bool DyArrayGetLast(DyArray* pArr,void** ppData); cp_bool DyArraySetByIndex(DyArray* pArr, cp_int32 nIndex, void* pData); cp_int32 DyArrayLength(DyArray* pArr); cp_int32 DyArrayFind(DyArray* pArr, DataCmpFunc pCmp,void *pData); cp_bool DyArrayForEach(DyArray* pArr, DataVisitFunc pVisit); void DyArrayDestroy(DyArray* pArr); void DyArrayDestroyCustom(DyArray* pArr,DataDestroyFunc pDataDestroy); void DyArrayReset(DyArray* pArr);//shrink void DyArrayClear(DyArray* pArr);//not shrink void DyArrayResetCustom(DyArray* pArr,DataDestroyFunc pDataDestroy); void DyArrayClearCustom(DyArray* pArr,DataDestroyFunc pDataDestroy);//not shrink #endif
//
// dyArray.c
// dataStruct
//
// Created by hherima on 14-7-28.
// Copyright (c) 2014年 . All rights reserved.
//
#include "dyArray.h"
void* f_malloc(cp_int32 size)
{
void* p = MALLOCFUN(size);
if(p)
MEMSETFUN(p, 0, size);
return p;
}
/**************************************************************************************************
【函数名】: DyArrayCreate
【描述】: 创建长度为MIN_PRE_ALLOCATE_SIZE的动态指针数组
【参数】:
pDataDestroy: the callback function to destroy one data in data array.
【返回值】: 动态指针数组的地址
***************************************************************************************************/
DyArray* DyArrayCreate(DataDestroyFunc pDataDestroy)
{
DyArray *pArr = NULL;
pArr = F_MALLOC_TY