James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file re_list.h Interface to Linked List |
| 3 | * |
| 4 | * Copyright (C) 2010 Creytiv.com |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | /** Linked-list element */ |
| 9 | struct 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 */ |
| 21 | struct 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 | */ |
| 38 | typedef 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 | */ |
| 49 | typedef bool (list_sort_h)(struct le *le1, struct le *le2, void *arg); |
| 50 | |
| 51 | |
| 52 | void list_init(struct list *list); |
| 53 | void list_flush(struct list *list); |
| 54 | void list_clear(struct list *list); |
| 55 | void list_append(struct list *list, struct le *le, void *data); |
| 56 | void list_prepend(struct list *list, struct le *le, void *data); |
| 57 | void list_insert_before(struct list *list, struct le *le, struct le *ile, |
| 58 | void *data); |
| 59 | void list_insert_after(struct list *list, struct le *le, struct le *ile, |
| 60 | void *data); |
| 61 | void list_unlink(struct le *le); |
| 62 | void list_sort(struct list *list, list_sort_h *sh, void *arg); |
| 63 | struct le *list_apply(const struct list *list, bool fwd, list_apply_h *ah, |
| 64 | void *arg); |
| 65 | struct le *list_head(const struct list *list); |
| 66 | struct le *list_tail(const struct list *list); |
| 67 | uint32_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 | */ |
| 77 | static inline void *list_ledata(const struct le *le) |
| 78 | { |
| 79 | return le ? le->data : NULL; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | static inline bool list_contains(const struct list *list, const struct le *le) |
| 84 | { |
| 85 | return le ? le->list == list : false; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | static 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) |