tarray.h 2.9 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 24 25
/*
 * 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"

#define TARRAY_MIN_SIZE 8
H
Haojun Liao 已提交
26
#define TARRAY_GET_ELEM(array, index) ((void*)((char*)((array)->pData) + (index) * (array)->elemSize))
H
more  
hzcheng 已提交
27 28 29 30 31

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

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

/**
 *
 * @param pArray
 * @param pData
 * @return
 */
void* taosArrayPush(SArray* pArray, void* pData);

/**
 *
 * @param pArray
 */
weixin_48148422's avatar
weixin_48148422 已提交
55
void* taosArrayPop(SArray* pArray);
H
more  
hzcheng 已提交
56 57

/**
H
hjxilinx 已提交
58
 * get the data from array
H
more  
hzcheng 已提交
59 60 61 62
 * @param pArray
 * @param index
 * @return
 */
63
void* taosArrayGet(const SArray* pArray, size_t index);
H
more  
hzcheng 已提交
64 65

/**
H
hjxilinx 已提交
66 67 68 69 70
 * get the pointer data from the array
 * @param pArray
 * @param index
 * @return
 */
71
void* taosArrayGetP(const SArray* pArray, size_t index);
H
hjxilinx 已提交
72 73 74

/**
 * return the size of array
H
more  
hzcheng 已提交
75 76 77
 * @param pArray
 * @return
 */
H
hjxilinx 已提交
78
size_t taosArrayGetSize(const SArray* pArray);
H
more  
hzcheng 已提交
79 80

/**
H
hjxilinx 已提交
81
 * insert data into array
H
more  
hzcheng 已提交
82 83 84 85
 * @param pArray
 * @param index
 * @param pData
 */
H
hjxilinx 已提交
86
void* taosArrayInsert(SArray* pArray, size_t index, void* pData);
H
more  
hzcheng 已提交
87 88

/**
89 90 91 92 93 94 95 96 97 98 99
 * 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
hjxilinx 已提交
100
void taosArrayCopy(SArray* pDst, const SArray* pSrc);
101

H
hjxilinx 已提交
102 103 104 105
/**
 * clone a new array
 * @param pSrc
 */
H
hjxilinx 已提交
106
SArray* taosArrayClone(const SArray* pSrc);
H
hjxilinx 已提交
107

weixin_48148422's avatar
weixin_48148422 已提交
108 109 110 111 112 113
/**
 * clear the array (remove all element)
 * @param pArray
 */
void taosArrayClear(SArray* pArray);

114 115
/**
 * destroy array list
H
more  
hzcheng 已提交
116 117
 * @param pArray
 */
H
hjxilinx 已提交
118
void taosArrayDestroy(SArray* pArray);
H
more  
hzcheng 已提交
119

weixin_48148422's avatar
weixin_48148422 已提交
120 121 122 123 124 125 126
/**
 * sort the array
 * @param pArray
 * @param compar
 */
void taosArraySort(SArray* pArray, int (*compar)(const void*, const void*));

weixin_48148422's avatar
weixin_48148422 已提交
127 128 129 130
/**
 * sort string array
 * @param pArray
 */
131
void taosArraySortString(SArray* pArray, __compar_fn_t comparFn);
weixin_48148422's avatar
weixin_48148422 已提交
132

weixin_48148422's avatar
weixin_48148422 已提交
133 134 135 136 137 138
/**
 * search the array
 * @param pArray
 * @param compar
 * @param key
 */
139
void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn);
weixin_48148422's avatar
weixin_48148422 已提交
140 141 142 143 144 145

/**
 * search the array
 * @param pArray
 * @param key
 */
146
char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t comparFn);
weixin_48148422's avatar
weixin_48148422 已提交
147

H
more  
hzcheng 已提交
148 149 150 151 152
#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_TAOSARRAY_H