From 53b8867aabc7595998b89d6e213abe28001ca73d Mon Sep 17 00:00:00 2001 From: hjxilinx Date: Thu, 16 Apr 2020 11:18:04 +0800 Subject: [PATCH] [td-98] refactor qsort and bsearch functions --- src/util/inc/talgo.h | 30 +++++++++++++++++++++++++++++- src/util/inc/tutil.h | 8 -------- src/util/src/talgo.c | 20 +++++++++++++++++--- src/vnode/tsdb/src/tsdbFile.c | 3 ++- src/vnode/tsdb/src/tsdbMain.c | 1 + src/vnode/tsdb/src/tsdbRead.c | 2 +- 6 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/util/inc/talgo.h b/src/util/inc/talgo.h index e89795c086..d5e089b687 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 c4c802a688..81cf177e73 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 1004d5ab67..7a682cd466 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 d025144ba9..7576717dd1 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 2b26eec53a..f558982419 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 8d48b1ec48..495cf8c7f5 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 -- GitLab