src/cx/list.h

Tue, 28 Dec 2021 17:38:02 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Dec 2021 17:38:02 +0100
changeset 489
af6be1e123aa
parent 488
9138acaa494b
child 490
e66551b47466
permissions
-rw-r--r--

add some const qualifiers

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2021 Mike Becker, 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  * \file list.h
    30  * \brief Interface for list implementations.
    31  * \author Mike Becker
    32  * \author Olaf Wintermann
    33  * \version 3.0
    34  * \copyright 2-Clause BSD License
    35  */
    37 #ifndef UCX_LIST_H
    38 #define UCX_LIST_H
    40 #include "common.h"
    41 #include "allocator.h"
    43 #ifdef __cplusplus
    44 extern "C" {
    45 #endif
    47 /**
    48  * A comparator function comparing two list elements.
    49  */
    50 typedef int(*CxListComparator)(
    51         void const *left,
    52         void const *right
    53 );
    55 /**
    56  * Internal type for the list structure - use CxList instead.
    57  */
    58 typedef struct cx_list_s cx_list_s;
    60 /**
    61  * The class definition for arbitrary lists.
    62  */
    63 typedef struct {
    64     /**
    65      * Member function for adding an element.
    66      */
    67     int (*add)(
    68             cx_list_s *list,
    69             void const *elem
    70     );
    72     /**
    73      * Member function for inserting an element.
    74      */
    75     int (*insert)(
    76             cx_list_s *list,
    77             size_t index,
    78             void const *elem
    79     );
    81     /**
    82      * Member function for removing an element.
    83      */
    84     int (*remove)(
    85             cx_list_s *list,
    86             size_t index
    87     );
    89     /**
    90      * Member function for element lookup.
    91      */
    92     void *(*at)(
    93             cx_list_s const *list,
    94             size_t index
    95     );
    97     /**
    98      * Member function for finding an element.
    99      */
   100     size_t (*find)(
   101             cx_list_s const *list,
   102             void const *elem
   103     );
   105     /**
   106      * Member function for sorting the list in place.
   107      */
   108     void (*sort)(cx_list_s *list);
   110     /**
   111      * Member function for comparing this list to another list of the same type.
   112      */
   113     int (*compare)(
   114             cx_list_s const *list,
   115             cx_list_s const *other
   116     );
   117 } cx_list_class;
   119 /**
   120  * Structure for holding the base data of a list.
   121  */
   122 struct cx_list_s {
   123     /**
   124      * The list class definition.
   125      */
   126     cx_list_class *cl;
   127     /**
   128      * The allocator to use.
   129      */
   130     CxAllocator allocator;
   131     /**
   132      * The comparator function for the elements.
   133      */
   134     CxListComparator cmpfunc;
   135     /**
   136      * The size of each element (payload only).
   137      */
   138     size_t itemsize;
   139     /**
   140      * The size of the list (number of currently stored elements).
   141      */
   142     size_t size;
   143     /**
   144      * The capacity of the list (maximum number of elements).
   145      */
   146     size_t capacity;
   147 };
   149 /**
   150  * Common type for all list implementations.
   151  */
   152 typedef cx_list_s *CxList;
   154 /**
   155  * Adds an item to the end of the list.
   156  *
   157  * \remark It is implementation defined whether
   158  * the contents of the element are copied or the
   159  * pointer is added to the list. In the latter case
   160  * the \c itemsize of the list SHALL be \c sizeof(void*).
   161  *
   162  * @param list the list
   163  * @param elem a pointer to the element to add
   164  * @return zero on success, non-zero on memory allocation failure
   165  */
   166 static inline int cxListAdd(
   167         CxList list,
   168         void const *elem
   169 ) {
   170     return list->cl->add(list, elem);
   171 }
   173 /**
   174  * Inserts an item at the specified index.
   175  *
   176  * If \p index equals the list \c size, this is effectively cxListAdd().
   177  *
   178  * \remark It is implementation defined whether
   179  * the contents of the element are copied or the
   180  * pointer is added to the list. In the latter case
   181  * the \c itemsize of the list SHALL be \c sizeof(void*).
   182  *
   183  * @param list the list
   184  * @param index the index the element shall have
   185  * @param elem a pointer to the element to add
   186  * @return zero on success, non-zero on memory allocation failure
   187  * or when the index is out of bounds
   188  */
   189 static inline int cxListInsert(
   190         CxList list,
   191         size_t index,
   192         void const *elem
   193 ) {
   194     return list->cl->insert(list, index, elem);
   195 }
   197 /**
   198  * Removes the element at the specified index.
   199  * @param list the list
   200  * @param index the index of the element
   201  * @return zero on success, non-zero if the index is out of bounds
   202  */
   203 static inline int cxListRemove(
   204         CxList list,
   205         size_t index
   206 ) {
   207     return list->cl->remove(list, index);
   208 }
   210 /**
   211  * Returns a pointer to the element at the specified index.
   212  *
   213  * @param list the list
   214  * @param index the index of the element
   215  * @return a pointer to the element or \c NULL if the index is out of bounds
   216  */
   217 static inline void *cxListAt(
   218         CxList list,
   219         size_t index
   220 ) {
   221     return list->cl->at(list, index);
   222 }
   224 /**
   225  * Returns the index of the first element that equals \p elem.
   226  *
   227  * Determining equality is performed by the list's comparator function.
   228  *
   229  * @param list the list
   230  * @param elem the element to find
   231  * @return the index of the element or \c (size+1) if the element is not found
   232  */
   233 static inline size_t cxListFind(
   234         CxList list,
   235         void const *elem
   236 ) {
   237     return list->cl->find(list, elem);
   238 }
   240 /**
   241  * Sorts the list in place.
   242  *
   243  * \remark The underlying sort algorithm is implementation defined.
   244  *
   245  * @param list the list
   246  */
   247 static inline void cxListSort(CxList list) {
   248     list->cl->sort(list);
   249 }
   251 /**
   252  * Compares a list to another list of the same type.
   253  *
   254  * First, the list sizes are compared. If they match, the lists are compared element-wise.
   255  *
   256  * @param list the list
   257  * @param other the list to compare to
   258  * @return zero, if both lists are equal element wise, negative if the first list is smaller, zero if the first list is larger
   259  */
   260 static inline int cxListCompare(
   261         CxList list,
   262         CxList other
   263 ) {
   264     return list->cl->compare(list, other);
   265 }
   267 #ifdef __cplusplus
   268 } /* extern "C" */
   269 #endif
   271 #endif /* UCX_LIST_H */

mercurial