8 #ifndef _DRAGONSTD_LIST_H_ // include guard
9 #define _DRAGONSTD_LIST_H_
11 #include "bits/callback.h" // for Iterator, Comparator
12 #include "bits/compare.h" // for cmp_ref (not used in file)
14 #define LIST_ITERATE(list, node) for (ListNode *node = (list)->fst; node != NULL; node = node->nxt)
16 typedef struct ListNode {
18 void *dat; // pointer to data
20 struct ListNode *nxt; // next node
25 ListNode *fst; // first element
26 ListNode **end; // where address of next node will be stored
29 void list_ini(List *list);
33 The list should be uninitialized or empty before passed to this function.
34 This function should be called before any other function is called on the list.
37 void *list_add(List *list, void *dat, Comparator cmp, Transformer trans);
39 Add an element to the list.
41 If an equal element is already on the list, return it and don't add anything.
42 Otherwise, return added element.
45 void *list_get(List *list, void *key, Comparator cmp, Transformer trans);
47 Get an element from the list.
49 The first matching element is returned, or NULL if none found.
52 void *list_del(List *list, void *key, Comparator cmp, Transformer trans);
54 Delete an element from the list.
56 The first matching element is returned (after being removed from the list), or NULL
60 void list_apd(List *list, void *dat);
62 Append an element at the end of the list.
65 void list_ppd(List *list, void *dat);
67 Prepend an element at the start of the list.
70 ListNode **list_nfd(List *list, void *key, Comparator cmp);
72 Find the location of the first node matching key.
74 This can be either an existing node, or the location the next new node would need to
78 void list_nmk(List *list, ListNode **node, void *dat);
80 Create a node with dat as data and store it's pointer at the given location.
83 void list_nrm(List *list, ListNode **node);
85 Remove the node at the given location.
88 void list_itr(List *list, Iterator iter, void *arg, Transformer trans);
90 Iterate over the list.
91 Calls iter on every element, with the extra argument arg.
93 Note: the LIST_ITERATE macro can be used to do this without function calls.
96 void list_clr(List *list, Iterator iter, void *arg, Transformer trans);
98 Iterates over the list and deletes all elements.
99 Calls iter on every element, with the extra argument arg.
101 The list is empty afterwards.
104 #endif // _DRAGONSTD_LIST_H_