tarray.h 5.3 KB
Newer Older
H
more  
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
Shengliang Guan 已提交
16 17
#ifndef _TD_UTIL_ARRAY_H_
#define _TD_UTIL_ARRAY_H_
H
more  
hzcheng 已提交
18

S
tarray  
Shengliang Guan 已提交
19 20
#include "talgo.h"

H
more  
hzcheng 已提交
21 22 23 24
#ifdef __cplusplus
extern "C" {
#endif

S
tarray  
Shengliang Guan 已提交
25
#define TARRAY_MIN_SIZE               8
H
Haojun Liao 已提交
26
#define TARRAY_GET_ELEM(array, index) ((void*)((char*)((array)->pData) + (index) * (array)->elemSize))
S
tarray  
Shengliang Guan 已提交
27
#define TARRAY_ELEM_IDX(array, ele)   (POINTER_DISTANCE(ele, (array)->pData) / (array)->elemSize)
H
more  
hzcheng 已提交
28 29

typedef struct SArray {
30
  size_t   size;
H
Haojun Liao 已提交
31 32
  uint32_t capacity;
  uint32_t elemSize;
33
  void*    pData;
H
more  
hzcheng 已提交
34 35
} SArray;

H
Hongze Cheng 已提交
36 37 38
#define TARRAY_SIZE(array) ((array)->size)
#define TARRAY_DATA(array) ((array)->pData)

H
more  
hzcheng 已提交
39 40 41 42 43 44
/**
 *
 * @param size
 * @param elemSize
 * @return
 */
45
SArray* taosArrayInit(size_t size, size_t elemSize);
46
SArray* taosArrayInit_s(size_t elemSize, size_t initialSize);
H
more  
hzcheng 已提交
47

L
Liu Jicong 已提交
48 49 50 51 52 53 54
/**
 *
 * @param tsize
 * @return
 */
int32_t taosArrayEnsureCap(SArray* pArray, size_t tsize);

H
more  
hzcheng 已提交
55 56 57 58
/**
 *
 * @param pArray
 * @param pData
H
Hongze Cheng 已提交
59
 * @param nEles
H
more  
hzcheng 已提交
60 61
 * @return
 */
S
tarray  
Shengliang Guan 已提交
62
void* taosArrayAddBatch(SArray* pArray, const void* pData, int32_t nEles);
H
Haojun Liao 已提交
63

64 65 66 67 68 69
/**
 *
 * @param pArray
 * @param comparFn
 * @param fp
 */
H
Hongze Cheng 已提交
70
void taosArrayRemoveDuplicate(SArray* pArray, __compar_fn_t comparFn, void (*fp)(void*));
71

H
Haojun Liao 已提交
72 73 74 75 76 77 78
/**
 *  add all element from the source array list into the destination
 * @param pArray
 * @param pInput
 * @return
 */
void* taosArrayAddAll(SArray* pArray, const SArray* pInput);
H
Hongze Cheng 已提交
79 80 81 82 83 84 85 86

/**
 *
 * @param pArray
 * @param pData
 * @return
 */
static FORCE_INLINE void* taosArrayPush(SArray* pArray, const void* pData) {
H
Haojun Liao 已提交
87
  return taosArrayAddBatch(pArray, pData, 1);
H
Hongze Cheng 已提交
88
}
H
more  
hzcheng 已提交
89

H
Hongze Cheng 已提交
90 91 92 93 94 95 96 97 98
/**
 * @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);

H
more  
hzcheng 已提交
99 100 101 102
/**
 *
 * @param pArray
 */
weixin_48148422's avatar
weixin_48148422 已提交
103
void* taosArrayPop(SArray* pArray);
H
more  
hzcheng 已提交
104 105

/**
H
hjxilinx 已提交
106
 * get the data from array
H
more  
hzcheng 已提交
107 108 109 110
 * @param pArray
 * @param index
 * @return
 */
111
void* taosArrayGet(const SArray* pArray, size_t index);
H
more  
hzcheng 已提交
112 113

/**
H
hjxilinx 已提交
114 115 116 117 118
 * get the pointer data from the array
 * @param pArray
 * @param index
 * @return
 */
119
void* taosArrayGetP(const SArray* pArray, size_t index);
H
hjxilinx 已提交
120

H
Haojun Liao 已提交
121 122 123 124 125 126 127
/**
 * get the last element in the array list
 * @param pArray
 * @return
 */
void* taosArrayGetLast(const SArray* pArray);

H
hjxilinx 已提交
128 129
/**
 * return the size of array
H
more  
hzcheng 已提交
130 131 132
 * @param pArray
 * @return
 */
L
Liu Jicong 已提交
133
size_t taosArrayGetSize(const SArray* pArray);
H
more  
hzcheng 已提交
134 135

/**
H
hjxilinx 已提交
136
 * insert data into array
H
more  
hzcheng 已提交
137 138 139 140
 * @param pArray
 * @param index
 * @param pData
 */
H
hjxilinx 已提交
141
void* taosArrayInsert(SArray* pArray, size_t index, void* pData);
H
more  
hzcheng 已提交
142

H
refact  
Hongze Cheng 已提交
143 144 145 146 147 148
/**
 * set data in array
 * @param pArray
 * @param index
 * @param pData
 */
H
Hongze Cheng 已提交
149
void taosArraySet(SArray* pArray, size_t index, void* pData);
H
refact  
Hongze Cheng 已提交
150

L
Liu Jicong 已提交
151 152 153 154 155 156 157
/**
 * remove some data entry from front
 * @param pArray
 * @param cnt
 */
void taosArrayPopFrontBatch(SArray* pArray, size_t cnt);

L
Liu Jicong 已提交
158 159 160 161 162 163 164
/**
 * remove some data entry from front
 * @param pArray
 * @param cnt
 */
void taosArrayPopTailBatch(SArray* pArray, size_t cnt);

H
more  
hzcheng 已提交
165
/**
166 167 168 169 170 171
 * remove data entry of the given index
 * @param pArray
 * @param index
 */
void taosArrayRemove(SArray* pArray, size_t index);

H
Hongze Cheng 已提交
172 173 174 175 176 177 178
/**
 * remove batch entry from the given index
 * @param pArray
 * @param index
 */
void taosArrayRemoveBatch(SArray* pArray, size_t index, size_t num, FDelete fp);

179 180 181 182 183
/**
 * copy the whole array from source to destination
 * @param pDst
 * @param pSrc
 */
H
Haojun Liao 已提交
184
SArray* taosArrayFromList(const void* src, size_t size, size_t elemSize);
185

H
hjxilinx 已提交
186 187 188 189
/**
 * clone a new array
 * @param pSrc
 */
H
Haojun Liao 已提交
190
SArray* taosArrayDup(const SArray* pSrc, __array_item_dup_fn_t fn);
weixin_48148422's avatar
weixin_48148422 已提交
191

192
/**
193
 * clear the array (remove all element)
H
more  
hzcheng 已提交
194 195
 * @param pArray
 */
196
void taosArrayClear(SArray* pArray);
H
more  
hzcheng 已提交
197

D
dapan1121 已提交
198 199 200 201 202 203 204
/**
 * clear the array (remove all element)
 * @param pArray
 * @param fp
 */
void taosArrayClearEx(SArray* pArray, void (*fp)(void*));

205
void* taosArrayDestroy(SArray* pArray);
H
Haojun Liao 已提交
206

207
void  taosArrayDestroyP(SArray* pArray, FDelete fp);
H
Haojun Liao 已提交
208

209
void  taosArrayDestroyEx(SArray* pArray, FDelete fp);
H
Haojun Liao 已提交
210

211 212
void taosArraySwap(SArray* a, SArray* b);

weixin_48148422's avatar
weixin_48148422 已提交
213 214 215 216 217
/**
 * sort the array
 * @param pArray
 * @param compar
 */
218
void taosArraySort(SArray* pArray, __compar_fn_t comparFn);
weixin_48148422's avatar
weixin_48148422 已提交
219 220 221 222 223 224 225

/**
 * search the array
 * @param pArray
 * @param compar
 * @param key
 */
S
tarray  
Shengliang Guan 已提交
226
void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags);
weixin_48148422's avatar
weixin_48148422 已提交
227

L
Liu Jicong 已提交
228 229 230 231 232 233
/**
 * search the array, return index of the element
 * @param pArray
 * @param compar
 * @param key
 */
S
tarray  
Shengliang Guan 已提交
234
int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags);
L
Liu Jicong 已提交
235

236 237 238
/**
 * sort the pointer data in the array
 * @param pArray
H
Hongze Cheng 已提交
239 240
 * @param compar
 * @param param
241 242 243
 * @return
 */

H
Hongze Cheng 已提交
244
void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* param);
245

246
int32_t taosEncodeArray(void** buf, const SArray* pArray, FEncode encode);
247
void*   taosDecodeArray(const void* buf, SArray** pArray, FDecode decode, int32_t dataSz, int8_t sver);
248

H
more  
hzcheng 已提交
249 250 251 252
#ifdef __cplusplus
}
#endif

S
Shengliang Guan 已提交
253
#endif /*_TD_UTIL_ARRAY_H_*/