blob: 30454533e1847680b09969aa82ca97110663b0e1 [file] [log] [blame]
James Kuszmaul82f6c042021-01-17 11:30:16 -08001/**
2 * @file re_list.h Interface to Linked List
3 *
4 * Copyright (C) 2010 Creytiv.com
5 */
6
7
8/** Linked-list element */
9struct le {
10 struct le *prev; /**< Previous element */
11 struct le *next; /**< Next element */
12 struct list *list; /**< Parent list (NULL if not linked-in) */
13 void *data; /**< User-data */
14};
15
16/** List Element Initializer */
17#define LE_INIT {NULL, NULL, NULL, NULL}
18
19
20/** Defines a linked list */
21struct list {
22 struct le *head; /**< First list element */
23 struct le *tail; /**< Last list element */
24};
25
26/** Linked list Initializer */
27#define LIST_INIT {NULL, NULL}
28
29
30/**
31 * Defines the list apply handler
32 *
33 * @param le List element
34 * @param arg Handler argument
35 *
36 * @return true to stop traversing, false to continue
37 */
38typedef bool (list_apply_h)(struct le *le, void *arg);
39
40/**
41 * Defines the list sort handler
42 *
43 * @param le1 Current list element
44 * @param le2 Next list element
45 * @param arg Handler argument
46 *
47 * @return true if sorted, otherwise false
48 */
49typedef bool (list_sort_h)(struct le *le1, struct le *le2, void *arg);
50
51
52void list_init(struct list *list);
53void list_flush(struct list *list);
54void list_clear(struct list *list);
55void list_append(struct list *list, struct le *le, void *data);
56void list_prepend(struct list *list, struct le *le, void *data);
57void list_insert_before(struct list *list, struct le *le, struct le *ile,
58 void *data);
59void list_insert_after(struct list *list, struct le *le, struct le *ile,
60 void *data);
61void list_unlink(struct le *le);
62void list_sort(struct list *list, list_sort_h *sh, void *arg);
63struct le *list_apply(const struct list *list, bool fwd, list_apply_h *ah,
64 void *arg);
65struct le *list_head(const struct list *list);
66struct le *list_tail(const struct list *list);
67uint32_t list_count(const struct list *list);
68
69
70/**
71 * Get the user-data from a list element
72 *
73 * @param le List element
74 *
75 * @return Pointer to user-data
76 */
77static inline void *list_ledata(const struct le *le)
78{
79 return le ? le->data : NULL;
80}
81
82
83static inline bool list_contains(const struct list *list, const struct le *le)
84{
85 return le ? le->list == list : false;
86}
87
88
89static inline bool list_isempty(const struct list *list)
90{
91 return list ? list->head == NULL : true;
92}
93
94
95#define LIST_FOREACH(list, le) \
96 for ((le) = list_head((list)); (le); (le) = (le)->next)