tlist.c 4.4 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
TD-4088  
Shengliang Guan 已提交
16
#include "os.h"
H
TD-34  
hzcheng 已提交
17 18 19 20 21 22 23 24 25
#include "tlist.h"

SList *tdListNew(int eleSize) {
  SList *list = (SList *)malloc(sizeof(SList));
  if (list == NULL) return NULL;

  list->eleSize = eleSize;
  list->numOfEles = 0;
  list->head = list->tail = NULL;
H
TD-34  
hzcheng 已提交
26
  return list;
H
TD-34  
hzcheng 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39
}

void tdListEmpty(SList *list) {
  SListNode *node = list->head;
  while (node) {
    list->head = node->next;
    free(node);
    node = list->head;
  }
  list->head = list->tail = 0;
  list->numOfEles = 0;
}

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

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

H
TD-34  
hzcheng 已提交
49
void tdListPrependNode(SList *list, SListNode *node) {
H
TD-34  
hzcheng 已提交
50 51 52 53 54 55 56 57 58 59 60 61
  if (list->head == NULL) {
    list->head = node;
    list->tail = node;
  } else {
    node->next = list->head;
    node->prev = NULL;
    list->head->prev = node;
    list->head = node;
  }
  list->numOfEles++;
}

H
TD-34  
hzcheng 已提交
62
void tdListAppendNode(SList *list, SListNode *node) {
H
TD-34  
hzcheng 已提交
63 64 65 66 67 68 69 70 71 72 73
  if (list->head == NULL) {
    list->head = node;
    list->tail = node;
  } else {
    node->prev = list->tail;
    node->next = NULL;
    list->tail->next = node;
    list->tail = node;
  }

  list->numOfEles++;
H
TD-34  
hzcheng 已提交
74 75 76 77 78 79
}

int tdListPrepend(SList *list, void *data) {
  SListNode *node = (SListNode *)malloc(sizeof(SListNode) + list->eleSize);
  if (node == NULL) return -1;

H
Haojun Liao 已提交
80
  node->next = node->prev = NULL;
H
TD-34  
hzcheng 已提交
81 82 83 84 85 86 87
  memcpy((void *)(node->data), data, list->eleSize);
  tdListPrependNode(list, node);

  return 0;
}

int tdListAppend(SList *list, void *data) {
H
TD-353  
Hongze Cheng 已提交
88
  SListNode *node = (SListNode *)calloc(1, sizeof(SListNode) + list->eleSize);
H
TD-34  
hzcheng 已提交
89 90 91 92 93
  if (node == NULL) return -1;

  memcpy((void *)(node->data), data, list->eleSize);
  tdListAppendNode(list, node);

H
TD-34  
hzcheng 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106
  return 0;
}

SListNode *tdListPopHead(SList *list) {
  if (list->head == NULL) return NULL;
  SListNode *node = list->head;
  if (node->next == NULL) {
    list->head = NULL;
    list->tail = NULL;
  } else {
    list->head = node->next;
  }
  list->numOfEles--;
H
TD-34  
hzcheng 已提交
107 108
  node->next = NULL;
  node->prev = NULL;
H
TD-34  
hzcheng 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121
  return node;
}

SListNode *tdListPopTail(SList *list) {
  if (list->tail == NULL) return NULL;
  SListNode *node = list->tail;
  if (node->prev == NULL) {
    list->head = NULL;
    list->tail = NULL;
  } else {
    list->tail = node->prev;
  }
  list->numOfEles--;
H
TD-34  
hzcheng 已提交
122
  node->next = node->prev = NULL;
H
TD-34  
hzcheng 已提交
123 124 125
  return node;
}

H
Haojun Liao 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
SListNode *tdListGetHead(SList *list) {
  if (list == NULL || list->numOfEles == 0) {
    return NULL;
  }

  return list->head;
}

SListNode *tsListGetTail(SList *list) {
  if (list == NULL || list->numOfEles == 0) {
    return NULL;
  }

  return list->tail;
}

H
TD-34  
hzcheng 已提交
142
SListNode *tdListPopNode(SList *list, SListNode *node) {
H
TD-34  
hzcheng 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156
  if (list->head == node) {
    list->head = node->next;
  }
  if (list->tail == node) {
    list->tail = node->prev;
  }

  if (node->prev != NULL) {
    node->prev->next = node->next;
  }
  if (node->next != NULL) {
    node->next->prev = node->prev;
  }
  list->numOfEles--;
H
TD-34  
hzcheng 已提交
157
  node->next = node->prev = NULL;
H
TD-34  
hzcheng 已提交
158 159

  return node;
H
TD-34  
hzcheng 已提交
160 161
}

H
TD-34  
hzcheng 已提交
162 163 164
// 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 已提交
165 166 167 168
  SListNode *node = NULL;
  while ((node = tdListPopHead(src)) != NULL) {
    tdListAppendNode(dst, node);
  }
H
TD-34  
hzcheng 已提交
169 170
}

H
TD-353  
Hongze Cheng 已提交
171 172 173 174 175 176 177
void tdListDiscard(SList *list) {
  if (list) {
    list->head = list->tail = NULL;
    list->numOfEles = 0;
  }
}

H
TD-34  
hzcheng 已提交
178
void tdListNodeGetData(SList *list, SListNode *node, void *target) { memcpy(target, node->data, list->eleSize); }
H
TD-34  
hzcheng 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

void tdListInitIter(SList *list, SListIter *pIter, TD_LIST_DIRECTION_T direction) {
  pIter->direction = direction;
  if (direction == TD_LIST_FORWARD) {
    pIter->next = list->head;
  } else {
    pIter->next = list->tail;
  }
}

SListNode *tdListNext(SListIter *pIter) {
  SListNode *node = pIter->next;
  if (node == NULL) return NULL;
  if (pIter->direction == TD_LIST_FORWARD) {
    pIter->next = node->next;
  } else {
    pIter->next = node->prev;
  }

  return node;
}