forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/wangzheng0822/algo
- Loading branch information
Showing
81 changed files
with
4,501 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# main files | ||
main.* | ||
|
||
# executives | ||
a.out | ||
|
||
# objective files | ||
*.o | ||
*.obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ |
Oops, something went wrong.