src/server/ucx/dlist.c

changeset 14
b8bf95b39952
parent 13
1fdbf4170ef4
child 15
cff9c4101dd7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/server/ucx/dlist.c	Sat Jan 14 13:53:44 2012 +0100
@@ -0,0 +1,102 @@
+#include "dlist.h"
+
+void ucx_dlist_free(UcxDlist *l) {
+    UcxDlist *e = l, *f;
+    while (e != NULL) {
+        f = e;
+        e = e->next;
+        free(f);
+    }
+}
+
+UcxDlist *ucx_dlist_append(UcxDlist *l, void *data)  {
+    UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist));
+    if (nl == NULL) return NULL;
+
+    nl->data = data;
+    nl->next = NULL;
+    if (l == NULL) {
+        return nl;
+    } else {
+        UcxDlist *t = ucx_dlist_last(l);
+        t->next = nl;
+        nl->prev = t;
+        return l;
+    }
+}
+
+UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) {
+    UcxDlist *nl = ucx_dlist_append(NULL, data);
+    if (nl == NULL) return NULL;
+
+    if (l != NULL) {
+        nl->next = l;
+        l->prev = nl;
+    }
+    return nl;
+}
+
+UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) {
+    if (l1 == NULL) {
+        return l2;
+    } else {
+        UcxDlist *last = ucx_dlist_last(l1);
+        last->next = l2;
+        l2->prev = last;
+        return l1;
+    }
+}
+
+UcxDlist *ucx_dlist_last(UcxDlist *l) {
+    if (l == NULL) return NULL;
+
+    UcxDlist *e = l;
+    while (e->next != NULL) {
+        e = e->next;
+    }
+    return e;
+}
+
+UcxDlist *ucx_dlist_get(UcxDlist *l, int index) {
+    if (l == NULL) return NULL;
+
+    UcxDlist *e = l;
+    while (e->next != NULL && index > 0) {
+        e = e->next;
+        index--;
+    }
+
+    return index == 0 ? e : NULL;
+}
+
+size_t ucx_dlist_size(UcxDlist *l) {
+    if (l == NULL) return 0;
+
+    UcxDlist *e = l;
+    size_t s = 1;
+    while (e->next != NULL) {
+        e = e->next;
+        s++;
+    }
+
+    return s;
+}
+
+void ucx_dlist_foreach(UcxDlist *l, ucx_callback fnc, void* data) {
+    UcxDlist *e = l;
+    while (e != NULL) {
+        fnc(e, data);
+        e = e->next;
+    }
+}
+
+/* dlist specific functions */
+UcxDlist *ucx_dlist_first(UcxDlist *l) {
+    if (l == NULL) return NULL;
+
+    UcxDlist *e = l;
+    while (e->prev != NULL) {
+        e = e->prev;
+    }
+    return e;
+}

mercurial