src/cx/list.h

Sat, 16 Apr 2022 20:44:47 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 16 Apr 2022 20:44:47 +0200
changeset 519
79d14e821b3a
parent 508
8aea65ae1eaf
child 524
e98b09018d32
permissions
-rw-r--r--

make cxListMemoryMgmt a linkable symbol

     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 const *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 __attribute__((__nonnull__(1)))
   206 void cxListMemoryMgmt(
   207         CxList *list,
   208         cx_destructor_func list_destructor,
   209         cx_destructor_func content_destructor,
   210         bool list_autofree,
   211         bool content_autofree
   212 );
   214 /**
   215  * Adds an item to the end of the list.
   216  *
   217  * @param list the list
   218  * @param elem a pointer to the element to add
   219  * @return zero on success, non-zero on memory allocation failure
   220  */
   221 __attribute__((__nonnull__))
   222 static inline int cxListAdd(
   223         CxList *list,
   224         void const *elem
   225 ) {
   226     return list->cl->add(list, elem);
   227 }
   229 /**
   230  * Inserts an item at the specified index.
   231  *
   232  * If \p index equals the list \c size, this is effectively cxListAdd().
   233  *
   234  * @param list the list
   235  * @param index the index the element shall have
   236  * @param elem a pointer to the element to add
   237  * @return zero on success, non-zero on memory allocation failure
   238  * or when the index is out of bounds
   239  * @see cxListInsertAfter()
   240  * @see cxListInsertBefore()
   241  */
   242 __attribute__((__nonnull__))
   243 static inline int cxListInsert(
   244         CxList *list,
   245         size_t index,
   246         void const *elem
   247 ) {
   248     return list->cl->insert(list, index, elem);
   249 }
   251 /**
   252  * Inserts an element after the current location of the specified iterator.
   253  *
   254  * The used iterator remains operational, but all other active iterators should
   255  * be considered invalidated.
   256  *
   257  * If \p iter is not a list iterator, the behavior is undefined.
   258  * If \p iter is a past-the-end iterator, the new element gets appended to the list.
   259  *
   260  * @param iter an iterator
   261  * @param elem the element to insert
   262  * @return zero on success, non-zero on memory allocation failure
   263  * @see cxListInsert()
   264  * @see cxListInsertBefore()
   265  */
   266 __attribute__((__nonnull__))
   267 static inline int cxListInsertAfter(
   268         CxIterator *iter,
   269         void const *elem
   270 ) {
   271     return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0);
   272 }
   274 /**
   275  * Inserts an element before the current location of the specified iterator.
   276  *
   277  * The used iterator remains operational, but all other active iterators should
   278  * be considered invalidated.
   279  *
   280  * If \p iter is not a list iterator, the behavior is undefined.
   281  * If \p iter is a past-the-end iterator, the new element gets appended to the list.
   282  *
   283  * @param iter an iterator
   284  * @param elem the element to insert
   285  * @return zero on success, non-zero on memory allocation failure
   286  * @see cxListInsert()
   287  * @see cxListInsertAfter()
   288  */
   289 __attribute__((__nonnull__))
   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 __attribute__((__nonnull__))
   304 static inline int cxListRemove(
   305         CxList *list,
   306         size_t index
   307 ) {
   308     return list->cl->remove(list, index);
   309 }
   311 /**
   312  * Returns a pointer to the element at the specified index.
   313  *
   314  * @param list the list
   315  * @param index the index of the element
   316  * @return a pointer to the element or \c NULL if the index is out of bounds
   317  */
   318 __attribute__((__nonnull__))
   319 static inline void *cxListAt(
   320         CxList *list,
   321         size_t index
   322 ) {
   323     return list->cl->at(list, index);
   324 }
   326 /**
   327  * Returns an iterator pointing to the item at the specified index.
   328  *
   329  * The returned iterator is position-aware.
   330  *
   331  * If the index is out of range, a past-the-end iterator will be returned.
   332  *
   333  * @param list the list
   334  * @param index the index where the iterator shall point at
   335  * @return a new iterator
   336  */
   337 __attribute__((__nonnull__, __warn_unused_result__))
   338 static inline CxIterator cxListIterator(
   339         CxList *list,
   340         size_t index
   341 ) {
   342     return list->cl->iterator(list, index);
   343 }
   345 /**
   346  * Returns an iterator pointing to the first item of the list.
   347  *
   348  * The returned iterator is position-aware.
   349  *
   350  * If the list is empty, a past-the-end iterator will be returned.
   351  *
   352  * @param list the list
   353  * @return a new iterator
   354  */
   355 __attribute__((__nonnull__, __warn_unused_result__))
   356 static inline CxIterator cxListBegin(CxList *list) {
   357     return list->cl->iterator(list, 0);
   358 }
   360 /**
   361  * Returns the index of the first element that equals \p elem.
   362  *
   363  * Determining equality is performed by the list's comparator function.
   364  *
   365  * @param list the list
   366  * @param elem the element to find
   367  * @return the index of the element or \c (size+1) if the element is not found
   368  */
   369 __attribute__((__nonnull__))
   370 static inline size_t cxListFind(
   371         CxList *list,
   372         void const *elem
   373 ) {
   374     return list->cl->find(list, elem);
   375 }
   377 /**
   378  * Sorts the list in place.
   379  *
   380  * \remark The underlying sort algorithm is implementation defined.
   381  *
   382  * @param list the list
   383  */
   384 __attribute__((__nonnull__))
   385 static inline void cxListSort(CxList *list) {
   386     list->cl->sort(list);
   387 }
   389 /**
   390  * Reverses the order of the items.
   391  *
   392  * @param list the list
   393  */
   394 __attribute__((__nonnull__))
   395 static inline void cxListReverse(CxList *list) {
   396     list->cl->reverse(list);
   397 }
   399 /**
   400  * Compares a list to another list of the same type.
   401  *
   402  * First, the list sizes are compared. If they match, the lists are compared element-wise.
   403  *
   404  * @param list the list
   405  * @param other the list to compare to
   406  * @return zero, if both lists are equal element wise, negative if the first list is smaller, zero if the first list is larger
   407  */
   408 __attribute__((__nonnull__))
   409 static inline int cxListCompare(
   410         CxList *list,
   411         CxList *other
   412 ) {
   413     return list->cl->compare(list, other);
   414 }
   416 /**
   417  * Calls the list's destructor function for every element.
   418  * If CxList.autofree_content is \c true, the elements are automatically free'd
   419  * unless the content destructor function did not already do that.
   420  * Similarly, if CxList.autofree is \c true, the list structure is free'd, unless
   421  * the list destructor function did not already do that.
   422  *
   423  * This function itself is a destructor function for the CxList.
   424  *
   425  * @param list the list which contents shall be destroyed
   426  * @return \p list if the list structure has not been free'd during the process
   427  */
   428 __attribute__((__nonnull__))
   429 CxList *cxListDestroy(CxList *list);
   431 #ifdef __cplusplus
   432 } /* extern "C" */
   433 #endif
   435 #endif /* UCX_LIST_H */

mercurial