-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked_List.h
449 lines (391 loc) · 11.4 KB
/
Linked_List.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
MIT License
Copyright (c) 2020 github0null
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef _H_C_List
#define _H_C_List
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
//
// list config
//
#if defined __has_include
#if __has_include("list_conf.h")
#include "list_conf.h"
#endif
#else
#include "list_conf.h"
#endif
#ifndef List_nullptr
#define List_nullptr ((void *)0)
#endif
#ifndef List_Inline
#define List_Inline
#endif
#ifndef List_mem_alloc
#define List_mem_alloc malloc
#endif
#ifndef List_mem_free
#define List_mem_free free
#endif
#ifdef LIST_THREAD_SAFED
#ifndef List_MutexNew
#error "We need 'List_MutexNew' in os !"
#endif
#ifndef List_MutexFree
#error "We need 'List_MutexFree' in os !"
#endif
#ifndef List_MutexAcquire
#error "We need 'List_MutexAcquire' in os !"
#endif
#ifndef List_MutexRelease
#error "We need 'List_MutexRelease' in os !"
#endif
#endif
//
// list define
//
typedef struct _ListNode {
struct _ListNode *prev;
struct _ListNode *next;
void *data;
} ListNode_t;
typedef struct List_t List_t;
//
// callback function type
//
/**
* @brief A List Node Comparer Callbk
*
* @param dat1 The data_1's pointer which will be compared
* @param dat2 The data_2's pointer which will be compared
*
* @return if ret == 0, dat1 == dat2;
* if ret > 0, dat1 > dat2;
* if ret < 0, dat1 < dat2;
*/
typedef int (*ListNodeComparer_t)(void *dat1, void *dat2);
/**
* @brief A List Node Matcher Callbk
*
* @param dat The data pointer
* @param params The user context data
*
* @return If true, match success, otherwise match failed.
*/
typedef bool (*ListNodeMatcher_t)(void *dat, void *params);
/**
* @brief A Data Destructor Callbk for 'List_CreateList(...)'
*
* @param dat The data pointer which will be freed
*
* @return none
*/
typedef void (*ListDataDestructor_t)(void *dat);
/**
* @brief A Visitor Callbk for 'List_Traverse(...)'
*
* @param dat The data pointer
* @param params The user context data, can be passed by 'List_Traverse(...)'
*
* @return If false, the traverse will be breaked, end early
*/
typedef bool (*ListVisitor_t)(void *dat, void *params);
//
// functions
//
/**
* @brief Create list
*
* @param destructor A data destructor callback function,
* will be called when use 'List_DestroyList' to destroy your list;
* If this params is NULL, we will use default destructor
* (!!! default destructor will do nothing for your data !!!)
*
* @return List_t* A list
*/
List_t *List_CreateList(ListDataDestructor_t destructor);
/**
* @brief Destroy list
*
* @param list The list pointer that will be freed
*/
void List_DestroyList(List_t *list);
/**
* @brief Get node data
*
* @param node The target list node
* @param type The node user data type
*
* @return data pointer <user data type> *
*/
#define List_GetNodeData(node, type) ((type *)((node) == List_nullptr ? List_nullptr : (node)->data))
/**
* @brief Get prev node data
*
* @param node The target list node (can't be NULL !!!)
* @param type The node user data type
*
* @return data pointer <user data type> *
*/
#define List_GetPrevData(node, type) ((type *)((node)->prev == List_nullptr ? List_nullptr : (node)->prev->data))
/**
* @brief Get next node data
*
* @param node The target list node (can't be NULL !!!)
* @param type The node user data type
*
* @return data pointer <user data type> *
*/
#define List_GetNextData(node, type) ((type *)((node)->next == List_nullptr ? List_nullptr : (node)->next->data))
/**
* @brief Insert a node at front of a list
*
* @param list The target list
* @param data A data pointer for new node
*
* @return ListNode_t* The new node
*/
ListNode_t *List_Prepend(List_t *list, void *data);
/**
* @brief Push a node at end of a list
*
* @param list The target list
* @param data A data pointer for new node
*
* @return ListNode_t* The new node
*/
ListNode_t *List_Push(List_t *list, void *data);
/**
* @brief Pop the last node of a list
*
* @param list The target list
*
* @return ListNode_t* The last node
*/
ListNode_t *List_Pop(List_t *list);
/**
* @brief Enqueue a node at end of a list (Equal to 'List_Push')
*
* @param list The target list
* @param data A data pointer for new node
*
* @return ListNode_t* The new node
*/
ListNode_t *List_Enqueue(List_t *list, void *data);
/**
* @brief Dequeue the first node at a list
*
* @param list The target list
*
* @return ListNode_t* The first node
*/
ListNode_t *List_Dequeue(List_t *list);
/**
* @brief Foreach a list
*
* @note !!! Don't remove the current element in foreach;
* If you want to remove current element, please use 'List_ForeachSafe'
*
* @param list The target list
* @param ele The current ListNode_t
*/
#define List_Foreach(list, ele) \
for (ele = List_First(list); \
ele != NULL; \
ele = ele->next)
/**
* @brief Foreach a list in reverse order
*
* @note !!! Don't remove the current element in foreach;
* If you want to remove current element, please use 'List_ForeachSafe'
*
* @param list The target list
* @param ele The current ListNode_t
*/
#define List_ForeachReverse(list, ele) \
for (ele = List_Last(list); \
ele != NULL; \
ele = ele->prev)
/**
* @brief Foreach a list (allow remove/delete current element)
*
* @param list The target list
* @param ele The current list node pointer
* @param tmp A temporary variable
*/
#define List_ForeachSafe(list, ele, tmp) \
for (ele = List_First(list), tmp = (ele != NULL ? ele->next : NULL); \
ele != NULL; \
ele = tmp, tmp = (tmp != NULL ? tmp->next : NULL))
/**
* @brief Foreach a list in reverse order (allow remove/delete current element)
*
* @param list The target list
* @param ele The current list node pointer
* @param tmp A temporary variable
*/
#define List_ForeachSafeReverse(list, ele, tmp) \
for (ele = List_Last(list), tmp = (ele != NULL ? ele->prev : NULL); \
ele != NULL; \
ele = tmp, tmp = (tmp != NULL ? tmp->prev : NULL))
/**
* @brief Find the first match node in a list
*
* @param list The target list
* @param matcher A node matcher, will be called for every node
* @param params User context data
*
* @return ListNode_t* The target node, if not found, return NULL
*/
ListNode_t *List_FindFirst(List_t *list, ListNodeMatcher_t matcher, void *params);
/**
* @brief Find the next match node in a list
*
* @param list The target list
* @param node The last matched node
* @param matcher A node matcher, will be called for every node
* @param params User context data
*
* @return ListNode_t* The target node, if not found, return NULL
*/
ListNode_t *List_FindNext(List_t *list, ListNode_t *node, ListNodeMatcher_t matcher, void *params);
/**
* @brief Get the first node at a list
*
* @param list The target list
*
* @return ListNode_t* The first node
*/
ListNode_t *List_First(List_t *list);
/**
* @brief Get the last node at a list
*
* @param list The target list
*
* @return ListNode_t* The last node
*/
ListNode_t *List_Last(List_t *list);
/**
* @brief Get list length
*
* @param list The target list
*
* @return uint32_t
*/
uint32_t List_Length(List_t *list);
/**
* @brief Check whether the list is empty
*
* @param list The target list
*
* @return true The list is empty
* @return false The list is not empty
*/
bool List_IsEmpty(List_t *list);
/**
* @brief Get the number of the matched nodes
*
* @param list The target list
* @param matcher A node matcher, will be called for every node
* @param params User context data
*
* @return uint32_t The number of the matched nodes
*/
uint32_t List_Count(List_t *list, ListNodeMatcher_t matcher, void *params);
/**
* @brief Insert a new node at the end of a existed node
*
* @param list The target list
* @param node The target existed node
* @param data A data pointer for new node
*
* @return ListNode_t* The new node
*/
ListNode_t *List_InsertNode(List_t *list, ListNode_t *node, void *data);
/**
* @brief Insert a new node before a existed node
*
* @param list The target list
* @param node The target existed node
* @param data A data pointer for new node
*
* @return ListNode_t* The new node
*/
ListNode_t *List_InsertNodeBefore(List_t *list, ListNode_t *node, void *data);
/**
* @brief Remove a node from target list (without free the node memory)
*
* @param list The target list
* @param node The target existed node
*
* @return ListNode_t* The removed node
*/
ListNode_t *List_RemoveNode(List_t *list, ListNode_t *node);
/**
* @brief Remove all node and destroy the memory for every node
*
* @param list The target list
*/
void List_Clear(List_t *list);
/**
* @brief Remove a node and destroy the node and user data memory
*
* @param list The target list
* @param node The target node
*/
void List_DeleteNode(List_t *list, ListNode_t *node);
/**
* @brief Remove a node and destroy the node memory (you can choose to free user data memory)
*
* @param list The target list
* @param node The target node
* @param free_user_data Free user data pointer by 'ListDataDestructor_t'
*
* @return If don't free user data (free_user_data==false), will return the node->data pointer, otherwise will return NULL
*/
void *List_DeleteNode2(List_t *list, ListNode_t *node, bool free_user_data);
/**
* @brief Remove a matched node and destroy the node and user data memory
*
* @param list The target list
* @param matcher A node matcher, will be called for every node
* @param params User context data
*/
void List_DeleteMatched(List_t *list, ListNodeMatcher_t matcher, void *params);
/**
* @brief Foreach a list with a visitor callback
*
* @param list The target list
* @param visitor A node visitor, will be called for every node
* @param params User context data
* @param isReverse If true, we will traverse the list in reverse order
*/
void List_Traverse(List_t *list, ListVisitor_t visitor, void *params, bool isReverse);
/**
* @brief QuickSort a list (ascending order)
*
* @note This function will swap the node data pointer to sort the list,
* so after sort done, the data pointer of the list node will be changed !
*
* @param list The target list
* @param comparer A node comparer, used to compare two node
*/
void List_QuickSort(List_t *list, ListNodeComparer_t comparer);
#endif