tarray.c 13.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
#include "tarray.h"
18
#include "tcoding.h"
H
more  
hzcheng 已提交
19

20 21
// todo refactor API

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

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

wafwerar's avatar
wafwerar 已提交
29
  SArray* pArray = taosMemoryMalloc(sizeof(SArray));
H
more  
hzcheng 已提交
30
  if (pArray == NULL) {
31
    terrno = TSDB_CODE_OUT_OF_MEMORY;
H
more  
hzcheng 已提交
32 33 34
    return NULL;
  }

H
Haojun Liao 已提交
35
  pArray->size = 0;
wafwerar's avatar
wafwerar 已提交
36
  pArray->pData = taosMemoryCalloc(size, elemSize);
H
more  
hzcheng 已提交
37
  if (pArray->pData == NULL) {
38
    terrno = TSDB_CODE_OUT_OF_MEMORY;
wafwerar's avatar
wafwerar 已提交
39
    taosMemoryFree(pArray);
H
more  
hzcheng 已提交
40 41 42 43 44 45 46 47
    return NULL;
  }

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

H
hjxilinx 已提交
48
static int32_t taosArrayResize(SArray* pArray) {
H
more  
hzcheng 已提交
49 50 51 52 53
  assert(pArray->size >= pArray->capacity);

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

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

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

H
hjxilinx 已提交
62
  return 0;
H
more  
hzcheng 已提交
63 64
}

L
Liu Jicong 已提交
65 66
int32_t taosArrayEnsureCap(SArray* pArray, size_t newCap) {
  if (newCap > pArray->capacity) {
H
Hongze Cheng 已提交
67
    size_t tsize = (pArray->capacity << 1u);
L
Liu Jicong 已提交
68
    while (newCap > tsize) {
H
Hongze Cheng 已提交
69 70 71
      tsize = (tsize << 1u);
    }

wafwerar's avatar
wafwerar 已提交
72
    pArray->pData = taosMemoryRealloc(pArray->pData, tsize * pArray->elemSize);
H
Hongze Cheng 已提交
73
    if (pArray->pData == NULL) {
L
Liu Jicong 已提交
74
      return -1;
H
hjxilinx 已提交
75
    }
H
Hongze Cheng 已提交
76 77

    pArray->capacity = tsize;
H
more  
hzcheng 已提交
78
  }
L
Liu Jicong 已提交
79 80 81
  return 0;
}

S
tarray  
Shengliang Guan 已提交
82
void* taosArrayAddBatch(SArray* pArray, const void* pData, int32_t nEles) {
L
Liu Jicong 已提交
83
  if (pData == NULL) {
L
Liu Jicong 已提交
84 85 86
    return NULL;
  }

S
tarray  
Shengliang Guan 已提交
87
  if (taosArrayEnsureCap(pArray, pArray->size + nEles) != 0) {
L
Liu Jicong 已提交
88 89
    return NULL;
  }
H
more  
hzcheng 已提交
90 91

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

H
Hongze Cheng 已提交
94
  pArray->size += nEles;
H
more  
hzcheng 已提交
95 96 97
  return dst;
}

S
tarray  
Shengliang Guan 已提交
98
void taosArrayRemoveDuplicate(SArray* pArray, __compar_fn_t comparFn, void (*fp)(void*)) {
99 100 101 102 103 104 105 106
  assert(pArray);

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

  int32_t pos = 0;
S
tarray  
Shengliang Guan 已提交
107
  for (int32_t i = 1; i < size; ++i) {
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    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 已提交
129
    for (int32_t i = pos + 1; i < pArray->size; ++i) {
130 131 132 133 134 135 136 137
      void* p = taosArrayGet(pArray, i);
      fp(p);
    }
  }

  pArray->size = pos + 1;
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
void taosArrayRemoveDuplicateP(SArray* pArray, __compar_fn_t comparFn, void (*fp)(void*)) {
  assert(pArray);

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

  int32_t pos = 0;
  for (int32_t i = 1; i < size; ++i) {
    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) {
    for (int32_t i = pos + 1; i < pArray->size; ++i) {
      void* p = taosArrayGetP(pArray, i);
      fp(p);
    }
  }

  pArray->size = pos + 1;
}

H
Haojun Liao 已提交
178
void* taosArrayAddAll(SArray* pArray, const SArray* pInput) {
179 180 181 182 183
  if (pInput) {
    return taosArrayAddBatch(pArray, pInput->pData, (int32_t)taosArrayGetSize(pInput));
  } else {
    return NULL;
  }
H
Haojun Liao 已提交
184 185
}

H
Hongze Cheng 已提交
186 187 188 189 190 191 192 193
void* taosArrayReserve(SArray* pArray, int32_t num) {
  if (taosArrayEnsureCap(pArray, pArray->size + num) != 0) {
    return NULL;
  }

  void* dst = TARRAY_GET_ELEM(pArray, pArray->size);
  pArray->size += num;

H
Hongze Cheng 已提交
194 195
  memset(dst, 0, num * pArray->elemSize);

H
Hongze Cheng 已提交
196 197 198
  return dst;
}

weixin_48148422's avatar
weixin_48148422 已提交
199
void* taosArrayPop(SArray* pArray) {
S
tarray  
Shengliang Guan 已提交
200
  assert(pArray != NULL);
201 202

  if (pArray->size == 0) {
weixin_48148422's avatar
weixin_48148422 已提交
203
    return NULL;
H
more  
hzcheng 已提交
204 205
  }
  pArray->size -= 1;
weixin_48148422's avatar
weixin_48148422 已提交
206
  return TARRAY_GET_ELEM(pArray, pArray->size);
H
more  
hzcheng 已提交
207 208
}

209
void* taosArrayGet(const SArray* pArray, size_t index) {
210 211 212
  if (NULL == pArray) {
    return NULL;
  }
H
more  
hzcheng 已提交
213 214 215 216
  assert(index < pArray->size);
  return TARRAY_GET_ELEM(pArray, index);
}

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

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

H
Haojun Liao 已提交
222
  return *(void**)d;
H
hjxilinx 已提交
223 224
}

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

L
Liu Jicong 已提交
227
size_t taosArrayGetSize(const SArray* pArray) {
S
Shengliang Guan 已提交
228 229 230
  if (pArray == NULL) {
    return 0;
  }
L
Liu Jicong 已提交
231
  return pArray->size;
S
Shengliang Guan 已提交
232
}
H
more  
hzcheng 已提交
233

234 235 236 237 238
void taosArraySetSize(SArray* pArray, size_t size) {
  assert(size <= pArray->capacity);
  pArray->size = size;
}

H
hjxilinx 已提交
239
void* taosArrayInsert(SArray* pArray, size_t index, void* pData) {
H
more  
hzcheng 已提交
240
  if (pArray == NULL || pData == NULL) {
H
hjxilinx 已提交
241
    return NULL;
H
more  
hzcheng 已提交
242 243 244
  }

  if (index >= pArray->size) {
H
hjxilinx 已提交
245
    return taosArrayPush(pArray, pData);
H
more  
hzcheng 已提交
246 247 248
  }

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

H
hjxilinx 已提交
251 252 253
    if (ret < 0) {
      return NULL;
    }
H
more  
hzcheng 已提交
254 255 256 257
  }

  void* dst = TARRAY_GET_ELEM(pArray, index);

S
Shengliang Guan 已提交
258 259
  int32_t remain = (int32_t)(pArray->size - index);
  memmove((char*)dst + pArray->elemSize, (char*)dst, pArray->elemSize * remain);
H
more  
hzcheng 已提交
260 261 262
  memcpy(dst, pData, pArray->elemSize);

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

H
hjxilinx 已提交
264
  return dst;
H
more  
hzcheng 已提交
265 266
}

H
Hongze Cheng 已提交
267
void taosArraySet(SArray* pArray, size_t index, void* pData) {
H
refact  
Hongze Cheng 已提交
268 269 270 271
  assert(index < pArray->size);
  memcpy(TARRAY_GET_ELEM(pArray, index), pData, pArray->elemSize);
}

L
Liu Jicong 已提交
272 273 274
void taosArrayPopFrontBatch(SArray* pArray, size_t cnt) {
  assert(cnt <= pArray->size);
  pArray->size = pArray->size - cnt;
L
Liu Jicong 已提交
275
  if (pArray->size == 0 || cnt == 0) {
L
Liu Jicong 已提交
276 277
    return;
  }
L
Liu Jicong 已提交
278
  memmove(pArray->pData, (char*)pArray->pData + cnt * pArray->elemSize, pArray->size * pArray->elemSize);
L
Liu Jicong 已提交
279 280
}

L
Liu Jicong 已提交
281 282 283 284 285
void taosArrayPopTailBatch(SArray* pArray, size_t cnt) {
  assert(cnt <= pArray->size);
  pArray->size = pArray->size - cnt;
}

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

289 290 291 292
  if (index == pArray->size - 1) {
    taosArrayPop(pArray);
    return;
  }
S
tarray  
Shengliang Guan 已提交
293

294
  size_t remain = pArray->size - index - 1;
S
tarray  
Shengliang Guan 已提交
295 296
  memmove((char*)pArray->pData + index * pArray->elemSize, (char*)pArray->pData + (index + 1) * pArray->elemSize,
          remain * pArray->elemSize);
297 298 299
  pArray->size -= 1;
}

H
Hongze Cheng 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313
void taosArrayRemoveBatch(SArray* pArray, size_t index, size_t num, FDelete fp) {
  ASSERT(index + num <= pArray->size);

  if (fp) {
    for (int32_t i = 0; i < num; i++) {
      fp(taosArrayGet(pArray, index + i));
    }
  }

  memmove((char*)pArray->pData + index * pArray->elemSize, (char*)pArray->pData + (index + num) * pArray->elemSize,
          (pArray->size - index - num) * pArray->elemSize);
  pArray->size -= num;
}

H
Haojun Liao 已提交
314 315 316 317 318 319 320 321
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;
322 323
}

H
Haojun Liao 已提交
324
SArray* taosArrayDup(const SArray* pSrc, __array_item_dup_fn_t fn) {
H
hjxilinx 已提交
325
  assert(pSrc != NULL);
S
tarray  
Shengliang Guan 已提交
326 327

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

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

H
Haojun Liao 已提交
333 334 335 336 337
  if (fn == NULL) {
    memcpy(dst->pData, pSrc->pData, pSrc->elemSize * pSrc->size);
  } else {
    ASSERT(pSrc->elemSize == sizeof(void*));

H
Hongze Cheng 已提交
338
    for (int32_t i = 0; i < pSrc->size; ++i) {
H
Haojun Liao 已提交
339
      void* p = fn(taosArrayGetP(pSrc, i));
H
Hongze Cheng 已提交
340
      memcpy(((char*)dst->pData) + i * dst->elemSize, &p, dst->elemSize);
H
Haojun Liao 已提交
341 342 343
    }
  }

H
hjxilinx 已提交
344
  dst->size = pSrc->size;
H
Haojun Liao 已提交
345

H
hjxilinx 已提交
346 347 348
  return dst;
}

weixin_48148422's avatar
weixin_48148422 已提交
349
void taosArrayClear(SArray* pArray) {
S
Shengliang Guan 已提交
350
  if (pArray == NULL) return;
weixin_48148422's avatar
weixin_48148422 已提交
351 352 353
  pArray->size = 0;
}

D
dapan1121 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367
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;
}

R
root 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381
void taosArrayClearP(SArray* pArray, FDelete fp) {
  if (pArray == NULL) return;
  if (fp == NULL) {
    pArray->size = 0;
    return;
  }

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

  pArray->size = 0;
}

H
Hongze Cheng 已提交
382 383
void* taosArrayDestroy(SArray* pArray) {
  if (pArray) {
wafwerar's avatar
wafwerar 已提交
384 385
    taosMemoryFree(pArray->pData);
    taosMemoryFree(pArray);
H
more  
hzcheng 已提交
386 387
  }

H
Hongze Cheng 已提交
388
  return NULL;
H
more  
hzcheng 已提交
389
}
weixin_48148422's avatar
weixin_48148422 已提交
390

391
void taosArrayDestroyP(SArray* pArray, FDelete fp) {
L
Liu Jicong 已提交
392 393 394 395 396
  if (pArray) {
    for (int32_t i = 0; i < pArray->size; i++) {
      fp(*(void**)TARRAY_GET_ELEM(pArray, i));
    }
    taosArrayDestroy(pArray);
397 398 399 400
  }
}

void taosArrayDestroyEx(SArray* pArray, FDelete fp) {
H
Haojun Liao 已提交
401 402 403 404
  if (pArray == NULL) {
    return;
  }

H
Haojun Liao 已提交
405
  if (fp == NULL) {
H
Haojun Liao 已提交
406 407
    taosArrayDestroy(pArray);
    return;
H
Haojun Liao 已提交
408 409
  }

S
tarray  
Shengliang Guan 已提交
410
  for (int32_t i = 0; i < pArray->size; ++i) {
H
Haojun Liao 已提交
411 412 413 414 415 416
    fp(TARRAY_GET_ELEM(pArray, i));
  }

  taosArrayDestroy(pArray);
}

417
void taosArraySort(SArray* pArray, __compar_fn_t compar) {
weixin_48148422's avatar
weixin_48148422 已提交
418 419 420
  assert(pArray != NULL);
  assert(compar != NULL);

wafwerar's avatar
wafwerar 已提交
421
  taosSort(pArray->pData, pArray->size, pArray->elemSize, compar);
weixin_48148422's avatar
weixin_48148422 已提交
422 423
}

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

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

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

436
void taosArraySortString(SArray* pArray, __compar_fn_t comparFn) {
weixin_48148422's avatar
weixin_48148422 已提交
437
  assert(pArray != NULL);
wafwerar's avatar
wafwerar 已提交
438
  taosSort(pArray->pData, pArray->size, pArray->elemSize, comparFn);
weixin_48148422's avatar
weixin_48148422 已提交
439 440
}

S
tarray  
Shengliang Guan 已提交
441 442
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);
443
  while (i < j) {
S
tarray  
Shengliang Guan 已提交
444 445 446 447 448
    while (i < j && fn(taosArrayGetP(pArray, j), key, userData) >= 0) {
      j--;
    }
    if (i < j) {
      void* a = taosArrayGetP(pArray, j);
449 450
      taosArraySet(pArray, i, &a);
    }
S
tarray  
Shengliang Guan 已提交
451 452 453
    while (i < j && fn(taosArrayGetP(pArray, i), key, userData) <= 0) {
      i++;
    }
454
    if (i < j) {
S
tarray  
Shengliang Guan 已提交
455
      void* a = taosArrayGetP(pArray, i);
456 457 458
      taosArraySet(pArray, j, &a);
    }
  }
S
tarray  
Shengliang Guan 已提交
459
  taosArraySet(pArray, i, &key);
460 461 462
  return i;
}

dengyihao's avatar
dengyihao 已提交
463
static void taosArrayQuicksortImpl(SArray* pArray, int32_t low, int32_t high, __ext_compar_fn_t fn, const void* param) {
464
  if (low < high) {
S
tarray  
Shengliang Guan 已提交
465
    int32_t idx = taosArrayPartition(pArray, low, high, fn, param);
dengyihao's avatar
dengyihao 已提交
466 467
    taosArrayQuicksortImpl(pArray, low, idx - 1, fn, param);
    taosArrayQuicksortImpl(pArray, idx + 1, high, fn, param);
S
tarray  
Shengliang Guan 已提交
468
  }
469 470
}

S
tarray  
Shengliang Guan 已提交
471
static void taosArrayQuickSort(SArray* pArray, __ext_compar_fn_t fn, const void* param) {
472
  if (pArray->size <= 1) {
473
    return;
S
tarray  
Shengliang Guan 已提交
474
  }
dengyihao's avatar
dengyihao 已提交
475
  taosArrayQuicksortImpl(pArray, 0, (int32_t)(taosArrayGetSize(pArray) - 1), fn, param);
476
}
S
tarray  
Shengliang Guan 已提交
477 478

static void taosArrayInsertSort(SArray* pArray, __ext_compar_fn_t fn, const void* param) {
479
  if (pArray->size <= 1) {
480
    return;
S
tarray  
Shengliang Guan 已提交
481 482 483
  }
  for (int32_t i = 1; i <= pArray->size - 1; ++i) {
    for (int32_t j = i; j > 0; --j) {
484
      if (fn(taosArrayGetP(pArray, j), taosArrayGetP(pArray, j - 1), param) == -1) {
S
tarray  
Shengliang Guan 已提交
485 486
        void* a = taosArrayGetP(pArray, j);
        void* b = taosArrayGetP(pArray, j - 1);
487 488 489 490 491 492 493
        taosArraySet(pArray, j - 1, &a);
        taosArraySet(pArray, j, &b);
      } else {
        break;
      }
    }
  }
494
  return;
S
tarray  
Shengliang Guan 已提交
495 496
}

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
int32_t taosEncodeArray(void** buf, const SArray* pArray, FEncode encode) {
  int32_t tlen = 0;
  int32_t sz = pArray->size;
  tlen += taosEncodeFixedI32(buf, sz);
  for (int32_t i = 0; i < sz; i++) {
    void* data = taosArrayGetP(pArray, i);
    tlen += encode(buf, data);
  }
  return tlen;
}

void* taosDecodeArray(const void* buf, SArray** pArray, FDecode decode, int32_t dataSz) {
  int32_t sz;
  buf = taosDecodeFixedI32(buf, &sz);
  *pArray = taosArrayInit(sz, sizeof(void*));
  for (int32_t i = 0; i < sz; i++) {
    void* data = taosMemoryCalloc(1, dataSz);
    buf = decode(buf, data);
L
Liu Jicong 已提交
515
    taosArrayPush(*pArray, &data);
516 517 518 519
  }
  return (void*)buf;
}

520
// todo remove it
S
tarray  
Shengliang Guan 已提交
521 522 523 524
// 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);
}
L
Liu Jicong 已提交
525

dengyihao's avatar
dengyihao 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
void taosArraySwap(SArray* a, SArray* b) {
  if (a == NULL || b == NULL) return;
  size_t t = a->size;
  a->size = b->size;
  b->size = t;

  uint32_t cap = a->capacity;
  a->capacity = b->capacity;
  b->capacity = cap;

  uint32_t elem = a->elemSize;
  a->elemSize = b->elemSize;
  b->elemSize = elem;

  void* data = a->pData;
  a->pData = b->pData;
  b->pData = data;
}