diff --git a/source/libs/index/inc/indexSparse.h b/source/libs/index/inc/indexSparse.h index 6424b5e2ac09c763a60d26d93d1d5766ef85a5e9..8035f6e08df264e27fda183775259079a236caa1 100644 --- a/source/libs/index/inc/indexSparse.h +++ b/source/libs/index/inc/indexSparse.h @@ -29,10 +29,10 @@ typedef struct FstSparseSet { } FstSparseSet; FstSparseSet *sparSetCreate(int32_t sz); -void sparSetDestroy(FstSparseSet **s); -int32_t sparSetLen(FstSparseSet *ss); -int32_t sparSetAdd(FstSparseSet *ss, uint32_t ip); -int32_t sparSetGet(FstSparseSet *ss, uint32_t i); +void sparSetDestroy(FstSparseSet *s); +uint32_t sparSetLen(FstSparseSet *ss); +uint32_t sparSetAdd(FstSparseSet *ss, uint32_t ip); +uint32_t sparSetGet(FstSparseSet *ss, uint32_t i); bool sparSetContains(FstSparseSet *ss, uint32_t ip); void sparSetClear(FstSparseSet *ss); diff --git a/source/libs/index/src/indexSparse.c b/source/libs/index/src/indexSparse.c index 75b661ada785d68271112ccd03bd06462b5beef9..8bcf04602f0369cdeb5c4072e6b9c5a7345e13b6 100644 --- a/source/libs/index/src/indexSparse.c +++ b/source/libs/index/src/indexSparse.c @@ -14,3 +14,58 @@ */ #include "indexSparse.h" + +FstSparseSet *sparSetCreate(int32_t sz) { + FstSparseSet *ss = taosMemoryCalloc(1, sizeof(FstSparseSet)); + if (ss = NULL) { + return NULL; + } + + ss->dense = taosArrayInit(sz, sizeof(uint32_t)); + ss->sparse = taosArrayInit(sz, sizeof(uint32_t)); + ss->size = sz; + return ss; +} +void sparSetDestroy(FstSparseSet *ss) { + if (ss == NULL) { + return; + } + taosArrayDestroy(ss->dense); + taosArrayDestroy(ss->sparse); + taosMemoryFree(ss); +} +uint32_t sparSetLen(FstSparseSet *ss) { return ss == NULL ? 0 : ss->size; } +uint32_t sparSetAdd(FstSparseSet *ss, uint32_t ip) { + if (ss == NULL) { + return 0; + } + uint32_t i = ss->size; + taosArraySet(ss->dense, i, &ip); + taosArraySet(ss->sparse, ip, &i); + ss->size += 1; + return i; +} +uint32_t sparSetGet(FstSparseSet *ss, uint32_t i) { + if (i >= taosArrayGetSize(ss->dense)) { + return 0; + } + uint32_t *v = taosArrayGet(ss->dense, i); + return *v; +} +bool sparSetContains(FstSparseSet *ss, uint32_t ip) { + if (ip >= taosArrayGetSize(ss->sparse)) { + return false; + } + uint32_t i = *(uint32_t *)taosArrayGet(ss->sparse, ip); + if (i >= taosArrayGetSize(ss->dense)) { + return false; + } + uint32_t v = *(uint32_t *)taosArrayGet(ss->dense, i); + return v == ip; +} +void sparSetClear(FstSparseSet *ss) { + if (ss == NULL) { + return; + } + ss->size = 0; +}