Line data Source code
1 : /** 2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd. 3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of 4 : * CANN Open Software License Agreement Version 2.0 (the "License"). 5 : * Please refer to the License for details. You may not use this file except in compliance with the License. 6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. 8 : * See LICENSE in the root of the software repository for the full text of the License. 9 : */ 10 : #ifndef C_BASE_SINGLE_LIST_H 11 : #define C_BASE_SINGLE_LIST_H 12 : #include "c_base.h" 13 : #ifdef __cplusplus 14 : extern "C" { 15 : #endif 16 : typedef struct CSingleListNode { 17 : struct CSingleListNode *next; 18 : } CSingleListNode; 19 : 20 : typedef struct { 21 : CSingleListNode *head; 22 : CSingleListNode *tail; 23 : } CSingleList; 24 : 25 : /** 26 : * You can use this macro to initialize the linked list while defining it, 27 : * or use the function InitCSingleList to initialize it before use 28 : */ 29 : #define CSINGLE_LIST_INIT {NULL, NULL} 30 : #define CSingleListNext(curNode) ((curNode) != NULL) ? (curNode)->next : NULL 31 : 32 : /** 33 : * Through this macro, the traversal of the linked list can be realized. 34 : * The user needs to define the loop variables curNode, nextNode, 35 : * and the loop variable does not need to be initialized. 36 : */ 37 : #define CSingleListForEach(list, curNode, nextNode) \ 38 : for ((curNode) = (list)->head, (nextNode) = CSingleListNext(curNode); (curNode) != NULL; \ 39 : (curNode) = (nextNode), (nextNode) = CSingleListNext(nextNode)) 40 : 41 0 : static inline void InitCSingleList(CSingleList *list) 42 : { 43 0 : list->head = NULL; 44 0 : list->tail = NULL; 45 0 : } 46 : 47 0 : static inline bool IsSingleListEmpty(CSingleList *list) 48 : { 49 0 : return list->head == NULL; 50 : } 51 : 52 0 : static inline CSingleListNode* GetSingleListHead(CSingleList *list) 53 : { 54 0 : return list->head; 55 : } 56 : 57 : static inline void InsertCSingleListHead(CSingleList *list, CSingleListNode *insertNode) 58 : { 59 : insertNode->next = list->head; 60 : list->head = insertNode; 61 : if (list->tail == NULL) { 62 : list->tail = insertNode; 63 : } 64 : } 65 : 66 0 : static inline void InsertCSingleListTail(CSingleList *list, CSingleListNode *insertNode) 67 : { 68 0 : insertNode->next = NULL; 69 0 : if (list->tail == NULL) { 70 0 : list->head = insertNode; 71 : } else { 72 0 : list->tail->next = insertNode; 73 : } 74 0 : list->tail = insertNode; 75 0 : } 76 : 77 : static inline void InsertCSingleList(CSingleList *list, CSingleListNode *listNode, CSingleListNode *insertNode) 78 : { 79 : insertNode->next = listNode->next; 80 : listNode->next = insertNode; 81 : if (list->tail == listNode) { 82 : list->tail = insertNode; 83 : } 84 : } 85 : 86 0 : static inline void RemoveCSingleListNode(CSingleList *list, CSingleListNode *listNode) 87 : { 88 : CSingleListNode *curNode; 89 : CSingleListNode *nextNode; 90 0 : CSingleListNode *preNode = NULL; 91 0 : CSingleListForEach(list, curNode, nextNode) { 92 0 : if (curNode != listNode) { 93 0 : preNode = curNode; 94 0 : continue; 95 : } 96 : 97 0 : if (preNode == NULL) { 98 0 : list->head = nextNode; 99 0 : if (list->head == NULL) { 100 0 : list->tail = NULL; 101 : } 102 : } else { 103 0 : preNode->next = curNode->next; 104 0 : if (nextNode == NULL) { 105 0 : list->tail = preNode; 106 : } 107 : } 108 : } 109 0 : } 110 : 111 0 : static inline void RemoveCSingleListHead(CSingleList *list) 112 : { 113 0 : RemoveCSingleListNode(list, list->head); 114 0 : } 115 : 116 : static inline void RemoveCSingleListTail(CSingleList *list) 117 : { 118 : RemoveCSingleListNode(list, list->tail); 119 : } 120 : #ifdef __cplusplus 121 : } 122 : #endif 123 : #endif // C_BASE_SINGLE_LIST_H