tarray.h 4.6 KB
Newer Older
H
more  
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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/>.
 */

#ifndef TDENGINE_TAOSARRAY_H
#define TDENGINE_TAOSARRAY_H

#ifdef __cplusplus
extern "C" {
#endif

#include "os.h"
H
refact  
Hongze Cheng 已提交
24
#include "talgo.h"
H
more  
hzcheng 已提交
25 26

#define TARRAY_MIN_SIZE 8
H
Haojun Liao 已提交
27
#define TARRAY_GET_ELEM(array, index) ((void*)((char*)((array)->pData) + (index) * (array)->elemSize))
H
Haojun Liao 已提交
28 29
#define TARRAY_ELEM_IDX(array, ele)   (POINTER_DISTANCE(ele, (array)->pData) / (array)->elemSize)
#define TARRAY_GET_START(array)       ((array)->pData)
H
more  
hzcheng 已提交
30 31 32 33 34

typedef struct SArray {
  size_t size;
  size_t capacity;
  size_t elemSize;
H
Haojun Liao 已提交
35
  void*  pData;
H
more  
hzcheng 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49
} SArray;

/**
 *
 * @param size
 * @param elemSize
 * @return
 */
void* taosArrayInit(size_t size, size_t elemSize);

/**
 *
 * @param pArray
 * @param pData
H
Hongze Cheng 已提交
50
 * @param nEles
H
more  
hzcheng 已提交
51 52
 * @return
 */
H
Haojun Liao 已提交
53 54
void *taosArrayAddBatch(SArray *pArray, const void *pData, int nEles);

55 56 57 58 59 60 61 62
/**
 *
 * @param pArray
 * @param pData           position array list
 * @param numOfElems      the number of removed position
 */
void taosArrayRemoveBatch(SArray *pArray, const int32_t* pData, int32_t numOfElems);

63 64 65 66 67 68 69
/**
 *
 * @param pArray
 * @param comparFn
 * @param fp
 */
void taosArrayRemoveDuplicate(SArray *pArray, __compar_fn_t comparFn, void (*fp)(void*));
70

H
Haojun Liao 已提交
71 72 73 74 75 76 77
/**
 *  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 已提交
78 79 80 81 82 83 84 85

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

/**
 *
 * @param pArray
 */
weixin_48148422's avatar
weixin_48148422 已提交
93
void* taosArrayPop(SArray* pArray);
H
more  
hzcheng 已提交
94 95

/**
H
hjxilinx 已提交
96
 * get the data from array
H
more  
hzcheng 已提交
97 98 99 100
 * @param pArray
 * @param index
 * @return
 */
101
void* taosArrayGet(const SArray* pArray, size_t index);
H
more  
hzcheng 已提交
102 103

/**
H
hjxilinx 已提交
104 105 106 107 108
 * get the pointer data from the array
 * @param pArray
 * @param index
 * @return
 */
109
void* taosArrayGetP(const SArray* pArray, size_t index);
H
hjxilinx 已提交
110

H
Haojun Liao 已提交
111 112 113 114 115 116 117
/**
 * get the last element in the array list
 * @param pArray
 * @return
 */
void* taosArrayGetLast(const SArray* pArray);

H
hjxilinx 已提交
118 119
/**
 * return the size of array
H
more  
hzcheng 已提交
120 121 122
 * @param pArray
 * @return
 */
H
hjxilinx 已提交
123
size_t taosArrayGetSize(const SArray* pArray);
H
more  
hzcheng 已提交
124

125 126 127 128 129 130 131 132
/**
 * set the size of array
 * @param pArray
 * @param size size of the array
 * @return
 */
void taosArraySetSize(SArray* pArray, size_t size);

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

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

H
more  
hzcheng 已提交
149
/**
150 151 152 153 154 155 156 157 158 159 160
 * 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 已提交
161
SArray* taosArrayFromList(const void* src, size_t size, size_t elemSize);
162

H
hjxilinx 已提交
163 164 165 166
/**
 * clone a new array
 * @param pSrc
 */
H
Haojun Liao 已提交
167
SArray* taosArrayDup(const SArray* pSrc);
H
hjxilinx 已提交
168

weixin_48148422's avatar
weixin_48148422 已提交
169 170 171 172 173 174
/**
 * clear the array (remove all element)
 * @param pArray
 */
void taosArrayClear(SArray* pArray);

175 176
/**
 * destroy array list
H
more  
hzcheng 已提交
177 178
 * @param pArray
 */
H
Hongze Cheng 已提交
179
void* taosArrayDestroy(SArray* pArray);
H
more  
hzcheng 已提交
180

H
Haojun Liao 已提交
181 182 183 184 185 186 187
/**
 *
 * @param pArray
 * @param fp
 */
void taosArrayDestroyEx(SArray* pArray, void (*fp)(void*));

weixin_48148422's avatar
weixin_48148422 已提交
188 189 190 191 192
/**
 * sort the array
 * @param pArray
 * @param compar
 */
193
void taosArraySort(SArray* pArray, __compar_fn_t comparFn);
weixin_48148422's avatar
weixin_48148422 已提交
194

weixin_48148422's avatar
weixin_48148422 已提交
195 196 197 198
/**
 * sort string array
 * @param pArray
 */
199
void taosArraySortString(SArray* pArray, __compar_fn_t comparFn);
weixin_48148422's avatar
weixin_48148422 已提交
200

weixin_48148422's avatar
weixin_48148422 已提交
201 202 203 204 205 206
/**
 * search the array
 * @param pArray
 * @param compar
 * @param key
 */
H
refact  
Hongze Cheng 已提交
207
void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int flags);
weixin_48148422's avatar
weixin_48148422 已提交
208 209 210 211 212 213

/**
 * search the array
 * @param pArray
 * @param key
 */
H
refact  
Hongze Cheng 已提交
214
char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t comparFn, int flags);
weixin_48148422's avatar
weixin_48148422 已提交
215

216 217 218 219 220 221 222 223 224 225 226

/**
 * sort the pointer data in the array
 * @param pArray
 * @param compar 
 * @param param  
 * @return
 */

void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void *param);

H
more  
hzcheng 已提交
227 228 229 230
#ifdef __cplusplus
}
#endif

231 232


H
more  
hzcheng 已提交
233
#endif  // TDENGINE_TAOSARRAY_H