src/cx/list.h

Tue, 28 Dec 2021 17:41:51 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Dec 2021 17:41:51 +0100
changeset 490
e66551b47466
parent 489
af6be1e123aa
child 494
6ce8cfa10a96
permissions
-rw-r--r--

add cxListReverse()

     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     );
   118     /**
   119      * Member function for reversing the order of the items.
   120      */
   121     void (*reverse)(cx_list_s *list);
   122 } cx_list_class;
   124 /**
   125  * Structure for holding the base data of a list.
   126  */
   127 struct cx_list_s {
   128     /**
   129      * The list class definition.
   130      */
   131     cx_list_class *cl;
   132     /**
   133      * The allocator to use.
   134      */
   135     CxAllocator allocator;
   136     /**
   137      * The comparator function for the elements.
   138      */
   139     CxListComparator cmpfunc;
   140     /**
   141      * The size of each element (payload only).
   142      */
   143     size_t itemsize;
   144     /**
   145      * The size of the list (number of currently stored elements).
   146      */
   147     size_t size;
   148     /**
   149      * The capacity of the list (maximum number of elements).
   150      */
   151     size_t capacity;
   152 };
   154 /**
   155  * Common type for all list implementations.
   156  */
   157 typedef cx_list_s *CxList;
   159 /**
   160  * Adds an item to the end of the list.
   161  *
   162  * \remark It is implementation defined whether
   163  * the contents of the element are copied or the
   164  * pointer is added to the list. In the latter case
   165  * the \c itemsize of the list SHALL be \c sizeof(void*).
   166  *
   167  * @param list the list
   168  * @param elem a pointer to the element to add
   169  * @return zero on success, non-zero on memory allocation failure
   170  */
   171 static inline int cxListAdd(
   172         CxList list,
   173         void const *elem
   174 ) {
   175     return list->cl->add(list, elem);
   176 }
   178 /**
   179  * Inserts an item at the specified index.
   180  *
   181  * If \p index equals the list \c size, this is effectively cxListAdd().
   182  *
   183  * \remark It is implementation defined whether
   184  * the contents of the element are copied or the
   185  * pointer is added to the list. In the latter case
   186  * the \c itemsize of the list SHALL be \c sizeof(void*).
   187  *
   188  * @param list the list
   189  * @param index the index the element shall have
   190  * @param elem a pointer to the element to add
   191  * @return zero on success, non-zero on memory allocation failure
   192  * or when the index is out of bounds
   193  */
   194 static inline int cxListInsert(
   195         CxList list,
   196         size_t index,
   197         void const *elem
   198 ) {
   199     return list->cl->insert(list, index, elem);
   200 }
   202 /**
   203  * Removes the element at the specified index.
   204  * @param list the list
   205  * @param index the index of the element
   206  * @return zero on success, non-zero if the index is out of bounds
   207  */
   208 static inline int cxListRemove(
   209         CxList list,
   210         size_t index
   211 ) {
   212     return list->cl->remove(list, index);
   213 }
   215 /**
   216  * Returns a pointer to the element at the specified index.
   217  *
   218  * @param list the list
   219  * @param index the index of the element
   220  * @return a pointer to the element or \c NULL if the index is out of bounds
   221  */
   222 static inline void *cxListAt(
   223         CxList list,
   224         size_t index
   225 ) {
   226     return list->cl->at(list, index);
   227 }
   229 /**
   230  * Returns the index of the first element that equals \p elem.
   231  *
   232  * Determining equality is performed by the list's comparator function.
   233  *
   234  * @param list the list
   235  * @param elem the element to find
   236  * @return the index of the element or \c (size+1) if the element is not found
   237  */
   238 static inline size_t cxListFind(
   239         CxList list,
   240         void const *elem
   241 ) {
   242     return list->cl->find(list, elem);
   243 }
   245 /**
   246  * Sorts the list in place.
   247  *
   248  * \remark The underlying sort algorithm is implementation defined.
   249  *
   250  * @param list the list
   251  */
   252 static inline void cxListSort(CxList list) {
   253     list->cl->sort(list);
   254 }
   256 /**
   257  * Reverses the order of the items.
   258  *
   259  * @param list the list
   260  */
   261 static inline void cxListReverse(CxList list) {
   262     list->cl->reverse(list);
   263 }
   265 /**
   266  * Compares a list to another list of the same type.
   267  *
   268  * First, the list sizes are compared. If they match, the lists are compared element-wise.
   269  *
   270  * @param list the list
   271  * @param other the list to compare to
   272  * @return zero, if both lists are equal element wise, negative if the first list is smaller, zero if the first list is larger
   273  */
   274 static inline int cxListCompare(
   275         CxList list,
   276         CxList other
   277 ) {
   278     return list->cl->compare(list, other);
   279 }
   281 #ifdef __cplusplus
   282 } /* extern "C" */
   283 #endif
   285 #endif /* UCX_LIST_H */

mercurial