tarray
This commit is contained in:
parent
426519d684
commit
c7e5ee3457
|
@ -16,13 +16,13 @@
|
||||||
#ifndef _TD_UTIL_ARRAY_H
|
#ifndef _TD_UTIL_ARRAY_H
|
||||||
#define _TD_UTIL_ARRAY_H
|
#define _TD_UTIL_ARRAY_H
|
||||||
|
|
||||||
|
#include "os.h"
|
||||||
|
#include "talgo.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "os.h"
|
|
||||||
#include "talgo.h"
|
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
#define TARRAY(TYPE) \
|
#define TARRAY(TYPE) \
|
||||||
struct { \
|
struct { \
|
||||||
|
@ -70,7 +70,7 @@ int32_t taosArrayEnsureCap(SArray* pArray, size_t tsize);
|
||||||
* @param nEles
|
* @param nEles
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
void* taosArrayAddBatch(SArray* pArray, const void* pData, int nEles);
|
void* taosArrayAddBatch(SArray* pArray, const void* pData, int32_t nEles);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -238,7 +238,7 @@ void taosArraySortString(SArray* pArray, __compar_fn_t comparFn);
|
||||||
* @param compar
|
* @param compar
|
||||||
* @param key
|
* @param key
|
||||||
*/
|
*/
|
||||||
void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int flags);
|
void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* search the array, return index of the element
|
* search the array, return index of the element
|
||||||
|
@ -246,14 +246,14 @@ void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t compa
|
||||||
* @param compar
|
* @param compar
|
||||||
* @param key
|
* @param key
|
||||||
*/
|
*/
|
||||||
int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int flags);
|
int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* search the array
|
* search the array
|
||||||
* @param pArray
|
* @param pArray
|
||||||
* @param key
|
* @param key
|
||||||
*/
|
*/
|
||||||
char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t comparFn, int flags);
|
char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t comparFn, int32_t flags);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sort the pointer data in the array
|
* sort the pointer data in the array
|
||||||
|
|
|
@ -61,7 +61,6 @@ typedef struct SExceptionNode {
|
||||||
SCleanupAction* cleanupActions;
|
SCleanupAction* cleanupActions;
|
||||||
} SExceptionNode;
|
} SExceptionNode;
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// functions & macros for auto-cleanup
|
// functions & macros for auto-cleanup
|
||||||
|
|
||||||
void cleanupPush_void_ptr_ptr(bool failOnly, void* func, void* arg1, void* arg2);
|
void cleanupPush_void_ptr_ptr(bool failOnly, void* func, void* arg1, void* arg2);
|
||||||
|
@ -92,7 +91,6 @@ bool cleanupExceedLimit();
|
||||||
#define CLEANUP_EXECUTE_TO(anchor, failed) cleanupExecuteTo((anchor), (failed))
|
#define CLEANUP_EXECUTE_TO(anchor, failed) cleanupExecuteTo((anchor), (failed))
|
||||||
#define CLEANUP_EXCEED_LIMIT() cleanupExceedLimit()
|
#define CLEANUP_EXCEED_LIMIT() cleanupExceedLimit()
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// functions & macros for exception handling
|
// functions & macros for exception handling
|
||||||
|
|
||||||
void exceptionPushNode(SExceptionNode* node);
|
void exceptionPushNode(SExceptionNode* node);
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _TD_UTIL_LIST_H_
|
#ifndef _TD_UTIL_LIST_H_
|
||||||
#define _TD_UTIL_LIST_H_
|
#define _TD_UTIL_LIST_H_
|
||||||
|
|
||||||
|
|
|
@ -13,9 +13,8 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "os.h"
|
#define _DEFAULT_SOURCE
|
||||||
#include "tarray.h"
|
#include "tarray.h"
|
||||||
#include "talgo.h"
|
|
||||||
|
|
||||||
SArray* taosArrayInit(size_t size, size_t elemSize) {
|
SArray* taosArrayInit(size_t size, size_t elemSize) {
|
||||||
assert(elemSize > 0);
|
assert(elemSize > 0);
|
||||||
|
@ -75,7 +74,7 @@ int32_t taosArrayEnsureCap(SArray* pArray, size_t newCap) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* taosArrayAddBatch(SArray* pArray, const void* pData, int nEles) {
|
void* taosArrayAddBatch(SArray* pArray, const void* pData, int32_t nEles) {
|
||||||
if (pArray == NULL || pData == NULL) {
|
if (pArray == NULL || pData == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -200,9 +199,7 @@ void* taosArrayGetP(const SArray* pArray, size_t index) {
|
||||||
return *(void**)d;
|
return *(void**)d;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* taosArrayGetLast(const SArray* pArray) {
|
void* taosArrayGetLast(const SArray* pArray) { return TARRAY_GET_ELEM(pArray, pArray->size - 1); }
|
||||||
return TARRAY_GET_ELEM(pArray, pArray->size - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t taosArrayGetSize(const SArray* pArray) {
|
size_t taosArrayGetSize(const SArray* pArray) {
|
||||||
if (pArray == NULL) {
|
if (pArray == NULL) {
|
||||||
|
@ -272,7 +269,8 @@ void taosArrayRemove(SArray* pArray, size_t index) {
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t remain = pArray->size - index - 1;
|
size_t remain = pArray->size - index - 1;
|
||||||
memmove((char*)pArray->pData + index * pArray->elemSize, (char*)pArray->pData + (index + 1) * pArray->elemSize, remain * pArray->elemSize);
|
memmove((char*)pArray->pData + index * pArray->elemSize, (char*)pArray->pData + (index + 1) * pArray->elemSize,
|
||||||
|
remain * pArray->elemSize);
|
||||||
pArray->size -= 1;
|
pArray->size -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -338,14 +336,14 @@ void taosArraySort(SArray* pArray, __compar_fn_t compar) {
|
||||||
qsort(pArray->pData, pArray->size, pArray->elemSize, compar);
|
qsort(pArray->pData, pArray->size, pArray->elemSize, compar);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int flags) {
|
void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags) {
|
||||||
assert(pArray != NULL && comparFn != NULL);
|
assert(pArray != NULL && comparFn != NULL);
|
||||||
assert(key != NULL);
|
assert(key != NULL);
|
||||||
|
|
||||||
return taosbsearch(key, pArray->pData, pArray->size, pArray->elemSize, comparFn, flags);
|
return taosbsearch(key, pArray->pData, pArray->size, pArray->elemSize, comparFn, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int flags) {
|
int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags) {
|
||||||
void* item = taosArraySearch(pArray, key, comparFn, flags);
|
void* item = taosArraySearch(pArray, key, comparFn, flags);
|
||||||
return item == NULL ? -1 : (int32_t)((char*)item - (char*)pArray->pData) / pArray->elemSize;
|
return item == NULL ? -1 : (int32_t)((char*)item - (char*)pArray->pData) / pArray->elemSize;
|
||||||
}
|
}
|
||||||
|
@ -355,7 +353,7 @@ void taosArraySortString(SArray* pArray, __compar_fn_t comparFn) {
|
||||||
qsort(pArray->pData, pArray->size, pArray->elemSize, comparFn);
|
qsort(pArray->pData, pArray->size, pArray->elemSize, comparFn);
|
||||||
}
|
}
|
||||||
|
|
||||||
char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t comparFn, int flags) {
|
char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t comparFn, int32_t flags) {
|
||||||
assert(pArray != NULL);
|
assert(pArray != NULL);
|
||||||
assert(key != NULL);
|
assert(key != NULL);
|
||||||
|
|
||||||
|
@ -366,15 +364,19 @@ char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t
|
||||||
return *(char**)p;
|
return *(char**)p;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int taosArrayPartition(SArray *pArray, int i, int j, __ext_compar_fn_t fn, const void *userData) {
|
static int32_t taosArrayPartition(SArray* pArray, int32_t i, int32_t j, __ext_compar_fn_t fn, const void* userData) {
|
||||||
void* key = taosArrayGetP(pArray, i);
|
void* key = taosArrayGetP(pArray, i);
|
||||||
while (i < j) {
|
while (i < j) {
|
||||||
while (i < j && fn(taosArrayGetP(pArray, j), key, userData) >= 0) { j--; }
|
while (i < j && fn(taosArrayGetP(pArray, j), key, userData) >= 0) {
|
||||||
|
j--;
|
||||||
|
}
|
||||||
if (i < j) {
|
if (i < j) {
|
||||||
void* a = taosArrayGetP(pArray, j);
|
void* a = taosArrayGetP(pArray, j);
|
||||||
taosArraySet(pArray, i, &a);
|
taosArraySet(pArray, i, &a);
|
||||||
}
|
}
|
||||||
while (i < j && fn(taosArrayGetP(pArray, i), key, userData) <= 0) { i++;}
|
while (i < j && fn(taosArrayGetP(pArray, i), key, userData) <= 0) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
if (i < j) {
|
if (i < j) {
|
||||||
void* a = taosArrayGetP(pArray, i);
|
void* a = taosArrayGetP(pArray, i);
|
||||||
taosArraySet(pArray, j, &a);
|
taosArraySet(pArray, j, &a);
|
||||||
|
@ -384,9 +386,10 @@ static int taosArrayPartition(SArray *pArray, int i, int j, __ext_compar_fn_t fn
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void taosArrayQuicksortHelper(SArray *pArray, int low, int high, __ext_compar_fn_t fn, const void *param) {
|
static void taosArrayQuicksortHelper(SArray* pArray, int32_t low, int32_t high, __ext_compar_fn_t fn,
|
||||||
|
const void* param) {
|
||||||
if (low < high) {
|
if (low < high) {
|
||||||
int idx = taosArrayPartition(pArray, low, high, fn, param);
|
int32_t idx = taosArrayPartition(pArray, low, high, fn, param);
|
||||||
taosArrayQuicksortHelper(pArray, low, idx - 1, fn, param);
|
taosArrayQuicksortHelper(pArray, low, idx - 1, fn, param);
|
||||||
taosArrayQuicksortHelper(pArray, idx + 1, high, fn, param);
|
taosArrayQuicksortHelper(pArray, idx + 1, high, fn, param);
|
||||||
}
|
}
|
||||||
|
@ -396,14 +399,15 @@ static void taosArrayQuickSort(SArray* pArray, __ext_compar_fn_t fn, const void
|
||||||
if (pArray->size <= 1) {
|
if (pArray->size <= 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
taosArrayQuicksortHelper(pArray, 0, (int)(taosArrayGetSize(pArray) - 1), fn, param);
|
taosArrayQuicksortHelper(pArray, 0, (int32_t)(taosArrayGetSize(pArray) - 1), fn, param);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void taosArrayInsertSort(SArray* pArray, __ext_compar_fn_t fn, const void* param) {
|
static void taosArrayInsertSort(SArray* pArray, __ext_compar_fn_t fn, const void* param) {
|
||||||
if (pArray->size <= 1) {
|
if (pArray->size <= 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (int i = 1; i <= pArray->size - 1; ++i) {
|
for (int32_t i = 1; i <= pArray->size - 1; ++i) {
|
||||||
for (int j = i; j > 0; --j) {
|
for (int32_t j = i; j > 0; --j) {
|
||||||
if (fn(taosArrayGetP(pArray, j), taosArrayGetP(pArray, j - 1), param) == -1) {
|
if (fn(taosArrayGetP(pArray, j), taosArrayGetP(pArray, j - 1), param) == -1) {
|
||||||
void* a = taosArrayGetP(pArray, j);
|
void* a = taosArrayGetP(pArray, j);
|
||||||
void* b = taosArrayGetP(pArray, j - 1);
|
void* b = taosArrayGetP(pArray, j - 1);
|
||||||
|
@ -415,11 +419,10 @@ static void taosArrayInsertSort(SArray* pArray, __ext_compar_fn_t fn, const void
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// order array<type *>
|
// order array<type *>
|
||||||
void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* param) {
|
void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* param) {
|
||||||
taosArrayGetSize(pArray) > 8 ?
|
taosArrayGetSize(pArray) > 8 ? taosArrayQuickSort(pArray, fn, param) : taosArrayInsertSort(pArray, fn, param);
|
||||||
taosArrayQuickSort(pArray, fn, param) : taosArrayInsertSort(pArray, fn, param);
|
|
||||||
}
|
}
|
||||||
// TODO(yihaoDeng) add order array<type>
|
// TODO(yihaoDeng) add order array<type>
|
||||||
|
|
Loading…
Reference in New Issue