设为首页 加入收藏

TOP

c语言实现动态指针数组Dynamic arrays(一)
2015-01-22 21:02:36 来源: 作者: 【 】 浏览:102
Tags:语言 实现 动态 指针 Dynamic arrays

c语言实现动态数组。

基本原理:事先准备好一个固定长度的数组。如果长度不够的时候,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
#include 
  
   

enum
{
    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
首页 上一页 1 2 3 4 5 6 下一页 尾页 1/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C和指针 (pointers on C)――第.. 下一篇C专家编程之编译器日期被破坏程序

评论

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