src/list.c

Wed, 02 May 2018 16:14:40 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 02 May 2018 16:14:40 +0200
changeset 277
f819fe5e20f5
parent 259
2f5dea574a75
child 291
deb0035635eb
permissions
-rw-r--r--

makes destructor functions for *_free_content() optional + more documentation for UcxProperties

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "ucx/list.h"
    31 UcxList *ucx_list_clone(UcxList *l, copy_func fnc, void *data) {
    32     return ucx_list_clone_a(ucx_default_allocator(), l, fnc, data);
    33 }
    35 UcxList *ucx_list_clone_a(UcxAllocator *alloc, UcxList *l,
    36         copy_func fnc, void *data) {
    37     UcxList *ret = NULL;
    38     while (l) {
    39         if (fnc) {
    40             ret = ucx_list_append_a(alloc, ret, fnc(l->data, data));
    41         } else {
    42             ret = ucx_list_append_a(alloc, ret, l->data);
    43         }
    44         l = l->next;
    45     }
    46     return ret;
    47 }
    49 int ucx_list_equals(const UcxList *l1, const UcxList *l2,
    50         cmp_func fnc, void* data) {
    51     if (l1 == l2) return 1;
    53     while (l1 != NULL && l2 != NULL) {
    54         if (fnc == NULL) {
    55             if (l1->data != l2->data) return 0;
    56         } else {
    57             if (fnc(l1->data, l2->data, data) != 0) return 0;
    58         }
    59         l1 = l1->next;
    60         l2 = l2->next;
    61     }
    63     return (l1 == NULL && l2 == NULL);
    64 }
    66 void ucx_list_free(UcxList *l) {
    67     ucx_list_free_a(ucx_default_allocator(), l);
    68 }
    70 void ucx_list_free_a(UcxAllocator *alloc, UcxList *l) {
    71     UcxList *e = l, *f;
    72     while (e != NULL) {
    73         f = e;
    74         e = e->next;
    75         alfree(alloc, f);
    76     }
    77 }
    79 void ucx_list_free_content(UcxList* list, ucx_destructor destr) {
    80     if (!destr) destr = free;
    81     while (list != NULL) {
    82         destr(list->data);
    83         list = list->next;
    84     }
    85 }
    87 UcxList *ucx_list_append(UcxList *l, void *data)  {
    88     return ucx_list_append_a(ucx_default_allocator(), l, data);
    89 }
    91 UcxList *ucx_list_append_a(UcxAllocator *alloc, UcxList *l, void *data)  {
    92     UcxList *nl = (UcxList*) almalloc(alloc, sizeof(UcxList));
    93     if (!nl) {
    94         return NULL;
    95     }
    97     nl->data = data;
    98     nl->next = NULL;
    99     if (l) {
   100         UcxList *t = ucx_list_last(l);
   101         t->next = nl;
   102         nl->prev = t;
   103         return l;
   104     } else {
   105         nl->prev = NULL;
   106         return nl;
   107     }
   108 }
   110 UcxList *ucx_list_append_once(UcxList *l, void *data,
   111         cmp_func cmpfnc, void *cmpdata) {
   112     return ucx_list_append_once_a(ucx_default_allocator(), l,
   113             data, cmpfnc, cmpdata);
   114 }
   116 UcxList *ucx_list_append_once_a(UcxAllocator *alloc, UcxList *l, void *data,
   117         cmp_func cmpfnc, void *cmpdata) {
   119     UcxList *last = NULL;
   120     {
   121         UcxList *e = l;
   122         while (e) {
   123             if (cmpfnc(e->data, data, cmpdata) == 0) {
   124                 return l;
   125             }
   126             last = e;
   127             e = e->next;
   128         }
   129     }
   131     UcxList *nl = ucx_list_append_a(alloc, NULL, data);
   132     if (!nl) {
   133         return NULL;
   134     }
   136     if (last == NULL) {
   137         return nl;
   138     } else {
   139         nl->prev = last;
   140         last->next = nl;
   141         return l;
   142     }
   143 }
   145 UcxList *ucx_list_prepend(UcxList *l, void *data) {
   146     return ucx_list_prepend_a(ucx_default_allocator(), l, data);
   147 }
   149 UcxList *ucx_list_prepend_a(UcxAllocator *alloc, UcxList *l, void *data) {
   150     UcxList *nl = ucx_list_append_a(alloc, NULL, data);
   151     if (!nl) {
   152         return NULL;
   153     }
   154     l = ucx_list_first(l);
   156     if (l) {
   157         nl->next = l;
   158         l->prev = nl;
   159     }
   160     return nl;
   161 }
   163 UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) {
   164     if (l1) {
   165         UcxList *last = ucx_list_last(l1);
   166         last->next = l2;
   167         if (l2) {
   168             l2->prev = last;
   169         }
   170         return l1;
   171     } else {
   172         return l2;
   173     }
   174 }
   176 UcxList *ucx_list_last(const UcxList *l) {
   177     if (l == NULL) return NULL;
   179     const UcxList *e = l;
   180     while (e->next != NULL) {
   181         e = e->next;
   182     }
   183     return (UcxList*)e;
   184 }
   186 ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem) {
   187     ssize_t index = 0;
   188     while (list) {
   189         if (list == elem) {
   190             return index;
   191         }
   192         list = list->next;
   193         index++;
   194     }
   195     return -1;
   196 }
   198 UcxList *ucx_list_get(const UcxList *l, size_t index) {
   199     if (l == NULL) return NULL;
   201     const UcxList *e = l;
   202     while (e->next && index > 0) {
   203         e = e->next;
   204         index--;
   205     }
   207     return (UcxList*)(index == 0 ? e : NULL);
   208 }
   210 ssize_t ucx_list_find(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) {
   211     ssize_t index = 0;
   212     UCX_FOREACH(e, l) {
   213         if (fnc) {
   214             if (fnc(elem, e->data, cmpdata) == 0) {
   215                 return index;
   216             }
   217         } else {
   218             if (elem == e->data) {
   219                 return index;
   220             }
   221         }
   222         index++;
   223     }
   224     return -1;
   225 }
   227 int ucx_list_contains(UcxList *l, void *elem, cmp_func fnc, void *cmpdata) {
   228     return ucx_list_find(l, elem, fnc, cmpdata) > -1;
   229 }
   231 size_t ucx_list_size(const UcxList *l) {
   232     if (l == NULL) return 0;
   234     const UcxList *e = l;
   235     size_t s = 1;
   236     while (e->next != NULL) {
   237         e = e->next;
   238         s++;
   239     }
   241     return s;
   242 }
   244 static UcxList *ucx_list_sort_merge(int length,
   245         UcxList* ls, UcxList* le, UcxList* re,
   246         cmp_func fnc, void* data) {
   248     UcxList** sorted = (UcxList**) malloc(sizeof(UcxList*)*length);
   249     UcxList *rc, *lc;
   251     lc = ls; rc = le;
   252     int n = 0;
   253     while (lc && lc != le && rc != re) {
   254         if (fnc(lc->data, rc->data, data) <= 0) {
   255             sorted[n] = lc;
   256             lc = lc->next;
   257         } else {
   258             sorted[n] = rc;
   259             rc = rc->next;
   260         }
   261         n++;
   262     }
   263     while (lc && lc != le) {
   264         sorted[n] = lc;
   265         lc = lc->next;
   266         n++;
   267     }
   268     while (rc && rc != re) {
   269         sorted[n] = rc;
   270         rc = rc->next;
   271         n++;
   272     }
   274     // Update pointer
   275     sorted[0]->prev = NULL;
   276     for (int i = 0 ; i < length-1 ; i++) {
   277         sorted[i]->next = sorted[i+1];
   278         sorted[i+1]->prev = sorted[i];
   279     }
   280     sorted[length-1]->next = NULL;
   282     UcxList *ret = sorted[0];
   283     free(sorted);
   284     return ret;
   285 }
   287 UcxList *ucx_list_sort(UcxList *l, cmp_func fnc, void *data) {
   288     if (l == NULL) {
   289         return NULL;
   290     }
   292     UcxList *lc;
   293     int ln = 1;
   295     UcxList *ls = l, *le, *re;
   297     // check how many elements are already sorted
   298     lc = ls;
   299     while (lc->next != NULL && fnc(lc->next->data, lc->data, data) > 0) {
   300         lc = lc->next;
   301         ln++;
   302     }
   303     le = lc->next;
   305     if (le == NULL) {
   306         return l; // this list is already sorted :)
   307     } else {
   308         UcxList *rc;
   309         int rn = 1;
   310         rc = le;
   311         // skip already sorted elements
   312         while (rc->next != NULL && fnc(rc->next->data, rc->data, data) > 0) {
   313             rc = rc->next;
   314             rn++;
   315         }
   316         re = rc->next;
   318         // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
   319         UcxList *sorted = ucx_list_sort_merge(ln+rn,
   320                 ls, le, re,
   321                 fnc, data);
   323         // Something left? Sort it!
   324         size_t remainder_length = ucx_list_size(re);
   325         if (remainder_length > 0) {
   326             UcxList *remainder = ucx_list_sort(re, fnc, data);
   328             // merge sorted list with (also sorted) remainder
   329             l = ucx_list_sort_merge(ln+rn+remainder_length,
   330                     sorted, remainder, NULL, fnc, data);
   331         } else {
   332             // no remainder - we've got our sorted list
   333             l = sorted;
   334         }
   336         return l;
   337     }
   338 }
   340 UcxList *ucx_list_first(const UcxList *l) {
   341     if (!l) {
   342         return NULL;
   343     }
   345     const UcxList *e = l;
   346     while (e->prev) {
   347         e = e->prev;
   348     }
   349     return (UcxList *)e;
   350 }
   352 UcxList *ucx_list_remove(UcxList *l, UcxList *e) {
   353     return ucx_list_remove_a(ucx_default_allocator(), l, e);
   354 }
   356 UcxList *ucx_list_remove_a(UcxAllocator *alloc, UcxList *l, UcxList *e) {
   357     if (l == e) {
   358         l = e->next;
   359     }
   361     if (e->next) {
   362         e->next->prev = e->prev;
   363     }
   365     if (e->prev) {
   366         e->prev->next = e->next;
   367     }
   369     alfree(alloc, e);
   370     return l;
   371 }

mercurial