ucx/list.h

Tue, 23 Aug 2016 12:41:04 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 12:41:04 +0200
changeset 220
1f57f196d221
parent 212
c766c423dee6
child 225
a1a068c2c4ef
permissions
-rw-r--r--

fixes UCX_FOREACH documentation

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2015 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  * Doubly 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"
    42 #ifdef	__cplusplus
    43 extern "C" {
    44 #endif
    46 /**
    47  * Loop statement for UCX lists.
    48  * 
    49  * The first argument is the name of the iteration variable. The scope of
    50  * this variable is limited to the <code>UCX_FOREACH</code> statement.
    51  * 
    52  * The second argument is a pointer to the list. In most cases this will be the
    53  * pointer to the first element of the list, but it may also be an arbitrary
    54  * element of the list. The iteration will then start with that element.
    55  * 
    56  * @param list The first element of the list
    57  * @param elem The variable name of the element
    58  */
    59 #define UCX_FOREACH(elem,list) \
    60         for (UcxList* elem = list ; elem != NULL ; elem = elem->next)
    62 /**
    63  * UCX list type.
    64  * @see UcxList
    65  */
    66 typedef struct UcxList UcxList;
    68 /**
    69  * UCX list structure.
    70  */
    71 struct UcxList {
    72     /**
    73      * List element payload.
    74      */
    75     void    *data;
    76     /**
    77      * Pointer to the next list element or <code>NULL</code>, if this is the
    78      * last element.
    79      */
    80     UcxList *next;
    81     /**
    82      * Pointer to the previous list element or <code>NULL</code>, if this is
    83      * the first element.
    84      */
    85     UcxList *prev;
    86 };
    88 /**
    89  * Creates an element-wise copy of a list.
    90  * 
    91  * This function clones the specified list by creating new list elements and
    92  * copying the data with the specified copy_func(). If no copy_func() is
    93  * specified, a shallow copy is created and the new list will reference the
    94  * same data as the source list.
    95  * 
    96  * @param list the list to copy
    97  * @param cpyfnc a pointer to the function that shall copy an element (may be
    98  * <code>NULL</code>)
    99  * @param data additional data for the copy_func()
   100  * @return a pointer to the copy
   101  */
   102 UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data);
   104 /**
   105  * Creates an element-wise copy of a list using an UcxAllocator.
   106  * 
   107  * See ucx_list_clone() for details.
   108  * 
   109  * Keep in mind, that you might want to pass the allocator as an (part of the)
   110  * argument for the <code>data</code> parameter, if you want the copy_func() to
   111  * make use of the allocator.
   112  * 
   113  * @param allocator the allocator to use
   114  * @param list the list to copy
   115  * @param cpyfnc a pointer to the function that shall copy an element (may be
   116  * <code>NULL</code>)
   117  * @param data additional data for the copy_func()
   118  * @return a pointer to the copy
   119  * @see ucx_list_clone()
   120  */
   121 UcxList *ucx_list_clone_a(UcxAllocator *allocator, UcxList *list,
   122         copy_func cpyfnc, void* data);
   124 /**
   125  * Compares two UCX lists element-wise by using a compare function.
   126  * 
   127  * Each element of the two specified lists are compared by using the specified
   128  * compare function and the additional data. The type and content of this
   129  * additional data depends on the cmp_func() used.
   130  * 
   131  * If the list pointers denote elements within a list, the lists are compared
   132  * starting with the denoted elements. Thus any previous elements are not taken
   133  * into account. This might be useful to check, if certain list tails match
   134  * each other.
   135  * 
   136  * @param list1 the first list
   137  * @param list2 the second list
   138  * @param cmpfnc the compare function
   139  * @param data additional data for the compare function
   140  * @return 1, if and only if the two lists equal element-wise, 0 otherwise
   141  */
   142 int ucx_list_equals(const UcxList *list1, const UcxList *list2,
   143         cmp_func cmpfnc, void* data);
   145 /**
   146  * Destroys the entire list.
   147  * 
   148  * The members of the list are not automatically freed, so ensure they are
   149  * otherwise referenced or destroyed by ucx_list_free_contents().
   150  * Otherwise, a memory leak is likely to occur.
   151  * 
   152  * <b>Caution:</b> the argument <b>MUST</b> denote an entire list (i.e. a call
   153  * to ucx_list_first() on the argument must return the argument itself)
   154  * 
   155  * @param list the list to free
   156  * @see ucx_list_free_contents()
   157  */
   158 void ucx_list_free(UcxList *list);
   160 /**
   161  * Destroys the entire list using an UcxAllocator.
   162  * 
   163  * See ucx_list_free() for details.
   164  * 
   165  * @param allocator the allocator to use
   166  * @param list the list to free
   167  * @see ucx_list_free()
   168  */
   169 void ucx_list_free_a(UcxAllocator *allocator, UcxList *list);
   171 /**
   172  * Destroys the contents of the specified list by calling the specified
   173  * destructor on each of them.
   174  * 
   175  * Note, that the contents are not usable afterwards and the list should be
   176  * destroyed with ucx_list_free().
   177  * 
   178  * @param list the list for which the contents shall be freed
   179  * @param destr the destructor function (e.g. stdlib free())
   180  * @see ucx_list_free()
   181  */
   182 void ucx_list_free_content(UcxList* list, ucx_destructor destr);
   185 /**
   186  * Inserts an element at the end of the list.
   187  * 
   188  * This is generally an O(n) operation, as the end of the list is retrieved with
   189  * ucx_list_last().
   190  * 
   191  * @param list the list where to append the data, or <code>NULL</code> to
   192  * create a new list
   193  * @param data the data to insert
   194  * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to
   195  * the newly created list otherwise
   196  */
   197 UcxList *ucx_list_append(UcxList *list, void *data);
   199 /**
   200  * Inserts an element at the end of the list using an UcxAllocator.
   201  * 
   202  * See ucx_list_append() for details.
   203  * 
   204  * @param allocator the allocator to use
   205  * @param list the list where to append the data, or <code>NULL</code> to
   206  * create a new list
   207  * @param data the data to insert
   208  * @return <code>list</code>, if it is not <code>NULL</code> or a pointer to
   209  * the newly created list otherwise
   210  * @see ucx_list_append()
   211  */
   212 UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data);
   214 /**
   215  * Inserts an element at the beginning of the list.
   216  * 
   217  * You <i>should</i> overwrite the old list pointer by calling
   218  * <code>mylist = ucx_list_prepend(mylist, mydata);</code>. However, you may
   219  * also perform successive calls of ucx_list_prepend() on the same list pointer,
   220  * as this function always searchs for the head of the list with
   221  * ucx_list_first().
   222  * 
   223  * @param list the list where to insert the data or <code>NULL</code> to create
   224  * a new list
   225  * @param data the data to insert
   226  * @return a pointer to the new list head
   227  */
   228 UcxList *ucx_list_prepend(UcxList *list, void *data);
   230 /**
   231  * Inserts an element at the beginning of the list using an UcxAllocator.
   232  * 
   233  * See ucx_list_prepend() for details.
   234  * 
   235  * @param allocator the allocator to use
   236  * @param list the list where to insert the data or <code>NULL</code> to create
   237  * a new list
   238  * @param data the data to insert
   239  * @return a pointer to the new list head
   240  * @see ucx_list_prepend()
   241  */
   242 UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data);
   244 /**
   245  * Concatenates two lists.
   246  * 
   247  * Either of the two arguments may be <code>NULL</code>.
   248  * 
   249  * This function modifies the references to the next/previous element of
   250  * the last/first element of <code>list1</code>/<code>
   251  * list2</code>.
   252  * 
   253  * @param list1 first list
   254  * @param list2 second list
   255  * @return if <code>list1</code> is <code>NULL</code>, <code>list2</code> is
   256  * returned, otherwise <code>list1</code> is returned
   257  */
   258 UcxList *ucx_list_concat(UcxList *list1, UcxList *list2);
   260 /**
   261  * Returns the first element of a list.
   262  * 
   263  * If the argument is the list pointer, it is directly returned. Otherwise
   264  * this function traverses to the first element of the list and returns the
   265  * list pointer.
   266  * 
   267  * @param elem one element of the list
   268  * @return the first element of the list, the specified element is a member of
   269  */
   270 UcxList *ucx_list_first(const UcxList *elem);
   272 /**
   273  * Returns the last element of a list.
   274  * 
   275  * If the argument has no successor, it is the last element and therefore
   276  * directly returned. Otherwise this function traverses to the last element of
   277  * the list and returns it.
   278  * 
   279  * @param elem one element of the list
   280  * @return the last element of the list, the specified element is a member of
   281  */
   282 UcxList *ucx_list_last(const UcxList *elem);
   284 /**
   285  * Returns the list element at the specified index.
   286  * 
   287  * @param list the list to retrieve the element from
   288  * @param index index of the element to return
   289  * @return the element at the specified index or <code>NULL</code>, if the
   290  * index is greater than the list size
   291  */
   292 UcxList *ucx_list_get(const UcxList *list, size_t index);
   294 /**
   295  * Returns the index of an element.
   296  * 
   297  * @param list the list where to search for the element
   298  * @param elem the element to find
   299  * @return the index of the element or -1 if the list does not contain the
   300  * element
   301  */
   302 ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem);
   304 /**
   305  * Returns the element count of the list.
   306  * 
   307  * @param list the list whose elements are counted
   308  * @return the element count
   309  */
   310 size_t ucx_list_size(const UcxList *list);
   312 /**
   313  * Returns the index of an element containing the specified data.
   314  *
   315  * This function uses a cmp_func() to compare the data of each list element
   316  * with the specified data. If no cmp_func is provided, the pointers are
   317  * compared.
   318  * 
   319  * If the list contains the data more than once, the index of the first
   320  * occurrence is returned.
   321  *  
   322  * @param list the list where to search for the data
   323  * @param elem the element data
   324  * @param cmpfnc the compare function
   325  * @param data additional data for the compare function
   326  * @return the index of the element containing the specified data or -1 if the
   327  * data is not found in this list
   328  */
   329 ssize_t ucx_list_find(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
   331 /**
   332  * Checks, if a list contains a specific element.
   333  * 
   334  * An element is found, if ucx_list_find() returns a value greater than -1.
   335  * 
   336  * @param list the list where to search for the data
   337  * @param elem the element data
   338  * @param cmpfnc the compare function
   339  * @param data additional data for the compare function
   340  * @return 1, if and only if the list contains the specified element data
   341  * @see ucx_list_find()
   342  */
   343 int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data);
   345 /**
   346  * Sorts an UcxList with natural merge sort.
   347  * 
   348  * This function uses O(n) additional temporary memory for merge operations
   349  * that is automatically freed after each merge.
   350  * 
   351  * As the head of the list might change, you <b>MUST</b> call this function
   352  * as follows: <code>mylist = ucx_list_sort(mylist, mycmpfnc, mydata);</code>.
   353  * 
   354  * @param list the list to sort
   355  * @param cmpfnc the function that shall be used to compare the element data
   356  * @param data additional data for the cmp_func()
   357  * @return the sorted list
   358  */
   359 UcxList *ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data);
   361 /**
   362  * Removes an element from the list.
   363  * 
   364  * If the first element is removed, the list pointer changes. So it is
   365  * <i>highly recommended</i> to <i>always</i> update the pointer by calling
   366  * <code>mylist = ucx_list_remove(mylist, myelem);</code>.
   367  * 
   368  * @param list the list from which the element shall be removed
   369  * @param element the element to remove
   370  * @return returns the updated list pointer or <code>NULL</code>, if the list
   371  * is now empty
   372  */
   373 UcxList *ucx_list_remove(UcxList *list, UcxList *element);
   375 /**
   376  * Removes an element from the list using an UcxAllocator.
   377  * 
   378  * See ucx_list_remove() for details.
   379  * 
   380  * @param allocator the allocator to use
   381  * @param list the list from which the element shall be removed
   382  * @param element the element to remove
   383  * @return returns the updated list pointer or <code>NULL</code>, if the list
   384  * @see ucx_list_remove()
   385  */
   386 UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list,
   387         UcxList *element);
   389 #ifdef	__cplusplus
   390 }
   391 #endif
   393 #endif	/* UCX_LIST_H */

mercurial