src/cx/list.h

Wed, 06 Oct 2021 14:10:19 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 06 Oct 2021 14:10:19 +0200
changeset 469
0458bff0b1cd
parent 464
7fafc95968fc
child 474
9c1fccda16bc
permissions
-rw-r--r--

add high level list sort and inlines method invocation functions

     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 <stdlib.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)(void const *left, void const *right);
    52 /**
    53  * Internal type for the list structure - use CxList instead.
    54  */
    55 typedef struct cx_list_s cx_list_s;
    57 /**
    58  * The class definition for arbitrary lists.
    59  */
    60 typedef struct {
    61     /**
    62      * Member function for adding an element.
    63      */
    64     int (*add)(cx_list_s *list, void *elem);
    66     /**
    67      * Member function for inserting an element.
    68      */
    69     int (*insert)(cx_list_s *list, size_t index, void *elem);
    71     /**
    72      * Member function for removing an element.
    73      */
    74     int (*remove)(cx_list_s *list, size_t index);
    76     /**
    77      * Member function for element lookup.
    78      */
    79     void *(*at)(cx_list_s *list, size_t index);
    81     /**
    82      * Member function for finding an element.
    83      */
    84     size_t (*find)(cx_list_s *list, void *elem);
    86     /**
    87      * Member function for retrieving the last element.
    88      */
    89     void *(*last)(cx_list_s *list);
    91     /**
    92      * Member function for sorting the list in place.
    93      */
    94     void (*sort)(cx_list_s *list);
    95 } cx_list_class;
    97 /**
    98  * Structure for holding the base data of a list.
    99  */
   100 struct cx_list_s {
   101     /**
   102      * The list class definition.
   103      */
   104     cx_list_class *cl;
   105     /**
   106      * The allocator to use.
   107      */
   108     CxAllocator allocator;
   109     /**
   110      * The comparator function for the elements.
   111      */
   112     CxListComparator cmpfunc;
   113     /**
   114      * The size of each element (payload only).
   115      */
   116     size_t itemsize;
   117     /**
   118      * The size of the list (number of currently stored elements).
   119      */
   120     size_t size;
   121     /**
   122      * The capacity of the list (maximum number of elements).
   123      */
   124     size_t capacity;
   125 };
   127 /**
   128  * Common type for all list implementations.
   129  */
   130 typedef cx_list_s *CxList;
   132 /**
   133  * Adds an item to the end of the list.
   134  *
   135  * \remark It is implementation defined whether
   136  * the contents of the element are copied or the
   137  * pointer is added to the list. In the latter case
   138  * the \c itemsize of the list SHALL be \c sizeof(void*).
   139  *
   140  * @param list the list
   141  * @param elem a pointer to the element to add
   142  * @return zero on success, non-zero on memory allocation failure
   143  */
   144 static inline int cxListAdd(CxList list, void *elem) {
   145     return list->cl->add(list, elem);
   146 }
   148 /**
   149  * Inserts an item at the specified index.
   150  *
   151  * If \p index equals the list \c size, this is effectively cxListAdd().
   152  *
   153  * \remark It is implementation defined whether
   154  * the contents of the element are copied or the
   155  * pointer is added to the list. In the latter case
   156  * the \c itemsize of the list SHALL be \c sizeof(void*).
   157  *
   158  * @param list the list
   159  * @param index the index the element shall have
   160  * @param elem a pointer to the element to add
   161  * @return zero on success, non-zero on memory allocation failure
   162  * or when the index is out of bounds
   163  */
   164 static inline int cxListInsert(CxList list, size_t index, void *elem) {
   165     return list->cl->insert(list, index, elem);
   166 }
   168 /**
   169  * Removes the element at the specified index.
   170  * @param list the list
   171  * @param index the index of the element
   172  * @return zero on success, non-zero if the index is out of bounds
   173  */
   174 static inline int cxListRemove(CxList list, size_t index) {
   175     return list->cl->remove(list, index);
   176 }
   178 /**
   179  * Returns a pointer to the element at the specified index.
   180  *
   181  * @param list the list
   182  * @param index the index of the element
   183  * @return a pointer to the element or \c NULL if the index is out of bounds
   184  */
   185 static inline void *cxListAt(CxList list, size_t index) {
   186     return list->cl->at(list, index);
   187 }
   189 /**
   190  * Returns the index of the first element that equals \p elem.
   191  *
   192  * Determining equality is performed by the list's comparator function.
   193  *
   194  * @param list the list
   195  * @param elem the element to find
   196  * @return the index of the element or \c (size+1) if the element is not found
   197  */
   198 static inline size_t cxListFind(CxList list, void *elem) {
   199     return list->cl->find(list, elem);
   200 }
   202 /**
   203  * Returns a pointer to the last element of the list.
   204  *
   205  * This is effectively the same as cxListAt() with \c index=size-1, but
   206  * this implementation may be more efficient depending on the list structure
   207  * and the conrecte implementation of cxListAt().
   208  *
   209  * @param list the list
   210  * @return a pointer to the last element or \c NULL if the list is empty
   211  */
   212 static inline void *cxListLast(CxList list) {
   213     return list->cl->last(list);
   214 }
   216 /**
   217  * Sorts the list in place.
   218  *
   219  * \remark The underlying sort algorithm is implementation defined.
   220  *
   221  * @param list the list
   222  */
   223 static inline void cxListSort(CxList list) {
   224     list->cl->sort(list);
   225 }
   227 #ifdef __cplusplus
   228 } /* extern "C" */
   229 #endif
   231 #endif /* UCX_LIST_H */

mercurial