tarray.h 5.8 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 107 108 109 110

/**
 *
 * @param pArray
 */
weixin_48148422's avatar
weixin_48148422 已提交
111
void* taosArrayPop(SArray* pArray);
H
more  
hzcheng 已提交
112 113

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

/**
H
hjxilinx 已提交
122 123 124 125 126
 * get the pointer data from the array
 * @param pArray
 * @param index
 * @return
 */
127
void* taosArrayGetP(const SArray* pArray, size_t index);
H
hjxilinx 已提交
128

H
Haojun Liao 已提交
129 130 131 132 133 134 135
/**
 * get the last element in the array list
 * @param pArray
 * @return
 */
void* taosArrayGetLast(const SArray* pArray);

H
hjxilinx 已提交
136 137
/**
 * return the size of array
H
more  
hzcheng 已提交
138 139 140
 * @param pArray
 * @return
 */
L
Liu Jicong 已提交
141
size_t taosArrayGetSize(const SArray* pArray);
H
more  
hzcheng 已提交
142

143 144 145 146 147 148 149 150
/**
 * set the size of array
 * @param pArray
 * @param size size of the array
 * @return
 */
void taosArraySetSize(SArray* pArray, size_t size);

H
more  
hzcheng 已提交
151
/**
H
hjxilinx 已提交
152
 * insert data into array
H
more  
hzcheng 已提交
153 154 155 156
 * @param pArray
 * @param index
 * @param pData
 */
H
hjxilinx 已提交
157
void* taosArrayInsert(SArray* pArray, size_t index, void* pData);
H
more  
hzcheng 已提交
158

H
refact  
Hongze Cheng 已提交
159 160 161 162 163 164
/**
 * set data in array
 * @param pArray
 * @param index
 * @param pData
 */
H
Hongze Cheng 已提交
165
void taosArraySet(SArray* pArray, size_t index, void* pData);
H
refact  
Hongze Cheng 已提交
166

L
Liu Jicong 已提交
167 168 169 170 171 172 173
/**
 * remove some data entry from front
 * @param pArray
 * @param cnt
 */
void taosArrayPopFrontBatch(SArray* pArray, size_t cnt);

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

H
more  
hzcheng 已提交
181
/**
182 183 184 185 186 187 188 189 190 191 192
 * 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 已提交
193
SArray* taosArrayFromList(const void* src, size_t size, size_t elemSize);
194

H
hjxilinx 已提交
195 196 197 198
/**
 * clone a new array
 * @param pSrc
 */
H
Haojun Liao 已提交
199
SArray* taosArrayDup(const SArray* pSrc);
H
hjxilinx 已提交
200

weixin_48148422's avatar
weixin_48148422 已提交
201
/**
202 203
 * deep copy a new array
 * @param pSrc
weixin_48148422's avatar
weixin_48148422 已提交
204
 */
205
SArray* taosArrayDeepCopy(const SArray* pSrc, FCopy deepCopy);
weixin_48148422's avatar
weixin_48148422 已提交
206

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

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

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

227 228 229
void* taosArrayDestroy(SArray* pArray);
void  taosArrayDestroyP(SArray* pArray, FDelete fp);
void  taosArrayDestroyEx(SArray* pArray, FDelete fp);
H
Haojun Liao 已提交
230

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

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

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

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

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

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

270 271 272
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 已提交
273 274 275 276 277 278 279
/**
 * swap array
 * @param a
 * @param b
 * @return
 */
void taosArraySwap(SArray* a, SArray* b);
280

H
more  
hzcheng 已提交
281 282 283 284
#ifdef __cplusplus
}
#endif

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