tarray
This commit is contained in:
parent
426519d684
commit
c7e5ee3457
|
@ -16,13 +16,13 @@
|
|||
#ifndef _TD_UTIL_ARRAY_H
|
||||
#define _TD_UTIL_ARRAY_H
|
||||
|
||||
#include "os.h"
|
||||
#include "talgo.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "os.h"
|
||||
#include "talgo.h"
|
||||
|
||||
#if 0
|
||||
#define TARRAY(TYPE) \
|
||||
struct { \
|
||||
|
@ -70,7 +70,7 @@ int32_t taosArrayEnsureCap(SArray* pArray, size_t tsize);
|
|||
* @param nEles
|
||||
* @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 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
|
||||
|
@ -246,14 +246,14 @@ void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t compa
|
|||
* @param compar
|
||||
* @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
|
||||
* @param pArray
|
||||
* @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
|
||||
|
|
|
@ -61,7 +61,6 @@ typedef struct SExceptionNode {
|
|||
SCleanupAction* cleanupActions;
|
||||
} SExceptionNode;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// functions & macros for auto-cleanup
|
||||
|
||||
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_EXCEED_LIMIT() cleanupExceedLimit()
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// functions & macros for exception handling
|
||||
|
||||
void exceptionPushNode(SExceptionNode* node);
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef _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/>.
|
||||
*/
|
||||
|
||||
#include "os.h"
|
||||
#define _DEFAULT_SOURCE
|
||||
#include "tarray.h"
|
||||
#include "talgo.h"
|
||||
|
||||
SArray* taosArrayInit(size_t size, size_t elemSize) {
|
||||
assert(elemSize > 0);
|
||||
|
@ -75,7 +74,7 @@ int32_t taosArrayEnsureCap(SArray* pArray, size_t newCap) {
|
|||
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) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -200,9 +199,7 @@ void* taosArrayGetP(const SArray* pArray, size_t index) {
|
|||
return *(void**)d;
|
||||
}
|
||||
|
||||
void* taosArrayGetLast(const SArray* pArray) {
|
||||
return TARRAY_GET_ELEM(pArray, pArray->size - 1);
|
||||
}
|
||||
void* taosArrayGetLast(const SArray* pArray) { return TARRAY_GET_ELEM(pArray, pArray->size - 1); }
|
||||
|
||||
size_t taosArrayGetSize(const SArray* pArray) {
|
||||
if (pArray == NULL) {
|
||||
|
@ -272,7 +269,8 @@ void taosArrayRemove(SArray* pArray, size_t index) {
|
|||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -338,14 +336,14 @@ void taosArraySort(SArray* pArray, __compar_fn_t 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(key != NULL);
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
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(key != NULL);
|
||||
|
||||
|
@ -366,15 +364,19 @@ char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t
|
|||
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);
|
||||
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) {
|
||||
void* a = taosArrayGetP(pArray, j);
|
||||
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) {
|
||||
void* a = taosArrayGetP(pArray, i);
|
||||
taosArraySet(pArray, j, &a);
|
||||
|
@ -384,9 +386,10 @@ static int taosArrayPartition(SArray *pArray, int i, int j, __ext_compar_fn_t fn
|
|||
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) {
|
||||
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, 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) {
|
||||
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) {
|
||||
if (pArray->size <= 1) {
|
||||
return;
|
||||
}
|
||||
for (int i = 1; i <= pArray->size - 1; ++i) {
|
||||
for (int j = i; j > 0; --j) {
|
||||
for (int32_t i = 1; i <= pArray->size - 1; ++i) {
|
||||
for (int32_t j = i; j > 0; --j) {
|
||||
if (fn(taosArrayGetP(pArray, j), taosArrayGetP(pArray, j - 1), param) == -1) {
|
||||
void* a = taosArrayGetP(pArray, j);
|
||||
void* b = taosArrayGetP(pArray, j - 1);
|
||||
|
@ -415,11 +419,10 @@ static void taosArrayInsertSort(SArray* pArray, __ext_compar_fn_t fn, const void
|
|||
}
|
||||
}
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
// order array<type *>
|
||||
void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* param) {
|
||||
taosArrayGetSize(pArray) > 8 ?
|
||||
taosArrayQuickSort(pArray, fn, param) : taosArrayInsertSort(pArray, fn, param);
|
||||
taosArrayGetSize(pArray) > 8 ? taosArrayQuickSort(pArray, fn, param) : taosArrayInsertSort(pArray, fn, param);
|
||||
}
|
||||
// TODO(yihaoDeng) add order array<type>
|
||||
|
|
Loading…
Reference in New Issue