Skip to content

Commit

Permalink
Merge pull request wangzheng0822#3 from wangzheng0822/master
Browse files Browse the repository at this point in the history
Merge
  • Loading branch information
Hkesd authored Oct 12, 2018
2 parents 7540c6c + 91d126d commit 0b6dd0a
Show file tree
Hide file tree
Showing 49 changed files with 2,646 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ hs_err_pid*

# editor files
.vscode
.*.swp
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# 数据结构和算法之美
# 请点击查看:[https://time.geekbang.org/column/intro/126](https://time.geekbang.org/column/intro/126)
# [https://time.geekbang.org/column/intro/126](https://time.geekbang.org/column/intro/126)

# Java rate limiting library/framework
# https://github.com/wangzheng0822/ratelimiter4j
278 changes: 278 additions & 0 deletions c-cpp/05_array/Array_gp.c
Original file line number Diff line number Diff line change
@@ -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 changes: 48 additions & 0 deletions c-cpp/05_array/Array_gp.h
Original file line number Diff line number Diff line change
@@ -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__
Loading

0 comments on commit 0b6dd0a

Please sign in to comment.