tlist.c 4.0 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 41 42 43 44 45
}

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

void tdListFree(SList *list) {
  tdListEmpty(list);
  free(list);
}

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

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

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

  return 0;
}

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

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

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

SListNode *tdListPopNode(SList *list, SListNode *node) {
H
TD-34  
hzcheng 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136
  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 已提交
137
  node->next = node->prev = NULL;
H
TD-34  
hzcheng 已提交
138 139

  return node;
H
TD-34  
hzcheng 已提交
140 141
}

H
TD-34  
hzcheng 已提交
142 143 144
// 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 已提交
145 146 147 148
  SListNode *node = NULL;
  while ((node = tdListPopHead(src)) != NULL) {
    tdListAppendNode(dst, node);
  }
H
TD-34  
hzcheng 已提交
149 150
}

H
TD-34  
hzcheng 已提交
151
void tdListNodeGetData(SList *list, SListNode *node, void *target) { memcpy(target, node->data, list->eleSize); }
H
TD-34  
hzcheng 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

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