universe@39: /* universe@39: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@39: * universe@39: * Copyright 2015 Olaf Wintermann. All rights reserved. universe@39: * universe@39: * Redistribution and use in source and binary forms, with or without universe@39: * modification, are permitted provided that the following conditions are met: universe@39: * universe@39: * 1. Redistributions of source code must retain the above copyright universe@39: * notice, this list of conditions and the following disclaimer. universe@39: * universe@39: * 2. Redistributions in binary form must reproduce the above copyright universe@39: * notice, this list of conditions and the following disclaimer in the universe@39: * documentation and/or other materials provided with the distribution. universe@39: * universe@39: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@39: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@39: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@39: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@39: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@39: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@39: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@39: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@39: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@39: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@39: * POSSIBILITY OF SUCH DAMAGE. universe@39: */ universe@39: /** universe@39: * Doubly linked list implementation. universe@39: * universe@39: * @file list.h universe@39: * @author Mike Becker universe@39: * @author Olaf Wintermann universe@39: */ universe@39: universe@39: #ifndef UCX_LIST_H universe@39: #define UCX_LIST_H universe@39: universe@39: #include "ucx.h" universe@39: #include "allocator.h" universe@39: universe@39: #ifdef __cplusplus universe@39: extern "C" { universe@39: #endif universe@39: universe@39: /** universe@39: * Loop statement for UCX lists. universe@39: * universe@39: * The first argument is the name of the iteration variable. The scope of universe@39: * this variable is limited to the UCX_FOREACH statement. universe@39: * universe@39: * The second argument is a pointer to the list. In most cases this will be the universe@39: * pointer to the first element of the list, but it may also be an arbitrary universe@39: * element of the list. The iteration will then start with that element. universe@39: * universe@39: * universe@39: * @param list The first element of the list universe@39: * @param elem The variable name of the element universe@39: */ universe@39: #define UCX_FOREACH(elem,list) \ universe@39: for (UcxList* elem = list ; elem != NULL ; elem = elem->next) universe@39: universe@39: /** universe@39: * UCX list type. universe@39: * @see UcxList universe@39: */ universe@39: typedef struct UcxList UcxList; universe@39: universe@39: /** universe@39: * UCX list structure. universe@39: */ universe@39: struct UcxList { universe@39: /** universe@39: * List element payload. universe@39: */ universe@39: void *data; universe@39: /** universe@39: * Pointer to the next list element or NULL, if this is the universe@39: * last element. universe@39: */ universe@39: UcxList *next; universe@39: /** universe@39: * Pointer to the previous list element or NULL, if this is universe@39: * the first element. universe@39: */ universe@39: UcxList *prev; universe@39: }; universe@39: universe@39: /** universe@39: * Creates an element-wise copy of a list. universe@39: * universe@39: * This function clones the specified list by creating new list elements and universe@39: * copying the data with the specified copy_func(). If no copy_func() is universe@39: * specified, a shallow copy is created and the new list will reference the universe@39: * same data as the source list. universe@39: * universe@39: * @param list the list to copy universe@39: * @param cpyfnc a pointer to the function that shall copy an element (may be universe@39: * NULL) universe@39: * @param data additional data for the copy_func() universe@39: * @return a pointer to the copy universe@39: */ universe@39: UcxList *ucx_list_clone(UcxList *list, copy_func cpyfnc, void* data); universe@39: universe@39: /** universe@39: * Creates an element-wise copy of a list using an UcxAllocator. universe@39: * universe@39: * See ucx_list_clone() for details. universe@39: * universe@39: * Keep in mind, that you might want to pass the allocator as an (part of the) universe@39: * argument for the data parameter, if you want the copy_func() to universe@39: * make use of the allocator. universe@39: * universe@39: * @param allocator the allocator to use universe@39: * @param list the list to copy universe@39: * @param cpyfnc a pointer to the function that shall copy an element (may be universe@39: * NULL) universe@39: * @param data additional data for the copy_func() universe@39: * @return a pointer to the copy universe@39: * @see ucx_list_clone() universe@39: */ universe@39: UcxList *ucx_list_clone_a(UcxAllocator *allocator, UcxList *list, universe@39: copy_func cpyfnc, void* data); universe@39: universe@39: /** universe@39: * Compares two UCX lists element-wise by using a compare function. universe@39: * universe@39: * Each element of the two specified lists are compared by using the specified universe@39: * compare function and the additional data. The type and content of this universe@39: * additional data depends on the cmp_func() used. universe@39: * universe@39: * If the list pointers denote elements within a list, the lists are compared universe@39: * starting with the denoted elements. Thus any previous elements are not taken universe@39: * into account. This might be useful to check, if certain list tails match universe@39: * each other. universe@39: * universe@39: * @param list1 the first list universe@39: * @param list2 the second list universe@39: * @param cmpfnc the compare function universe@39: * @param data additional data for the compare function universe@39: * @return 1, if and only if the two lists equal element-wise, 0 otherwise universe@39: */ universe@39: int ucx_list_equals(const UcxList *list1, const UcxList *list2, universe@39: cmp_func cmpfnc, void* data); universe@39: universe@39: /** universe@39: * Destroys the entire list. universe@39: * universe@39: * The members of the list are not automatically freed, so ensure they are universe@39: * otherwise referenced or destroyed by ucx_list_free_contents(). universe@39: * Otherwise, a memory leak is likely to occur. universe@39: * universe@39: * Caution: the argument MUST denote an entire list (i.e. a call universe@39: * to ucx_list_first() on the argument must return the argument itself) universe@39: * universe@39: * @param list the list to free universe@39: * @see ucx_list_free_contents() universe@39: */ universe@39: void ucx_list_free(UcxList *list); universe@39: universe@39: /** universe@39: * Destroys the entire list using an UcxAllocator. universe@39: * universe@39: * See ucx_list_free() for details. universe@39: * universe@39: * @param allocator the allocator to use universe@39: * @param list the list to free universe@39: * @see ucx_list_free() universe@39: */ universe@39: void ucx_list_free_a(UcxAllocator *allocator, UcxList *list); universe@39: universe@39: /** universe@39: * Destroys the contents of the specified list by calling the specified universe@39: * destructor on each of them. universe@39: * universe@39: * Note, that the contents are not usable afterwards and the list should be universe@39: * destroyed with ucx_list_free(). universe@39: * universe@39: * @param list the list for which the contents shall be freed universe@39: * @param destr the destructor function (e.g. stdlib free()) universe@39: * @see ucx_list_free() universe@39: */ universe@39: void ucx_list_free_content(UcxList* list, ucx_destructor destr); universe@39: universe@39: universe@39: /** universe@39: * Inserts an element at the end of the list. universe@39: * universe@39: * This is generally an O(n) operation, as the end of the list is retrieved with universe@39: * ucx_list_last(). universe@39: * universe@39: * @param list the list where to append the data, or NULL to universe@39: * create a new list universe@39: * @param data the data to insert universe@39: * @return list, if it is not NULL or a pointer to universe@39: * the newly created list otherwise universe@39: */ universe@39: UcxList *ucx_list_append(UcxList *list, void *data); universe@39: universe@39: /** universe@39: * Inserts an element at the end of the list using an UcxAllocator. universe@39: * universe@39: * See ucx_list_append() for details. universe@39: * universe@39: * @param allocator the allocator to use universe@39: * @param list the list where to append the data, or NULL to universe@39: * create a new list universe@39: * @param data the data to insert universe@39: * @return list, if it is not NULL or a pointer to universe@39: * the newly created list otherwise universe@39: * @see ucx_list_append() universe@39: */ universe@39: UcxList *ucx_list_append_a(UcxAllocator *allocator, UcxList *list, void *data); universe@39: universe@39: /** universe@39: * Inserts an element at the beginning of the list. universe@39: * universe@39: * You should overwrite the old list pointer by calling universe@39: * mylist = ucx_list_prepend(mylist, mydata);. However, you may universe@39: * also perform successive calls of ucx_list_prepend() on the same list pointer, universe@39: * as this function always searchs for the head of the list with universe@39: * ucx_list_first(). universe@39: * universe@39: * @param list the list where to insert the data or NULL to create universe@39: * a new list universe@39: * @param data the data to insert universe@39: * @return a pointer to the new list head universe@39: */ universe@39: UcxList *ucx_list_prepend(UcxList *list, void *data); universe@39: universe@39: /** universe@39: * Inserts an element at the beginning of the list using an UcxAllocator. universe@39: * universe@39: * See ucx_list_prepend() for details. universe@39: * universe@39: * @param allocator the allocator to use universe@39: * @param list the list where to insert the data or NULL to create universe@39: * a new list universe@39: * @param data the data to insert universe@39: * @return a pointer to the new list head universe@39: * @see ucx_list_prepend() universe@39: */ universe@39: UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data); universe@39: universe@39: /** universe@39: * Concatenates two lists. universe@39: * universe@39: * Either of the two arguments may be NULL. universe@39: * universe@39: * This function modifies the references to the next/previous element of universe@39: * the last/first element of list1/ universe@39: * list2. universe@39: * universe@39: * @param list1 first list universe@39: * @param list2 second list universe@39: * @return if list1 is NULL, list2 is universe@39: * returned, otherwise list1 is returned universe@39: */ universe@39: UcxList *ucx_list_concat(UcxList *list1, UcxList *list2); universe@39: universe@39: /** universe@39: * Returns the first element of a list. universe@39: * universe@39: * If the argument is the list pointer, it is directly returned. Otherwise universe@39: * this function traverses to the first element of the list and returns the universe@39: * list pointer. universe@39: * universe@39: * @param elem one element of the list universe@39: * @return the first element of the list, the specified element is a member of universe@39: */ universe@39: UcxList *ucx_list_first(const UcxList *elem); universe@39: universe@39: /** universe@39: * Returns the last element of a list. universe@39: * universe@39: * If the argument has no successor, it is the last element and therefore universe@39: * directly returned. Otherwise this function traverses to the last element of universe@39: * the list and returns it. universe@39: * universe@39: * @param elem one element of the list universe@39: * @return the last element of the list, the specified element is a member of universe@39: */ universe@39: UcxList *ucx_list_last(const UcxList *elem); universe@39: universe@39: /** universe@39: * Returns the list element at the specified index. universe@39: * universe@39: * @param list the list to retrieve the element from universe@39: * @param index index of the element to return universe@39: * @return the element at the specified index or NULL, if the universe@39: * index is greater than the list size universe@39: */ universe@39: UcxList *ucx_list_get(const UcxList *list, size_t index); universe@39: universe@39: /** universe@39: * Returns the index of an element. universe@39: * universe@39: * @param list the list where to search for the element universe@39: * @param elem the element to find universe@39: * @return the index of the element or -1 if the list does not contain the universe@39: * element universe@39: */ universe@39: ssize_t ucx_list_indexof(const UcxList *list, const UcxList *elem); universe@39: universe@39: /** universe@39: * Returns the element count of the list. universe@39: * universe@39: * @param list the list whose elements are counted universe@39: * @return the element count universe@39: */ universe@39: size_t ucx_list_size(const UcxList *list); universe@39: universe@39: /** universe@39: * Returns the index of an element containing the specified data. universe@39: * universe@39: * This function uses a cmp_func() to compare the data of each list element universe@39: * with the specified data. If no cmp_func is provided, the pointers are universe@39: * compared. universe@39: * universe@39: * If the list contains the data more than once, the index of the first universe@39: * occurrence is returned. universe@39: * universe@39: * @param list the list where to search for the data universe@39: * @param elem the element data universe@39: * @param cmpfnc the compare function universe@39: * @param data additional data for the compare function universe@39: * @return the index of the element containing the specified data or -1 if the universe@39: * data is not found in this list universe@39: */ universe@39: ssize_t ucx_list_find(UcxList *list, void *elem, cmp_func cmpfnc, void *data); universe@39: universe@39: /** universe@39: * Checks, if a list contains a specific element. universe@39: * universe@39: * An element is found, if ucx_list_find() returns a value greater than -1. universe@39: * universe@39: * @param list the list where to search for the data universe@39: * @param elem the element data universe@39: * @param cmpfnc the compare function universe@39: * @param data additional data for the compare function universe@39: * @return 1, if and only if the list contains the specified element data universe@39: * @see ucx_list_find() universe@39: */ universe@39: int ucx_list_contains(UcxList *list, void *elem, cmp_func cmpfnc, void *data); universe@39: universe@39: /** universe@39: * Sorts an UcxList with natural merge sort. universe@39: * universe@39: * This function uses O(n) additional temporary memory for merge operations universe@39: * that is automatically freed after each merge. universe@39: * universe@39: * As the head of the list might change, you MUST call this function universe@39: * as follows: mylist = ucx_list_sort(mylist, mycmpfnc, mydata);. universe@39: * universe@39: * @param list the list to sort universe@39: * @param cmpfnc the function that shall be used to compare the element data universe@39: * @param data additional data for the cmp_func() universe@39: * @return the sorted list universe@39: */ universe@39: UcxList *ucx_list_sort(UcxList *list, cmp_func cmpfnc, void *data); universe@39: universe@39: /** universe@39: * Removes an element from the list. universe@39: * universe@39: * If the first element is removed, the list pointer changes. So it is universe@39: * highly recommended to always update the pointer by calling universe@39: * mylist = ucx_list_remove(mylist, myelem);. universe@39: * universe@39: * @param list the list from which the element shall be removed universe@39: * @param element the element to remove universe@39: * @return returns the updated list pointer or NULL, if the list universe@39: * is now empty universe@39: */ universe@39: UcxList *ucx_list_remove(UcxList *list, UcxList *element); universe@39: universe@39: /** universe@39: * Removes an element from the list using an UcxAllocator. universe@39: * universe@39: * See ucx_list_remove() for details. universe@39: * universe@39: * @param allocator the allocator to use universe@39: * @param list the list from which the element shall be removed universe@39: * @param element the element to remove universe@39: * @return returns the updated list pointer or NULL, if the list universe@39: * @see ucx_list_remove() universe@39: */ universe@39: UcxList *ucx_list_remove_a(UcxAllocator *allocator, UcxList *list, universe@39: UcxList *element); universe@39: universe@39: #ifdef __cplusplus universe@39: } universe@39: #endif universe@39: universe@39: #endif /* UCX_LIST_H */ universe@39: