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 \
30 : { \
31 : NULL, NULL \
32 : }
33 : #define CSingleListNext(curNode) ((curNode) != NULL) ? (curNode)->next : NULL
34 :
35 : /**
36 : * Through this macro, the traversal of the linked list can be realized.
37 : * The user needs to define the loop variables curNode, nextNode,
38 : * and the loop variable does not need to be initialized.
39 : */
40 : #define CSingleListForEach(list, curNode, nextNode) \
41 : for ((curNode) = (list)->head, (nextNode) = CSingleListNext(curNode); (curNode) != NULL; \
42 : (curNode) = (nextNode), (nextNode) = CSingleListNext(nextNode))
43 :
44 0 : static inline void InitCSingleList(CSingleList* list)
45 : {
46 0 : list->head = NULL;
47 0 : list->tail = NULL;
48 0 : }
49 :
50 0 : static inline bool IsSingleListEmpty(CSingleList* list) { return list->head == NULL; }
51 :
52 0 : static inline CSingleListNode* GetSingleListHead(CSingleList* list) { return list->head; }
53 :
54 : static inline void InsertCSingleListHead(CSingleList* list, CSingleListNode* insertNode)
55 : {
56 : insertNode->next = list->head;
57 : list->head = insertNode;
58 : if (list->tail == NULL) {
59 : list->tail = insertNode;
60 : }
61 : }
62 :
63 0 : static inline void InsertCSingleListTail(CSingleList* list, CSingleListNode* insertNode)
64 : {
65 0 : insertNode->next = NULL;
66 0 : if (list->tail == NULL) {
67 0 : list->head = insertNode;
68 : } else {
69 0 : list->tail->next = insertNode;
70 : }
71 0 : list->tail = insertNode;
72 0 : }
73 :
74 : static inline void InsertCSingleList(CSingleList* list, CSingleListNode* listNode, CSingleListNode* insertNode)
75 : {
76 : insertNode->next = listNode->next;
77 : listNode->next = insertNode;
78 : if (list->tail == listNode) {
79 : list->tail = insertNode;
80 : }
81 : }
82 :
83 0 : static inline void RemoveCSingleListNode(CSingleList* list, CSingleListNode* listNode)
84 : {
85 : CSingleListNode* curNode;
86 : CSingleListNode* nextNode;
87 0 : CSingleListNode* preNode = NULL;
88 0 : CSingleListForEach(list, curNode, nextNode)
89 : {
90 0 : if (curNode != listNode) {
91 0 : preNode = curNode;
92 0 : continue;
93 : }
94 :
95 0 : if (preNode == NULL) {
96 0 : list->head = nextNode;
97 0 : if (list->head == NULL) {
98 0 : list->tail = NULL;
99 : }
100 : } else {
101 0 : preNode->next = curNode->next;
102 0 : if (nextNode == NULL) {
103 0 : list->tail = preNode;
104 : }
105 : }
106 : }
107 0 : }
108 :
109 0 : static inline void RemoveCSingleListHead(CSingleList* list) { RemoveCSingleListNode(list, list->head); }
110 :
111 : static inline void RemoveCSingleListTail(CSingleList* list) { RemoveCSingleListNode(list, list->tail); }
112 : #ifdef __cplusplus
113 : }
114 : #endif
115 : #endif // C_BASE_SINGLE_LIST_H
|