diff --git a/src/util/inc/talgo.h b/src/util/inc/talgo.h index e89795c08615573d92752c9d2c76ea24330d661d..d5e089b687807cf8e028ca721c64959afc92914d 100644 --- a/src/util/inc/talgo.h +++ b/src/util/inc/talgo.h @@ -20,9 +20,37 @@ extern "C" { #endif +#define TD_EQ 0x1 +#define TD_GT 0x2 +#define TD_LT 0x4 +#define TD_GE (TD_EQ | TD_GT) +#define TD_LE (TD_EQ | TD_LT) + typedef int32_t (*__ext_compar_fn_t)(const void *p1, const void *p2, const void *param); -void tqsort(void *src, size_t numOfElem, size_t size, const void* param, __ext_compar_fn_t comparFn); +/** + * quick sort, with the compare function requiring additional parameters support + * + * @param src + * @param numOfElem + * @param size + * @param param + * @param comparFn + */ +void taosqsort(void *src, size_t numOfElem, size_t size, const void* param, __ext_compar_fn_t comparFn); + +/** + * binary search, with range support + * + * @param key + * @param base + * @param nmemb + * @param size + * @param fn + * @param flags + * @return + */ +void *taosbsearch(const void *key, const void *base, size_t nmemb, size_t size, __compar_fn_t fn, int flags); #ifdef __cplusplus } diff --git a/src/util/inc/tutil.h b/src/util/inc/tutil.h index c4c802a68881107da8a09044ea4eb4aa206c9340..81cf177e73696723d147aefb4352df721240a1cf 100644 --- a/src/util/inc/tutil.h +++ b/src/util/inc/tutil.h @@ -170,14 +170,6 @@ uint32_t ip2uint(const char *const ip_addr); void taosSetAllocMode(int mode, const char* path, bool autoDump); void taosDumpMemoryLeak(); -#define TD_EQ 0x1 -#define TD_GT 0x2 -#define TD_LT 0x4 -#define TD_GE (TD_EQ | TD_GT) -#define TD_LE (TD_EQ | TD_LT) -void *taosbsearch(const void *key, const void *base, size_t nmemb, size_t size, - int (*compar)(const void *, const void *), int flags); - #ifdef TAOS_MEM_CHECK void * taos_malloc(size_t size, const char *file, uint32_t line); diff --git a/src/util/src/talgo.c b/src/util/src/talgo.c index 1004d5ab6777694345f1dfb445b80944ae37e6f7..7a682cd466adaadd39cf38190725ff8ace7ba2ce 100644 --- a/src/util/src/talgo.c +++ b/src/util/src/talgo.c @@ -1,3 +1,17 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ #include "os.h" #include "tutil.h" @@ -47,7 +61,7 @@ static void tInsertSort(void *src, size_t size, int32_t s, int32_t e, const void } } -void tqsortImpl(void *src, int32_t start, int32_t end, size_t size, const void *param, __ext_compar_fn_t comparFn, +static void tqsortImpl(void *src, int32_t start, int32_t end, size_t size, const void *param, __ext_compar_fn_t comparFn, void* buf) { // short array sort, incur another sort procedure instead of quick sort process const int32_t THRESHOLD_SIZE = 6; @@ -138,13 +152,13 @@ void tqsortImpl(void *src, int32_t start, int32_t end, size_t size, const void * } } -void tqsort(void *src, size_t numOfElem, size_t size, const void* param, __ext_compar_fn_t comparFn) { +void taosqsort(void *src, size_t numOfElem, size_t size, const void* param, __ext_compar_fn_t comparFn) { char *buf = calloc(1, size); // prepare the swap buffer tqsortImpl(src, 0, numOfElem - 1, size, param, comparFn, buf); tfree(buf); } -void * taosbsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *), int flags) { +void * taosbsearch(const void *key, const void *base, size_t nmemb, size_t size, __compar_fn_t compar, int flags) { // TODO: need to check the correctness of this function int l = 0; int r = nmemb; diff --git a/src/vnode/tsdb/src/tsdbFile.c b/src/vnode/tsdb/src/tsdbFile.c index d025144ba93ce239529a1c872065945d62c6ea7a..7576717dd1a9eeef30d3ef9aa7eaee6373d74fff 100644 --- a/src/vnode/tsdb/src/tsdbFile.c +++ b/src/vnode/tsdb/src/tsdbFile.c @@ -22,8 +22,9 @@ #include #include -#include "tutil.h" #include "tsdbMain.h" +#include "tutil.h" +#include "talgo.h" const char *tsdbFileSuffix[] = { ".head", // TSDB_FILE_TYPE_HEAD diff --git a/src/vnode/tsdb/src/tsdbMain.c b/src/vnode/tsdb/src/tsdbMain.c index 2b26eec53a3cbadc3d8b27c7bf58774380e3ac73..f558982419548efcdc23bcc418370b0aa04c69b0 100644 --- a/src/vnode/tsdb/src/tsdbMain.c +++ b/src/vnode/tsdb/src/tsdbMain.c @@ -15,6 +15,7 @@ // #include "taosdef.h" // #include "disk.h" #include "os.h" +#include "talgo.h" #include "tsdb.h" #include "tsdbMain.h" diff --git a/src/vnode/tsdb/src/tsdbRead.c b/src/vnode/tsdb/src/tsdbRead.c index 8d48b1ec486bbfa01a7265141b429de1edb67cb7..495cf8c7f5717d2bbbbd011ded38ccf6879295be 100644 --- a/src/vnode/tsdb/src/tsdbRead.c +++ b/src/vnode/tsdb/src/tsdbRead.c @@ -1347,7 +1347,7 @@ SArray* createTableGroup(SArray* pTableList, STSchema* pTagSchema, SColIndex* pC pSupp->pTagSchema = pTagSchema; pSupp->pCols = pCols; - tqsort(pTableList->pData, size, POINTER_BYTES, pSupp, tableGroupComparFn); + taosqsort(pTableList->pData, size, POINTER_BYTES, pSupp, tableGroupComparFn); createTableGroupImpl(pTableGroup, pTableList->pData, size, pSupp, tableGroupComparFn); #ifdef _DEBUG_VIEW