tarray.c 11.0 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
tarray  
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
H
more  
hzcheng 已提交
17 18
#include "tarray.h"

19
SArray* taosArrayInit(size_t size, size_t elemSize) {
H
more  
hzcheng 已提交
20 21 22 23 24 25
  assert(elemSize > 0);

  if (size < TARRAY_MIN_SIZE) {
    size = TARRAY_MIN_SIZE;
  }

wafwerar's avatar
wafwerar 已提交
26
  SArray* pArray = taosMemoryMalloc(sizeof(SArray));
H
more  
hzcheng 已提交
27 28 29 30
  if (pArray == NULL) {
    return NULL;
  }

H
Haojun Liao 已提交
31
  pArray->size = 0;
wafwerar's avatar
wafwerar 已提交
32
  pArray->pData = taosMemoryCalloc(size, elemSize);
H
more  
hzcheng 已提交
33
  if (pArray->pData == NULL) {
wafwerar's avatar
wafwerar 已提交
34
    taosMemoryFree(pArray);
H
more  
hzcheng 已提交
35 36 37 38 39 40 41 42
    return NULL;
  }

  pArray->capacity = size;
  pArray->elemSize = elemSize;
  return pArray;
}

H
hjxilinx 已提交
43
static int32_t taosArrayResize(SArray* pArray) {
H
more  
hzcheng 已提交
44 45 46 47 48
  assert(pArray->size >= pArray->capacity);

  size_t size = pArray->capacity;
  size = (size << 1u);

wafwerar's avatar
wafwerar 已提交
49
  void* tmp = taosMemoryRealloc(pArray->pData, size * pArray->elemSize);
H
hjxilinx 已提交
50 51
  if (tmp == NULL) {  // reallocate failed, the original buffer remains
    return -1;
H
more  
hzcheng 已提交
52 53 54 55
  }

  pArray->pData = tmp;
  pArray->capacity = size;
S
tarray  
Shengliang Guan 已提交
56

H
hjxilinx 已提交
57
  return 0;
H
more  
hzcheng 已提交
58 59
}

L
Liu Jicong 已提交
60 61
int32_t taosArrayEnsureCap(SArray* pArray, size_t newCap) {
  if (newCap > pArray->capacity) {
H
Hongze Cheng 已提交
62
    size_t tsize = (pArray->capacity << 1u);
L
Liu Jicong 已提交
63
    while (newCap > tsize) {
H
Hongze Cheng 已提交
64 65 66
      tsize = (tsize << 1u);
    }

wafwerar's avatar
wafwerar 已提交
67
    pArray->pData = taosMemoryRealloc(pArray->pData, tsize * pArray->elemSize);
H
Hongze Cheng 已提交
68
    if (pArray->pData == NULL) {
L
Liu Jicong 已提交
69
      return -1;
H
hjxilinx 已提交
70
    }
H
Hongze Cheng 已提交
71 72

    pArray->capacity = tsize;
H
more  
hzcheng 已提交
73
  }
L
Liu Jicong 已提交
74 75 76
  return 0;
}

S
tarray  
Shengliang Guan 已提交
77
void* taosArrayAddBatch(SArray* pArray, const void* pData, int32_t nEles) {
L
Liu Jicong 已提交
78 79 80 81
  if (pArray == NULL || pData == NULL) {
    return NULL;
  }

S
tarray  
Shengliang Guan 已提交
82
  if (taosArrayEnsureCap(pArray, pArray->size + nEles) != 0) {
L
Liu Jicong 已提交
83 84
    return NULL;
  }
H
more  
hzcheng 已提交
85 86

  void* dst = TARRAY_GET_ELEM(pArray, pArray->size);
H
Hongze Cheng 已提交
87
  memcpy(dst, pData, pArray->elemSize * nEles);
H
more  
hzcheng 已提交
88

H
Hongze Cheng 已提交
89
  pArray->size += nEles;
H
more  
hzcheng 已提交
90 91 92
  return dst;
}

S
tarray  
Shengliang Guan 已提交
93
void taosArrayRemoveBatch(SArray* pArray, const int32_t* pData, int32_t numOfElems) {
94 95 96 97 98 99 100 101 102 103 104 105
  assert(pArray != NULL && pData != NULL);
  if (numOfElems <= 0) {
    return;
  }

  size_t size = taosArrayGetSize(pArray);
  if (numOfElems >= size) {
    taosArrayClear(pArray);
    return;
  }

  int32_t i = pData[0] + 1, j = 0;
S
tarray  
Shengliang Guan 已提交
106
  while (i < size) {
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    if (j == numOfElems - 1) {
      break;
    }

    char* p = TARRAY_GET_ELEM(pArray, i);
    if (i > pData[j] && i < pData[j + 1]) {
      char* dst = TARRAY_GET_ELEM(pArray, i - (j + 1));
      memmove(dst, p, pArray->elemSize);
    } else if (i == pData[j + 1]) {
      j += 1;
    }

    i += 1;
  }

H
Haojun Liao 已提交
122
  assert(i == pData[numOfElems - 1] + 1 && i <= size);
123 124

  int32_t srcIndex = pData[numOfElems - 1] + 1;
H
Haojun Liao 已提交
125 126 127 128 129 130
  int32_t dstIndex = pData[numOfElems - 1] - numOfElems + 1;
  if (pArray->size - srcIndex > 0) {
    char* dst = TARRAY_GET_ELEM(pArray, dstIndex);
    char* src = TARRAY_GET_ELEM(pArray, srcIndex);
    memmove(dst, src, pArray->elemSize * (pArray->size - srcIndex));
  }
131 132 133 134

  pArray->size -= numOfElems;
}

S
tarray  
Shengliang Guan 已提交
135
void taosArrayRemoveDuplicate(SArray* pArray, __compar_fn_t comparFn, void (*fp)(void*)) {
136 137 138 139 140 141 142 143
  assert(pArray);

  size_t size = pArray->size;
  if (size <= 1) {
    return;
  }

  int32_t pos = 0;
S
tarray  
Shengliang Guan 已提交
144
  for (int32_t i = 1; i < size; ++i) {
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    char* p1 = taosArrayGet(pArray, pos);
    char* p2 = taosArrayGet(pArray, i);

    if (comparFn(p1, p2) == 0) {
      // do nothing
    } else {
      if (pos + 1 != i) {
        void* p = taosArrayGet(pArray, pos + 1);
        if (fp != NULL) {
          fp(p);
        }

        taosArraySet(pArray, pos + 1, p2);
        pos += 1;
      } else {
        pos += 1;
      }
    }
  }

  if (fp != NULL) {
S
tarray  
Shengliang Guan 已提交
166
    for (int32_t i = pos + 1; i < pArray->size; ++i) {
167 168 169 170 171 172 173 174
      void* p = taosArrayGet(pArray, i);
      fp(p);
    }
  }

  pArray->size = pos + 1;
}

H
Haojun Liao 已提交
175
void* taosArrayAddAll(SArray* pArray, const SArray* pInput) {
S
tarray  
Shengliang Guan 已提交
176
  return taosArrayAddBatch(pArray, pInput->pData, (int32_t)taosArrayGetSize(pInput));
H
Haojun Liao 已提交
177 178
}

weixin_48148422's avatar
weixin_48148422 已提交
179
void* taosArrayPop(SArray* pArray) {
S
tarray  
Shengliang Guan 已提交
180
  assert(pArray != NULL);
181 182

  if (pArray->size == 0) {
weixin_48148422's avatar
weixin_48148422 已提交
183
    return NULL;
H
more  
hzcheng 已提交
184 185
  }
  pArray->size -= 1;
weixin_48148422's avatar
weixin_48148422 已提交
186
  return TARRAY_GET_ELEM(pArray, pArray->size);
H
more  
hzcheng 已提交
187 188
}

189
void* taosArrayGet(const SArray* pArray, size_t index) {
H
more  
hzcheng 已提交
190 191 192 193
  assert(index < pArray->size);
  return TARRAY_GET_ELEM(pArray, index);
}

194
void* taosArrayGetP(const SArray* pArray, size_t index) {
H
Haojun Liao 已提交
195
  assert(index < pArray->size);
S
tarray  
Shengliang Guan 已提交
196

H
Haojun Liao 已提交
197
  void* d = TARRAY_GET_ELEM(pArray, index);
S
tarray  
Shengliang Guan 已提交
198

H
Haojun Liao 已提交
199
  return *(void**)d;
H
hjxilinx 已提交
200 201
}

S
tarray  
Shengliang Guan 已提交
202
void* taosArrayGetLast(const SArray* pArray) { return TARRAY_GET_ELEM(pArray, pArray->size - 1); }
H
Haojun Liao 已提交
203

S
Shengliang Guan 已提交
204 205 206 207 208 209
size_t taosArrayGetSize(const SArray* pArray) {
  if (pArray == NULL) {
    return 0;
  }
  return pArray->size;
}
H
more  
hzcheng 已提交
210

211 212 213 214 215
void taosArraySetSize(SArray* pArray, size_t size) {
  assert(size <= pArray->capacity);
  pArray->size = size;
}

H
hjxilinx 已提交
216
void* taosArrayInsert(SArray* pArray, size_t index, void* pData) {
H
more  
hzcheng 已提交
217
  if (pArray == NULL || pData == NULL) {
H
hjxilinx 已提交
218
    return NULL;
H
more  
hzcheng 已提交
219 220 221
  }

  if (index >= pArray->size) {
H
hjxilinx 已提交
222
    return taosArrayPush(pArray, pData);
H
more  
hzcheng 已提交
223 224 225
  }

  if (pArray->size >= pArray->capacity) {
H
hjxilinx 已提交
226
    int32_t ret = taosArrayResize(pArray);
S
tarray  
Shengliang Guan 已提交
227

H
hjxilinx 已提交
228 229 230
    if (ret < 0) {
      return NULL;
    }
H
more  
hzcheng 已提交
231 232 233 234
  }

  void* dst = TARRAY_GET_ELEM(pArray, index);

S
Shengliang Guan 已提交
235 236
  int32_t remain = (int32_t)(pArray->size - index);
  memmove((char*)dst + pArray->elemSize, (char*)dst, pArray->elemSize * remain);
H
more  
hzcheng 已提交
237 238 239
  memcpy(dst, pData, pArray->elemSize);

  pArray->size += 1;
S
tarray  
Shengliang Guan 已提交
240

H
hjxilinx 已提交
241
  return dst;
H
more  
hzcheng 已提交
242 243
}

H
Hongze Cheng 已提交
244
void taosArraySet(SArray* pArray, size_t index, void* pData) {
H
refact  
Hongze Cheng 已提交
245 246 247 248
  assert(index < pArray->size);
  memcpy(TARRAY_GET_ELEM(pArray, index), pData, pArray->elemSize);
}

L
Liu Jicong 已提交
249 250 251
void taosArrayPopFrontBatch(SArray* pArray, size_t cnt) {
  assert(cnt <= pArray->size);
  pArray->size = pArray->size - cnt;
S
tarray  
Shengliang Guan 已提交
252
  if (pArray->size == 0) {
L
Liu Jicong 已提交
253 254
    return;
  }
L
Liu Jicong 已提交
255
  memmove(pArray->pData, (char*)pArray->pData + cnt * pArray->elemSize, pArray->size * pArray->elemSize);
L
Liu Jicong 已提交
256 257
}

L
Liu Jicong 已提交
258 259 260 261 262
void taosArrayPopTailBatch(SArray* pArray, size_t cnt) {
  assert(cnt <= pArray->size);
  pArray->size = pArray->size - cnt;
}

263 264
void taosArrayRemove(SArray* pArray, size_t index) {
  assert(index < pArray->size);
S
tarray  
Shengliang Guan 已提交
265

266 267 268 269
  if (index == pArray->size - 1) {
    taosArrayPop(pArray);
    return;
  }
S
tarray  
Shengliang Guan 已提交
270

271
  size_t remain = pArray->size - index - 1;
S
tarray  
Shengliang Guan 已提交
272 273
  memmove((char*)pArray->pData + index * pArray->elemSize, (char*)pArray->pData + (index + 1) * pArray->elemSize,
          remain * pArray->elemSize);
274 275 276
  pArray->size -= 1;
}

H
Haojun Liao 已提交
277 278 279 280 281 282 283 284
SArray* taosArrayFromList(const void* src, size_t size, size_t elemSize) {
  assert(src != NULL && elemSize > 0);
  SArray* pDst = taosArrayInit(size, elemSize);

  memcpy(pDst->pData, src, elemSize * size);
  pDst->size = size;

  return pDst;
285 286
}

H
Haojun Liao 已提交
287
SArray* taosArrayDup(const SArray* pSrc) {
H
hjxilinx 已提交
288
  assert(pSrc != NULL);
S
tarray  
Shengliang Guan 已提交
289 290

  if (pSrc->size == 0) {  // empty array list
H
hjxilinx 已提交
291 292
    return taosArrayInit(8, pSrc->elemSize);
  }
S
tarray  
Shengliang Guan 已提交
293

H
hjxilinx 已提交
294
  SArray* dst = taosArrayInit(pSrc->size, pSrc->elemSize);
S
tarray  
Shengliang Guan 已提交
295

H
hjxilinx 已提交
296 297 298 299 300
  memcpy(dst->pData, pSrc->pData, pSrc->elemSize * pSrc->size);
  dst->size = pSrc->size;
  return dst;
}

weixin_48148422's avatar
weixin_48148422 已提交
301
void taosArrayClear(SArray* pArray) {
S
Shengliang Guan 已提交
302
  if (pArray == NULL) return;
weixin_48148422's avatar
weixin_48148422 已提交
303 304 305
  pArray->size = 0;
}

D
dapan1121 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
void taosArrayClearEx(SArray* pArray, void (*fp)(void*)) {
  if (pArray == NULL) return;
  if (fp == NULL) {
    pArray->size = 0;
    return;
  }

  for (int32_t i = 0; i < pArray->size; ++i) {
    fp(TARRAY_GET_ELEM(pArray, i));
  }

  pArray->size = 0;
}


H
Hongze Cheng 已提交
321 322
void* taosArrayDestroy(SArray* pArray) {
  if (pArray) {
wafwerar's avatar
wafwerar 已提交
323 324
    taosMemoryFree(pArray->pData);
    taosMemoryFree(pArray);
H
more  
hzcheng 已提交
325 326
  }

H
Hongze Cheng 已提交
327
  return NULL;
H
more  
hzcheng 已提交
328
}
weixin_48148422's avatar
weixin_48148422 已提交
329

H
Haojun Liao 已提交
330
void taosArrayDestroyEx(SArray* pArray, void (*fp)(void*)) {
H
Haojun Liao 已提交
331 332 333 334
  if (pArray == NULL) {
    return;
  }

H
Haojun Liao 已提交
335
  if (fp == NULL) {
H
Haojun Liao 已提交
336 337
    taosArrayDestroy(pArray);
    return;
H
Haojun Liao 已提交
338 339
  }

S
tarray  
Shengliang Guan 已提交
340
  for (int32_t i = 0; i < pArray->size; ++i) {
H
Haojun Liao 已提交
341 342 343 344 345 346
    fp(TARRAY_GET_ELEM(pArray, i));
  }

  taosArrayDestroy(pArray);
}

347
void taosArraySort(SArray* pArray, __compar_fn_t compar) {
weixin_48148422's avatar
weixin_48148422 已提交
348 349 350 351 352 353
  assert(pArray != NULL);
  assert(compar != NULL);

  qsort(pArray->pData, pArray->size, pArray->elemSize, compar);
}

S
tarray  
Shengliang Guan 已提交
354
void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags) {
355
  assert(pArray != NULL && comparFn != NULL);
weixin_48148422's avatar
weixin_48148422 已提交
356 357
  assert(key != NULL);

H
refact  
Hongze Cheng 已提交
358
  return taosbsearch(key, pArray->pData, pArray->size, pArray->elemSize, comparFn, flags);
weixin_48148422's avatar
weixin_48148422 已提交
359 360
}

S
tarray  
Shengliang Guan 已提交
361
int32_t taosArraySearchIdx(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags) {
L
Liu Jicong 已提交
362
  void* item = taosArraySearch(pArray, key, comparFn, flags);
L
Liu Jicong 已提交
363
  return item == NULL ? -1 : (int32_t)((char*)item - (char*)pArray->pData) / pArray->elemSize;
L
Liu Jicong 已提交
364 365
}

366
void taosArraySortString(SArray* pArray, __compar_fn_t comparFn) {
weixin_48148422's avatar
weixin_48148422 已提交
367
  assert(pArray != NULL);
368
  qsort(pArray->pData, pArray->size, pArray->elemSize, comparFn);
weixin_48148422's avatar
weixin_48148422 已提交
369 370
}

S
tarray  
Shengliang Guan 已提交
371
char* taosArraySearchString(const SArray* pArray, const char* key, __compar_fn_t comparFn, int32_t flags) {
weixin_48148422's avatar
weixin_48148422 已提交
372 373 374
  assert(pArray != NULL);
  assert(key != NULL);

H
refact  
Hongze Cheng 已提交
375
  void* p = taosbsearch(&key, pArray->pData, pArray->size, pArray->elemSize, comparFn, flags);
weixin_48148422's avatar
weixin_48148422 已提交
376 377 378 379
  if (p == NULL) {
    return NULL;
  }
  return *(char**)p;
380 381
}

S
tarray  
Shengliang Guan 已提交
382 383
static int32_t taosArrayPartition(SArray* pArray, int32_t i, int32_t j, __ext_compar_fn_t fn, const void* userData) {
  void* key = taosArrayGetP(pArray, i);
384
  while (i < j) {
S
tarray  
Shengliang Guan 已提交
385 386 387 388 389
    while (i < j && fn(taosArrayGetP(pArray, j), key, userData) >= 0) {
      j--;
    }
    if (i < j) {
      void* a = taosArrayGetP(pArray, j);
390 391
      taosArraySet(pArray, i, &a);
    }
S
tarray  
Shengliang Guan 已提交
392 393 394
    while (i < j && fn(taosArrayGetP(pArray, i), key, userData) <= 0) {
      i++;
    }
395
    if (i < j) {
S
tarray  
Shengliang Guan 已提交
396
      void* a = taosArrayGetP(pArray, i);
397 398 399
      taosArraySet(pArray, j, &a);
    }
  }
S
tarray  
Shengliang Guan 已提交
400
  taosArraySet(pArray, i, &key);
401 402 403
  return i;
}

S
tarray  
Shengliang Guan 已提交
404 405
static void taosArrayQuicksortHelper(SArray* pArray, int32_t low, int32_t high, __ext_compar_fn_t fn,
                                     const void* param) {
406
  if (low < high) {
S
tarray  
Shengliang Guan 已提交
407
    int32_t idx = taosArrayPartition(pArray, low, high, fn, param);
408 409
    taosArrayQuicksortHelper(pArray, low, idx - 1, fn, param);
    taosArrayQuicksortHelper(pArray, idx + 1, high, fn, param);
S
tarray  
Shengliang Guan 已提交
410
  }
411 412
}

S
tarray  
Shengliang Guan 已提交
413
static void taosArrayQuickSort(SArray* pArray, __ext_compar_fn_t fn, const void* param) {
414
  if (pArray->size <= 1) {
415
    return;
S
tarray  
Shengliang Guan 已提交
416 417
  }
  taosArrayQuicksortHelper(pArray, 0, (int32_t)(taosArrayGetSize(pArray) - 1), fn, param);
418
}
S
tarray  
Shengliang Guan 已提交
419 420

static void taosArrayInsertSort(SArray* pArray, __ext_compar_fn_t fn, const void* param) {
421
  if (pArray->size <= 1) {
422
    return;
S
tarray  
Shengliang Guan 已提交
423 424 425
  }
  for (int32_t i = 1; i <= pArray->size - 1; ++i) {
    for (int32_t j = i; j > 0; --j) {
426
      if (fn(taosArrayGetP(pArray, j), taosArrayGetP(pArray, j - 1), param) == -1) {
S
tarray  
Shengliang Guan 已提交
427 428
        void* a = taosArrayGetP(pArray, j);
        void* b = taosArrayGetP(pArray, j - 1);
429 430 431 432 433 434 435
        taosArraySet(pArray, j - 1, &a);
        taosArraySet(pArray, j, &b);
      } else {
        break;
      }
    }
  }
436
  return;
S
tarray  
Shengliang Guan 已提交
437 438 439 440 441 442 443
}

// order array<type *>
void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* param) {
  taosArrayGetSize(pArray) > 8 ? taosArrayQuickSort(pArray, fn, param) : taosArrayInsertSort(pArray, fn, param);
}
// TODO(yihaoDeng) add order array<type>