From e7f541396715ee2895abcf73044b91ae9b746201 Mon Sep 17 00:00:00 2001 From: xXx-caillou-xXx Date: Wed, 20 Dec 2017 18:09:53 +0100 Subject: Better parsing of the rules Thanks to this huge commit from @xXx-caillou-xXx, we can now write amazingly flexible rules.--- src/sp_list.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/sp_list.c') diff --git a/src/sp_list.c b/src/sp_list.c index c671f51..70d0ebe 100644 --- a/src/sp_list.c +++ b/src/sp_list.c @@ -17,6 +17,34 @@ sp_node_t *sp_list_new() { return new; } +// Thanks to https://en.wikipedia.org/wiki/Insertion_sort :> +sp_node_t *sp_list_sort(sp_node_t *pList, int (*cmp_func)(sp_node_t *, sp_node_t *)) { + sp_node_t *head = NULL; + + if (pList == NULL || pList->next == NULL) { + return pList; + } + while (pList != NULL) { + sp_node_t *current = pList; + pList = pList->next; + if (head == NULL || 0 > cmp_func(current, head)) { + current->next = head; + head = current; + } else { + sp_node_t *p = head; + while (p != NULL) { + if (p->next == NULL || 0 > cmp_func(current, p->next)) { + current->next = p->next; + p->next = current; + break; + } + p = p->next; + } + } + } + return head; +} + void sp_list_insert(sp_node_t *list, void *data) { if (list->head == NULL) { list->data = data; -- cgit v1.3