tarray2.h 9.5 KB
Newer Older
H
Hongze Cheng 已提交
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 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
 * 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/>.
 */

#include "talgo.h"

#ifndef _TD_UTIL_TARRAY2_H_
#define _TD_UTIL_TARRAY2_H_

#ifdef __cplusplus
extern "C" {
#endif

// a: a
// e: element
// ep: element pointer
// cmp: compare function
// idx: index
// cb: callback function

#define TARRAY2(TYPE) \
  struct {            \
    int32_t size;     \
    int32_t capacity; \
    TYPE   *data;     \
  }

H
Hongze Cheng 已提交
39 40
typedef void (*TArray2Cb)(void *);

H
Hongze Cheng 已提交
41 42
#define TARRAY2_MIN_SIZE 16

H
Hongze Cheng 已提交
43
#define TARRAY2_SIZE(a)       ((a)->size)
H
Hongze Cheng 已提交
44 45
#define TARRAY2_CAPACITY(a)   ((a)->capacity)
#define TARRAY2_DATA(a)       ((a)->data)
H
Hongze Cheng 已提交
46
#define TARRAY2_GET(a, i)     ((a)->data[i])
H
Hongze Cheng 已提交
47
#define TARRAY2_GET_PTR(a, i) ((a)->data + i)
H
Hongze Cheng 已提交
48 49
#define TARRAY2_FIRST(a)      ((a)->data[0])
#define TARRAY2_LAST(a)       ((a)->data[(a)->size - 1])
H
Hongze Cheng 已提交
50
#define TARRAY2_DATA_LEN(a)   ((a)->size * sizeof(typeof((a)->data[0])))
H
Hongze Cheng 已提交
51

H
Hongze Cheng 已提交
52 53 54 55
static FORCE_INLINE int32_t tarray2_make_room(  //
    void   *arg,                                // array
    int32_t es,                                 // expected size
    int32_t sz                                  // size of element
H
Hongze Cheng 已提交
56 57
) {
  TARRAY2(void) *a = arg;
H
Hongze Cheng 已提交
58
  int32_t capacity = (a->capacity > 0) ? (a->capacity << 1) : TARRAY2_MIN_SIZE;
H
Hongze Cheng 已提交
59 60 61 62
  while (capacity < es) {
    capacity <<= 1;
  }
  void *p = taosMemoryRealloc(a->data, capacity * sz);
H
Hongze Cheng 已提交
63
  if (p == NULL) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
64 65
  a->capacity = capacity;
  a->data = p;
H
Hongze Cheng 已提交
66 67 68
  return 0;
}

H
Hongze Cheng 已提交
69 70 71 72 73
#define TARRAY2_INIT_EX(a, size_, capacity_, data_) \
  do {                                              \
    (a)->size = (size_);                            \
    (a)->capacity = (capacity_);                    \
    (a)->data = (data_);                            \
H
Hongze Cheng 已提交
74 75
  } while (0)

H
Hongze Cheng 已提交
76 77
#define TARRAY2_INIT(a) TARRAY2_INIT_EX(a, 0, 0, NULL)

H
Hongze Cheng 已提交
78 79
#define TARRAY2_CLEAR(a, cb)                    \
  do {                                          \
H
Hongze Cheng 已提交
80
    if ((cb) && (a)->size > 0) {                \
H
Hongze Cheng 已提交
81
      TArray2Cb cb_ = (TArray2Cb)(cb);          \
H
Hongze Cheng 已提交
82
      for (int32_t i = 0; i < (a)->size; ++i) { \
H
Hongze Cheng 已提交
83
        cb_((a)->data + i);                     \
H
Hongze Cheng 已提交
84 85 86 87 88
      }                                         \
    }                                           \
    (a)->size = 0;                              \
  } while (0)

H
Hongze Cheng 已提交
89 90 91 92 93 94 95 96
#define TARRAY2_DESTROY(a, cb)   \
  do {                           \
    TARRAY2_CLEAR(a, cb);        \
    if ((a)->data) {             \
      taosMemoryFree((a)->data); \
      (a)->data = NULL;          \
    }                            \
    (a)->capacity = 0;           \
H
Hongze Cheng 已提交
97 98 99 100 101 102
  } while (0)

#define TARRAY2_INSERT(a, idx, e)                                                                              \
  ({                                                                                                           \
    int32_t __ret = 0;                                                                                         \
    if ((a)->size >= (a)->capacity) {                                                                          \
H
Hongze Cheng 已提交
103
      __ret = tarray2_make_room((a), (a)->size + 1, sizeof(typeof((a)->data[0])));                             \
H
Hongze Cheng 已提交
104 105 106 107 108
    }                                                                                                          \
    if (!__ret) {                                                                                              \
      if ((a)->size > (idx)) {                                                                                 \
        memmove((a)->data + (idx) + 1, (a)->data + (idx), sizeof(typeof((a)->data[0])) * ((a)->size - (idx))); \
      }                                                                                                        \
H
Hongze Cheng 已提交
109
      (a)->data[(idx)] = (e);                                                                                  \
H
Hongze Cheng 已提交
110 111 112
      (a)->size++;                                                                                             \
    }                                                                                                          \
    __ret;                                                                                                     \
H
Hongze Cheng 已提交
113 114
  })

H
Hongze Cheng 已提交
115 116 117
#define TARRAY2_INSERT_PTR(a, idx, ep) TARRAY2_INSERT(a, idx, *(ep))
#define TARRAY2_APPEND(a, e)           TARRAY2_INSERT(a, (a)->size, e)
#define TARRAY2_APPEND_PTR(a, ep)      TARRAY2_APPEND(a, *(ep))
H
Hongze Cheng 已提交
118

H
Hongze Cheng 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131
#define TARRAY2_APPEND_BATCH(a, ep, n)                                               \
  ({                                                                                 \
    int32_t __ret = 0;                                                               \
    if ((a)->size + (n) > (a)->capacity) {                                           \
      __ret = tarray2_make_room((a), (a)->size + (n), sizeof(typeof((a)->data[0]))); \
    }                                                                                \
    if (!__ret) {                                                                    \
      memcpy((a)->data + (a)->size, (ep), sizeof(typeof((a)->data[0])) * (n));       \
      (a)->size += (n);                                                              \
    }                                                                                \
    __ret;                                                                           \
  })

H
Hongze Cheng 已提交
132
// return (TYPE *)
H
Hongze Cheng 已提交
133 134 135 136 137 138 139 140 141 142 143
#define TARRAY2_SEARCH(a, ep, cmp, flag)                                                                     \
  ({                                                                                                         \
    typeof((a)->data) __ep = (ep);                                                                           \
    typeof((a)->data) __p;                                                                                   \
    if ((a)->size > 0) {                                                                                     \
      __p = taosbsearch(__ep, (a)->data, (a)->size, sizeof(typeof((a)->data[0])), (__compar_fn_t)cmp, flag); \
    } else {                                                                                                 \
      __p = NULL;                                                                                            \
    }                                                                                                        \
    __p;                                                                                                     \
  })
H
Hongze Cheng 已提交
144 145 146 147 148 149 150

// return (TYPE)
#define TARRAY2_SEARCH_EX(a, ep, cmp, flag)                   \
  ({                                                          \
    typeof((a)->data) __p = TARRAY2_SEARCH(a, ep, cmp, flag); \
    __p ? __p[0] : NULL;                                      \
  })
H
Hongze Cheng 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

#define TARRAY2_SEARCH_IDX(a, ep, cmp, flag)                  \
  ({                                                          \
    typeof((a)->data) __p = TARRAY2_SEARCH(a, ep, cmp, flag); \
    __p ? __p - (a)->data : -1;                               \
  })

#define TARRAY2_SORT_INSERT(a, e, cmp)                       \
  ({                                                         \
    int32_t __idx = TARRAY2_SEARCH_IDX(a, &(e), cmp, TD_GT); \
    TARRAY2_INSERT(a, __idx < 0 ? (a)->size : __idx, e);     \
  })

#define TARRAY2_SORT_INSERT_P(a, ep, cmp) TARRAY2_SORT_INSERT(a, *(ep), cmp)

H
Hongze Cheng 已提交
166 167 168 169
#define TARRAY2_REMOVE(a, idx, cb)                                                                             \
  do {                                                                                                         \
    if ((idx) < (a)->size) {                                                                                   \
      if (cb) {                                                                                                \
H
Hongze Cheng 已提交
170
        TArray2Cb cb_ = (TArray2Cb)(cb);                                                                       \
H
Hongze Cheng 已提交
171 172 173 174 175 176 177
        cb_((a)->data + (idx));                                                                                \
      }                                                                                                        \
      if ((idx) < (a)->size - 1) {                                                                             \
        memmove((a)->data + (idx), (a)->data + (idx) + 1, sizeof(typeof(*(a)->data)) * ((a)->size - (idx)-1)); \
      }                                                                                                        \
      (a)->size--;                                                                                             \
    }                                                                                                          \
H
Hongze Cheng 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190
  } while (0)

#define TARRAY2_FOREACH(a, e)         for (int32_t __i = 0; __i < (a)->size && ((e) = (a)->data[__i], 1); __i++)
#define TARRAY2_FOREACH_REVERSE(a, e) for (int32_t __i = (a)->size - 1; __i >= 0 && ((e) = (a)->data[__i], 1); __i--)
#define TARRAY2_FOREACH_PTR(a, ep)    for (int32_t __i = 0; __i < (a)->size && ((ep) = &(a)->data[__i], 1); __i++)
#define TARRAY2_FOREACH_PTR_REVERSE(a, ep) \
  for (int32_t __i = (a)->size - 1; __i >= 0 && ((ep) = &(a)->data[__i], 1); __i--)

#ifdef __cplusplus
}
#endif

#endif /*_TD_UTIL_TARRAY2_H_*/