1 #ifndef _LIST_H_ 2 #define _LIST_H_ 3 4 #include <stdbool.h> 5 #include <stddef.h> 6 struct list_node_t; 7 typedef struct list_node_t list_node_t; 8 9 struct list_t; 10 typedef struct list_t list_t; 11 12 typedef void (*list_free_cb)(void *data); 13 typedef bool (*list_iter_cb)(void *data, void *context); 14 15 // Returns a new, empty list. Returns NULL if not enough memory could be allocated 16 // for the list structure. The returned list must be freed with |list_free|. The 17 // |callback| specifies a function to be called whenever a list element is removed 18 // from the list. It can be used to release resources held by the list element, e.g. 19 // memory or file descriptor. |callback| may be NULL if no cleanup is necessary on 20 // element removal. 21 list_t *list_new(list_free_cb callback); 22 23 24 list_node_t *list_free_node(list_t *list, list_node_t *node); 25 26 // similar with list_free_node, this function doesn't free the node data 27 list_node_t *list_delete_node(list_t *list, list_node_t *node); 28 29 // Frees the list. This function accepts NULL as an argument, in which case it 30 // behaves like a no-op. 31 void list_free(list_t *list); 32 33 // Returns true if |list| is empty (has no elements), false otherwise. 34 // |list| may not be NULL. 35 bool list_is_empty(const list_t *list); 36 37 // Returns true if the list contains |data|, false otherwise. 38 // |list| may not be NULL. 39 bool list_contains(const list_t *list, const void *data); 40 41 // Returns list_node which contains |data|, NULL otherwise. 42 // |list| may not be NULL. 43 list_node_t *list_get_node(const list_t *list, const void *data); 44 45 // Returns the length of the |list|. |list| may not be NULL. 46 size_t list_length(const list_t *list); 47 48 // Returns the first element in the list without removing it. |list| may not 49 // be NULL or empty. 50 void *list_front(const list_t *list); 51 52 // Returns the last element in the list without removing it. |list| may not 53 // be NULL or empty. 54 void *list_back(const list_t *list); 55 list_node_t *list_back_node(const list_t *list); 56 57 // Inserts |data| after |prev_node| in |list|. |data|, |list|, and |prev_node| 58 // may not be NULL. This function does not make a copy of |data| so the pointer 59 // must remain valid at least until the element is removed from the list or the 60 // list is freed. Returns true if |data| could be inserted, false otherwise 61 // (e.g. out of memory). 62 bool list_insert_after(list_t *list, list_node_t *prev_node, void *data); 63 64 // Inserts |data| at the beginning of |list|. Neither |data| nor |list| may be NULL. 65 // This function does not make a copy of |data| so the pointer must remain valid 66 // at least until the element is removed from the list or the list is freed. 67 // Returns true if |data| could be inserted, false otherwise (e.g. out of memory). 68 bool list_prepend(list_t *list, void *data); 69 70 // Inserts |data| at the end of |list|. Neither |data| nor |list| may be NULL. 71 // This function does not make a copy of |data| so the pointer must remain valid 72 // at least until the element is removed from the list or the list is freed. 73 // Returns true if |data| could be inserted, false otherwise (e.g. out of memory). 74 bool list_append(list_t *list, void *data); 75 76 // Removes |data| from the list. Neither |list| nor |data| may be NULL. If |data| 77 // is inserted multiple times in the list, this function will only remove the first 78 // instance. If a free function was specified in |list_new|, it will be called back 79 // with |data|. This function returns true if |data| was found in the list and removed, 80 // false otherwise. 81 //list_node_t list_remove_node(list_t *list, list_node_t *prev_node, list_node_t *node); 82 //list_node_t list_insert_node(list_t *list, list_node_t *prev_node, list_node_t *node); 83 84 bool list_remove(list_t *list, void *data); 85 86 // similar with list_remove, but do not free the node data 87 bool list_delete(list_t *list, void *data); 88 89 // Removes all elements in the list. Calling this function will return the list to the 90 // same state it was in after |list_new|. |list| may not be NULL. 91 void list_clear(list_t *list); 92 93 // Iterates through the entire |list| and calls |callback| for each data element. 94 // If the list is empty, |callback| will never be called. It is safe to mutate the 95 // list inside the callback. If an element is added before the node being visited, 96 // there will be no callback for the newly-inserted node. Neither |list| nor 97 // |callback| may be NULL. 98 list_node_t *list_foreach(const list_t *list, list_iter_cb callback, void *context); 99 100 // Returns an iterator to the first element in |list|. |list| may not be NULL. 101 // The returned iterator is valid as long as it does not equal the value returned 102 // by |list_end|. 103 list_node_t *list_begin(const list_t *list); 104 105 // Returns an iterator that points past the end of the list. In other words, 106 // this function returns the value of an invalid iterator for the given list. 107 // When an iterator has the same value as what's returned by this function, you 108 // may no longer call |list_next| with the iterator. |list| may not be NULL. 109 list_node_t *list_end(const list_t *list); 110 111 // Given a valid iterator |node|, this function returns the next value for the 112 // iterator. If the returned value equals the value returned by |list_end|, the 113 // iterator has reached the end of the list and may no longer be used for any 114 // purpose. 115 list_node_t *list_next(const list_node_t *node); 116 117 // Returns the value stored at the location pointed to by the iterator |node|. 118 // |node| must not equal the value returned by |list_end|. 119 void *list_node(const list_node_t *node); 120 121 #endif /* _LIST_H_ */ 122