ucx/list.h

Tue, 23 Jul 2013 12:06:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Jul 2013 12:06:28 +0200
changeset 125
fca8efb122de
parent 124
8b44653541ef
child 126
dffb551c5f18
permissions
-rw-r--r--

changed suffix for allocator aware functions + added allocator aware functions for UcxList

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013 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  */
    28 /**
    29  * Double linked list implementation.
    30  * 
    31  * @file   list.h
    32  * @author Mike Becker
    33  * @author Olaf Wintermann
    34  */
    36 #ifndef UCX_LIST_H
    37 #define	UCX_LIST_H
    39 #include "ucx.h"
    40 #include "allocator.h"
    41 #include <stddef.h>
    43 #ifdef	__cplusplus
    44 extern "C" {
    45 #endif
    47 /**
    48  * Loop statement for UCX lists.
    49  * 
    50  * The first argument is a pointer to the list. In most cases this will be the
    51  * pointer to the first element of the list, but it may also be an arbitrary
    52  * element of the list. The iteration will then start with that element.
    53  * 
    54  * The second argument is the name of the iteration variable. The scope of
    55  * this variable is limited to the <code>UCX_FOREACH</code> statement.
    56  * 
    57  * @param list The first element of the list
    58  * @param elem The variable name of the element
    59  */
    60 #define UCX_FOREACH(elem,list) \
    61         for (UcxList* elem = list ; elem != NULL ; elem = elem->next)
    63 /**
    64  * UCX list type
    65  * @see UcxList
    66  */
    67 typedef struct UcxList UcxList;
    68 struct UcxList {
    69     /**
    70      * List element payload.
    71      */
    72     void    *data;
    73     /**
    74      * Pointer to the next list element or <code>NULL</code>, if this is the
    75      * last element.
    76      */
    77     UcxList *next;
    78     /**
    79      * Pointer to the previous list element or <code>NULL</code>, if this is
    80      * the first element.
    81      */
    82     UcxList *prev;
    83 };
    85 UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data);
    86 UcxList *ucx_list_clone_a(UcxAllocator *allocator, UcxList *list,
    87         copy_func cpyfnc, void* data);
    89 /**
    90  * Compares two UCX lists element-wise by using a compare function.
    91  * 
    92  * Each element of the two specified lists are compared by using the specified
    93  * compare function and the additional data. The type and content of this
    94  * additional data depends on the cmp_func() used.
    95  * 
    96  * If the list pointers denote elements within a list, the lists are compared
    97  * starting with the denoted elements. Thus any previous elements are not taken
    98  * into account. This might be useful to check, if certain list tails match
    99  * each other.
   100  * 
   101  * @param list1 the first list
   102  * @param list2 the second list
   103  * @param cmpfnc the compare function
   104  * @param data additional data for the compare function
   105  * @return 1, if and only if the two lists equal element-wise, 0 otherwise
   106  */
   107 int ucx_list_equals(const UcxList *list1, const UcxList *list2,
   108         cmp_func cmpfnc, void* data);
   110 /**
   111  * Destroys the entire list.
   112  * 
   113  * The members of the list are not automatically freed, so ensure they are
   114  * otherwise referenced or a memory leak will occur.
   115  * 
   116  * <b>Caution:</b> the argument <b>MUST</b> denote an entire list (i.e. a call
   117  * to ucx_list_first() on the argument must return the argument itself)
   118  * 
   119  * @param list The list to free.
   120  */
   121 void ucx_list_free(UcxList *list);
   122 void ucx_list_free_a(UcxAllocator *allocator, UcxList *list);
   123 UcxList *ucx_list_append(UcxList *list, void *data);
   124 UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data);
   125 UcxList *ucx_list_prepend(UcxList *list, void *data);
   126 UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data);
   127 UcxList *ucx_list_concat(UcxList *list1, UcxList *list2);
   128 /**
   129  * Returns the first element of a list.
   130  * 
   131  * If the argument is the list pointer, it is directly returned. Otherwise
   132  * this function traverses to the first element of the list and returns the
   133  * list pointer.
   134  * 
   135  * @param elem one element of the list
   136  * @return the first element of the list, the specified element is a member of
   137  */
   138 UcxList *ucx_list_first(const UcxList *elem);
   139 /**
   140  * Returns the last element of a list.
   141  * 
   142  * If the argument has no successor, it is the last element and therefore
   143  * directly returned. Otherwise this function traverses to the last element of
   144  * the list and returns it.
   145  * 
   146  * @param elem one element of the list
   147  * @return the last element of the list, the specified element is a member of
   148  */
   149 UcxList *ucx_list_last(const UcxList *elem);
   150 UcxList *ucx_list_get(const UcxList *list, int index);
   151 ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem);
   152 size_t ucx_list_size(const UcxList *list);
   153 ssize_t ucx_list_find(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
   154 int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
   156 UcxList *ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data);
   158 /**
   159  * Removes an element from the list.
   160  * 
   161  * If the first element is removed, the list pointer changes. So it is
   162  * <i>highly recommended</i> to <i>always</i> update the pointer by calling
   163  * <code>mylist = ucx_list_remove(mylist, myelem);</code>.
   164  * 
   165  * @param list the list from which the element shall be removed
   166  * @param element the element to removed
   167  * @return returns the updated list pointer or <code>NULL</code>, if the list
   168  * is now empty
   169  */
   170 UcxList *ucx_list_remove(UcxList *list, UcxList *element);
   171 UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list,
   172         UcxList *element);
   174 #ifdef	__cplusplus
   175 }
   176 #endif
   178 #endif	/* UCX_LIST_H */

mercurial