Tue, 05 Oct 2021 12:23:46 +0200
add documentation for list.h
universe@390 | 1 | /* |
universe@390 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@390 | 3 | * |
universe@390 | 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
universe@390 | 5 | * |
universe@390 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@390 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@390 | 8 | * |
universe@390 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@390 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@390 | 11 | * |
universe@390 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@390 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@390 | 14 | * documentation and/or other materials provided with the distribution. |
universe@390 | 15 | * |
universe@390 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@390 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@390 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@390 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@390 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@390 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@390 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@390 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@390 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@390 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@390 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@390 | 27 | */ |
universe@453 | 28 | /** |
universe@453 | 29 | * \file list.h |
universe@453 | 30 | * \brief Interface for list implementations. |
universe@453 | 31 | * \author Mike Becker |
universe@453 | 32 | * \author Olaf Wintermann |
universe@453 | 33 | * \version 3.0 |
universe@453 | 34 | * \copyright 2-Clause BSD License |
universe@453 | 35 | */ |
universe@390 | 36 | |
universe@390 | 37 | #ifndef UCX_LIST_H |
universe@390 | 38 | #define UCX_LIST_H |
universe@390 | 39 | |
universe@398 | 40 | #include <stdlib.h> |
universe@398 | 41 | #include "allocator.h" |
universe@398 | 42 | |
universe@415 | 43 | #ifdef __cplusplus |
universe@415 | 44 | extern "C" { |
universe@415 | 45 | #endif |
universe@415 | 46 | |
universe@464 | 47 | /** |
universe@464 | 48 | * A comparator function comparing two list elements. |
universe@464 | 49 | */ |
universe@412 | 50 | typedef int(*CxListComparator)(void const *left, void const *right); |
universe@398 | 51 | |
universe@464 | 52 | /** |
universe@464 | 53 | * Internal type for the list structure - use CxList instead. |
universe@464 | 54 | */ |
universe@435 | 55 | typedef struct cx_list_s cx_list_s; |
universe@435 | 56 | |
universe@464 | 57 | /** |
universe@464 | 58 | * The class definition for arbitrary lists. |
universe@464 | 59 | */ |
universe@398 | 60 | typedef struct { |
universe@464 | 61 | /** |
universe@464 | 62 | * Member function for adding an element. |
universe@464 | 63 | */ |
universe@435 | 64 | int (*add)(cx_list_s *list, void *elem); |
universe@435 | 65 | |
universe@464 | 66 | /** |
universe@464 | 67 | * Member function for inserting an element. |
universe@464 | 68 | */ |
universe@435 | 69 | int (*insert)(cx_list_s *list, size_t index, void *elem); |
universe@435 | 70 | |
universe@464 | 71 | /** |
universe@464 | 72 | * Member function for removing an element. |
universe@464 | 73 | */ |
universe@438 | 74 | int (*remove)(cx_list_s *list, size_t index); |
universe@435 | 75 | |
universe@464 | 76 | /** |
universe@464 | 77 | * Member function for element lookup. |
universe@464 | 78 | */ |
universe@439 | 79 | void *(*at)(cx_list_s *list, size_t index); |
universe@439 | 80 | |
universe@464 | 81 | /** |
universe@464 | 82 | * Member function for finding an element. |
universe@464 | 83 | */ |
universe@435 | 84 | size_t (*find)(cx_list_s *list, void *elem); |
universe@435 | 85 | |
universe@464 | 86 | /** |
universe@464 | 87 | * Member function for retrieving the last element. |
universe@464 | 88 | */ |
universe@435 | 89 | void *(*last)(cx_list_s *list); |
universe@435 | 90 | } cx_list_class; |
universe@435 | 91 | |
universe@464 | 92 | /** |
universe@464 | 93 | * Structure for holding the base data of a list. |
universe@464 | 94 | */ |
universe@435 | 95 | struct cx_list_s { |
universe@464 | 96 | /** |
universe@464 | 97 | * The list class definition. |
universe@464 | 98 | */ |
universe@435 | 99 | cx_list_class *cl; |
universe@464 | 100 | /** |
universe@464 | 101 | * The allocator to use. |
universe@464 | 102 | */ |
universe@398 | 103 | CxAllocator allocator; |
universe@464 | 104 | /** |
universe@464 | 105 | * The comparator function for the elements. |
universe@464 | 106 | */ |
universe@398 | 107 | CxListComparator cmpfunc; |
universe@464 | 108 | /** |
universe@464 | 109 | * The size of each element (payload only). |
universe@464 | 110 | */ |
universe@401 | 111 | size_t itemsize; |
universe@464 | 112 | /** |
universe@464 | 113 | * The size of the list (number of currently stored elements). |
universe@464 | 114 | */ |
universe@401 | 115 | size_t size; |
universe@464 | 116 | /** |
universe@464 | 117 | * The capacity of the list (maximum number of elements). |
universe@464 | 118 | */ |
universe@401 | 119 | size_t capacity; |
universe@435 | 120 | }; |
universe@398 | 121 | |
universe@464 | 122 | /** |
universe@464 | 123 | * Common type for all list implementations. |
universe@464 | 124 | */ |
universe@412 | 125 | typedef cx_list_s *CxList; |
universe@398 | 126 | |
universe@464 | 127 | /** |
universe@464 | 128 | * Adds an item to the end of the list. |
universe@464 | 129 | * |
universe@464 | 130 | * \remark It is implementation defined whether |
universe@464 | 131 | * the contents of the element are copied or the |
universe@464 | 132 | * pointer is added to the list. In the latter case |
universe@464 | 133 | * the \c itemsize of the list SHALL be \c sizeof(void*). |
universe@464 | 134 | * |
universe@464 | 135 | * @param list the list |
universe@464 | 136 | * @param elem a pointer to the element to add |
universe@464 | 137 | * @return zero on success, non-zero on memory allocation failure |
universe@464 | 138 | */ |
universe@398 | 139 | int cxListAdd(CxList list, void *elem); |
universe@398 | 140 | |
universe@464 | 141 | /** |
universe@464 | 142 | * Inserts an item at the specified index. |
universe@464 | 143 | * |
universe@464 | 144 | * If \p index equals the list \c size, this is effectively cxListAdd(). |
universe@464 | 145 | * |
universe@464 | 146 | * \remark It is implementation defined whether |
universe@464 | 147 | * the contents of the element are copied or the |
universe@464 | 148 | * pointer is added to the list. In the latter case |
universe@464 | 149 | * the \c itemsize of the list SHALL be \c sizeof(void*). |
universe@464 | 150 | * |
universe@464 | 151 | * @param list the list |
universe@464 | 152 | * @param index the index the element shall have |
universe@464 | 153 | * @param elem a pointer to the element to add |
universe@464 | 154 | * @return zero on success, non-zero on memory allocation failure |
universe@464 | 155 | * or when the index is out of bounds |
universe@464 | 156 | */ |
universe@398 | 157 | int cxListInsert(CxList list, size_t index, void *elem); |
universe@398 | 158 | |
universe@464 | 159 | /** |
universe@464 | 160 | * Removes the element at the specified index. |
universe@464 | 161 | * @param list the list |
universe@464 | 162 | * @param index the index of the element |
universe@464 | 163 | * @return zero on success, non-zero if the index is out of bounds |
universe@464 | 164 | */ |
universe@438 | 165 | int cxListRemove(CxList list, size_t index); |
universe@398 | 166 | |
universe@464 | 167 | /** |
universe@464 | 168 | * Returns a pointer to the element at the specified index. |
universe@464 | 169 | * |
universe@464 | 170 | * @param list the list |
universe@464 | 171 | * @param index the index of the element |
universe@464 | 172 | * @return a pointer to the element or \c NULL if the index is out of bounds |
universe@464 | 173 | */ |
universe@439 | 174 | void *cxListAt(CxList list, size_t index); |
universe@439 | 175 | |
universe@464 | 176 | /** |
universe@464 | 177 | * Returns the index of the first element that equals \p elem. |
universe@464 | 178 | * |
universe@464 | 179 | * Determining equality is performed by the list's comparator function. |
universe@464 | 180 | * |
universe@464 | 181 | * @param list the list |
universe@464 | 182 | * @param elem the element to find |
universe@464 | 183 | * @return the index of the element or \c (size+1) if the element is not found |
universe@464 | 184 | */ |
universe@398 | 185 | size_t cxListFind(CxList list, void *elem); |
universe@398 | 186 | |
universe@464 | 187 | /** |
universe@464 | 188 | * Returns a pointer to the last element of the list. |
universe@464 | 189 | * |
universe@464 | 190 | * This is effectively the same as cxListAt() with \c index=size-1, but |
universe@464 | 191 | * this implementation may be more efficient depending on the list structure |
universe@464 | 192 | * and the conrecte implementation of cxListAt(). |
universe@464 | 193 | * |
universe@464 | 194 | * @param list the list |
universe@464 | 195 | * @return a pointer to the last element or \c NULL if the list is empty |
universe@464 | 196 | */ |
universe@404 | 197 | void *cxListLast(CxList list); |
universe@404 | 198 | |
universe@415 | 199 | #ifdef __cplusplus |
universe@415 | 200 | } /* extern "C" */ |
universe@415 | 201 | #endif |
universe@415 | 202 | |
universe@393 | 203 | #endif /* UCX_LIST_H */ |