-#ifndef _DRAGONSTD_LIST_H_
-#define _DRAGONSTD_LIST_H_
+/*
+ List
+ ----
+
+ A linked list.
+*/
-#include <stdbool.h>
+#ifndef _DRAGONSTD_LIST_H_ // include guard
+#define _DRAGONSTD_LIST_H_
-#define ITERATE_LIST(list, pair) for (ListPair *pair = (list)->first; pair != NULL; pair = pair->next)
+#include "bits/callback.h" // for Iterator, Comparator
-typedef struct ListPair
-{
- struct ListPair *next;
- void *key;
- void *value;
-} ListPair;
+#define LIST_ITERATE(list, node) for (ListNode *node = (list)->fst; node != NULL; node = node->nxt)
-typedef bool (*ListComparator)(void *v1, void *v2);
+typedef struct ListNode {
+ /* public */
+ void *dat; // pointer to data
+ /* private */
+ struct ListNode *nxt; // next node
+} ListNode;
-typedef struct
-{
- ListComparator cmp;
- ListPair *first;
+typedef struct {
+ /* private */
+ ListNode *fst; // first element
+ ListNode **end; // where address of next node will be stored
} List;
-bool list_compare_default(void *v1, void *v2);
-bool list_compare_string(void *v1, void *v2);
+void list_ini(List *list);
+/*
+ Initializes the list.
+
+ The list should be uninitialized or empty before passed to this function.
+ This function should be called before any other function is called on the list.
+*/
+
+void *list_add(List *list, void *dat, Comparator cmp, Transformer func);
+/*
+ Add an element to the list.
+
+ If an equal element is already on the list, return it and don't add anything.
+ Otherwise, return added element.
+*/
+
+void *list_get(List *list, void *key, Comparator cmp, Transformer func);
+/*
+ Get an element from the list.
+
+ The first matching element is returned, or NULL if none found.
+*/
+
+void *list_del(List *list, void *key, Comparator cmp, Transformer func);
+/*
+ Delete an element from the list.
+
+ The first matching element is returned (after being removed from the list), or NULL
+ if none found.
+*/
+
+void list_apd(List *list, void *dat);
+/*
+ Append an element at the end of the list.
+*/
+
+ListNode **list_nfd(List *list, void *key, Comparator cmp);
+/*
+ Find the location of the first node matching key.
+
+ This can be either an existing node, or the location the next new node would need to
+ be stored.
+*/
+
+void list_nmk(List *list, ListNode **node, void *dat);
+/*
+ Create a node with dat as data and store it's pointer at the given location.
+*/
+
+void list_nrm(List *list, ListNode **node);
+/*
+ Remove the node at the given location.
+*/
+
+void list_itr(List *list, Iterator func, void *arg);
+/*
+ Iterate over the list.
+ Calls func on every element, with the extra argument arg.
-List list_create(ListComparator cmp);
-void list_clear(List *list);
-void list_clear_func(List *list, void (*func)(void *key, void *value, void *arg), void *arg);
+ Note: the LIST_ITERATE macro can be used to do this without function calls.
+*/
-bool list_put(List *list, void *key, void *value);
-void *list_get_cached(List *list, void *key, void *(*provider)(void *key));
-void list_set(List *list, void *key, void *value);
-void *list_get(List *list, void *key);
-void *list_delete(List *list, void *key);
+void list_clr(List *list, Iterator func, void *arg);
+/*
+ Iterates over the list and deletes all elements.
+ Calls func on every element, with the extra argument arg.
-bool list_serialize(int fd, List *list); // ToDo
-bool list_deserialize(int fd, List *list); // ToDo
+ The list is empty afterwards.
+*/
-#endif
+#endif // _DRAGONSTD_LIST_H_