ucx/dlist.c

Sat, 31 Dec 2011 21:05:59 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 31 Dec 2011 21:05:59 +0100
changeset 13
98ac89e3aa37
parent 8
9cd2b2460db0
child 18
69636f81db31
permissions
-rw-r--r--

Added mempool

     1 #include "dlist.h"
     3 void ucx_dlist_free(UcxDlist *l) {
     4     UcxDlist *e = l, *f;
     5     while (e != NULL) {
     6         f = e;
     7         e = e->next;
     8         free(f);
     9     }
    10 }
    12 UcxDlist *ucx_dlist_append(UcxDlist *l, void *data)  {
    13     UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist));
    14     if (nl == NULL) return NULL;
    16     nl->data = data;
    17     nl->next = NULL;
    18     if (l == NULL) {
    19         return nl;
    20     } else {
    21         UcxDlist *t = ucx_dlist_last(l);
    22         t->next = nl;
    23         nl->prev = t;
    24         return l;
    25     }
    26 }
    28 UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) {
    29     UcxDlist *nl = ucx_dlist_append(NULL, data);
    30     if (nl == NULL) return NULL;
    32     if (l != NULL) {
    33         nl->next = l;
    34         l->prev = nl;
    35     }
    36     return nl;
    37 }
    39 UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) {
    40     if (l1 == NULL) {
    41         return l2;
    42     } else {
    43         UcxDlist *last = ucx_dlist_last(l1);
    44         last->next = l2;
    45         l2->prev = last;
    46         return l1;
    47     }
    48 }
    50 UcxDlist *ucx_dlist_last(UcxDlist *l) {
    51     if (l == NULL) return NULL;
    53     UcxDlist *e = l;
    54     while (e->next != NULL) {
    55         e = e->next;
    56     }
    57     return e;
    58 }
    60 UcxDlist *ucx_dlist_get(UcxDlist *l, int index) {
    61     if (l == NULL) return NULL;
    63     UcxDlist *e = l;
    64     while (e->next != NULL && index > 0) {
    65         e = e->next;
    66         index--;
    67     }
    69     return index == 0 ? e : NULL;
    70 }
    72 size_t ucx_dlist_size(UcxDlist *l) {
    73     if (l == NULL) return 0;
    75     UcxDlist *e = l;
    76     size_t s = 1;
    77     while (e->next != NULL) {
    78         e = e->next;
    79         s++;
    80     }
    82     return s;
    83 }
    85 void ucx_dlist_foreach(UcxDlist *l, ucx_callback fnc, void* data) {
    86     UcxDlist *e = l;
    87     while (e != NULL) {
    88         fnc(e, data);
    89         e = e->next;
    90     }
    91 }
    93 /* dlist specific functions */
    94 UcxDlist *ucx_dlist_first(UcxDlist *l) {
    95     if (l == NULL) return NULL;
    97     UcxDlist *e = l;
    98     while (e->prev != NULL) {
    99         e = e->prev;
   100     }
   101     return e;
   102 }

mercurial