diff --git a/src/util/inc/tarray.h b/src/util/inc/tarray.h index eed165240c06a35c958cbd4e4e1b25c744c55fa8..96bcd64c3ecc4f559bf99ad78cc655b2bca64b6f 100644 --- a/src/util/inc/tarray.h +++ b/src/util/inc/tarray.h @@ -25,6 +25,7 @@ extern "C" { #define TARRAY_MIN_SIZE 8 #define TARRAY_GET_ELEM(array, index) ((void*)((char*)((array)->pData) + (index) * (array)->elemSize)) +#define TARRAY_ELEM_IDX(array, ele) (POINTER_DISTANCE(ele, (array)->pData) / (array)->elemSize) typedef struct SArray { size_t size; @@ -93,6 +94,14 @@ size_t taosArrayGetSize(const SArray* pArray); */ void* taosArrayInsert(SArray* pArray, size_t index, void* pData); +/** + * set data in array + * @param pArray + * @param index + * @param pData + */ +void* taosArraySet(SArray* pArray, size_t index, void* pData); + /** * remove data entry of the given index * @param pArray diff --git a/src/util/src/tarray.c b/src/util/src/tarray.c index 35b66bd905cf77d45fc54e81019bb81481ef06dc..df6d7b0ba2e6c6e60932eab82d2445f7846cc716 100644 --- a/src/util/src/tarray.c +++ b/src/util/src/tarray.c @@ -133,6 +133,11 @@ void* taosArrayInsert(SArray* pArray, size_t index, void* pData) { return dst; } +void* taosArraySet(SArray* pArray, size_t index, void* pData) { + assert(index < pArray->size); + memcpy(TARRAY_GET_ELEM(pArray, index), pData, pArray->elemSize); +} + void taosArrayRemove(SArray* pArray, size_t index) { assert(index < pArray->size);