From cb71affec50f31ade7d69d94ab40f8a54456deeb Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 28 Feb 2022 13:57:10 +0800 Subject: [PATCH] hash --- include/util/theap.h | 24 ++++++------ include/util/tidpool.h | 29 ++++++--------- source/util/src/thashutil.c | 11 +++--- source/util/src/theap.c | 73 ++++++++++++++----------------------- source/util/src/tidpool.c | 39 ++++++++++---------- 5 files changed, 76 insertions(+), 100 deletions(-) diff --git a/include/util/theap.h b/include/util/theap.h index fd1a39f8dd..fb5ff8301a 100644 --- a/include/util/theap.h +++ b/include/util/theap.h @@ -12,19 +12,20 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -#ifndef TDENGINE_HEAP_H -#define TDENGINE_HEAP_H + +#ifndef _TD_UTIL_HEAP_H_ +#define _TD_UTIL_HEAP_H_ + +#include "os.h" #ifdef __cplusplus extern "C" { #endif -#include "os.h" - struct HeapNode; /* Return non-zero if a < b. */ -typedef int (*HeapCompareFn)(const struct HeapNode* a, const struct HeapNode* b); +typedef int32_t (*HeapCompareFn)(const struct HeapNode* a, const struct HeapNode* b); typedef struct HeapNode { struct HeapNode* left; @@ -38,15 +39,14 @@ typedef struct HeapNode { * */ typedef struct { - HeapNode* min; - size_t nelts; - HeapCompareFn compFn; + HeapNode* min; + size_t nelts; + HeapCompareFn compFn; } Heap; - Heap* heapCreate(HeapCompareFn fn); -void heapDestroy(Heap *heap); +void heapDestroy(Heap* heap); HeapNode* heapMin(const Heap* heap); @@ -56,10 +56,10 @@ void heapRemove(Heap* heap, struct HeapNode* node); void heapDequeue(Heap* heap); -size_t heapSize(Heap *heap); +size_t heapSize(Heap* heap); #ifdef __cplusplus } #endif -#endif // TDENGINE_HASH_H +#endif /*_TD_UTIL_HEAP_H_*/ diff --git a/include/util/tidpool.h b/include/util/tidpool.h index 1a977fd04c..8a9e0c2413 100644 --- a/include/util/tidpool.h +++ b/include/util/tidpool.h @@ -13,31 +13,24 @@ * along with this program. If not, see . */ -#ifndef _TD_UTIL_IDPOOL_H -#define _TD_UTIL_IDPOOL_H +#ifndef _TD_UTIL_IDPOOL_H_ +#define _TD_UTIL_IDPOOL_H_ #ifdef __cplusplus extern "C" { #endif -void *taosInitIdPool(int maxId); - -int taosUpdateIdPool(void *handle, int maxId); - -int taosIdPoolMaxSize(void *handle); - -int taosAllocateId(void *handle); - -void taosFreeId(void *handle, int id); - -void taosIdPoolCleanUp(void *handle); - -int taosIdPoolNumOfUsed(void *handle); - -bool taosIdPoolMarkStatus(void *handle, int id); +void *taosInitIdPool(int32_t maxId); +int32_t taosUpdateIdPool(void *handle, int32_t maxId); +int32_t taosIdPoolMaxSize(void *handle); +int32_t taosAllocateId(void *handle); +void taosFreeId(void *handle, int32_t id); +void taosIdPoolCleanUp(void *handle); +int32_t taosIdPoolNumOfUsed(void *handle); +bool taosIdPoolMarkStatus(void *handle, int32_t id); #ifdef __cplusplus } #endif -#endif /*_TD_UTIL_IDPOOL_H*/ +#endif /*_TD_UTIL_IDPOOL_H_*/ diff --git a/source/util/src/thashutil.c b/source/util/src/thashutil.c index 9084c05d29..d5182cb892 100644 --- a/source/util/src/thashutil.c +++ b/source/util/src/thashutil.c @@ -13,10 +13,9 @@ * along with this program. If not, see . */ -#include "os.h" +#define _DEFAULT_SOURCE #include "thash.h" #include "tcompare.h" -#include "taos.h" #include "types.h" #define ROTL32(x, r) ((x) << (r) | (x) >> (32u - (r))) @@ -34,7 +33,7 @@ uint32_t MurmurHash3_32(const char *key, uint32_t len) { const uint8_t *data = (const uint8_t *)key; - const int nblocks = len >> 2u; + const int32_t nblocks = len >> 2u; uint32_t h1 = 0x12345678; @@ -43,7 +42,7 @@ uint32_t MurmurHash3_32(const char *key, uint32_t len) { const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4); - for (int i = -nblocks; i; i++) { + for (int32_t i = -nblocks; i; i++) { uint32_t k1 = blocks[i]; k1 *= c1; @@ -92,7 +91,7 @@ uint32_t taosFloatHash(const char *key, uint32_t UNUSED_PARAM(len)) { return 0; } if (fabs(f) < FLT_MAX/BASE - DLT) { - int t = (int)(round(BASE * (f + DLT))); + int32_t t = (int32_t)(round(BASE * (f + DLT))); return (uint32_t)t; } else { return 0x7fc00000; @@ -108,7 +107,7 @@ uint32_t taosDoubleHash(const char *key, uint32_t UNUSED_PARAM(len)) { return 0; } if (fabs(f) < DBL_MAX/BASE - DLT) { - int t = (int)(round(BASE * (f + DLT))); + int32_t t = (int32_t)(round(BASE * (f + DLT))); return (uint32_t)t; } else { return 0x7fc00000; diff --git a/source/util/src/theap.c b/source/util/src/theap.c index aa822c7d5e..30af0483cc 100644 --- a/source/util/src/theap.c +++ b/source/util/src/theap.c @@ -13,15 +13,16 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "theap.h" -size_t heapSize(Heap* heap) { - return heap->nelts; -} +size_t heapSize(Heap* heap) { return heap->nelts; } Heap* heapCreate(HeapCompareFn fn) { - Heap *heap = calloc(1, sizeof(Heap)); - if (heap == NULL) { return NULL; } + Heap* heap = calloc(1, sizeof(Heap)); + if (heap == NULL) { + return NULL; + } heap->min = NULL; heap->nelts = 0; @@ -29,18 +30,14 @@ Heap* heapCreate(HeapCompareFn fn) { return heap; } -void heapDestroy(Heap *heap) { - free(heap); -} +void heapDestroy(Heap* heap) { free(heap); } -HeapNode* heapMin(const Heap* heap) { - return heap->min; -} +HeapNode* heapMin(const Heap* heap) { return heap->min; } /* Swap parent with child. Child moves closer to the root, parent moves away. */ static void heapNodeSwap(Heap* heap, HeapNode* parent, HeapNode* child) { HeapNode* sibling; - HeapNode t; + HeapNode t; t = *parent; *parent = *child; @@ -54,13 +51,10 @@ static void heapNodeSwap(Heap* heap, HeapNode* parent, HeapNode* child) { child->right = parent; sibling = child->left; } - if (sibling != NULL) - sibling->parent = child; + if (sibling != NULL) sibling->parent = child; - if (parent->left != NULL) - parent->left->parent = parent; - if (parent->right != NULL) - parent->right->parent = parent; + if (parent->left != NULL) parent->left->parent = parent; + if (parent->right != NULL) parent->right->parent = parent; if (child->parent == NULL) heap->min = child; @@ -73,9 +67,9 @@ static void heapNodeSwap(Heap* heap, HeapNode* parent, HeapNode* child) { void heapInsert(Heap* heap, HeapNode* newnode) { HeapNode** parent; HeapNode** child; - unsigned int path; - unsigned int n; - unsigned int k; + uint32_t path; + uint32_t n; + uint32_t k; newnode->left = NULL; newnode->right = NULL; @@ -85,8 +79,7 @@ void heapInsert(Heap* heap, HeapNode* newnode) { * heap so we always insert at the left-most free node of the bottom row. */ path = 0; - for (k = 0, n = 1 + heap->nelts; n >= 2; k += 1, n /= 2) - path = (path << 1) | (n & 1); + for (k = 0, n = 1 + heap->nelts; n >= 2; k += 1, n /= 2) path = (path << 1) | (n & 1); /* Now traverse the heap using the path we calculated in the previous step. */ parent = child = &heap->min; @@ -113,22 +106,20 @@ void heapInsert(Heap* heap, HeapNode* newnode) { } void heapRemove(Heap* heap, HeapNode* node) { - HeapNode* smallest; + HeapNode* smallest; HeapNode** max; - HeapNode* child; - unsigned int path; - unsigned int k; - unsigned int n; + HeapNode* child; + uint32_t path; + uint32_t k; + uint32_t n; - if (heap->nelts == 0) - return; + if (heap->nelts == 0) return; /* Calculate the path from the min (the root) to the max, the left-most node * of the bottom row. */ path = 0; - for (k = 0, n = heap->nelts; n >= 2; k += 1, n /= 2) - path = (path << 1) | (n & 1); + for (k = 0, n = heap->nelts; n >= 2; k += 1, n /= 2) path = (path << 1) | (n & 1); /* Now traverse the heap using the path we calculated in the previous step. */ max = &heap->min; @@ -182,12 +173,9 @@ void heapRemove(Heap* heap, HeapNode* node) { */ for (;;) { smallest = child; - if (child->left != NULL && (heap->compFn)(child->left, smallest)) - smallest = child->left; - if (child->right != NULL && (heap->compFn)(child->right, smallest)) - smallest = child->right; - if (smallest == child) - break; + if (child->left != NULL && (heap->compFn)(child->left, smallest)) smallest = child->left; + if (child->right != NULL && (heap->compFn)(child->right, smallest)) smallest = child->right; + if (smallest == child) break; heapNodeSwap(heap, child, smallest); } @@ -195,12 +183,7 @@ void heapRemove(Heap* heap, HeapNode* node) { * this is required, because `max` node is not guaranteed to be the * actual maximum in tree */ - while (child->parent != NULL && (heap->compFn)(child, child->parent)) - heapNodeSwap(heap, child->parent, child); + while (child->parent != NULL && (heap->compFn)(child, child->parent)) heapNodeSwap(heap, child->parent, child); } -void heapDequeue(Heap* heap) { - heapRemove(heap, heap->min); -} - - +void heapDequeue(Heap* heap) { heapRemove(heap, heap->min); } diff --git a/source/util/src/tidpool.c b/source/util/src/tidpool.c index b4d76e6fb5..d7f733b1ae 100644 --- a/source/util/src/tidpool.c +++ b/source/util/src/tidpool.c @@ -13,17 +13,18 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "tlog.h" typedef struct { - int maxId; - int numOfFree; - int freeSlot; - bool * freeList; + int32_t maxId; + int32_t numOfFree; + int32_t freeSlot; + bool *freeList; pthread_mutex_t mutex; } id_pool_t; -void *taosInitIdPool(int maxId) { +void *taosInitIdPool(int32_t maxId) { id_pool_t *pIdPool = calloc(1, sizeof(id_pool_t)); if (pIdPool == NULL) return NULL; @@ -44,17 +45,17 @@ void *taosInitIdPool(int maxId) { return pIdPool; } -int taosAllocateId(void *handle) { +int32_t taosAllocateId(void *handle) { id_pool_t *pIdPool = handle; if (handle == NULL) { return -1; } - int slot = -1; + int32_t slot = -1; pthread_mutex_lock(&pIdPool->mutex); if (pIdPool->numOfFree > 0) { - for (int i = 0; i < pIdPool->maxId; ++i) { + for (int32_t i = 0; i < pIdPool->maxId; ++i) { slot = (i + pIdPool->freeSlot) % pIdPool->maxId; if (!pIdPool->freeList[slot]) { pIdPool->freeList[slot] = true; @@ -69,13 +70,13 @@ int taosAllocateId(void *handle) { return slot + 1; } -void taosFreeId(void *handle, int id) { +void taosFreeId(void *handle, int32_t id) { id_pool_t *pIdPool = handle; if (handle == NULL) return; pthread_mutex_lock(&pIdPool->mutex); - int slot = (id - 1) % pIdPool->maxId; + int32_t slot = (id - 1) % pIdPool->maxId; if (pIdPool->freeList[slot]) { pIdPool->freeList[slot] = false; pIdPool->numOfFree++; @@ -100,22 +101,22 @@ void taosIdPoolCleanUp(void *handle) { free(pIdPool); } -int taosIdPoolNumOfUsed(void *handle) { +int32_t taosIdPoolNumOfUsed(void *handle) { id_pool_t *pIdPool = handle; pthread_mutex_lock(&pIdPool->mutex); - int ret = pIdPool->maxId - pIdPool->numOfFree; + int32_t ret = pIdPool->maxId - pIdPool->numOfFree; pthread_mutex_unlock(&pIdPool->mutex); return ret; } -bool taosIdPoolMarkStatus(void *handle, int id) { - bool ret = false; +bool taosIdPoolMarkStatus(void *handle, int32_t id) { + bool ret = false; id_pool_t *pIdPool = handle; pthread_mutex_lock(&pIdPool->mutex); - int slot = (id - 1) % pIdPool->maxId; + int32_t slot = (id - 1) % pIdPool->maxId; if (!pIdPool->freeList[slot]) { pIdPool->freeList[slot] = true; pIdPool->numOfFree--; @@ -128,8 +129,8 @@ bool taosIdPoolMarkStatus(void *handle, int id) { return ret; } -int taosUpdateIdPool(id_pool_t *handle, int maxId) { - id_pool_t *pIdPool = (id_pool_t*)handle; +int32_t taosUpdateIdPool(id_pool_t *handle, int32_t maxId) { + id_pool_t *pIdPool = (id_pool_t *)handle; if (maxId <= pIdPool->maxId) { return 0; } @@ -154,11 +155,11 @@ int taosUpdateIdPool(id_pool_t *handle, int maxId) { return 0; } -int taosIdPoolMaxSize(void *handle) { +int32_t taosIdPoolMaxSize(void *handle) { id_pool_t *pIdPool = (id_pool_t *)handle; pthread_mutex_lock(&pIdPool->mutex); - int ret = pIdPool->maxId; + int32_t ret = pIdPool->maxId; pthread_mutex_unlock(&pIdPool->mutex); return ret;