Merge pull request #48 from bkcarlos/master

数组通用类
This commit is contained in:
wangzheng0822 2018-10-12 17:50:05 +08:00 committed by GitHub
commit 43b267f76f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 326 additions and 0 deletions

278
c-cpp/05_array/Array_gp.c Normal file
View File

@ -0,0 +1,278 @@
#include "Array.h"
#include <string.h>
#include <stdbool.h>
Array* arrayCreate()
{
struct Array *array = NULL;
array = malloc(sizeof(*array));
if (NULL == array)
{
return NULL;
}
array->p = NULL;
array->size = 0;
array->typeSize = 0;
array->len = 0;
array->dup = NULL;
array->free = NULL;
array->match = NULL;
return array;
}
void arrayInit(Array *array, int size, int typeSize)
{
if (NULL == array
|| typeSize <= 0
|| size < 0)
{
return;
}
void *p = calloc(1, size* typeSize);
if (NULL == p)
{
return;
}
array->p = p;
array->len = 0;
array->size = size;
array->typeSize = typeSize;
}
int arrayInsert(Array *array, size_t pos, void *const value)
{
if (NULL == array)
{
return -1;
}
if (array->len >= array->size)
{
return -2;
}
if (pos > array->size || pos <= 0)
{
return -3;
}
char *pBegin = array->p;
for (size_t i = array->len; i > pos - 1; --i)
{
void *pNew = pBegin + i * array->typeSize;
void *pOld = pBegin + (i - 1) *array->typeSize;
if (NULL != array->dup)
{
array->dup(pNew, pOld);
}
else
{
memcpy(pNew, pOld, array->typeSize);
}
}
void *pCopy = (void*)(pBegin + ((pos - 1) * array->typeSize));
if (NULL != array->dup)
{
array->dup(pCopy, value);
}
else
{
memcpy(pCopy, value, array->typeSize);
}
++array->len;
return 0;
}
size_t arraySearchValue(Array *array, void* const value)
{
if (NULL == array)
{
return -1;
}
char *pBegin = array->p;
size_t i = 0;
for (; i < array->len; ++i)
{
int nCmp = 0;
if (NULL != array->match)
{
nCmp = array->match(pBegin + i * array->typeSize, value);
}
else
{
nCmp = memcmp(pBegin + i * array->typeSize, value, array->typeSize);
}
if (nCmp == 0)
{
break;
}
}
return i;
}
void* arrayIndex(Array *array, size_t index)
{
if (NULL == array)
{
return NULL;
}
if (index > array->len
|| index <= 0)
{
return NULL;
}
char *pBegin = array->p;
return pBegin + array->typeSize * (index - 1);
}
int arrayModify(Array *array, size_t pos, void *const value)
{
if (NULL == array)
{
return -1;
}
if (pos > array->len
|| pos <= 0)
{
return -2;
}
char *pBegin = array->p;
void *pOld = pBegin + (pos - 1) * array->typeSize;
if (NULL != array->dup)
{
array->dup(pOld, value);
}
else
{
memcpy(pOld, value, array->typeSize);
}
return 0;
}
size_t arrayLen(Array *array)
{
if (NULL == array)
{
return 0;
}
return array->len;
}
size_t arraySize(Array *array)
{
if (NULL == array)
{
return 0;
}
return array->size;
}
void arrayEmpty(Array *array)
{
if (NULL == array)
{
return;
}
free(array->p);
array->p = NULL;
free(array);
array = NULL;
}
void arrayDelValue(Array *array, void *value)
{
if (NULL == array)
{
return;
}
char* pBegin = array->p;
bool bCopy = false;
for (size_t i = 0; i < array->len; ++i)
{
if (!bCopy)
{
int nCmp = 0;
if (NULL != array->match)
{
nCmp = array->match(pBegin + i * array->typeSize, value);
}
else
{
nCmp = memcmp(pBegin + i * array->typeSize, value, array->typeSize);
}
if (0 == nCmp)
{
bCopy = true;
continue;
}
}
else
{
void *pOld = pBegin + (i + 1) * array->typeSize;
void *pNew = pBegin + i * array->typeSize;
if (NULL != array->dup)
{
array->dup(pNew, pOld);
}
else
{
memcpy(pNew, pOld, array->typeSize);
}
}
}
if (bCopy)
{
--array->len;
}
}
void arrayDelIndex(Array *array, size_t pos)
{
if (NULL == array)
{
return;
}
if (pos > array->len || pos <= 0)
{
return;
}
char* pBegin = array->p;
for (size_t i = pos - 1; i < array->len - 1; ++i)
{
void *pOld = pBegin + (i + 1) * array->typeSize;
void *pNew = pBegin + i * array->typeSize;
if (NULL != array->dup)
{
array->dup(pNew, pOld);
}
else
{
memcpy(pNew, pOld, array->typeSize);
}
}
--array->len;
}

48
c-cpp/05_array/Array_gp.h Normal file
View File

@ -0,0 +1,48 @@
#ifndef __ARRAY_H__
#define __ARRAY_H__
#include <stdio.h>
#include <stdlib.h>
typedef struct Array
{
// p指针的空间大小
size_t size;
// p指针已经使用的空间大小
size_t len;
// 数据类型的大小
size_t typeSize;
// 值复制函数
void(*dup)(void *ptr, void *key);
// 值释放函数
void(*free)(void *ptr);
// 值比较函数
int(*match)(void *ptr, void *key);
// 存放数据的指针
void *p;
}Array;
#define arraySetDupMethod(a, m) ((a)->dup = (m))
#define arraySetFreeMethod(a, m) ((a)->free = (m))
#define arraySetMatchMethod(a, m) ((a)->match = (m))
#define arrayGetDupMethod(a) ((a)->dup)
#define arrayGetFree(a) ((a)->free)
#define arrayGetMatchMethod(a) ((a)->match)
Array* arrayCreate();
void arrayInit(Array *array, int size, int typeSize);
int arrayInsert(Array *array, size_t pos, void *const value);
size_t arraySearchValue(Array *array, void* const value);
void* arrayIndex(Array *array, size_t index);
int arrayModify(Array *array, size_t pos, void *const value);
size_t arrayLen(Array *array);
size_t arraySize(Array *array);
void arrayEmpty(Array *array);
void arrayDelValue(Array *array, void *value);
void arrayDelIndex(Array *array, size_t pos);
#endif // !__ARRAY_H__