TDengine/include/util/tarray.h

264 lines
5.5 KiB
C
Raw Normal View History

2020-03-04 05:58:56 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
2022-02-28 02:47:44 +00:00
#ifndef _TD_UTIL_ARRAY_H_
#define _TD_UTIL_ARRAY_H_
2020-03-04 05:58:56 +00:00
2022-02-28 02:15:07 +00:00
#include "talgo.h"
2020-03-04 05:58:56 +00:00
#ifdef __cplusplus
extern "C" {
#endif
2023-07-06 02:15:50 +00:00
#define TARRAY_MIN_SIZE 4
2020-07-11 09:01:35 +00:00
#define TARRAY_GET_ELEM(array, index) ((void*)((char*)((array)->pData) + (index) * (array)->elemSize))
2022-02-28 02:15:07 +00:00
#define TARRAY_ELEM_IDX(array, ele) (POINTER_DISTANCE(ele, (array)->pData) / (array)->elemSize)
2020-03-04 05:58:56 +00:00
typedef struct SArray {
size_t size;
2022-02-23 07:10:15 +00:00
uint32_t capacity;
uint32_t elemSize;
void* pData;
2020-03-04 05:58:56 +00:00
} SArray;
2022-11-29 08:28:12 +00:00
#define TARRAY_SIZE(array) ((array)->size)
#define TARRAY_DATA(array) ((array)->pData)
2020-03-04 05:58:56 +00:00
/**
*
* @param size
* @param elemSize
* @return
*/
SArray* taosArrayInit(size_t size, size_t elemSize);
2023-02-20 02:12:27 +00:00
SArray* taosArrayInit_s(size_t elemSize, size_t initialSize);
2020-03-04 05:58:56 +00:00
2021-12-13 05:58:54 +00:00
/**
*
* @param tsize
* @return
*/
int32_t taosArrayEnsureCap(SArray* pArray, size_t tsize);
2020-03-04 05:58:56 +00:00
/**
*
* @param pArray
* @param pData
2021-01-08 10:17:37 +00:00
* @param nEles
2020-03-04 05:58:56 +00:00
* @return
*/
2022-02-28 02:15:07 +00:00
void* taosArrayAddBatch(SArray* pArray, const void* pData, int32_t nEles);
2021-05-13 02:54:28 +00:00
/**
*
* @param pArray
* @param comparFn
* @param fp
*/
2022-01-10 12:51:40 +00:00
void taosArrayRemoveDuplicate(SArray* pArray, __compar_fn_t comparFn, void (*fp)(void*));
2021-05-13 02:54:28 +00:00
/**
* add all element from the source array list into the destination
* @param pArray
* @param pInput
* @return
*/
void* taosArrayAddAll(SArray* pArray, const SArray* pInput);
2021-01-08 10:17:37 +00:00
/**
*
* @param pArray
* @param pData
* @return
*/
static FORCE_INLINE void* taosArrayPush(SArray* pArray, const void* pData) {
2021-05-13 02:54:28 +00:00
return taosArrayAddBatch(pArray, pData, 1);
2021-01-08 10:17:37 +00:00
}
2020-03-04 05:58:56 +00:00
2022-11-22 02:10:23 +00:00
/**
* @brief reserve the capacity of the array
*
* @param pArray
* @param num
* @return void* the start position of the reserved memory
*/
void* taosArrayReserve(SArray* pArray, int32_t num);
2020-03-04 05:58:56 +00:00
/**
*
* @param pArray
*/
2020-04-21 11:44:17 +00:00
void* taosArrayPop(SArray* pArray);
2020-03-04 05:58:56 +00:00
/**
2020-03-08 14:19:41 +00:00
* get the data from array
2020-03-04 05:58:56 +00:00
* @param pArray
* @param index
* @return
*/
void* taosArrayGet(const SArray* pArray, size_t index);
2020-03-04 05:58:56 +00:00
/**
2020-03-08 14:19:41 +00:00
* get the pointer data from the array
* @param pArray
* @param index
* @return
*/
void* taosArrayGetP(const SArray* pArray, size_t index);
2020-03-08 14:19:41 +00:00
2020-11-09 08:24:25 +00:00
/**
* get the last element in the array list
* @param pArray
* @return
*/
void* taosArrayGetLast(const SArray* pArray);
2020-03-08 14:19:41 +00:00
/**
* return the size of array
2020-03-04 05:58:56 +00:00
* @param pArray
* @return
*/
2022-07-01 08:25:11 +00:00
size_t taosArrayGetSize(const SArray* pArray);
2020-03-04 05:58:56 +00:00
/**
2020-03-08 14:19:41 +00:00
* insert data into array
2020-03-04 05:58:56 +00:00
* @param pArray
* @param index
* @param pData
*/
2023-05-17 08:46:10 +00:00
void* taosArrayInsert(SArray* pArray, size_t index, const void* pData);
2020-03-04 05:58:56 +00:00
2020-12-01 09:10:30 +00:00
/**
* set data in array
* @param pArray
* @param index
* @param pData
*/
2020-12-01 10:42:07 +00:00
void taosArraySet(SArray* pArray, size_t index, void* pData);
2020-12-01 09:10:30 +00:00
2021-12-10 02:30:57 +00:00
/**
* remove some data entry from front
* @param pArray
* @param cnt
*/
void taosArrayPopFrontBatch(SArray* pArray, size_t cnt);
2021-12-11 09:17:21 +00:00
/**
* remove some data entry from front
* @param pArray
* @param cnt
*/
void taosArrayPopTailBatch(SArray* pArray, size_t cnt);
2020-03-04 05:58:56 +00:00
/**
* remove data entry of the given index
* @param pArray
* @param index
*/
void taosArrayRemove(SArray* pArray, size_t index);
2022-11-27 07:27:37 +00:00
/**
* remove batch entry from the given index
* @param pArray
* @param index
*/
void taosArrayRemoveBatch(SArray* pArray, size_t index, size_t num, FDelete fp);
/**
* copy the whole array from source to destination
* @param pDst
* @param pSrc
*/
2021-02-03 10:29:40 +00:00
SArray* taosArrayFromList(const void* src, size_t size, size_t elemSize);
2020-04-10 07:06:20 +00:00
/**
* clone a new array
* @param pSrc
*/
2022-11-28 08:13:18 +00:00
SArray* taosArrayDup(const SArray* pSrc, __array_item_dup_fn_t fn);
2020-05-09 03:54:51 +00:00
/**
* clear the array (remove all element)
2020-03-04 05:58:56 +00:00
* @param pArray
*/
void taosArrayClear(SArray* pArray);
2020-03-04 05:58:56 +00:00
2022-04-12 11:10:52 +00:00
/**
* clear the array (remove all element)
* @param pArray
* @param fp
*/
2023-09-01 05:24:47 +00:00
2022-04-12 11:10:52 +00:00
void taosArrayClearEx(SArray* pArray, void (*fp)(void*));
2023-09-01 05:24:47 +00:00
void taosArrayClearP(SArray* pArray, void (*fp)(void*));
void* taosArrayDestroy(SArray* pArray);
2022-11-30 09:52:12 +00:00
2023-05-12 08:23:07 +00:00
void taosArrayDestroyP(SArray* pArray, FDelete fp);
2022-11-30 09:52:12 +00:00
2023-05-12 08:23:07 +00:00
void taosArrayDestroyEx(SArray* pArray, FDelete fp);
2020-11-09 08:24:25 +00:00
2023-03-28 03:24:12 +00:00
void taosArraySwap(SArray* a, SArray* b);
2020-04-21 11:44:17 +00:00
/**
* sort the array use qsort
2020-04-21 11:44:17 +00:00
* @param pArray
* @param compar
*/
void taosArraySort(SArray* pArray, __compar_fn_t comparFn);
2020-04-21 11:44:17 +00:00
/**
* sort the array use merge sort
* @param pArray
* @param compar
*/
2023-09-18 05:46:29 +00:00
int32_t taosArrayMSort(SArray* pArray, __compar_fn_t comparFn);
2020-04-21 11:44:17 +00:00
/**
* search the array
* @param pArray
* @param compar
* @param key
*/
2022-02-28 02:15:07 +00:00
void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags);
2020-04-23 07:11:44 +00:00
2021-12-11 09:17:21 +00:00
/**
* search the array, return index of the element
* @param pArray
* @param compar
* @param key
*/
2022-02-28 02:15:07 +00:00
int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags);
2021-12-11 09:17:21 +00:00
/**
* sort the pointer data in the array
* @param pArray
2022-01-10 12:51:40 +00:00
* @param compar
* @param param
* @return
*/
2022-01-10 12:51:40 +00:00
void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* param);
int32_t taosEncodeArray(void** buf, const SArray* pArray, FEncode encode);
void* taosDecodeArray(const void* buf, SArray** pArray, FDecode decode, int32_t dataSz, int8_t sver);
2020-03-04 05:58:56 +00:00
#ifdef __cplusplus
}
#endif
2022-02-28 02:47:44 +00:00
#endif /*_TD_UTIL_ARRAY_H_*/