8 #ifndef _DRAGONSTD_LIST_H_ // include guard
9 #define _DRAGONSTD_LIST_H_
11 #include <stdbool.h> // for bool
12 #include "bits/callback.h" // for Iterator, Comparator, Transformer, Callback
13 #include "bits/compare.h" // for cmp_ref (not used in file)
15 #define LIST_ITERATE(list, node) for (ListNode *node = (list)->fst; node != NULL; node = node->nxt)
17 typedef struct ListNode {
19 void *dat; // pointer to data
21 struct ListNode *nxt; // next node
26 ListNode *fst; // first element
27 ListNode **end; // where address of next node will be stored
30 void list_ini(List *list);
34 The list should be uninitialized or empty before passed to this function.
35 This function should be called before any other function is called on the list.
38 bool list_add(List *list, void *dat, Comparator cmp, Transformer trans);
40 Add an element to the list.
42 If an equal element is already in the list, don't add anything.
43 Return whether an element has been added.
46 void *list_get(List *list, void *key, Comparator cmp, Transformer trans);
48 Get an element from the list.
50 The first matching element is returned, or NULL if none found.
53 bool list_del(List *list, void *key, Comparator cmp, Callback call);
55 Delete an element from the list if it is found.
56 Return whether an element has been deleted.
58 The first matching element is deleted.
61 void list_apd(List *list, void *dat);
63 Append an element at the end of the list.
66 void list_ppd(List *list, void *dat);
68 Prepend an element at the start of the list.
71 ListNode **list_nfd(List *list, void *key, Comparator cmp);
73 Find the location of the first node matching key.
75 This can be either an existing node, or the location the next new node would need to
79 void list_nmk(List *list, ListNode **node, void *dat);
81 Create a node with dat as data and store it's pointer at the given location.
84 void list_nrm(List *list, ListNode **node);
86 Remove the node at the given location.
89 void list_itr(List *list, Iterator iter, void *arg, Transformer trans);
91 Iterate over the list.
92 Calls iter on every element, with the extra argument arg.
94 Note: the LIST_ITERATE macro can be used to do this without function calls.
97 void list_clr(List *list, Iterator iter, void *arg, Transformer trans);
99 Iterates over the list and deletes all elements.
100 Calls iter on every element, with the extra argument arg.
102 The list is empty afterwards.
105 #endif // _DRAGONSTD_LIST_H_