src/ucx/list.h

Tue, 23 Aug 2016 13:49:38 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Aug 2016 13:49:38 +0200
changeset 39
ac35daceb24c
permissions
-rw-r--r--

adds UCX + changes how the input file is read (uses an consecutive memory area now)

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

mercurial