tlist.c 4.5 KB
Newer Older
H
TD-34  
hzcheng 已提交
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
/*
 * 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 <stdlib.h>
#include <string.h>

#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 已提交
27
  return list;
H
TD-34  
hzcheng 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40
}

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 已提交
41
void *tdListFree(SList *list) {
H
TD-353  
Hongze Cheng 已提交
42 43 44 45
  if (list) {
    tdListEmpty(list);
    free(list);
  }
H
Hongze Cheng 已提交
46 47

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

H
TD-34  
hzcheng 已提交
50
void tdListPrependNode(SList *list, SListNode *node) {
H
TD-34  
hzcheng 已提交
51 52 53 54 55 56 57 58 59 60 61 62
  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 已提交
63
void tdListAppendNode(SList *list, SListNode *node) {
H
TD-34  
hzcheng 已提交
64 65 66 67 68 69 70 71 72 73 74
  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 已提交
75 76 77 78 79 80
}

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

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

  return 0;
}

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

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

H
TD-34  
hzcheng 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107
  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 已提交
108 109
  node->next = NULL;
  node->prev = NULL;
H
TD-34  
hzcheng 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122
  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 已提交
123
  node->next = node->prev = NULL;
H
TD-34  
hzcheng 已提交
124 125 126
  return node;
}

H
Haojun Liao 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
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 已提交
143
SListNode *tdListPopNode(SList *list, SListNode *node) {
H
TD-34  
hzcheng 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157
  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 已提交
158
  node->next = node->prev = NULL;
H
TD-34  
hzcheng 已提交
159 160

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

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

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

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

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;
}