From e865255b203f07bd2a15d0de5145ee80eace1933 Mon Sep 17 00:00:00 2001 From: hzcheng Date: Fri, 20 Mar 2020 15:56:44 +0800 Subject: [PATCH] TD-34 --- src/util/inc/tlist.h | 12 ++++++++++-- src/util/src/tlist.c | 23 ++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/util/inc/tlist.h b/src/util/inc/tlist.h index 3f58c35e3b..cc99e183ed 100644 --- a/src/util/inc/tlist.h +++ b/src/util/inc/tlist.h @@ -19,6 +19,11 @@ extern "C" { #endif +typedef enum { + TD_LIST_FORWARD, + TD_LIST_BACKWARD +} TD_LIST_DIRECTION_T; + typedef struct _list_node { struct _list_node *next; struct _list_node *prev; @@ -33,7 +38,8 @@ typedef struct { } SList; typedef struct { - SListNode *node; + SListNode * next; + TD_LIST_DIRECTION_T direction; } SListIter; #define listHead(l) (l)->head @@ -52,7 +58,9 @@ SListNode *tdListPopHead(SList *list); SListNode *tdListPopTail(SList *list); SListNode *tdListPopNode(SList *list, SListNode *node); -void tdListNodeGetData(SList *list, SListNode *node, void *target); +void tdListNodeGetData(SList *list, SListNode *node, void *target); +void tdListInitIter(SList *list, SListIter *pIter, TD_LIST_DIRECTION_T direction); +SListNode *tdListNext(SListIter *pIter); #ifdef __cplusplus } diff --git a/src/util/src/tlist.c b/src/util/src/tlist.c index 42068a717e..a6cb98df5f 100644 --- a/src/util/src/tlist.c +++ b/src/util/src/tlist.c @@ -122,4 +122,25 @@ SListNode *tdListPopNode(SList *list, SListNode *node) { return node; } -void tdListNodeGetData(SList *list, SListNode *node, void *target) { memcpy(node->data, target, list->eleSize); } \ No newline at end of file +void tdListNodeGetData(SList *list, SListNode *node, void *target) { memcpy(node->data, target, list->eleSize); } + +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; +} \ No newline at end of file -- GitLab