list: sort() function
authorMarko Kreen <markokr@gmail.com>
Mon, 25 Jan 2010 22:11:25 +0000 (00:11 +0200)
committerMarko Kreen <markokr@gmail.com>
Wed, 3 Mar 2010 07:50:22 +0000 (09:50 +0200)
usual/list.c [new file with mode: 0644]
usual/list.h

diff --git a/usual/list.c b/usual/list.c
new file mode 100644 (file)
index 0000000..34f297f
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Circular doubly linked list implementation.
+ *
+ * Copyright (c) 2010 Marko Kreen, Skype Technologies OÜ
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <usual/list.h>
+
+/* merge 2 ordered arrays into one */
+static struct List *merge(list_cmp_f cmp_func, struct List *p, struct List *q)
+{
+       struct List res[1], *tail = res, *e;
+
+       while (p && q) {
+               if (cmp_func(p, q) <= 0) {
+                       e = p;
+                       p = p->next;
+               } else {
+                       e = q;
+                       q = q->next;
+               }
+               tail->next = e;
+               tail = e;
+       }
+       tail->next = p ? p : q;
+       return res->next;
+}
+
+/*
+ * non-recursive merge sort
+ *
+ * uses singly-linked NULL-terminated arrays internally.
+ */
+void list_sort(struct List *list, list_cmp_f cmp_func)
+{
+       int i, top = 0;
+       struct List *p;
+       struct List *stack[64];
+
+       if (list_empty(list))
+               return;
+
+       /* merge small sorted fragments into larger ones */
+       while (list->next != list) {
+               p = list->next;
+               list->next = p->next;
+               p->next = NULL;
+
+               for (i = 0; (i < top) && stack[i]; i++) {
+                       p = merge(cmp_func, stack[i], p);
+                       stack[i] = NULL;
+               }
+
+               stack[i] = p;
+               if (i == top)
+                       top++;
+       }
+
+       /* merge remaining fragments */
+       for (p = NULL, i = 0; i < top; i++)
+               p = merge(cmp_func, stack[i], p);
+
+       /* restore proper List */
+       list->next = p;
+       for (p = list; p->next; p = p->next)
+               p->next->prev = p;
+       list->prev = p;
+       p->next = list;
+}
+
index 5c460fcd78dd75556d1d9ae17d2dd6231a30ee7f..b03298d03f5aef33ee6458dd852d2f1747e0c9b9 100644 (file)
@@ -111,5 +111,9 @@ static inline struct List *list_last(const struct List *list)
             (item) != (list); \
             (item) = (tmp), (tmp) = (tmp)->next)
 
+/* sort list */
+typedef int (*list_cmp_f)(const struct List *a, const struct List *b);
+void list_sort(struct List *list, list_cmp_f cmp_func);
+
 #endif