TDengine/include/util/tarray.h

273 lines
5.4 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/>.
*/
2021-09-30 13:21:51 +00:00
#ifndef _TD_UTIL_ARRAY_H
#define _TD_UTIL_ARRAY_H
2020-03-04 05:58:56 +00:00
#ifdef __cplusplus
extern "C" {
#endif
#include "os.h"
2020-12-01 08:19:07 +00:00
#include "talgo.h"
2020-03-04 05:58:56 +00:00
2022-01-10 12:51:40 +00:00
#if 0
#define TARRAY(TYPE) \
struct { \
int32_t tarray_size_; \
int32_t tarray_neles_; \
struct TYPE* td_array_data_; \
}
#define TARRAY_SIZE(ARRAY) (ARRAY)->tarray_size_
#define TARRAY_NELES(ARRAY) (ARRAY)->tarray_neles_
#define TARRAY_ELE_AT(ARRAY, IDX) ((ARRAY)->td_array_data_ + idx)
#endif
2020-03-04 05:58:56 +00:00
#define TARRAY_MIN_SIZE 8
2020-07-11 09:01:35 +00:00
#define TARRAY_GET_ELEM(array, index) ((void*)((char*)((array)->pData) + (index) * (array)->elemSize))
2022-01-10 12:51:40 +00:00
#define TARRAY_ELEM_IDX(array, ele) (POINTER_DISTANCE(ele, (array)->pData) / (array)->elemSize)
#define TARRAY_GET_START(array) ((array)->pData)
2020-03-04 05:58:56 +00:00
typedef struct SArray {
size_t size;
size_t capacity;
size_t elemSize;
2020-07-11 09:01:35 +00:00
void* pData;
2020-03-04 05:58:56 +00:00
} SArray;
/**
*
* @param size
* @param elemSize
* @return
*/
SArray* taosArrayInit(size_t size, size_t elemSize);
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-01-10 12:51:40 +00:00
void* taosArrayAddBatch(SArray* pArray, const void* pData, int nEles);
2021-05-13 02:54:28 +00:00
/**
*
* @param pArray
* @param pData position array list
* @param numOfElems the number of removed position
*/
2022-01-10 12:51:40 +00:00
void taosArrayRemoveBatch(SArray* pArray, const int32_t* pData, int32_t numOfElems);
/**
*
* @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
/**
*
* @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
*/
2020-03-12 06:02:16 +00:00
size_t taosArrayGetSize(const SArray* pArray);
2020-03-04 05:58:56 +00:00
/**
* set the size of array
* @param pArray
* @param size size of the array
* @return
*/
void taosArraySetSize(SArray* pArray, size_t size);
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
*/
2020-03-08 14:19:41 +00:00
void* taosArrayInsert(SArray* pArray, size_t index, 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);
/**
* 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
*/
2021-01-17 14:50:23 +00:00
SArray* taosArrayDup(const SArray* pSrc);
2020-04-10 07:06:20 +00:00
2020-05-09 03:54:51 +00:00
/**
* clear the array (remove all element)
* @param pArray
*/
void taosArrayClear(SArray* pArray);
/**
* destroy array list
2020-03-04 05:58:56 +00:00
* @param pArray
*/
2021-01-07 08:31:16 +00:00
void* taosArrayDestroy(SArray* pArray);
2020-03-04 05:58:56 +00:00
2020-11-09 08:24:25 +00:00
/**
*
* @param pArray
* @param fp
*/
void taosArrayDestroyEx(SArray* pArray, void (*fp)(void*));
2020-04-21 11:44:17 +00:00
/**
* sort the array
* @param pArray
* @param compar
*/
void taosArraySort(SArray* pArray, __compar_fn_t comparFn);
2020-04-21 11:44:17 +00:00
2020-04-23 07:11:44 +00:00
/**
* sort string array
* @param pArray
*/
2020-05-23 09:34:15 +00:00
void taosArraySortString(SArray* pArray, __compar_fn_t comparFn);
2020-04-23 07:11:44 +00:00
2020-04-21 11:44:17 +00:00
/**
* search the array
* @param pArray
* @param compar
* @param key
*/
2020-12-01 08:19:07 +00:00
void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int 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
*/
int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int flags);
2020-04-23 07:11:44 +00:00
/**
* search the array
* @param pArray
* @param key
*/
2020-12-01 08:19:07 +00:00
char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t comparFn, int flags);
2020-04-21 11:44:17 +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);
2020-03-04 05:58:56 +00:00
#ifdef __cplusplus
}
#endif
2022-01-10 12:51:40 +00:00
#endif /*_TD_UTIL_ARRAY_H*/