TDengine/source/util/src/tlist.c

199 lines
4.4 KiB
C
Raw Normal View History

2020-03-20 06:38:13 +00:00
/*
* 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/>.
*/
2021-05-09 07:47:07 +00:00
#include "os.h"
2020-03-20 06:38:13 +00:00
#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;
2020-03-21 04:13:06 +00:00
return list;
2020-03-20 06:38:13 +00:00
}
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;
}
2021-01-13 09:09:32 +00:00
void *tdListFree(SList *list) {
2020-06-18 08:00:03 +00:00
if (list) {
tdListEmpty(list);
free(list);
}
2021-01-13 09:09:32 +00:00
return NULL;
2020-03-20 06:38:13 +00:00
}
2020-03-20 15:02:42 +00:00
void tdListPrependNode(SList *list, SListNode *node) {
2020-03-20 06:38:13 +00:00
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++;
}
2020-03-20 15:02:42 +00:00
void tdListAppendNode(SList *list, SListNode *node) {
2020-03-20 06:38:13 +00:00
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++;
2020-03-20 15:02:42 +00:00
}
int tdListPrepend(SList *list, void *data) {
SListNode *node = (SListNode *)malloc(sizeof(SListNode) + list->eleSize);
if (node == NULL) return -1;
2020-07-22 06:02:23 +00:00
node->next = node->prev = NULL;
2020-03-20 15:02:42 +00:00
memcpy((void *)(node->data), data, list->eleSize);
tdListPrependNode(list, node);
return 0;
}
int tdListAppend(SList *list, void *data) {
2020-06-18 16:10:50 +00:00
SListNode *node = (SListNode *)calloc(1, sizeof(SListNode) + list->eleSize);
2020-03-20 15:02:42 +00:00
if (node == NULL) return -1;
memcpy((void *)(node->data), data, list->eleSize);
tdListAppendNode(list, node);
2020-03-20 06:38:13 +00:00
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--;
2020-03-29 11:19:51 +00:00
node->next = NULL;
node->prev = NULL;
2020-03-20 06:38:13 +00:00
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--;
2020-03-29 11:19:51 +00:00
node->next = node->prev = NULL;
2020-03-20 06:38:13 +00:00
return node;
}
2020-07-27 07:32:53 +00:00
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;
}
2020-03-20 06:38:13 +00:00
SListNode *tdListPopNode(SList *list, SListNode *node) {
2020-03-20 15:02:42 +00:00
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--;
2020-03-29 11:19:51 +00:00
node->next = node->prev = NULL;
2020-03-20 15:02:42 +00:00
return node;
2020-03-20 06:38:13 +00:00
}
2020-03-21 07:36:23 +00:00
// 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);
2020-03-29 07:01:00 +00:00
SListNode *node = NULL;
while ((node = tdListPopHead(src)) != NULL) {
tdListAppendNode(dst, node);
}
2020-03-21 07:36:23 +00:00
}
2020-06-12 10:52:48 +00:00
void tdListDiscard(SList *list) {
if (list) {
list->head = list->tail = NULL;
list->numOfEles = 0;
}
}
2020-03-21 04:13:06 +00:00
void tdListNodeGetData(SList *list, SListNode *node, void *target) { memcpy(target, node->data, list->eleSize); }
2020-03-20 07:56:44 +00:00
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;
}