tlist.c 3.5 KB
Newer Older
H
TD-34  
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
tlist  
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
H
TD-34  
hzcheng 已提交
17
#include "tlist.h"
H
more  
Hongze Cheng 已提交
18

S
tlist  
Shengliang Guan 已提交
19
void tdListInit(SList *list, int32_t eleSize) {
H
Hongze Cheng 已提交
20 21
  TD_DLIST_INIT(list);
  listEleSize(list) = eleSize;
H
more  
Hongze Cheng 已提交
22
}
H
TD-34  
hzcheng 已提交
23

S
tlist  
Shengliang Guan 已提交
24
SList *tdListNew(int32_t eleSize) {
wafwerar's avatar
wafwerar 已提交
25
  SList *list = (SList *)taosMemoryMalloc(sizeof(SList));
H
TD-34  
hzcheng 已提交
26 27
  if (list == NULL) return NULL;

H
more  
Hongze Cheng 已提交
28
  tdListInit(list, eleSize);
H
TD-34  
hzcheng 已提交
29
  return list;
H
TD-34  
hzcheng 已提交
30 31 32
}

void tdListEmpty(SList *list) {
H
Hongze Cheng 已提交
33 34 35
  SListNode *node;
  while ((node = TD_DLIST_HEAD(list)) != NULL) {
    TD_DLIST_POP(list, node);
wafwerar's avatar
wafwerar 已提交
36
    taosMemoryFree(node);
H
TD-34  
hzcheng 已提交
37 38 39
  }
}

H
Hongze Cheng 已提交
40
void *tdListFree(SList *list) {
H
TD-353  
Hongze Cheng 已提交
41 42
  if (list) {
    tdListEmpty(list);
wafwerar's avatar
wafwerar 已提交
43
    taosMemoryFree(list);
H
TD-353  
Hongze Cheng 已提交
44
  }
H
Hongze Cheng 已提交
45 46

  return NULL;
H
TD-34  
hzcheng 已提交
47 48
}

H
Hongze Cheng 已提交
49
void tdListPrependNode(SList *list, SListNode *node) { TD_DLIST_PREPEND(list, node); }
H
TD-34  
hzcheng 已提交
50

H
Hongze Cheng 已提交
51
void tdListAppendNode(SList *list, SListNode *node) { TD_DLIST_APPEND(list, node); }
H
TD-34  
hzcheng 已提交
52

S
tlist  
Shengliang Guan 已提交
53
int32_t tdListPrepend(SList *list, void *data) {
wafwerar's avatar
wafwerar 已提交
54
  SListNode *node = (SListNode *)taosMemoryMalloc(sizeof(SListNode) + list->eleSize);
H
TD-34  
hzcheng 已提交
55 56 57
  if (node == NULL) return -1;

  memcpy((void *)(node->data), data, list->eleSize);
H
Hongze Cheng 已提交
58
  TD_DLIST_PREPEND(list, node);
H
TD-34  
hzcheng 已提交
59 60 61 62

  return 0;
}

H
Haojun Liao 已提交
63
int32_t tdListAppend(SList *list, const void *data) {
wafwerar's avatar
wafwerar 已提交
64
  SListNode *node = (SListNode *)taosMemoryCalloc(1, sizeof(SListNode) + list->eleSize);
H
TD-34  
hzcheng 已提交
65 66 67
  if (node == NULL) return -1;

  memcpy((void *)(node->data), data, list->eleSize);
H
Hongze Cheng 已提交
68
  TD_DLIST_APPEND(list, node);
H
TD-34  
hzcheng 已提交
69

H
TD-34  
hzcheng 已提交
70 71 72 73
  return 0;
}

SListNode *tdListPopHead(SList *list) {
H
Hongze Cheng 已提交
74 75 76 77 78 79
  SListNode *node;

  node = TD_DLIST_HEAD(list);

  if (node) {
    TD_DLIST_POP(list, node);
H
TD-34  
hzcheng 已提交
80
  }
H
Hongze Cheng 已提交
81

H
TD-34  
hzcheng 已提交
82 83 84 85
  return node;
}

SListNode *tdListPopTail(SList *list) {
H
Hongze Cheng 已提交
86
  SListNode *node;
H
TD-34  
hzcheng 已提交
87

H
Hongze Cheng 已提交
88 89 90
  node = TD_DLIST_TAIL(list);
  if (node) {
    TD_DLIST_POP(list, node);
H
Haojun Liao 已提交
91 92
  }

H
Hongze Cheng 已提交
93
  return node;
H
Haojun Liao 已提交
94 95
}

H
Hongze Cheng 已提交
96
SListNode *tdListGetHead(SList *list) { return TD_DLIST_HEAD(list); }
H
Haojun Liao 已提交
97

98
SListNode *tdListGetTail(SList *list) { return TD_DLIST_TAIL(list); }
H
Haojun Liao 已提交
99

H
TD-34  
hzcheng 已提交
100
SListNode *tdListPopNode(SList *list, SListNode *node) {
H
Hongze Cheng 已提交
101
  TD_DLIST_POP(list, node);
H
TD-34  
hzcheng 已提交
102
  return node;
H
TD-34  
hzcheng 已提交
103 104
}

H
TD-34  
hzcheng 已提交
105 106 107
// Move all node elements from src to dst, the dst is assumed as an empty list
void tdListMove(SList *src, SList *dst) {
  // assert(dst->eleSize == src->eleSize);
H
TD-34  
hzcheng 已提交
108 109 110 111
  SListNode *node = NULL;
  while ((node = tdListPopHead(src)) != NULL) {
    tdListAppendNode(dst, node);
  }
H
TD-34  
hzcheng 已提交
112 113
}

H
TD-353  
Hongze Cheng 已提交
114 115
void tdListDiscard(SList *list) {
  if (list) {
H
Hongze Cheng 已提交
116 117
    listHead(list) = listTail(list) = NULL;
    listNEles(list) = 0;
H
TD-353  
Hongze Cheng 已提交
118 119 120
  }
}

H
Hongze Cheng 已提交
121
void tdListNodeGetData(SList *list, SListNode *node, void *target) { memcpy(target, node->data, listEleSize(list)); }
H
TD-34  
hzcheng 已提交
122 123 124 125

void tdListInitIter(SList *list, SListIter *pIter, TD_LIST_DIRECTION_T direction) {
  pIter->direction = direction;
  if (direction == TD_LIST_FORWARD) {
H
Hongze Cheng 已提交
126
    pIter->next = listHead(list);
H
TD-34  
hzcheng 已提交
127
  } else {
H
Hongze Cheng 已提交
128
    pIter->next = listTail(list);
H
TD-34  
hzcheng 已提交
129 130 131 132 133 134 135
  }
}

SListNode *tdListNext(SListIter *pIter) {
  SListNode *node = pIter->next;
  if (node == NULL) return NULL;
  if (pIter->direction == TD_LIST_FORWARD) {
H
Hongze Cheng 已提交
136
    pIter->next = TD_DLIST_NODE_NEXT(node);
H
TD-34  
hzcheng 已提交
137
  } else {
H
Hongze Cheng 已提交
138
    pIter->next = TD_DLIST_NODE_PREV(node);
H
TD-34  
hzcheng 已提交
139 140 141 142
  }

  return node;
}