]> git.lizzy.rs Git - dragonstd.git/blobdiff - list.h
Add transformer to del
[dragonstd.git] / list.h
diff --git a/list.h b/list.h
index 8e4d844bf96f7da4d7ed433eba4cddfa89ccd48c..16fd46b94029bf9b81ce1d5203b4f8fb5e1a551f 100644 (file)
--- a/list.h
+++ b/list.h
-#ifndef _DRAGONTYPE_LIST_H_
-#define _DRAGONTYPE_LIST_H_
+/*
+       List
+       ----
 
-#include <stdbool.h>
+       A linked list.
+*/
 
-#define ITERATE_LIST(list, pair) for (ListPair *pair = (list)->first; pair != NULL; pair = pair->next)
+#ifndef _DRAGONSTD_LIST_H_ // include guard
+#define _DRAGONSTD_LIST_H_
 
-typedef struct ListPair
-{
-       struct ListPair *next;
-       void *key;
-       void *value;
-} ListPair;
+#include <stdbool.h>       // for bool
+#include "bits/callback.h" // for Callback, Comparator, Transformer, Callback
+#include "bits/compare.h"  // for cmp_ref (not used in file)
 
-typedef bool (*ListComparator)(void *v1, void *v2);
+#define LIST_ITERATE(list, node) for (ListNode *node = (list)->fst; node != NULL; node = node->nxt)
 
-typedef struct
-{
-       ListComparator cmp;
-       ListPair *first;
+typedef struct ListNode {
+       /* public */
+       void *dat;            // pointer to data
+       /* private */
+       struct ListNode *nxt; // next node
+} ListNode;
+
+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.
+*/
+
+bool list_add(List *list, void *dat, Comparator cmp, Transformer trans);
+/*
+       Add an element to the list.
+
+       If an equal element is already in the list, don't add anything.
+       Return whether an element has been added.
+*/
+
+void *list_get(List *list, void *key, Comparator cmp, Transformer trans);
+/*
+       Get an element from the list.
+
+       The first matching element is returned, or NULL if none found.
+*/
+
+bool list_del(List *list, void *key, Comparator cmp, Callback call, void *arg, Transformer trans);
+/*
+       Delete an element from the list if it is found.
+       Return whether an element has been deleted.
+
+       The first matching element is deleted.
+*/
+
+void list_apd(List *list, void *dat);
+/*
+       Append an element at the end of the list.
+*/
+
+void list_ppd(List *list, void *dat);
+/*
+       Prepend an element at the start 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, Callback iter, void *arg, Transformer trans);
+/*
+       Iterate over the list.
+       Calls iter 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, Callback iter, void *arg, Transformer trans);
+/*
+       Iterates over the list and deletes all elements.
+       Calls iter 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_