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
|
|
|
|
|
|
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_; \
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 02:15:07 +00:00
|
|
|
#define TARRAY_SIZE(ARRAY) (ARRAY)->tarray_size_
|
|
|
|
|
#define TARRAY_NELES(ARRAY) (ARRAY)->tarray_neles_
|
2022-01-10 12:51:40 +00:00
|
|
|
#define TARRAY_ELE_AT(ARRAY, IDX) ((ARRAY)->td_array_data_ + idx)
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-02-28 02:15:07 +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-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 {
|
2022-04-19 09:02:49 +00:00
|
|
|
size_t size;
|
2022-02-23 07:10:15 +00:00
|
|
|
uint32_t capacity;
|
|
|
|
|
uint32_t elemSize;
|
2022-04-19 09:02:49 +00:00
|
|
|
void* pData;
|
2020-03-04 05:58:56 +00:00
|
|
|
} SArray;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param size
|
|
|
|
|
* @param elemSize
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2021-12-16 03:18:51 +00:00
|
|
|
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-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
|
|
|
|
2021-07-19 09:55:42 +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-07-19 06:49:11 +00:00
|
|
|
|
2022-07-19 07:52:08 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param pArray
|
|
|
|
|
* @param comparFn
|
|
|
|
|
* @param fp
|
|
|
|
|
*/
|
|
|
|
|
void taosArrayRemoveDuplicateP(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
|
|
|
|
|
*/
|
2020-04-22 09:14:39 +00:00
|
|
|
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
|
|
|
|
|
*/
|
2020-04-22 09:14:39 +00:00
|
|
|
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
|
|
|
|
2021-05-28 08:16:18 +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
|
|
|
/**
|
2020-03-27 09:55:54 +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-03-27 09:55:54 +00:00
|
|
|
|
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
|
|
|
/**
|
2022-04-19 09:02:49 +00:00
|
|
|
* deep copy a new array
|
|
|
|
|
* @param pSrc
|
2020-05-09 03:54:51 +00:00
|
|
|
*/
|
2022-04-19 09:02:49 +00:00
|
|
|
SArray* taosArrayDeepCopy(const SArray* pSrc, FCopy deepCopy);
|
2020-05-09 03:54:51 +00:00
|
|
|
|
2020-03-27 09:55:54 +00:00
|
|
|
/**
|
2022-04-19 09:02:49 +00:00
|
|
|
* clear the array (remove all element)
|
2020-03-04 05:58:56 +00:00
|
|
|
* @param pArray
|
|
|
|
|
*/
|
2022-04-19 09:02:49 +00:00
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
void taosArrayClearEx(SArray* pArray, void (*fp)(void*));
|
|
|
|
|
|
2022-04-25 03:45:32 +00:00
|
|
|
/**
|
|
|
|
|
* clear the array (remove all element)
|
|
|
|
|
* @param pArray
|
|
|
|
|
* @param fp
|
|
|
|
|
*/
|
|
|
|
|
void taosArrayClearP(SArray* pArray, FDelete fp);
|
|
|
|
|
|
2022-04-19 09:02:49 +00:00
|
|
|
void* taosArrayDestroy(SArray* pArray);
|
|
|
|
|
void taosArrayDestroyP(SArray* pArray, FDelete fp);
|
|
|
|
|
void taosArrayDestroyEx(SArray* pArray, FDelete fp);
|
2020-11-09 08:24:25 +00:00
|
|
|
|
2020-04-21 11:44:17 +00:00
|
|
|
/**
|
|
|
|
|
* sort the array
|
|
|
|
|
* @param pArray
|
|
|
|
|
* @param compar
|
|
|
|
|
*/
|
2021-01-28 15:21:33 +00:00
|
|
|
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
|
|
|
|
|
*/
|
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
|
|
|
|
2021-06-26 01:08:59 +00:00
|
|
|
/**
|
|
|
|
|
* sort the pointer data in the array
|
|
|
|
|
* @param pArray
|
2022-01-10 12:51:40 +00:00
|
|
|
* @param compar
|
|
|
|
|
* @param param
|
2021-06-26 01:08:59 +00:00
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
|
2022-01-10 12:51:40 +00:00
|
|
|
void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* param);
|
2021-06-26 01:08:59 +00:00
|
|
|
|
2022-04-19 09:02:49 +00:00
|
|
|
int32_t taosEncodeArray(void** buf, const SArray* pArray, FEncode encode);
|
|
|
|
|
void* taosDecodeArray(const void* buf, SArray** pArray, FDecode decode, int32_t dataSz);
|
|
|
|
|
|
2022-10-08 07:48:52 +00:00
|
|
|
/**
|
|
|
|
|
* swap array
|
|
|
|
|
* @param a
|
|
|
|
|
* @param b
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
void taosArraySwap(SArray* a, SArray* b);
|
2022-10-26 01:47:11 +00:00
|
|
|
|
2020-03-04 05:58:56 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-02-28 02:47:44 +00:00
|
|
|
#endif /*_TD_UTIL_ARRAY_H_*/
|