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