src/cx/list.h

Tue, 15 Feb 2022 20:01:59 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 15 Feb 2022 20:01:59 +0100
changeset 505
03e9151bea32
parent 504
aaf89ce0cbf6
child 508
8aea65ae1eaf
permissions
-rw-r--r--

fix typo in documentation

     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"
    42 #include "iterator.h"
    44 #ifdef __cplusplus
    45 extern "C" {
    46 #endif
    48 /**
    49  * A comparator function comparing two list elements.
    50  */
    51 typedef int(*CxListComparator)(
    52         void const *left,
    53         void const *right
    54 );
    56 /**
    57  * List class type.
    58  */
    59 typedef struct cx_list_class_s cx_list_class;
    61 /**
    62  * Structure for holding the base data of a list.
    63  */
    64 struct cx_list_s {
    65     /**
    66      * The list class definition.
    67      */
    68     cx_list_class *cl;
    69     /**
    70      * The allocator to use.
    71      */
    72     CxAllocator *allocator;
    73     /**
    74      * A mandatory destructor for the list structure.
    75      */
    76     cx_destructor_func list_destructor;
    77     /**
    78      * An optional destructor for the list contents.
    79      */
    80     cx_destructor_func content_destructor;
    81     /**
    82      * The comparator function for the elements.
    83      */
    84     CxListComparator cmpfunc;
    85     /**
    86      * The size of each element (payload only).
    87      */
    88     size_t itemsize;
    89     /**
    90      * The size of the list (number of currently stored elements).
    91      */
    92     size_t size;
    93     /**
    94      * The capacity of the list (maximum number of elements).
    95      */
    96     size_t capacity;
    97     /**
    98      * Flag indicating whether cxListDestroy() shall free the list structure,
    99      * even if cx_list_s.list_destructor did not do that.
   100      */
   101     bool autofree;
   102     /**
   103      * Flag indicating whether cxListDestroy() shall free each list element,
   104      * even if cx_list_s.content_destructor did not do that.
   105      */
   106     bool autofree_contents;
   107 };
   109 /**
   110  * The class definition for arbitrary lists.
   111  */
   112 struct cx_list_class_s {
   113     /**
   114      * Member function for adding an element.
   115      */
   116     int (*add)(
   117             struct cx_list_s *list,
   118             void const *elem
   119     );
   121     /**
   122      * Member function for inserting an element.
   123      */
   124     int (*insert)(
   125             struct cx_list_s *list,
   126             size_t index,
   127             void const *elem
   128     );
   130     /**
   131      * Member function for inserting an element relative to an iterator position.
   132      */
   133     int (*insert_iter)(
   134             struct cx_iterator_s *iter,
   135             void const *elem,
   136             int prepend
   137     );
   139     /**
   140      * Member function for removing an element.
   141      */
   142     int (*remove)(
   143             struct cx_list_s *list,
   144             size_t index
   145     );
   147     /**
   148      * Member function for element lookup.
   149      */
   150     void *(*at)(
   151             struct cx_list_s const *list,
   152             size_t index
   153     );
   155     /**
   156      * Member function for finding an element.
   157      */
   158     size_t (*find)(
   159             struct cx_list_s const *list,
   160             void const *elem
   161     );
   163     /**
   164      * Member function for sorting the list in place.
   165      */
   166     void (*sort)(struct cx_list_s *list);
   168     /**
   169      * Member function for comparing this list to another list of the same type.
   170      */
   171     int (*compare)(
   172             struct cx_list_s const *list,
   173             struct cx_list_s const *other
   174     );
   176     /**
   177      * Member function for reversing the order of the items.
   178      */
   179     void (*reverse)(struct cx_list_s *list);
   181     /**
   182      * Returns an iterator pointing to the specified index.
   183      */
   184     struct cx_iterator_s (*iterator)(
   185             struct cx_list_s *list,
   186             size_t index
   187     );
   188 };
   190 /**
   191  * Common type for all list implementations.
   192  */
   193 typedef struct cx_list_s CxList;
   195 /**
   196  * Convenience function to configure the memory management for this list.
   197  *
   198  * @param list the list to configure
   199  * @param list_destructor an alternative list destructor to use (if \c NULL, the current destructor remains unchanged)
   200  * @param content_destructor the content destructor to use (if \c NULL, no content destructor is used)
   201  * @param list_autofree a flag indicating, if the list allocator shall free the list, if the destructor did not do that
   202  * @param content_autofree a flag indicating, if the list allocator shall free an element,
   203  * if the content destructor did not do that or no content destructor exists
   204  */
   205 static inline void cxListMemoryMgmt(
   206         CxList *list,
   207         cx_destructor_func list_destructor,
   208         cx_destructor_func content_destructor,
   209         bool list_autofree,
   210         bool content_autofree
   211 ) {
   212     if (list_destructor != NULL) list->list_destructor = list_destructor;
   213     list->content_destructor = content_destructor;
   214     list->autofree = list_autofree;
   215     list->autofree_contents = content_autofree;
   216 }
   218 /**
   219  * Adds an item to the end of the list.
   220  *
   221  * @param list the list
   222  * @param elem a pointer to the element to add
   223  * @return zero on success, non-zero on memory allocation failure
   224  */
   225 static inline int cxListAdd(
   226         CxList *list,
   227         void const *elem
   228 ) {
   229     return list->cl->add(list, elem);
   230 }
   232 /**
   233  * Inserts an item at the specified index.
   234  *
   235  * If \p index equals the list \c size, this is effectively cxListAdd().
   236  *
   237  * @param list the list
   238  * @param index the index the element shall have
   239  * @param elem a pointer to the element to add
   240  * @return zero on success, non-zero on memory allocation failure
   241  * or when the index is out of bounds
   242  * @see cxListInsertAfter()
   243  * @see cxListInsertBefore()
   244  */
   245 static inline int cxListInsert(
   246         CxList *list,
   247         size_t index,
   248         void const *elem
   249 ) {
   250     return list->cl->insert(list, index, elem);
   251 }
   253 /**
   254  * Inserts an element after the current location of the specified iterator.
   255  *
   256  * The used iterator remains operational, but all other active iterators should
   257  * be considered invalidated.
   258  *
   259  * If \p iter is not a list iterator, the behavior is undefined.
   260  * If \p iter is a past-the-end iterator, the new element gets appended to the list.
   261  *
   262  * @param iter an iterator
   263  * @param elem the element to insert
   264  * @return zero on success, non-zero on memory allocation failure
   265  * @see cxListInsert()
   266  * @see cxListInsertBefore()
   267  */
   268 static inline int cxListInsertAfter(
   269         CxIterator *iter,
   270         void const *elem
   271 ) {
   272     return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0);
   273 }
   275 /**
   276  * Inserts an element before the current location of the specified iterator.
   277  *
   278  * The used iterator remains operational, but all other active iterators should
   279  * be considered invalidated.
   280  *
   281  * If \p iter is not a list iterator, the behavior is undefined.
   282  * If \p iter is a past-the-end iterator, the new element gets appended to the list.
   283  *
   284  * @param iter an iterator
   285  * @param elem the element to insert
   286  * @return zero on success, non-zero on memory allocation failure
   287  * @see cxListInsert()
   288  * @see cxListInsertAfter()
   289  */
   290 static inline int cxListInsertBefore(
   291         CxIterator *iter,
   292         void const *elem
   293 ) {
   294     return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1);
   295 }
   297 /**
   298  * Removes the element at the specified index.
   299  * @param list the list
   300  * @param index the index of the element
   301  * @return zero on success, non-zero if the index is out of bounds
   302  */
   303 static inline int cxListRemove(
   304         CxList *list,
   305         size_t index
   306 ) {
   307     return list->cl->remove(list, index);
   308 }
   310 /**
   311  * Returns a pointer to the element at the specified index.
   312  *
   313  * @param list the list
   314  * @param index the index of the element
   315  * @return a pointer to the element or \c NULL if the index is out of bounds
   316  */
   317 static inline void *cxListAt(
   318         CxList *list,
   319         size_t index
   320 ) {
   321     return list->cl->at(list, index);
   322 }
   324 /**
   325  * Returns an iterator pointing to the item at the specified index.
   326  *
   327  * The returned iterator is position-aware.
   328  *
   329  * If the index is out of range, a past-the-end iterator will be returned.
   330  *
   331  * @param list the list
   332  * @param index the index where the iterator shall point at
   333  * @return a new iterator
   334  */
   335 static inline CxIterator cxListIterator(
   336         CxList *list,
   337         size_t index
   338 ) {
   339     return list->cl->iterator(list, index);
   340 }
   342 /**
   343  * Returns an iterator pointing to the first item of the list.
   344  *
   345  * The returned iterator is position-aware.
   346  *
   347  * If the list is empty, a past-the-end iterator will be returned.
   348  *
   349  * @param list the list
   350  * @return a new iterator
   351  */
   352 static inline CxIterator cxListBegin(CxList *list) {
   353     return list->cl->iterator(list, 0);
   354 }
   356 /**
   357  * Returns the index of the first element that equals \p elem.
   358  *
   359  * Determining equality is performed by the list's comparator function.
   360  *
   361  * @param list the list
   362  * @param elem the element to find
   363  * @return the index of the element or \c (size+1) if the element is not found
   364  */
   365 static inline size_t cxListFind(
   366         CxList *list,
   367         void const *elem
   368 ) {
   369     return list->cl->find(list, elem);
   370 }
   372 /**
   373  * Sorts the list in place.
   374  *
   375  * \remark The underlying sort algorithm is implementation defined.
   376  *
   377  * @param list the list
   378  */
   379 static inline void cxListSort(CxList *list) {
   380     list->cl->sort(list);
   381 }
   383 /**
   384  * Reverses the order of the items.
   385  *
   386  * @param list the list
   387  */
   388 static inline void cxListReverse(CxList *list) {
   389     list->cl->reverse(list);
   390 }
   392 /**
   393  * Compares a list to another list of the same type.
   394  *
   395  * First, the list sizes are compared. If they match, the lists are compared element-wise.
   396  *
   397  * @param list the list
   398  * @param other the list to compare to
   399  * @return zero, if both lists are equal element wise, negative if the first list is smaller, zero if the first list is larger
   400  */
   401 static inline int cxListCompare(
   402         CxList *list,
   403         CxList *other
   404 ) {
   405     return list->cl->compare(list, other);
   406 }
   408 /**
   409  * Calls the list's destructor function for every element.
   410  * If CxList.autofree_content is \c true, the elements are automatically free'd
   411  * unless the content destructor function did not already do that.
   412  * Similarly, if CxList.autofree is \c true, the list structure is free'd, unless
   413  * the list destructor function did not already do that.
   414  *
   415  * This function itself is a destructor function for the CxList.
   416  *
   417  * @param list the list which contents shall be destroyed
   418  * @return \p list if the list structure has been free'd during the process
   419  */
   420 __attribute__((__nonnull__))
   421 CxList *cxListDestroy(CxList *list);
   423 #ifdef __cplusplus
   424 } /* extern "C" */
   425 #endif
   427 #endif /* UCX_LIST_H */

mercurial