diff -r 95e42a963520 -r 75ae1dccd101 src/linked_list.c --- a/src/linked_list.c Tue Oct 05 13:04:20 2021 +0200 +++ b/src/linked_list.c Tue Oct 05 16:33:11 2021 +0200 @@ -33,13 +33,13 @@ /* LOW LEVEL LINKED LIST FUNCTIONS */ -#define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off)) +#define CX_LL_PTR(cur, off) (*(void**)(((char*)cur)+off)) void *cx_linked_list_at(void *start, size_t start_index, ptrdiff_t loc_advance, size_t index) { size_t i = start_index; void *cur = start; while (i != index && cur != NULL) { - cur = *CX_LL_PTR(cur, loc_advance); + cur = CX_LL_PTR(cur, loc_advance); i < index ? i++ : i--; } return cur; @@ -53,7 +53,7 @@ void *last; do { last = cur; - } while ((cur = *CX_LL_PTR(cur, loc_next)) != NULL); + } while ((cur = CX_LL_PTR(cur, loc_next)) != NULL); return last; } @@ -71,8 +71,7 @@ *begin = new_node; } else { // if there is a last node, update its next pointer - void **next = CX_LL_PTR(last, loc_next); - *next = new_node; + CX_LL_PTR(last, loc_next) = new_node; } // if there is an end pointer, update it @@ -82,11 +81,126 @@ // if the nodes use a prev pointer, update it if (loc_prev >= 0) { - void **prev = CX_LL_PTR(new_node, loc_prev); - *prev = last; + CX_LL_PTR(new_node, loc_prev) = last; } } +size_t cx_linked_list_size(void *node, ptrdiff_t loc_next) { + size_t size = 0; + while (node != NULL) { + node = CX_LL_PTR(node, loc_next); + size++; + } + return size; +} + +#define ll_prev(node) CX_LL_PTR(node, loc_prev) +#define ll_next(node) CX_LL_PTR(node, loc_next) +#define ll_data(node) (follow_ptr?CX_LL_PTR(node, loc_data):(((char*)node)+loc_data)) + +static void *cx_linked_list_sort_merge(ptrdiff_t loc_prev, ptrdiff_t loc_next, + ptrdiff_t loc_data, int follow_ptr, + size_t length, void *ls, void *le, void *re, + CxListComparator cmp_func) { + const size_t sbo_len = 1024; + void *sbo[sbo_len]; + void **sorted = (length >= sbo_len) ? malloc(sizeof(void *) * length) : sbo; + void *rc, *lc; + + lc = ls; + rc = le; + size_t n = 0; + while (lc && lc != le && rc != re) { + if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) { + sorted[n] = lc; + lc = ll_next(lc); + } else { + sorted[n] = rc; + rc = ll_next(rc); + } + n++; + } + while (lc && lc != le) { + sorted[n] = lc; + lc = ll_next(lc); + n++; + } + while (rc && rc != re) { + sorted[n] = rc; + rc = ll_next(rc); + n++; + } + + // Update pointer + if (loc_prev >= 0) ll_prev(sorted[0]) = NULL; + for (size_t i = 0; i < length - 1; i++) { + ll_next(sorted[i]) = sorted[i + 1]; + if (loc_prev >= 0) ll_prev(sorted[i + 1]) = sorted[i]; + } + ll_next(sorted[length - 1]) = NULL; + + void *ret = sorted[0]; + if (sorted != sbo) { + free(sorted); + } + return ret; +} + +void cx_linked_list_sort(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, + ptrdiff_t loc_data, int follow_ptr, CxListComparator cmp_func) { + assert(begin != NULL); + + void *lc, *ls, *le, *re; + + // set start node + ls = *begin; + + // check how many elements are already sorted + lc = ls; + size_t ln = 1; + while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) { + lc = ll_next(lc); + ln++; + } + le = ll_next(lc); + + // if first unsorted node is NULL, the list is already completely sorted + if (le != NULL) { + void *rc; + size_t rn = 1; + rc = le; + // skip already sorted elements + while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) { + rc = ll_next(rc); + rn++; + } + re = ll_next(rc); + + // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them + void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr, + ln + rn, ls, le, re, cmp_func); + + // Something left? Sort it! + size_t remainder_length = cx_linked_list_size(re, loc_next); + if (remainder_length > 0) { + void *remainder = re; + cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, follow_ptr, cmp_func); + + // merge sorted list with (also sorted) remainder + *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr, + ln + rn + remainder_length, + sorted, remainder, NULL, cmp_func); + } else { + // no remainder - we've got our sorted list + *begin = sorted; + } + if (end) *end = cx_linked_list_last(sorted, loc_next); + } +} + +#undef ll_next +#undef ll_data + /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ typedef struct cx_linked_list_node cx_linked_list_node; @@ -109,7 +223,7 @@ if (index >= list->base.size) { return NULL; } else if (index > list->base.size / 2) { - return cx_linked_list_at(list->end, list->base.size-1, CX_LL_LOC_PREV, index); + return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index); } else { return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index); } @@ -144,7 +258,7 @@ node->next = NULL; ll->end = node; } - // check if this shall be the new start node + // check if this shall be the new start node else if (index == 0) { ll->begin->prev = node; node->next = ll->begin; @@ -219,7 +333,7 @@ static void *cx_pll_at(cx_list_s *list, size_t index) { cx_linked_list *ll = (cx_linked_list *) list; cx_linked_list_node *node = cx_ll_node_at(ll, index); - return node == NULL ? NULL : *(void**)node->payload; + return node == NULL ? NULL : *(void **) node->payload; } static size_t cx_ll_find(cx_list_s *list, void *elem) { @@ -245,7 +359,7 @@ size_t index; cx_linked_list_node *node = ll->begin; for (index = 0; index < list->size; index++) { - void *current = *(void**)node->payload; + void *current = *(void **) node->payload; if (cmp(current, elem) == 0) { return index; } @@ -263,7 +377,7 @@ static void *cx_pll_last(cx_list_s *list) { cx_linked_list *ll = (cx_linked_list *) list; cx_linked_list_node *last = ll->end; - return last == NULL ? NULL : *(void**)last->payload; + return last == NULL ? NULL : *(void **) last->payload; } static cx_list_class cx_linked_list_class = { @@ -310,7 +424,7 @@ list->base.cl = &cx_pointer_linked_list_class; list->base.allocator = allocator; list->base.cmpfunc = comparator; - list->base.itemsize = sizeof(void*); + list->base.itemsize = sizeof(void *); list->base.capacity = SIZE_MAX; list->base.size = 0;