• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C语言实现动态数组代码

OC/C/C++ 水墨上仙 3118次浏览

c语言版本的 动态数组创建 完整版
来源: http://bugkill.01safe.com/thread-217-1-1.html

#include <stdlib.h>
#include <string.h>
#include <app_mem.h>
#include <imath.h>
struct DynamicArray_s
{
unsigned int numEntries; 
unsigned int capacity; 
unsigned int entrySize; 
void* pData; 
DynamicArray_CompEntryFn_t* pCompEntryFn; 
DynamicArray_DeleteEntryFn_t* pDeleteEntryFn; 
};
static __inline__ void* FastGetEntryAt(DynamicArray_t* pArray, unsigned int index);
static __inline__ unsigned int Capacity(unsigned int v);
DynamicArray_t* DynamicArray_Create(unsigned int numEntries, unsigned int capacity, unsigned int entrySize, DynamicArray_CompEntryFn_t pCompEntryFn, DynamicArray_DeleteEntryFn_t pDeleteEntryFn)
{
DynamicArray_t* pArray = NULL;
if (entrySize > 0)
{
pArray = malloc( sizeof(*pArray));
if (pArray)
{
if (capacity < numEntries)
capacity = numEntries;
pArray->numEntries = 0;
pArray->entrySize = entrySize;
pArray->pCompEntryFn = pCompEntryFn;
pArray->pDeleteEntryFn = pDeleteEntryFn;
pArray->pData = NULL;
if (DynamicArray_SetCapacity(pArray, capacity) == FALSE)
{
free(pArray);
pArray = NULL;
}
else
{
DynamicArray_Grow(pArray, numEntries);
}
}
}
return pArray;
}
DynamicArray_t* DynamicArray_Copy(DynamicArray_t* pArray)
{
DynamicArray_t* pCopy = NULL;
if (pArray)
{
pCopy = DynamicArray_Create(pArray->numEntries, 0, pArray->entrySize, pArray->pCompEntryFn, pArray->pDeleteEntryFn);
if (pCopy)
{
memcpy(pCopy->pData, pArray->pData, pArray->numEntries * pArray->entrySize);
}
}
return pCopy;
}
void DynamicArray_Delete(DynamicArray_t* pArray)
{
unsigned int i;
void* pEntry;
if (pArray)
{
if (pArray->pDeleteEntryFn)
{
for (i = 0; i < pArray->numEntries; i++)
{
pEntry = FastGetEntryAt(pArray, i);
pArray->pDeleteEntryFn(pEntry);
}
}
free(pArray->pData);
free(pArray);
}
}
unsigned int DynamicArray_SizeOf(DynamicArray_t* pArray)
{
if (pArray)
return pArray->numEntries;
return 0;
}
void* DynamicArray_Apply(DynamicArray_t* pArray, DynamicArray_ApplyFn_t* pFunc, void* pArg)
{
unsigned int i;
if (pArray)
{
for (i = 0; i < pArray->numEntries; i++)
pArg = pFunc(FastGetEntryAt(pArray, i), pArg);
}
return pArg;
}
void DynamicArray_Concat(DynamicArray_t* pArray, DynamicArray_t* pAddThisArray)
{
unsigned int nItems;
for(nItems = 0; nItems < DynamicArray_SizeOf(pAddThisArray); nItems++)
{
DynamicArray_Append(pArray, DynamicArray_GetEntryAt(pAddThisArray,nItems));
}
}
void* DynamicArray_Append(DynamicArray_t* pArray, const void* pEntry)
{
void* pArrayEntry;
pArrayEntry = DynamicArray_Grow(pArray, 1);
if (pArrayEntry)
{
memcpy(pArrayEntry, pEntry, pArray->entrySize);
}
return pArrayEntry;
}
void* DynamicArray_InsertSorted(DynamicArray_t* pArray, const void* pEntry)
{
void* pArrayEntry = NULL;
unsigned int nIndex = 0;
(void)DynamicArray_BinarySearch(pArray, pEntry, &nIndex);
pArrayEntry = DynamicArray_Insert(pArray, pEntry, nIndex);
return pArrayEntry;
}
void* DynamicArray_Insert(DynamicArray_t* pArray, const void* pEntry, unsigned int nIndex)
{
void* pArrayEntry;
pArrayEntry = DynamicArray_Grow(pArray, 1);
if (pArrayEntry)
{
void* pPos = FastGetEntryAt(pArray, nIndex);
unsigned int nLastEntry = pArray->numEntries - 1;
/* Insert in the array, have to move some elements */
if (nIndex != nLastEntry)
{
size_t uSize = (nLastEntry - nIndex) * pArray->entrySize;
void* pDst = FastGetEntryAt(pArray, nIndex + 1);
memmove(pDst, pPos, uSize);
}
memcpy(pPos, pEntry, pArray->entrySize);
pArrayEntry = pPos;
}
return pArrayEntry;
}
void* DynamicArray_Grow(DynamicArray_t* pArray, unsigned int growNumEntries)
{
unsigned int tmp;
unsigned int numEntries;
unsigned int capacity;
void* pEntry = NULL;
if (pArray && growNumEntries > 0)
{
numEntries = pArray->numEntries + growNumEntries;
capacity = pArray->capacity;
if (numEntries > capacity)
{
capacity = Capacity(numEntries);
DynamicArray_SetCapacity(pArray, capacity);
}
if (capacity == pArray->capacity)
{
tmp = pArray->numEntries;
pArray->numEntries = numEntries;
pEntry = FastGetEntryAt(pArray, tmp);
memset(pEntry, 0, growNumEntries * pArray->entrySize);
}
}
return pEntry;
}
bool DynamicArray_Shrink(DynamicArray_t* pArray, unsigned int shrinkNumEntries)
{
unsigned int i;
void* pEntry = NULL;
if (pArray && pArray->numEntries > 0 && shrinkNumEntries > 0)
{
if (shrinkNumEntries > pArray->numEntries)
shrinkNumEntries = pArray->numEntries;
if (pArray->pDeleteEntryFn)
{
for (i = pArray->numEntries - shrinkNumEntries; i < pArray->numEntries; i++)
{
pEntry = FastGetEntryAt(pArray, i);
pArray->pDeleteEntryFn(pEntry);
}
}
pArray->numEntries -= shrinkNumEntries;
}
return (pArray ? TRUE : FALSE);
}
/* 后面的省略了。。直接下载代码看吧。http://bugkill.01safe.com/thread-217-1-1.html*/
}


喜欢 (0)
加载中……