1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef LIST_H
17 #define LIST_H
18 
19 #include <stdint.h>
20 #include <stdbool.h>
21 
22 #ifndef NO_SANITIZE
23 #ifdef __has_attribute
24 #if __has_attribute(no_sanitize)
25 #define NO_SANITIZE(type) __attribute__((no_sanitize(type)))
26 #endif
27 #endif
28 #endif
29 
30 #ifndef NO_SANITIZE
31 #define NO_SANITIZE(type)
32 #endif
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 typedef struct List List;
39 typedef struct ListNode ListNode;
40 typedef void (*FreeDataCb)(void *data);
41 typedef bool (*ListCmpFunc)(void *nodeData, void *parameter);
42 
43 /**
44  * @brief Perform instantiation of the list.
45  *
46  * @param cb list free data callback.
47  * @return List pointer.
48  * @since 6
49  */
50 List *ListCreate(FreeDataCb cb);
51 
52 /**
53  * @brief Clear all nodes in list.
54  *
55  * @param list List pointer.
56  * @since 6
57  */
58 void ListClear(List *list);
59 
60 /**
61  * @brief Delete the given list.
62  *
63  * @param list List pointer.
64  * @since 6
65  */
66 void ListDelete(List *list);
67 
68 /**
69  * @brief Return the length of the list.
70  *
71  * @param list List pointer.
72  * @return List size.
73  * @since 6
74  */
75 int32_t ListGetSize(const List *list);
76 
77 /**
78  * @brief Append node to the beginning of the list with the given data.
79  *
80  * @param list List pointer.
81  * @param data Node data.
82  * @since 6
83  */
84 void ListAddFirst(List *list, void *data);
85 
86 /**
87  * @brief Append node to the end of the list with the given data.
88  *
89  * @param list List pointer.
90  * @param data Node data.
91  * @since 6
92  */
93 void ListAddLast(List *list, void *data);
94 
95 /**
96  * @brief Execute compare-function for each node in the list, until compare-function return true.
97  *
98  * @param list List pointer.
99  * @param cmp Compare function.
100  * @param parameter Compare parameter.
101  * @return If ListCmpFunc return true, return Corresponding node's data.
102  *               Else if ListCmpFunc always return false all the time, return NULL.
103  * @since 6
104  */
105 void *ListForEachData(const List *list, const ListCmpFunc cmp, void *parameter);
106 
107 /**
108  * @brief Return the head node of the list. If the list is empty, return NULL.
109  *
110  * @param list List pointer.
111  * @return ListNode pointer.
112  * @since 6
113  */
114 ListNode *ListGetFirstNode(const List *list);
115 
116 /**
117  * @brief Return the tail node of the list. If the list is empty, return NULL.
118  *
119  * @param list List pointer.
120  * @return ListNode pointer.
121  * @since 6
122  */
123 ListNode *ListGetLastNode(const List *list);
124 
125 /**
126  * @brief Return the next node of listNode. If next node is null return NULL.
127  *
128  * @param[in] listNode ListNode pointer.
129  * @return ListNode* Next listNode pointer.
130  * @since 6
131  */
132 ListNode *ListGetNextNode(const ListNode *listNode);
133 
134 /**
135  * @brief Return data stored in the given node.
136  *
137  * @param node ListNode pointer.
138  * @return void* ListNode data.
139  * @since 6
140  */
141 void *ListGetNodeData(const ListNode *node);
142 
143 /**
144  * @brief Delete the node with the given data.
145  *
146  * @param list List pointer.
147  * @param data Data pointer.
148  * @return true: Success remove data from list. false: Failed remove data from list.
149  * @since 6
150  */
151 bool ListRemoveNode(List *list, void *data);
152 
153 /**
154  * @brief Delete the first node of the list.
155  *
156  * @param list List pointer.
157  * @return true: Success remove list first node. false: Failed remove list first node.
158  * @since 6
159  */
160 bool ListRemoveFirst(List *list);
161 
162 /**
163  * @brief Delete the last node of the list.
164  *
165  * @param list List pointer.
166  * @return true: Success remove list last node. false: Failed remove list last node.
167  * @since 6
168  */
169 bool ListRemoveLast(List *list);
170 
171 /**
172  * @brief Check if the list is empty.
173  *
174  * @param list List pointer.
175  * @return true: The list is empty. false: The list is not empty.
176  * @since 6
177  */
178 bool ListIsEmpty(const List* list);
179 
180 #ifdef __cplusplus
181 }
182 #endif
183 
184 #endif  // LIST_H
185