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 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

221 222 223
void* taosArrayDestroy(SArray* pArray);
void  taosArrayDestroyP(SArray* pArray, FDelete fp);
void  taosArrayDestroyEx(SArray* pArray, FDelete fp);
H
Haojun Liao 已提交
224

weixin_48148422's avatar
weixin_48148422 已提交
225 226 227 228 229
/**
 * sort the array
 * @param pArray
 * @param compar
 */
230
void taosArraySort(SArray* pArray, __compar_fn_t comparFn);
weixin_48148422's avatar
weixin_48148422 已提交
231

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

weixin_48148422's avatar
weixin_48148422 已提交
238 239 240 241 242 243
/**
 * search the array
 * @param pArray
 * @param compar
 * @param key
 */
S
tarray  
Shengliang Guan 已提交
244
void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags);
weixin_48148422's avatar
weixin_48148422 已提交
245

L
Liu Jicong 已提交
246 247 248 249 250 251
/**
 * search the array, return index of the element
 * @param pArray
 * @param compar
 * @param key
 */
S
tarray  
Shengliang Guan 已提交
252
int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags);
L
Liu Jicong 已提交
253

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

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

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

271 272 273
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 已提交
274 275
char* taosShowStrArray(const SArray* pArray);

H
more  
hzcheng 已提交
276 277 278 279
#ifdef __cplusplus
}
#endif

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