tarray.h 6.2 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

H
Hongze Cheng 已提交
25 26 27 28 29 30 31 32
#if 0
#define TARRAY(TYPE)             \
  struct {                       \
    int32_t      tarray_size_;   \
    int32_t      tarray_neles_;  \
    struct TYPE* td_array_data_; \
  }

S
tarray  
Shengliang Guan 已提交
33 34
#define TARRAY_SIZE(ARRAY)        (ARRAY)->tarray_size_
#define TARRAY_NELES(ARRAY)       (ARRAY)->tarray_neles_
H
Hongze Cheng 已提交
35 36 37
#define TARRAY_ELE_AT(ARRAY, IDX) ((ARRAY)->td_array_data_ + idx)
#endif

S
tarray  
Shengliang Guan 已提交
38
#define TARRAY_MIN_SIZE               8
H
Haojun Liao 已提交
39
#define TARRAY_GET_ELEM(array, index) ((void*)((char*)((array)->pData) + (index) * (array)->elemSize))
S
tarray  
Shengliang Guan 已提交
40 41
#define TARRAY_ELEM_IDX(array, ele)   (POINTER_DISTANCE(ele, (array)->pData) / (array)->elemSize)
#define TARRAY_GET_START(array)       ((array)->pData)
H
more  
hzcheng 已提交
42 43

typedef struct SArray {
44
  size_t   size;
H
Haojun Liao 已提交
45 46
  uint32_t capacity;
  uint32_t elemSize;
47
  void*    pData;
H
more  
hzcheng 已提交
48 49 50 51 52 53 54 55
} SArray;

/**
 *
 * @param size
 * @param elemSize
 * @return
 */
56
SArray* taosArrayInit(size_t size, size_t elemSize);
H
more  
hzcheng 已提交
57

L
Liu Jicong 已提交
58 59 60 61 62 63 64
/**
 *
 * @param tsize
 * @return
 */
int32_t taosArrayEnsureCap(SArray* pArray, size_t tsize);

H
more  
hzcheng 已提交
65 66 67 68
/**
 *
 * @param pArray
 * @param pData
H
Hongze Cheng 已提交
69
 * @param nEles
H
more  
hzcheng 已提交
70 71
 * @return
 */
S
tarray  
Shengliang Guan 已提交
72
void* taosArrayAddBatch(SArray* pArray, const void* pData, int32_t nEles);
H
Haojun Liao 已提交
73

74 75 76 77 78 79
/**
 *
 * @param pArray
 * @param pData           position array list
 * @param numOfElems      the number of removed position
 */
H
Hongze Cheng 已提交
80
void taosArrayRemoveBatch(SArray* pArray, const int32_t* pData, int32_t numOfElems);
81

82 83 84 85 86 87
/**
 *
 * @param pArray
 * @param comparFn
 * @param fp
 */
H
Hongze Cheng 已提交
88
void taosArrayRemoveDuplicate(SArray* pArray, __compar_fn_t comparFn, void (*fp)(void*));
89

90 91 92 93 94 95 96 97
/**
 *
 * @param pArray
 * @param comparFn
 * @param fp
 */
void taosArrayRemoveDuplicateP(SArray* pArray, __compar_fn_t comparFn, void (*fp)(void*));

H
Haojun Liao 已提交
98 99 100 101 102 103 104
/**
 *  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 已提交
105 106 107 108 109 110 111 112

/**
 *
 * @param pArray
 * @param pData
 * @return
 */
static FORCE_INLINE void* taosArrayPush(SArray* pArray, const void* pData) {
H
Haojun Liao 已提交
113
  return taosArrayAddBatch(pArray, pData, 1);
H
Hongze Cheng 已提交
114
}
H
more  
hzcheng 已提交
115 116 117 118 119

/**
 *
 * @param pArray
 */
weixin_48148422's avatar
weixin_48148422 已提交
120
void* taosArrayPop(SArray* pArray);
H
more  
hzcheng 已提交
121 122

/**
H
hjxilinx 已提交
123
 * get the data from array
H
more  
hzcheng 已提交
124 125 126 127
 * @param pArray
 * @param index
 * @return
 */
128
void* taosArrayGet(const SArray* pArray, size_t index);
H
more  
hzcheng 已提交
129 130

/**
H
hjxilinx 已提交
131 132 133 134 135
 * get the pointer data from the array
 * @param pArray
 * @param index
 * @return
 */
136
void* taosArrayGetP(const SArray* pArray, size_t index);
H
hjxilinx 已提交
137

H
Haojun Liao 已提交
138 139 140 141 142 143 144
/**
 * get the last element in the array list
 * @param pArray
 * @return
 */
void* taosArrayGetLast(const SArray* pArray);

H
hjxilinx 已提交
145 146
/**
 * return the size of array
H
more  
hzcheng 已提交
147 148 149
 * @param pArray
 * @return
 */
L
Liu Jicong 已提交
150
size_t taosArrayGetSize(const SArray* pArray);
H
more  
hzcheng 已提交
151

152 153 154 155 156 157 158 159
/**
 * set the size of array
 * @param pArray
 * @param size size of the array
 * @return
 */
void taosArraySetSize(SArray* pArray, size_t size);

H
more  
hzcheng 已提交
160
/**
H
hjxilinx 已提交
161
 * insert data into array
H
more  
hzcheng 已提交
162 163 164 165
 * @param pArray
 * @param index
 * @param pData
 */
H
hjxilinx 已提交
166
void* taosArrayInsert(SArray* pArray, size_t index, void* pData);
H
more  
hzcheng 已提交
167

H
refact  
Hongze Cheng 已提交
168 169 170 171 172 173
/**
 * set data in array
 * @param pArray
 * @param index
 * @param pData
 */
H
Hongze Cheng 已提交
174
void taosArraySet(SArray* pArray, size_t index, void* pData);
H
refact  
Hongze Cheng 已提交
175

L
Liu Jicong 已提交
176 177 178 179 180 181 182
/**
 * remove some data entry from front
 * @param pArray
 * @param cnt
 */
void taosArrayPopFrontBatch(SArray* pArray, size_t cnt);

L
Liu Jicong 已提交
183 184 185 186 187 188 189
/**
 * remove some data entry from front
 * @param pArray
 * @param cnt
 */
void taosArrayPopTailBatch(SArray* pArray, size_t cnt);

H
more  
hzcheng 已提交
190
/**
191 192 193 194 195 196 197 198 199 200 201
 * 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
 */
H
Haojun Liao 已提交
202
SArray* taosArrayFromList(const void* src, size_t size, size_t elemSize);
203

H
hjxilinx 已提交
204 205 206 207
/**
 * clone a new array
 * @param pSrc
 */
H
Haojun Liao 已提交
208
SArray* taosArrayDup(const SArray* pSrc);
H
hjxilinx 已提交
209

weixin_48148422's avatar
weixin_48148422 已提交
210
/**
211 212
 * deep copy a new array
 * @param pSrc
weixin_48148422's avatar
weixin_48148422 已提交
213
 */
214
SArray* taosArrayDeepCopy(const SArray* pSrc, FCopy deepCopy);
weixin_48148422's avatar
weixin_48148422 已提交
215

216
/**
217
 * clear the array (remove all element)
H
more  
hzcheng 已提交
218 219
 * @param pArray
 */
220
void taosArrayClear(SArray* pArray);
H
more  
hzcheng 已提交
221

D
dapan1121 已提交
222 223 224 225 226 227 228
/**
 * clear the array (remove all element)
 * @param pArray
 * @param fp
 */
void taosArrayClearEx(SArray* pArray, void (*fp)(void*));

R
root 已提交
229 230 231 232 233 234 235
/**
 * clear the array (remove all element)
 * @param pArray
 * @param fp
 */
void taosArrayClearP(SArray* pArray, FDelete fp);

236 237 238
void* taosArrayDestroy(SArray* pArray);
void  taosArrayDestroyP(SArray* pArray, FDelete fp);
void  taosArrayDestroyEx(SArray* pArray, FDelete fp);
H
Haojun Liao 已提交
239

weixin_48148422's avatar
weixin_48148422 已提交
240 241 242 243 244
/**
 * sort the array
 * @param pArray
 * @param compar
 */
245
void taosArraySort(SArray* pArray, __compar_fn_t comparFn);
weixin_48148422's avatar
weixin_48148422 已提交
246

weixin_48148422's avatar
weixin_48148422 已提交
247 248 249 250
/**
 * sort string array
 * @param pArray
 */
251
void taosArraySortString(SArray* pArray, __compar_fn_t comparFn);
weixin_48148422's avatar
weixin_48148422 已提交
252

weixin_48148422's avatar
weixin_48148422 已提交
253 254 255 256 257 258
/**
 * search the array
 * @param pArray
 * @param compar
 * @param key
 */
S
tarray  
Shengliang Guan 已提交
259
void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags);
weixin_48148422's avatar
weixin_48148422 已提交
260

L
Liu Jicong 已提交
261 262 263 264 265 266
/**
 * search the array, return index of the element
 * @param pArray
 * @param compar
 * @param key
 */
S
tarray  
Shengliang Guan 已提交
267
int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags);
L
Liu Jicong 已提交
268

weixin_48148422's avatar
weixin_48148422 已提交
269 270 271 272 273
/**
 * search the array
 * @param pArray
 * @param key
 */
S
tarray  
Shengliang Guan 已提交
274
char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t comparFn, int32_t flags);
weixin_48148422's avatar
weixin_48148422 已提交
275

276 277 278
/**
 * sort the pointer data in the array
 * @param pArray
H
Hongze Cheng 已提交
279 280
 * @param compar
 * @param param
281 282 283
 * @return
 */

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

286 287 288
int32_t taosEncodeArray(void** buf, const SArray* pArray, FEncode encode);
void*   taosDecodeArray(const void* buf, SArray** pArray, FDecode decode, int32_t dataSz);

L
Liu Jicong 已提交
289 290
char* taosShowStrArray(const SArray* pArray);

H
more  
hzcheng 已提交
291 292 293 294
#ifdef __cplusplus
}
#endif

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