8 #ifndef _DRAGONSTD_LIST_H_ // include guard
9 #define _DRAGONSTD_LIST_H_
11 #include "bits/callback.h" // for Iterator, Comparator
13 #define LIST_ITERATE(list, node) for (ListNode *node = (list)->fst; node != NULL; node = node->nxt)
15 typedef struct ListNode {
17 void *dat; // pointer to data
19 struct ListNode *nxt; // next node
24 ListNode *fst; // first element
25 ListNode **end; // where address of next node will be stored
28 void list_ini(List *list);
32 The list should be uninitialized or empty before passed to this function.
33 This function should be called before any other function is called on the list.
36 void *list_add(List *list, void *dat, Comparator cmp, Transformer trans);
38 Add an element to the list.
40 If an equal element is already on the list, return it and don't add anything.
41 Otherwise, return added element.
44 void *list_get(List *list, void *key, Comparator cmp, Transformer trans);
46 Get an element from the list.
48 The first matching element is returned, or NULL if none found.
51 void *list_del(List *list, void *key, Comparator cmp, Transformer trans);
53 Delete an element from the list.
55 The first matching element is returned (after being removed from the list), or NULL
59 void list_apd(List *list, void *dat);
61 Append an element at the end of the list.
64 void list_ppd(List *list, void *dat);
66 Prepend an element at the start of the list.
69 ListNode **list_nfd(List *list, void *key, Comparator cmp);
71 Find the location of the first node matching key.
73 This can be either an existing node, or the location the next new node would need to
77 void list_nmk(List *list, ListNode **node, void *dat);
79 Create a node with dat as data and store it's pointer at the given location.
82 void list_nrm(List *list, ListNode **node);
84 Remove the node at the given location.
87 void list_itr(List *list, Iterator iter, void *arg, Transformer trans);
89 Iterate over the list.
90 Calls iter on every element, with the extra argument arg.
92 Note: the LIST_ITERATE macro can be used to do this without function calls.
95 void list_clr(List *list, Iterator iter, void *arg, Transformer trans);
97 Iterates over the list and deletes all elements.
98 Calls iter on every element, with the extra argument arg.
100 The list is empty afterwards.
103 #endif // _DRAGONSTD_LIST_H_