tlist.h 11.9 KB
Newer Older
H
TD-34  
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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
Shengliang Guan 已提交
15 16
#ifndef _TD_UTIL_LIST_H
#define _TD_UTIL_LIST_H
H
TD-34  
hzcheng 已提交
17 18 19 20 21

#ifdef __cplusplus
extern "C" {
#endif

H
Hongze Cheng 已提交
22
// Single linked list ================
H
Hongze Cheng 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36
#define TD_SLIST_NODE(TYPE) \
  struct {                  \
    struct TYPE *sl_next_;  \
  }

#define TD_SLIST(TYPE)      \
  struct {                  \
    struct TYPE *sl_head_;  \
    int          sl_neles_; \
  }

#define TD_SLIST_HEAD(sl) ((sl)->sl_head_)
#define TD_SLIST_NELES(sl) ((sl)->sl_neles_)
#define TD_SLIST_NODE_NEXT(sln) ((sln)->sl_next_)
H
Hongze Cheng 已提交
37
#define TD_SLIST_NODE_NEXT_WITH_FIELD(sln, field) ((sln)->field.sl_next_)
H
Hongze Cheng 已提交
38

H
Hongze Cheng 已提交
39
#define TD_SLIST_INIT(sl)  \
H
Hongze Cheng 已提交
40 41 42 43 44
  do {                     \
    (sl)->sl_head_ = NULL; \
    (sl)->sl_neles_ = 0;   \
  } while (0)

H
Hongze Cheng 已提交
45
#define TD_SLIST_PUSH(sl, sln)                   \
H
Hongze Cheng 已提交
46 47 48 49 50 51
  do {                                           \
    TD_SLIST_NODE_NEXT(sln) = TD_SLIST_HEAD(sl); \
    TD_SLIST_HEAD(sl) = (sln);                   \
    TD_SLIST_NELES(sl) += 1;                     \
  } while (0)

H
Hongze Cheng 已提交
52
#define TD_SLIST_PUSH_WITH_FIELD(sl, sln, field)                   \
H
Hongze Cheng 已提交
53
  do {                                                             \
H
Hongze Cheng 已提交
54
    TD_SLIST_NODE_NEXT_WITH_FIELD(sln, field) = TD_SLIST_HEAD(sl); \
H
Hongze Cheng 已提交
55 56 57 58
    TD_SLIST_HEAD(sl) = (sln);                                     \
    TD_SLIST_NELES(sl) += 1;                                       \
  } while (0)

H
Hongze Cheng 已提交
59
#define TD_SLIST_POP(sl)                                       \
H
Hongze Cheng 已提交
60 61 62 63 64
  do {                                                         \
    TD_SLIST_HEAD(sl) = TD_SLIST_NODE_NEXT(TD_SLIST_HEAD(sl)); \
    TD_SLIST_NELES(sl) -= 1;                                   \
  } while (0)

H
Hongze Cheng 已提交
65
#define TD_SLIST_POP_WITH_FIELD(sl, field)                                       \
H
Hongze Cheng 已提交
66
  do {                                                                           \
H
Hongze Cheng 已提交
67
    TD_SLIST_HEAD(sl) = TD_SLIST_NODE_NEXT_WITH_FIELD(TD_SLIST_HEAD(sl), field); \
H
Hongze Cheng 已提交
68 69 70
    TD_SLIST_NELES(sl) -= 1;                                                     \
  } while (0)

H
Hongze Cheng 已提交
71
// Double linked list ================
H
Hongze Cheng 已提交
72 73
#define TD_DLIST_NODE(TYPE) \
  struct {                  \
H
Hongze Cheng 已提交
74 75
    struct TYPE *dl_prev_;  \
    struct TYPE *dl_next_;  \
H
Hongze Cheng 已提交
76 77 78 79 80 81 82 83 84 85 86
  }

#define TD_DLIST(TYPE)      \
  struct {                  \
    struct TYPE *dl_head_;  \
    struct TYPE *dl_tail_;  \
    int          dl_neles_; \
  }

#define TD_DLIST_NODE_PREV(dln) ((dln)->dl_prev_)
#define TD_DLIST_NODE_NEXT(dln) ((dln)->dl_next_)
H
Hongze Cheng 已提交
87 88
#define TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) ((dln)->field.dl_prev_)
#define TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) ((dln)->field.dl_next_)
H
Hongze Cheng 已提交
89 90 91 92
#define TD_DLIST_HEAD(dl) ((dl)->dl_head_)
#define TD_DLIST_TAIL(dl) ((dl)->dl_tail_)
#define TD_DLIST_NELES(dl) ((dl)->dl_neles_)

H
Hongze Cheng 已提交
93
#define TD_DLIST_INIT(dl)                         \
H
Hongze Cheng 已提交
94 95 96 97 98
  do {                                            \
    TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = NULL; \
    TD_DLIST_NELES(dl) = 0;                       \
  } while (0)

H
Hongze Cheng 已提交
99
#define TD_DLIST_APPEND(dl, dln)                                \
H
Hongze Cheng 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112
  do {                                                          \
    if (TD_DLIST_HEAD(dl) == NULL) {                            \
      TD_DLIST_NODE_PREV(dln) = TD_DLIST_NODE_NEXT(dln) = NULL; \
      TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = (dln);            \
    } else {                                                    \
      TD_DLIST_NODE_PREV(dln) = TD_DLIST_TAIL(dl);              \
      TD_DLIST_NODE_NEXT(dln) = NULL;                           \
      TD_DLIST_NODE_NEXT(TD_DLIST_TAIL(dl)) = (dln);            \
      TD_DLIST_TAIL(dl) = (dln);                                \
    }                                                           \
    TD_DLIST_NELES(dl) += 1;                                    \
  } while (0)

H
Hongze Cheng 已提交
113
#define TD_DLIST_APPEND_WITH_FIELD(dl, dln, field)                                                  \
H
Hongze Cheng 已提交
114 115
  do {                                                                                              \
    if (TD_DLIST_HEAD(dl) == NULL) {                                                                \
H
Hongze Cheng 已提交
116
      TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = NULL; \
H
Hongze Cheng 已提交
117 118
      TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = (dln);                                                \
    } else {                                                                                        \
H
Hongze Cheng 已提交
119 120 121
      TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = TD_DLIST_TAIL(dl);                                \
      TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = NULL;                                             \
      TD_DLIST_NODE_NEXT_WITH_FIELD(TD_DLIST_TAIL(dl), field) = (dln);                              \
H
Hongze Cheng 已提交
122 123 124 125 126
      TD_DLIST_TAIL(dl) = (dln);                                                                    \
    }                                                                                               \
    TD_DLIST_NELES(dl) += 1;                                                                        \
  } while (0)

H
Hongze Cheng 已提交
127
#define TD_DLIST_PREPEND(dl, dln)                               \
H
Hongze Cheng 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140
  do {                                                          \
    if (TD_DLIST_HEAD(dl) == NULL) {                            \
      TD_DLIST_NODE_PREV(dln) = TD_DLIST_NODE_NEXT(dln) = NULL; \
      TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = (dln);            \
    } else {                                                    \
      TD_DLIST_NODE_PREV(dln) = NULL;                           \
      TD_DLIST_NODE_NEXT(dln) = TD_DLIST_HEAD(dl);              \
      TD_DLIST_NODE_PREV(TD_DLIST_HEAD(dl)) = (dln);            \
      TD_DLIST_HEAD(dl) = (dln);                                \
    }                                                           \
    TD_DLIST_NELES(dl) += 1;                                    \
  } while (0)

H
Hongze Cheng 已提交
141
#define TD_DLIST_PREPEND_WITH_FIELD(dl, dln, field)                                                 \
H
Hongze Cheng 已提交
142 143
  do {                                                                                              \
    if (TD_DLIST_HEAD(dl) == NULL) {                                                                \
H
Hongze Cheng 已提交
144
      TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = NULL; \
H
Hongze Cheng 已提交
145 146
      TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = (dln);                                                \
    } else {                                                                                        \
H
Hongze Cheng 已提交
147 148 149
      TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = NULL;                                             \
      TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = TD_DLIST_HEAD(dl);                                \
      TD_DLIST_NODE_PREV_WITH_FIELD(TD_DLIST_HEAD(dl), field) = (dln);                              \
H
Hongze Cheng 已提交
150 151 152 153 154
      TD_DLIST_HEAD(dl) = (dln);                                                                    \
    }                                                                                               \
    TD_DLIST_NELES(dl) += 1;                                                                        \
  } while (0)

H
Hongze Cheng 已提交
155
#define TD_DLIST_POP(dl, dln)                                                \
H
Hongze Cheng 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  do {                                                                       \
    if (TD_DLIST_HEAD(dl) == (dln)) {                                        \
      TD_DLIST_HEAD(dl) = TD_DLIST_NODE_NEXT(dln);                           \
    }                                                                        \
    if (TD_DLIST_TAIL(dl) == (dln)) {                                        \
      TD_DLIST_TAIL(dl) = TD_DLIST_NODE_PREV(dln);                           \
    }                                                                        \
    if (TD_DLIST_NODE_PREV(dln) != NULL) {                                   \
      TD_DLIST_NODE_NEXT(TD_DLIST_NODE_PREV(dln)) = TD_DLIST_NODE_NEXT(dln); \
    }                                                                        \
    if (TD_DLIST_NODE_NEXT(dln) != NULL) {                                   \
      TD_DLIST_NODE_PREV(TD_DLIST_NODE_NEXT(dln)) = TD_DLIST_NODE_PREV(dln); \
    }                                                                        \
    TD_DLIST_NELES(dl) -= 1;                                                 \
    TD_DLIST_NODE_PREV(dln) = TD_DLIST_NODE_NEXT(dln) = NULL;                \
  } while (0)

H
Hongze Cheng 已提交
173 174 175
#define TD_DLIST_POP_WITH_FIELD(dl, dln, field)                                                   \
  do {                                                                                            \
    if (TD_DLIST_HEAD(dl) == (dln)) {                                                             \
H
Hongze Cheng 已提交
176
      TD_DLIST_HEAD(dl) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field);                              \
H
Hongze Cheng 已提交
177 178
    }                                                                                             \
    if (TD_DLIST_TAIL(dl) == (dln)) {                                                             \
H
Hongze Cheng 已提交
179
      TD_DLIST_TAIL(dl) = TD_DLIST_NODE_PREV_WITH_FIELD(dln, field);                              \
H
Hongze Cheng 已提交
180
    }                                                                                             \
H
Hongze Cheng 已提交
181 182 183
    if (TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) != NULL) {                                      \
      TD_DLIST_NODE_NEXT_WITH_FIELD(TD_DLIST_NODE_PREV_WITH_FIELD(dln, field), field) =           \
          TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field);                                              \
H
Hongze Cheng 已提交
184
    }                                                                                             \
H
Hongze Cheng 已提交
185 186 187
    if (TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) != NULL) {                                      \
      TD_DLIST_NODE_PREV_WITH_FIELD(TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field), field) =           \
          TD_DLIST_NODE_PREV_WITH_FIELD(dln, field);                                              \
H
Hongze Cheng 已提交
188 189
    }                                                                                             \
    TD_DLIST_NELES(dl) -= 1;                                                                      \
H
Hongze Cheng 已提交
190
    TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = NULL; \
H
Hongze Cheng 已提交
191 192
  } while (0)

H
Hongze Cheng 已提交
193
// General double linked list
H
TD-34  
hzcheng 已提交
194
typedef enum { TD_LIST_FORWARD, TD_LIST_BACKWARD } TD_LIST_DIRECTION_T;
H
TD-34  
hzcheng 已提交
195

H
Hongze Cheng 已提交
196 197 198
typedef struct SListNode {
  TD_DLIST_NODE(SListNode);
  char data[];
H
TD-34  
hzcheng 已提交
199 200 201
} SListNode;

typedef struct {
H
Hongze Cheng 已提交
202 203
  TD_DLIST(SListNode);
  int eleSize;
H
TD-34  
hzcheng 已提交
204 205 206
} SList;

typedef struct {
H
TD-34  
hzcheng 已提交
207 208
  SListNode *         next;
  TD_LIST_DIRECTION_T direction;
H
TD-34  
hzcheng 已提交
209 210
} SListIter;

H
Hongze Cheng 已提交
211 212 213 214 215
#define listHead(l) TD_DLIST_HEAD(l)
#define listTail(l) TD_DLIST_TAIL(l)
#define listNEles(l) TD_DLIST_NELES(l)
#define listEleSize(l) ((l)->eleSize)
#define isListEmpty(l) (TD_DLIST_NELES(l) == 0)
216
#define listNodeFree(n) free(n)
H
TD-34  
hzcheng 已提交
217

H
more  
Hongze Cheng 已提交
218 219
void       tdListInit(SList *list, int eleSize);
void       tdListEmpty(SList *list);
H
TD-34  
hzcheng 已提交
220
SList *    tdListNew(int eleSize);
H
Hongze Cheng 已提交
221
void *     tdListFree(SList *list);
H
TD-34  
hzcheng 已提交
222 223
void       tdListPrependNode(SList *list, SListNode *node);
void       tdListAppendNode(SList *list, SListNode *node);
H
TD-34  
hzcheng 已提交
224 225 226 227
int        tdListPrepend(SList *list, void *data);
int        tdListAppend(SList *list, void *data);
SListNode *tdListPopHead(SList *list);
SListNode *tdListPopTail(SList *list);
H
Haojun Liao 已提交
228 229
SListNode *tdListGetHead(SList *list);
SListNode *tsListGetTail(SList *list);
H
TD-34  
hzcheng 已提交
230
SListNode *tdListPopNode(SList *list, SListNode *node);
H
TD-34  
hzcheng 已提交
231
void       tdListMove(SList *src, SList *dst);
H
TD-353  
Hongze Cheng 已提交
232
void       tdListDiscard(SList *list);
H
TD-34  
hzcheng 已提交
233

H
TD-34  
hzcheng 已提交
234 235 236
void       tdListNodeGetData(SList *list, SListNode *node, void *target);
void       tdListInitIter(SList *list, SListIter *pIter, TD_LIST_DIRECTION_T direction);
SListNode *tdListNext(SListIter *pIter);
H
TD-34  
hzcheng 已提交
237 238 239 240 241

#ifdef __cplusplus
}
#endif

S
Shengliang Guan 已提交
242
#endif /*_TD_UTIL_LIST_H*/