summaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
Diffstat (limited to 'shared')
-rw-r--r--shared/nm-utils/c-list-util.c62
1 files changed, 40 insertions, 22 deletions
diff --git a/shared/nm-utils/c-list-util.c b/shared/nm-utils/c-list-util.c
index 070323c69d..509f4e12ab 100644
--- a/shared/nm-utils/c-list-util.c
+++ b/shared/nm-utils/c-list-util.c
@@ -58,39 +58,35 @@ c_list_relink (CList *lst)
/*****************************************************************************/
static CList *
-_c_list_sort (CList *ls,
- CListSortCmp cmp,
- const void *user_data)
+_c_list_srt_split (CList *ls)
{
- CList *ls1, *ls2;
- CList head;
+ CList *ls2;
- if (!ls->next)
- return ls;
-
- /* split list in two halfs @ls1 and @ls2. */
- ls1 = ls;
ls2 = ls;
ls = ls->next;
- while (ls) {
+ if (!ls)
+ return NULL;
+ do {
ls = ls->next;
if (!ls)
break;
ls = ls->next;
ls2 = ls2->next;
- }
- ls = ls2;
- ls2 = ls->next;
- ls->next = NULL;
-
- /* recurse */
- ls1 = _c_list_sort (ls1, cmp, user_data);
- if (!ls2)
- return ls1;
+ } while (ls);
+ ls = ls2->next;
+ ls2->next = NULL;
+ return ls;
+}
- ls2 = _c_list_sort (ls2, cmp, user_data);
+static CList *
+_c_list_srt_merge (CList *ls1,
+ CList *ls2,
+ CListSortCmp cmp,
+ const void *user_data)
+{
+ CList *ls;
+ CList head;
- /* merge */
ls = &head;
for (;;) {
/* while invoking the @cmp function, the list
@@ -115,6 +111,28 @@ _c_list_sort (CList *ls,
return head.next;
}
+static CList *
+_c_list_sort (CList *ls,
+ CListSortCmp cmp,
+ const void *user_data)
+{
+ CList *ls1, *ls2;
+
+ if (!ls->next)
+ return ls;
+ ls1 = ls;
+
+ ls2 = _c_list_srt_split (ls1);
+
+ ls1 = _c_list_sort (ls1, cmp, user_data);
+ if (!ls2)
+ return ls1;
+
+ ls2 = _c_list_sort (ls2, cmp, user_data);
+
+ return _c_list_srt_merge (ls1, ls2, cmp, user_data);
+}
+
/**
* c_list_sort_headless:
* @lst: the list.