tarray.c 12.3 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
Haojun Liao 已提交
23 24 25 26
  if (elemSize == 0) {
    terrno = TSDB_CODE_INVALID_PARA;
    return NULL;
  }
H
more  
hzcheng 已提交
27 28 29 30 31

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

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

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

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

51
SArray* taosArrayInit_s(size_t elemSize, size_t initialSize) {
H
Haojun Liao 已提交
52 53 54 55 56 57
  SArray* pArray = taosMemoryMalloc(sizeof(SArray));
  if (pArray == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

H
Haojun Liao 已提交
58
  pArray->size = initialSize;
H
Haojun Liao 已提交
59 60 61 62 63 64 65 66 67 68 69 70
  pArray->pData = taosMemoryCalloc(initialSize, elemSize);
  if (pArray->pData == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    taosMemoryFree(pArray);
    return NULL;
  }

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

H
hjxilinx 已提交
71
static int32_t taosArrayResize(SArray* pArray) {
H
more  
hzcheng 已提交
72 73 74
  size_t size = pArray->capacity;
  size = (size << 1u);

wafwerar's avatar
wafwerar 已提交
75
  void* tmp = taosMemoryRealloc(pArray->pData, size * pArray->elemSize);
H
hjxilinx 已提交
76 77
  if (tmp == NULL) {  // reallocate failed, the original buffer remains
    return -1;
H
more  
hzcheng 已提交
78 79 80 81
  }

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

H
hjxilinx 已提交
83
  return 0;
H
more  
hzcheng 已提交
84 85
}

L
Liu Jicong 已提交
86 87
int32_t taosArrayEnsureCap(SArray* pArray, size_t newCap) {
  if (newCap > pArray->capacity) {
H
Hongze Cheng 已提交
88
    size_t tsize = (pArray->capacity << 1u);
L
Liu Jicong 已提交
89
    while (newCap > tsize) {
H
Hongze Cheng 已提交
90 91 92
      tsize = (tsize << 1u);
    }

wafwerar's avatar
wafwerar 已提交
93
    pArray->pData = taosMemoryRealloc(pArray->pData, tsize * pArray->elemSize);
H
Hongze Cheng 已提交
94
    if (pArray->pData == NULL) {
L
Liu Jicong 已提交
95
      return -1;
H
hjxilinx 已提交
96
    }
H
Hongze Cheng 已提交
97 98

    pArray->capacity = tsize;
H
more  
hzcheng 已提交
99
  }
L
Liu Jicong 已提交
100 101 102
  return 0;
}

S
tarray  
Shengliang Guan 已提交
103
void* taosArrayAddBatch(SArray* pArray, const void* pData, int32_t nEles) {
L
Liu Jicong 已提交
104
  if (pData == NULL) {
L
Liu Jicong 已提交
105 106 107
    return NULL;
  }

S
tarray  
Shengliang Guan 已提交
108
  if (taosArrayEnsureCap(pArray, pArray->size + nEles) != 0) {
L
Liu Jicong 已提交
109 110
    return NULL;
  }
H
more  
hzcheng 已提交
111 112

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

H
Hongze Cheng 已提交
115
  pArray->size += nEles;
H
more  
hzcheng 已提交
116 117 118
  return dst;
}

S
tarray  
Shengliang Guan 已提交
119
void taosArrayRemoveDuplicate(SArray* pArray, __compar_fn_t comparFn, void (*fp)(void*)) {
120 121 122 123 124 125
  size_t size = pArray->size;
  if (size <= 1) {
    return;
  }

  int32_t pos = 0;
S
tarray  
Shengliang Guan 已提交
126
  for (int32_t i = 1; i < size; ++i) {
127 128 129 130 131 132 133 134 135 136 137 138 139
    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);
140
        memset(TARRAY_GET_ELEM(pArray, i), 0, pArray->elemSize);
141
        pos += 1;
142 143 144 145 146 147 148 149
      } else {
        pos += 1;
      }
    }
  }

  if (fp != NULL) {
    for (int32_t i = pos + 1; i < pArray->size; ++i) {
150
      void* p = taosArrayGet(pArray, i);
151 152 153 154 155 156 157
      fp(p);
    }
  }

  pArray->size = pos + 1;
}

H
Haojun Liao 已提交
158
void* taosArrayAddAll(SArray* pArray, const SArray* pInput) {
159 160 161 162 163
  if (pInput) {
    return taosArrayAddBatch(pArray, pInput->pData, (int32_t)taosArrayGetSize(pInput));
  } else {
    return NULL;
  }
H
Haojun Liao 已提交
164 165
}

H
Hongze Cheng 已提交
166 167 168 169 170 171 172 173
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 已提交
174 175
  memset(dst, 0, num * pArray->elemSize);

H
Hongze Cheng 已提交
176 177 178
  return dst;
}

weixin_48148422's avatar
weixin_48148422 已提交
179
void* taosArrayPop(SArray* pArray) {
180
  if (pArray->size == 0) {
weixin_48148422's avatar
weixin_48148422 已提交
181
    return NULL;
H
more  
hzcheng 已提交
182
  }
H
Haojun Liao 已提交
183

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

188
void* taosArrayGet(const SArray* pArray, size_t index) {
189 190 191
  if (NULL == pArray) {
    return NULL;
  }
H
Haojun Liao 已提交
192 193 194 195 196 197

  if (index >= pArray->size) {
    uError("index is out of range, current:%"PRIzu" max:%d", index, pArray->capacity);
    return NULL;
  }

H
more  
hzcheng 已提交
198 199 200
  return TARRAY_GET_ELEM(pArray, index);
}

201
void* taosArrayGetP(const SArray* pArray, size_t index) {
H
Haojun Liao 已提交
202 203 204 205 206
  void** p = taosArrayGet(pArray, index);
  if (p == NULL) {
    return NULL;
  }
  return *p;
H
hjxilinx 已提交
207 208
}

H
Haojun Liao 已提交
209 210 211 212 213 214 215
void* taosArrayGetLast(const SArray* pArray) {
  if (pArray->size == 0) {
    return NULL;
  }

  return TARRAY_GET_ELEM(pArray, pArray->size - 1);
}
H
Haojun Liao 已提交
216

L
Liu Jicong 已提交
217
size_t taosArrayGetSize(const SArray* pArray) {
S
Shengliang Guan 已提交
218 219 220
  if (pArray == NULL) {
    return 0;
  }
H
Hongze Cheng 已提交
221
  return TARRAY_SIZE(pArray);
S
Shengliang Guan 已提交
222
}
H
more  
hzcheng 已提交
223

H
hjxilinx 已提交
224
void* taosArrayInsert(SArray* pArray, size_t index, void* pData) {
H
more  
hzcheng 已提交
225
  if (pArray == NULL || pData == NULL) {
H
hjxilinx 已提交
226
    return NULL;
H
more  
hzcheng 已提交
227 228 229
  }

  if (index >= pArray->size) {
H
hjxilinx 已提交
230
    return taosArrayPush(pArray, pData);
H
more  
hzcheng 已提交
231 232 233
  }

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

H
hjxilinx 已提交
236 237 238
    if (ret < 0) {
      return NULL;
    }
H
more  
hzcheng 已提交
239 240 241 242
  }

  void* dst = TARRAY_GET_ELEM(pArray, index);

S
Shengliang Guan 已提交
243 244
  int32_t remain = (int32_t)(pArray->size - index);
  memmove((char*)dst + pArray->elemSize, (char*)dst, pArray->elemSize * remain);
H
more  
hzcheng 已提交
245 246 247
  memcpy(dst, pData, pArray->elemSize);

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

H
hjxilinx 已提交
249
  return dst;
H
more  
hzcheng 已提交
250 251
}

H
Hongze Cheng 已提交
252
void taosArraySet(SArray* pArray, size_t index, void* pData) {
H
Haojun Liao 已提交
253
  ASSERT(index < pArray->size);
H
refact  
Hongze Cheng 已提交
254 255 256
  memcpy(TARRAY_GET_ELEM(pArray, index), pData, pArray->elemSize);
}

L
Liu Jicong 已提交
257
void taosArrayPopFrontBatch(SArray* pArray, size_t cnt) {
H
Haojun Liao 已提交
258
  ASSERT(cnt <= pArray->size);
L
Liu Jicong 已提交
259
  pArray->size = pArray->size - cnt;
L
Liu Jicong 已提交
260
  if (pArray->size == 0 || cnt == 0) {
L
Liu Jicong 已提交
261 262
    return;
  }
L
Liu Jicong 已提交
263
  memmove(pArray->pData, (char*)pArray->pData + cnt * pArray->elemSize, pArray->size * pArray->elemSize);
L
Liu Jicong 已提交
264 265
}

L
Liu Jicong 已提交
266
void taosArrayPopTailBatch(SArray* pArray, size_t cnt) {
H
Haojun Liao 已提交
267 268 269 270
  if (cnt >= pArray->size) {
    cnt = pArray->size;
  }

L
Liu Jicong 已提交
271 272 273
  pArray->size = pArray->size - cnt;
}

274
void taosArrayRemove(SArray* pArray, size_t index) {
H
Haojun Liao 已提交
275
  ASSERT(index < pArray->size);
S
tarray  
Shengliang Guan 已提交
276

277 278 279 280
  if (index == pArray->size - 1) {
    taosArrayPop(pArray);
    return;
  }
S
tarray  
Shengliang Guan 已提交
281

282
  size_t remain = pArray->size - index - 1;
S
tarray  
Shengliang Guan 已提交
283 284
  memmove((char*)pArray->pData + index * pArray->elemSize, (char*)pArray->pData + (index + 1) * pArray->elemSize,
          remain * pArray->elemSize);
285 286 287
  pArray->size -= 1;
}

H
Hongze Cheng 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301
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 已提交
302
SArray* taosArrayFromList(const void* src, size_t size, size_t elemSize) {
H
Haojun Liao 已提交
303 304 305 306
  if (elemSize <= 0) {
    terrno = TSDB_CODE_INVALID_PARA;
    return NULL;
  }
H
Haojun Liao 已提交
307

H
Haojun Liao 已提交
308
  SArray* pDst = taosArrayInit(size, elemSize);
H
Haojun Liao 已提交
309 310 311 312
  memcpy(pDst->pData, src, elemSize * size);
  pDst->size = size;

  return pDst;
313 314
}

H
Haojun Liao 已提交
315
SArray* taosArrayDup(const SArray* pSrc, __array_item_dup_fn_t fn) {
S
tarray  
Shengliang Guan 已提交
316
  if (pSrc->size == 0) {  // empty array list
H
hjxilinx 已提交
317 318
    return taosArrayInit(8, pSrc->elemSize);
  }
S
tarray  
Shengliang Guan 已提交
319

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

H
Haojun Liao 已提交
322 323 324 325 326
  if (fn == NULL) {
    memcpy(dst->pData, pSrc->pData, pSrc->elemSize * pSrc->size);
  } else {
    ASSERT(pSrc->elemSize == sizeof(void*));

H
Hongze Cheng 已提交
327
    for (int32_t i = 0; i < pSrc->size; ++i) {
H
Haojun Liao 已提交
328
      void* p = fn(taosArrayGetP(pSrc, i));
H
Hongze Cheng 已提交
329
      memcpy(((char*)dst->pData) + i * dst->elemSize, &p, dst->elemSize);
H
Haojun Liao 已提交
330 331 332
    }
  }

H
hjxilinx 已提交
333
  dst->size = pSrc->size;
H
Haojun Liao 已提交
334

H
hjxilinx 已提交
335 336 337
  return dst;
}

weixin_48148422's avatar
weixin_48148422 已提交
338
void taosArrayClear(SArray* pArray) {
S
Shengliang Guan 已提交
339
  if (pArray == NULL) return;
weixin_48148422's avatar
weixin_48148422 已提交
340 341 342
  pArray->size = 0;
}

D
dapan1121 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356
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 已提交
357 358
void* taosArrayDestroy(SArray* pArray) {
  if (pArray) {
wafwerar's avatar
wafwerar 已提交
359 360
    taosMemoryFree(pArray->pData);
    taosMemoryFree(pArray);
H
more  
hzcheng 已提交
361 362
  }

H
Hongze Cheng 已提交
363
  return NULL;
H
more  
hzcheng 已提交
364
}
weixin_48148422's avatar
weixin_48148422 已提交
365

366
void taosArrayDestroyP(SArray* pArray, FDelete fp) {
L
Liu Jicong 已提交
367 368 369 370 371
  if (pArray) {
    for (int32_t i = 0; i < pArray->size; i++) {
      fp(*(void**)TARRAY_GET_ELEM(pArray, i));
    }
    taosArrayDestroy(pArray);
372 373 374 375
  }
}

void taosArrayDestroyEx(SArray* pArray, FDelete fp) {
H
Haojun Liao 已提交
376 377 378 379
  if (pArray == NULL) {
    return;
  }

H
Haojun Liao 已提交
380
  if (fp == NULL) {
H
Haojun Liao 已提交
381 382
    taosArrayDestroy(pArray);
    return;
H
Haojun Liao 已提交
383 384
  }

S
tarray  
Shengliang Guan 已提交
385
  for (int32_t i = 0; i < pArray->size; ++i) {
H
Haojun Liao 已提交
386 387 388 389 390 391
    fp(TARRAY_GET_ELEM(pArray, i));
  }

  taosArrayDestroy(pArray);
}

392
void taosArraySort(SArray* pArray, __compar_fn_t compar) {
wafwerar's avatar
wafwerar 已提交
393
  taosSort(pArray->pData, pArray->size, pArray->elemSize, compar);
weixin_48148422's avatar
weixin_48148422 已提交
394 395
}

S
tarray  
Shengliang Guan 已提交
396
void* taosArraySearch(const SArray* pArray, const void* key, __compar_fn_t comparFn, int32_t flags) {
H
refact  
Hongze Cheng 已提交
397
  return taosbsearch(key, pArray->pData, pArray->size, pArray->elemSize, comparFn, flags);
weixin_48148422's avatar
weixin_48148422 已提交
398 399
}

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

S
tarray  
Shengliang Guan 已提交
405 406
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);
407
  while (i < j) {
S
tarray  
Shengliang Guan 已提交
408 409 410 411 412
    while (i < j && fn(taosArrayGetP(pArray, j), key, userData) >= 0) {
      j--;
    }
    if (i < j) {
      void* a = taosArrayGetP(pArray, j);
413 414
      taosArraySet(pArray, i, &a);
    }
S
tarray  
Shengliang Guan 已提交
415 416 417
    while (i < j && fn(taosArrayGetP(pArray, i), key, userData) <= 0) {
      i++;
    }
418
    if (i < j) {
S
tarray  
Shengliang Guan 已提交
419
      void* a = taosArrayGetP(pArray, i);
420 421 422
      taosArraySet(pArray, j, &a);
    }
  }
S
tarray  
Shengliang Guan 已提交
423
  taosArraySet(pArray, i, &key);
424 425 426
  return i;
}

dengyihao's avatar
dengyihao 已提交
427
static void taosArrayQuicksortImpl(SArray* pArray, int32_t low, int32_t high, __ext_compar_fn_t fn, const void* param) {
428
  if (low < high) {
S
tarray  
Shengliang Guan 已提交
429
    int32_t idx = taosArrayPartition(pArray, low, high, fn, param);
dengyihao's avatar
dengyihao 已提交
430 431
    taosArrayQuicksortImpl(pArray, low, idx - 1, fn, param);
    taosArrayQuicksortImpl(pArray, idx + 1, high, fn, param);
S
tarray  
Shengliang Guan 已提交
432
  }
433 434
}

S
tarray  
Shengliang Guan 已提交
435
static void taosArrayQuickSort(SArray* pArray, __ext_compar_fn_t fn, const void* param) {
436
  if (pArray->size <= 1) {
437
    return;
S
tarray  
Shengliang Guan 已提交
438
  }
dengyihao's avatar
dengyihao 已提交
439
  taosArrayQuicksortImpl(pArray, 0, (int32_t)(taosArrayGetSize(pArray) - 1), fn, param);
440
}
S
tarray  
Shengliang Guan 已提交
441 442

static void taosArrayInsertSort(SArray* pArray, __ext_compar_fn_t fn, const void* param) {
443
  if (pArray->size <= 1) {
444
    return;
S
tarray  
Shengliang Guan 已提交
445
  }
446

S
tarray  
Shengliang Guan 已提交
447 448
  for (int32_t i = 1; i <= pArray->size - 1; ++i) {
    for (int32_t j = i; j > 0; --j) {
449
      if (fn(taosArrayGetP(pArray, j), taosArrayGetP(pArray, j - 1), param) == -1) {
S
tarray  
Shengliang Guan 已提交
450 451
        void* a = taosArrayGetP(pArray, j);
        void* b = taosArrayGetP(pArray, j - 1);
452 453 454 455 456 457 458
        taosArraySet(pArray, j - 1, &a);
        taosArraySet(pArray, j, &b);
      } else {
        break;
      }
    }
  }
S
tarray  
Shengliang Guan 已提交
459 460
}

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
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 已提交
479
    taosArrayPush(*pArray, &data);
480 481 482 483
  }
  return (void*)buf;
}

484
// todo remove it
S
tarray  
Shengliang Guan 已提交
485 486
// order array<type *>
void taosArraySortPWithExt(SArray* pArray, __ext_compar_fn_t fn, const void* param) {
487 488
  taosqsort(pArray->pData, pArray->size, pArray->elemSize, param, fn);
//  taosArrayGetSize(pArray) > 8 ? taosArrayQuickSort(pArray, fn, param) : taosArrayInsertSort(pArray, fn, param);
S
tarray  
Shengliang Guan 已提交
489
}
L
Liu Jicong 已提交
490

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
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;
}