tarray.h 5.9 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
#define TARRAY_ELEM_IDX(array, ele)   (POINTER_DISTANCE(ele, (array)->pData) / (array)->elemSize)
H
more  
hzcheng 已提交
41 42

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

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

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

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

73 74 75 76 77 78
/**
 *
 * @param pArray
 * @param comparFn
 * @param fp
 */
H
Hongze Cheng 已提交
79
void taosArrayRemoveDuplicate(SArray* pArray, __compar_fn_t comparFn, void (*fp)(void*));
80

81 82 83 84 85 86 87 88
/**
 *
 * @param pArray
 * @param comparFn
 * @param fp
 */
void taosArrayRemoveDuplicateP(SArray* pArray, __compar_fn_t comparFn, void (*fp)(void*));

H
Haojun Liao 已提交
89 90 91 92 93 94 95
/**
 *  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 已提交
96 97 98 99 100 101 102 103

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

H
Hongze Cheng 已提交
107 108 109 110 111 112 113 114 115
/**
 * @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 已提交
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, __array_item_dup_fn_t fn);
weixin_48148422's avatar
weixin_48148422 已提交
209

210
/**
211
 * clear the array (remove all element)
H
more  
hzcheng 已提交
212 213
 * @param pArray
 */
214
void taosArrayClear(SArray* pArray);
H
more  
hzcheng 已提交
215

D
dapan1121 已提交
216 217 218 219 220 221 222
/**
 * clear the array (remove all element)
 * @param pArray
 * @param fp
 */
void taosArrayClearEx(SArray* pArray, void (*fp)(void*));

R
root 已提交
223 224 225 226 227 228 229
/**
 * clear the array (remove all element)
 * @param pArray
 * @param fp
 */
void taosArrayClearP(SArray* pArray, FDelete fp);

230 231 232
void* taosArrayDestroy(SArray* pArray);
void  taosArrayDestroyP(SArray* pArray, FDelete fp);
void  taosArrayDestroyEx(SArray* pArray, FDelete fp);
H
Haojun Liao 已提交
233

weixin_48148422's avatar
weixin_48148422 已提交
234 235 236 237 238
/**
 * sort the array
 * @param pArray
 * @param compar
 */
239
void taosArraySort(SArray* pArray, __compar_fn_t comparFn);
weixin_48148422's avatar
weixin_48148422 已提交
240

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

weixin_48148422's avatar
weixin_48148422 已提交
247 248 249 250 251 252
/**
 * search the array
 * @param pArray
 * @param compar
 * @param key
 */
S
tarray  
Shengliang Guan 已提交
253
void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags);
weixin_48148422's avatar
weixin_48148422 已提交
254

L
Liu Jicong 已提交
255 256 257 258 259 260
/**
 * search the array, return index of the element
 * @param pArray
 * @param compar
 * @param key
 */
S
tarray  
Shengliang Guan 已提交
261
int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags);
L
Liu Jicong 已提交
262

263 264 265
/**
 * sort the pointer data in the array
 * @param pArray
H
Hongze Cheng 已提交
266 267
 * @param compar
 * @param param
268 269 270
 * @return
 */

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

273 274 275
int32_t taosEncodeArray(void** buf, const SArray* pArray, FEncode encode);
void*   taosDecodeArray(const void* buf, SArray** pArray, FDecode decode, int32_t dataSz);

dengyihao's avatar
dengyihao 已提交
276 277 278 279 280 281 282
/**
 * swap array
 * @param a
 * @param b
 * @return
 */
void taosArraySwap(SArray* a, SArray* b);
283

H
more  
hzcheng 已提交
284 285 286 287
#ifdef __cplusplus
}
#endif

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