add return code

This commit is contained in:
Hongze Cheng 2024-07-16 14:56:30 +08:00
parent c819ac1abe
commit 9003371dff
4 changed files with 36 additions and 17 deletions

View File

@ -52,7 +52,7 @@ typedef int32_t (*__ext_compar_fn_t)(const void *p1, const void *p2, const void
* @param param * @param param
* @param comparFn * @param comparFn
*/ */
void taosqsort(void *src, int64_t numOfElem, int64_t size, const void *param, __ext_compar_fn_t comparFn); int32_t taosqsort(void *src, int64_t numOfElem, int64_t size, const void *param, __ext_compar_fn_t comparFn);
/** /**
* Non-recursive quick sort. * Non-recursive quick sort.
@ -98,8 +98,8 @@ void *taosbsearch(const void *key, const void *base, int32_t nmemb, int32_t size
* @param maxroot: if heap is max root heap * @param maxroot: if heap is max root heap
* @return * @return
*/ */
void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const void *parcompar, int32_t taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const void *parcompar,
__ext_compar_fn_t compar, char *buf, bool maxroot); __ext_compar_fn_t compar, char *buf, bool maxroot);
/** /**
* sort heap to make sure it is a max/min root heap * sort heap to make sure it is a max/min root heap
@ -114,7 +114,8 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const
* @param maxroot: if heap is max root heap * @param maxroot: if heap is max root heap
* @return * @return
*/ */
void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, __ext_compar_fn_t compar, bool maxroot); int32_t taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, __ext_compar_fn_t compar,
bool maxroot);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -251,7 +251,7 @@ int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t
* @return * @return
*/ */
void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* param); int32_t taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* param);
int32_t taosEncodeArray(void** buf, const SArray* pArray, FEncode encode); int32_t taosEncodeArray(void** buf, const SArray* pArray, FEncode encode);
void* taosDecodeArray(const void* buf, SArray** pArray, FDecode decode, int32_t dataSz, int8_t sver); void* taosDecodeArray(const void* buf, SArray** pArray, FDecode decode, int32_t dataSz, int8_t sver);

View File

@ -147,10 +147,14 @@ static void tqsortImpl(void *src, int32_t start, int32_t end, int64_t size, cons
} }
} }
void taosqsort(void *src, int64_t numOfElem, int64_t size, const void *param, __ext_compar_fn_t comparFn) { int32_t taosqsort(void *src, int64_t numOfElem, int64_t size, const void *param, __ext_compar_fn_t comparFn) {
char *buf = taosMemoryCalloc(1, size); // prepare the swap buffer char *buf = taosMemoryCalloc(1, size); // prepare the swap buffer
if (NULL == buf) {
return TSDB_CODE_OUT_OF_MEMORY;
}
tqsortImpl(src, 0, (int32_t)numOfElem - 1, (int32_t)size, param, comparFn, buf); tqsortImpl(src, 0, (int32_t)numOfElem - 1, (int32_t)size, param, comparFn, buf);
taosMemoryFreeClear(buf); taosMemoryFreeClear(buf);
return 0;
} }
#define DOSWAP(a, b, size) \ #define DOSWAP(a, b, size) \
@ -324,14 +328,17 @@ void *taosbsearch(const void *key, const void *base, int32_t nmemb, int32_t size
} }
} }
void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const void *parcompar, int32_t taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const void *parcompar,
__ext_compar_fn_t compar, char *buf, bool maxroot) { __ext_compar_fn_t compar, char *buf, bool maxroot) {
int32_t parent; int32_t parent;
int32_t child; int32_t child;
char *tmp = NULL; char *tmp = NULL;
if (buf == NULL) { if (buf == NULL) {
tmp = taosMemoryMalloc(size); tmp = taosMemoryMalloc(size);
if (NULL == tmp) {
return TSDB_CODE_OUT_OF_MEMORY;
}
} else { } else {
tmp = buf; tmp = buf;
} }
@ -378,24 +385,31 @@ void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const
if (buf == NULL) { if (buf == NULL) {
taosMemoryFree(tmp); taosMemoryFree(tmp);
} }
return 0;
} }
void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, __ext_compar_fn_t compar, int32_t taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, __ext_compar_fn_t compar,
bool maxroot) { bool maxroot) {
int32_t i; int32_t i;
char *buf = taosMemoryCalloc(1, size); char *buf = taosMemoryCalloc(1, size);
if (buf == NULL) { if (buf == NULL) {
return; return TSDB_CODE_OUT_OF_MEMORY;
} }
if (base && size > 0) { if (base && size > 0) {
for (i = len / 2 - 1; i >= 0; i--) { for (i = len / 2 - 1; i >= 0; i--) {
taosheapadjust(base, size, i, len - 1, parcompar, compar, buf, maxroot); int32_t code = taosheapadjust(base, size, i, len - 1, parcompar, compar, buf, maxroot);
if (code) {
taosMemoryFree(buf);
return code;
}
} }
} }
taosMemoryFree(buf); taosMemoryFree(buf);
return 0;
} }
static void taosMerge(void *src, int32_t start, int32_t leftend, int32_t end, int64_t size, const void *param, static void taosMerge(void *src, int32_t start, int32_t leftend, int32_t end, int64_t size, const void *param,
@ -441,7 +455,10 @@ static int32_t taosMergeSortHelper(void *src, int64_t numOfElem, int64_t size, c
// short array sort, instead of merge sort process // short array sort, instead of merge sort process
const int32_t THRESHOLD_SIZE = 6; const int32_t THRESHOLD_SIZE = 6;
char *buf = taosMemoryCalloc(1, size); // prepare the swap buffer char *buf = taosMemoryCalloc(1, size); // prepare the swap buffer
if (buf == NULL) return TSDB_CODE_OUT_OF_MEMORY; if (buf == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
for (int32_t start = 0; start < numOfElem - 1; start += THRESHOLD_SIZE) { for (int32_t start = 0; start < numOfElem - 1; start += THRESHOLD_SIZE) {
int32_t end = (start + THRESHOLD_SIZE - 1) <= numOfElem - 1 ? (start + THRESHOLD_SIZE - 1) : numOfElem - 1; int32_t end = (start + THRESHOLD_SIZE - 1) <= numOfElem - 1 ? (start + THRESHOLD_SIZE - 1) : numOfElem - 1;
tInsertSort(src, size, start, end, param, comparFn, buf); tInsertSort(src, size, start, end, param, comparFn, buf);
@ -451,7 +468,9 @@ static int32_t taosMergeSortHelper(void *src, int64_t numOfElem, int64_t size, c
if (numOfElem > THRESHOLD_SIZE) { if (numOfElem > THRESHOLD_SIZE) {
int32_t currSize; int32_t currSize;
void *tmp = taosMemoryMalloc(numOfElem * size); void *tmp = taosMemoryMalloc(numOfElem * size);
if (tmp == NULL) return TSDB_CODE_OUT_OF_MEMORY; if (tmp == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
for (currSize = THRESHOLD_SIZE; currSize <= numOfElem - 1; currSize = 2 * currSize) { for (currSize = THRESHOLD_SIZE; currSize <= numOfElem - 1; currSize = 2 * currSize) {
int32_t leftStart; int32_t leftStart;

View File

@ -518,9 +518,8 @@ void* taosDecodeArray(const void* buf, SArray** pArray, FDecode decode, int32_t
// todo remove it // todo remove it
// order array<type *> // order array<type *>
void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* param) { int32_t taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* param) {
taosqsort(pArray->pData, pArray->size, pArray->elemSize, param, fn); return taosqsort(pArray->pData, pArray->size, pArray->elemSize, param, fn);
// taosArrayGetSize(pArray) > 8 ? taosArrayQuickSort(pArray, fn, param) : taosArrayInsertSort(pArray, fn, param);
} }
void taosArraySwap(SArray* a, SArray* b) { void taosArraySwap(SArray* a, SArray* b) {