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
|
|
|
|
|
|
|
|
#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))
|
2021-03-06 05:45:45 +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
|
|
|
|
|
*/
|
|
|
|
|
void* taosArrayInit(size_t size, size_t elemSize);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param pArray
|
|
|
|
|
* @param pData
|
2021-01-08 10:17:37 +00:00
|
|
|
* @param nEles
|
2020-03-04 05:58:56 +00:00
|
|
|
* @return
|
|
|
|
|
*/
|
2021-05-13 02:54:28 +00:00
|
|
|
void *taosArrayAddBatch(SArray *pArray, const void *pData, int nEles);
|
|
|
|
|
|
2021-07-19 06:49:11 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param pArray
|
|
|
|
|
* @param pData position array list
|
|
|
|
|
* @param numOfElems the number of removed position
|
|
|
|
|
*/
|
|
|
|
|
void taosArrayRemoveBatch(SArray *pArray, const int32_t* pData, int32_t numOfElems);
|
|
|
|
|
|
2021-07-19 09:55:42 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param pArray
|
|
|
|
|
* @param comparFn
|
|
|
|
|
* @param fp
|
|
|
|
|
*/
|
|
|
|
|
void taosArrayRemoveDuplicate(SArray *pArray, __compar_fn_t comparFn, void (*fp)(void*));
|
2021-07-19 06:49:11 +00:00
|
|
|
|
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
|
|
|
|
|
*/
|
2020-03-12 06:02:16 +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
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* clear the array (remove all element)
|
|
|
|
|
* @param pArray
|
|
|
|
|
*/
|
|
|
|
|
void taosArrayClear(SArray* pArray);
|
|
|
|
|
|
2020-03-27 09:55:54 +00:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
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
|
|
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
2021-06-26 01:08:59 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* sort the pointer data in the array
|
|
|
|
|
* @param pArray
|
|
|
|
|
* @param compar
|
|
|
|
|
* @param param
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void *param);
|
|
|
|
|
|
2020-03-04 05:58:56 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-09-30 13:21:51 +00:00
|
|
|
#endif /*_TD_UTIL_ARRAY_H*/
|