src/cx/list.h

Tue, 14 Mar 2023 20:25:24 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 14 Mar 2023 20:25:24 +0100
changeset 664
af5bf4603a5d
parent 655
7340c4255f1f
child 666
b5dd654deb3b
permissions
-rw-r--r--

add cxListClear and fix missing destructor invocations - #241 #246

     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 const *cl;
    69     /**
    70      * The actual implementation in case the list class is delegating.
    71      */
    72     cx_list_class const *climpl;
    73     /**
    74      * The allocator to use.
    75      */
    76     CxAllocator const *allocator;
    77     /**
    78      * The comparator function for the elements.
    79      */
    80     CxListComparator cmpfunc;
    81     /**
    82      * The size of each element (payload only).
    83      */
    84     size_t itemsize;
    85     /**
    86      * The size of the list (number of currently stored elements).
    87      */
    88     size_t size;
    89     /**
    90      * The capacity of the list (maximum number of elements).
    91      */
    92     size_t capacity;
    93     union {
    94         /**
    95          * An optional simple destructor for the list contents that admits the free() interface.
    96          *
    97          * @remark Set content_destructor_type to #CX_DESTRUCTOR_SIMPLE.
    98          *
    99          * @attention Read the documentation of the particular list implementation
   100          * whether this destructor shall only destroy the contents or also free the memory.
   101          */
   102         cx_destructor_func simple_destructor;
   103         /**
   104          * An optional advanced destructor for the list contents providing additional data.
   105          *
   106          * @remark Set content_destructor_type to #CX_DESTRUCTOR_ADVANCED.
   107          *
   108          * @attention Read the documentation of the particular list implementation
   109          * whether this destructor shall only destroy the contents or also free the memory.
   110          */
   111         cx_advanced_destructor advanced_destructor;
   112     };
   113     /**
   114      * The type of destructor to use.
   115      */
   116     enum cx_destructor_type content_destructor_type;
   117 };
   119 /**
   120  * The class definition for arbitrary lists.
   121  */
   122 struct cx_list_class_s {
   123     /**
   124      * Destructor function.
   125      */
   126     void (*destructor)(struct cx_list_s *list);
   128     /**
   129      * Member function for inserting a single elements.
   130      * Implementors SHOULD see to performant implementations for corner cases.
   131      */
   132     int (*insert_element)(
   133             struct cx_list_s *list,
   134             size_t index,
   135             void const *data
   136     );
   138     /**
   139      * Member function for inserting multiple elements.
   140      * Implementors SHOULD see to performant implementations for corner cases.
   141      */
   142     size_t (*insert_array)(
   143             struct cx_list_s *list,
   144             size_t index,
   145             void const *data,
   146             size_t n
   147     );
   149     /**
   150      * Member function for inserting an element relative to an iterator position.
   151      */
   152     int (*insert_iter)(
   153             struct cx_mut_iterator_s *iter,
   154             void const *elem,
   155             int prepend
   156     );
   158     /**
   159      * Member function for removing an element.
   160      */
   161     int (*remove)(
   162             struct cx_list_s *list,
   163             size_t index
   164     );
   166     /**
   167      * Member function for removing all elements.
   168      */
   169     void (*clear)(struct cx_list_s *list);
   171     /**
   172      * Member function for swapping two elements.
   173      */
   174     int (*swap)(
   175             struct cx_list_s *list,
   176             size_t i,
   177             size_t j
   178     );
   180     /**
   181      * Member function for element lookup.
   182      */
   183     void *(*at)(
   184             struct cx_list_s const *list,
   185             size_t index
   186     );
   188     /**
   189      * Member function for finding an element.
   190      */
   191     size_t (*find)(
   192             struct cx_list_s const *list,
   193             void const *elem
   194     );
   196     /**
   197      * Member function for sorting the list in place.
   198      */
   199     void (*sort)(struct cx_list_s *list);
   201     /**
   202      * Member function for comparing this list to another list of the same type.
   203      */
   204     int (*compare)(
   205             struct cx_list_s const *list,
   206             struct cx_list_s const *other
   207     );
   209     /**
   210      * Member function for reversing the order of the items.
   211      */
   212     void (*reverse)(struct cx_list_s *list);
   214     /**
   215      * Member function for returning an iterator pointing to the specified index.
   216      */
   217     struct cx_iterator_s (*iterator)(
   218             struct cx_list_s const *list,
   219             size_t index,
   220             bool backward
   221     );
   222 };
   224 /**
   225  * Common type for all list implementations.
   226  */
   227 typedef struct cx_list_s CxList;
   229 /**
   230  * Invokes the destructor function for a specific element.
   231  *
   232  * Usually only used by list implementations. There should be no need
   233  * to invoke this function manually.
   234  *
   235  * @param list the list
   236  * @param elem the element
   237  */
   238 __attribute__((__nonnull__))
   239 void cx_list_invoke_destructor(
   240         struct cx_list_s const *list,
   241         void *elem
   242 );
   244 /**
   245  * Advises the list to store copies of the objects (default mode of operation).
   246  *
   247  * Retrieving objects from this list will yield pointers to the copies stored
   248  * within this list.
   249  *
   250  * @param list the list
   251  * @see cxListStorePointers()
   252  */
   253 __attribute__((__nonnull__))
   254 void cxListStoreObjects(CxList *list);
   256 /**
   257  * Advises the list to only store pointers to the objects.
   258  *
   259  * Retrieving objects from this list will yield the original pointers stored.
   260  *
   261  * @note This function forcibly sets the element size to the size of a pointer.
   262  * Invoking this function on a non-empty list that already stores copies of
   263  * objects is undefined.
   264  *
   265  * @param list the list
   266  * @see cxListStoreObjects()
   267  */
   268 __attribute__((__nonnull__))
   269 void cxListStorePointers(CxList *list);
   271 /**
   272  * Returns true, if this list is storing pointers instead of the actual data.
   273  *
   274  * @param list
   275  * @return
   276  * @see cxListStorePointers()
   277  */
   278 __attribute__((__nonnull__))
   279 bool cxListIsStoringPointers(CxList *list);
   281 /**
   282  * Adds an item to the end of the list.
   283  *
   284  * @param list the list
   285  * @param elem a pointer to the element to add
   286  * @return zero on success, non-zero on memory allocation failure
   287  * @see cxListAddArray()
   288  */
   289 __attribute__((__nonnull__))
   290 static inline int cxListAdd(
   291         CxList *list,
   292         void const *elem
   293 ) {
   294     return list->cl->insert_element(list, list->size, elem);
   295 }
   297 /**
   298  * Adds multiple items to the end of the list.
   299  *
   300  * This method is more efficient than invoking cxListAdd() multiple times.
   301  *
   302  * If there is not enough memory to add all elements, the returned value is
   303  * less than \p n.
   304  *
   305  * If this list is storing pointers instead of objects \p array is expected to
   306  * be an array of pointers.
   307  *
   308  * @param list the list
   309  * @param array a pointer to the elements to add
   310  * @param n the number of elements to add
   311  * @return the number of added elements
   312  */
   313 __attribute__((__nonnull__))
   314 static inline size_t cxListAddArray(
   315         CxList *list,
   316         void const *array,
   317         size_t n
   318 ) {
   319     return list->cl->insert_array(list, list->size, array, n);
   320 }
   322 /**
   323  * Inserts an item at the specified index.
   324  *
   325  * If \p index equals the list \c size, this is effectively cxListAdd().
   326  *
   327  * @param list the list
   328  * @param index the index the element shall have
   329  * @param elem a pointer to the element to add
   330  * @return zero on success, non-zero on memory allocation failure
   331  * or when the index is out of bounds
   332  * @see cxListInsertAfter()
   333  * @see cxListInsertBefore()
   334  */
   335 __attribute__((__nonnull__))
   336 static inline int cxListInsert(
   337         CxList *list,
   338         size_t index,
   339         void const *elem
   340 ) {
   341     return list->cl->insert_element(list, index, elem);
   342 }
   344 /**
   345  * Inserts multiple items to the list at the specified index.
   346  * If \p index equals the list size, this is effectively cxListAddArray().
   347  *
   348  * This method is usually more efficient than invoking cxListInsert()
   349  * multiple times.
   350  *
   351  * If there is not enough memory to add all elements, the returned value is
   352  * less than \p n.
   353  *
   354  * If this list is storing pointers instead of objects \p array is expected to
   355  * be an array of pointers.
   356  *
   357  * @param list the list
   358  * @param index the index where to add the elements
   359  * @param array a pointer to the elements to add
   360  * @param n the number of elements to add
   361  * @return the number of added elements
   362  */
   363 __attribute__((__nonnull__))
   364 static inline size_t cxListInsertArray(
   365         CxList *list,
   366         size_t index,
   367         void const *array,
   368         size_t n
   369 ) {
   370     return list->cl->insert_array(list, index, array, n);
   371 }
   373 /**
   374  * Inserts an element after the current location of the specified iterator.
   375  *
   376  * The used iterator remains operational, but all other active iterators should
   377  * be considered invalidated.
   378  *
   379  * If \p iter is not a list iterator, the behavior is undefined.
   380  * If \p iter is a past-the-end iterator, the new element gets appended to the list.
   381  *
   382  * @param iter an iterator
   383  * @param elem the element to insert
   384  * @return zero on success, non-zero on memory allocation failure
   385  * @see cxListInsert()
   386  * @see cxListInsertBefore()
   387  */
   388 __attribute__((__nonnull__))
   389 static inline int cxListInsertAfter(
   390         CxMutIterator *iter,
   391         void const *elem
   392 ) {
   393     return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0);
   394 }
   396 /**
   397  * Inserts an element before the current location of the specified iterator.
   398  *
   399  * The used iterator remains operational, but all other active iterators should
   400  * be considered invalidated.
   401  *
   402  * If \p iter is not a list iterator, the behavior is undefined.
   403  * If \p iter is a past-the-end iterator, the new element gets appended to the list.
   404  *
   405  * @param iter an iterator
   406  * @param elem the element to insert
   407  * @return zero on success, non-zero on memory allocation failure
   408  * @see cxListInsert()
   409  * @see cxListInsertAfter()
   410  */
   411 __attribute__((__nonnull__))
   412 static inline int cxListInsertBefore(
   413         CxMutIterator *iter,
   414         void const *elem
   415 ) {
   416     return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1);
   417 }
   419 /**
   420  * Removes the element at the specified index.
   421  *
   422  * If an element destructor function is specified, it is called before
   423  * removing the element.
   424  *
   425  * @param list the list
   426  * @param index the index of the element
   427  * @return zero on success, non-zero if the index is out of bounds
   428  */
   429 __attribute__((__nonnull__))
   430 static inline int cxListRemove(
   431         CxList *list,
   432         size_t index
   433 ) {
   434     return list->cl->remove(list, index);
   435 }
   437 /**
   438  * Removes all elements from this list.
   439  *
   440  * If an element destructor function is specified, it is called for each
   441  * element before removing them.
   442  *
   443  * @param list the list
   444  */
   445 __attribute__((__nonnull__))
   446 static inline void cxListClear(CxList *list) {
   447     list->cl->clear(list);
   448 }
   450 /**
   451  * Swaps two items in the list.
   452  *
   453  * Implementations should only allocate temporary memory for the swap, if
   454  * it is necessary.
   455  *
   456  * @param list the list
   457  * @param i the index of the first element
   458  * @param j the index of the second element
   459  * @return zero on success, non-zero if one of the indices is out of bounds
   460  */
   461 __attribute__((__nonnull__))
   462 static inline int cxListSwap(
   463         CxList *list,
   464         size_t i,
   465         size_t j
   466 ) {
   467     return list->cl->swap(list, i, j);
   468 }
   470 /**
   471  * Returns a pointer to the element at the specified index.
   472  *
   473  * @param list the list
   474  * @param index the index of the element
   475  * @return a pointer to the element or \c NULL if the index is out of bounds
   476  */
   477 __attribute__((__nonnull__))
   478 static inline void *cxListAt(
   479         CxList *list,
   480         size_t index
   481 ) {
   482     return list->cl->at(list, index);
   483 }
   485 /**
   486  * Returns an iterator pointing to the item at the specified index.
   487  *
   488  * The returned iterator is position-aware.
   489  *
   490  * If the index is out of range, a past-the-end iterator will be returned.
   491  *
   492  * @param list the list
   493  * @param index the index where the iterator shall point at
   494  * @return a new iterator
   495  */
   496 __attribute__((__nonnull__, __warn_unused_result__))
   497 static inline CxIterator cxListIteratorAt(
   498         CxList const *list,
   499         size_t index
   500 ) {
   501     return list->cl->iterator(list, index, false);
   502 }
   504 /**
   505  * Returns a backwards iterator pointing to the item at the specified index.
   506  *
   507  * The returned iterator is position-aware.
   508  *
   509  * If the index is out of range, a past-the-end iterator will be returned.
   510  *
   511  * @param list the list
   512  * @param index the index where the iterator shall point at
   513  * @return a new iterator
   514  */
   515 __attribute__((__nonnull__, __warn_unused_result__))
   516 static inline CxIterator cxListBackwardsIteratorAt(
   517         CxList const *list,
   518         size_t index
   519 ) {
   520     return list->cl->iterator(list, index, true);
   521 }
   523 /**
   524  * Returns a mutating iterator pointing to the item at the specified index.
   525  *
   526  * The returned iterator is position-aware.
   527  *
   528  * If the index is out of range, a past-the-end iterator will be returned.
   529  *
   530  * @param list the list
   531  * @param index the index where the iterator shall point at
   532  * @return a new iterator
   533  */
   534 __attribute__((__nonnull__, __warn_unused_result__))
   535 CxMutIterator cxListMutIteratorAt(
   536         CxList *list,
   537         size_t index
   538 );
   540 /**
   541  * Returns a mutating backwards iterator pointing to the item at the
   542  * specified index.
   543  *
   544  * The returned iterator is position-aware.
   545  *
   546  * If the index is out of range, a past-the-end iterator will be returned.
   547  *
   548  * @param list the list
   549  * @param index the index where the iterator shall point at
   550  * @return a new iterator
   551  */
   552 __attribute__((__nonnull__, __warn_unused_result__))
   553 CxMutIterator cxListMutBackwardsIteratorAt(
   554         CxList *list,
   555         size_t index
   556 );
   558 /**
   559  * Returns an iterator pointing to the first item of the list.
   560  *
   561  * The returned iterator is position-aware.
   562  *
   563  * If the list is empty, a past-the-end iterator will be returned.
   564  *
   565  * @param list the list
   566  * @return a new iterator
   567  */
   568 __attribute__((__nonnull__, __warn_unused_result__))
   569 static inline CxIterator cxListIterator(CxList const *list) {
   570     return list->cl->iterator(list, 0, false);
   571 }
   573 /**
   574  * Returns a mutating iterator pointing to the first item of the list.
   575  *
   576  * The returned iterator is position-aware.
   577  *
   578  * If the list is empty, a past-the-end iterator will be returned.
   579  *
   580  * @param list the list
   581  * @return a new iterator
   582  */
   583 __attribute__((__nonnull__, __warn_unused_result__))
   584 static inline CxMutIterator cxListMutIterator(CxList *list) {
   585     return cxListMutIteratorAt(list, 0);
   586 }
   589 /**
   590  * Returns a backwards iterator pointing to the last item of the list.
   591  *
   592  * The returned iterator is position-aware.
   593  *
   594  * If the list is empty, a past-the-end iterator will be returned.
   595  *
   596  * @param list the list
   597  * @return a new iterator
   598  */
   599 __attribute__((__nonnull__, __warn_unused_result__))
   600 static inline CxIterator cxListBackwardsIterator(CxList const *list) {
   601     return list->cl->iterator(list, list->size - 1, true);
   602 }
   604 /**
   605  * Returns a mutating backwards iterator pointing to the last item of the list.
   606  *
   607  * The returned iterator is position-aware.
   608  *
   609  * If the list is empty, a past-the-end iterator will be returned.
   610  *
   611  * @param list the list
   612  * @return a new iterator
   613  */
   614 __attribute__((__nonnull__, __warn_unused_result__))
   615 static inline CxMutIterator cxListMutBackwardsIterator(CxList *list) {
   616     return cxListMutBackwardsIteratorAt(list, list->size - 1);
   617 }
   619 /**
   620  * Returns the index of the first element that equals \p elem.
   621  *
   622  * Determining equality is performed by the list's comparator function.
   623  *
   624  * @param list the list
   625  * @param elem the element to find
   626  * @return the index of the element or \c (size+1) if the element is not found
   627  */
   628 __attribute__((__nonnull__))
   629 static inline size_t cxListFind(
   630         CxList const *list,
   631         void const *elem
   632 ) {
   633     return list->cl->find(list, elem);
   634 }
   636 /**
   637  * Sorts the list in place.
   638  *
   639  * \remark The underlying sort algorithm is implementation defined.
   640  *
   641  * @param list the list
   642  */
   643 __attribute__((__nonnull__))
   644 static inline void cxListSort(CxList *list) {
   645     list->cl->sort(list);
   646 }
   648 /**
   649  * Reverses the order of the items.
   650  *
   651  * @param list the list
   652  */
   653 __attribute__((__nonnull__))
   654 static inline void cxListReverse(CxList *list) {
   655     list->cl->reverse(list);
   656 }
   658 /**
   659  * Compares a list to another list of the same type.
   660  *
   661  * First, the list sizes are compared.
   662  * If they match, the lists are compared element-wise.
   663  *
   664  * @param list the list
   665  * @param other the list to compare to
   666  * @return zero, if both lists are equal element wise,
   667  * negative if the first list is smaller, positive if the first list is larger
   668  */
   669 __attribute__((__nonnull__))
   670 int cxListCompare(
   671         CxList const *list,
   672         CxList const *other
   673 );
   675 /**
   676  * Deallocates the memory of the specified list structure.
   677  *
   678  * Also calls content a destructor function, depending on the configuration
   679  * in CxList.content_destructor_type.
   680  *
   681  * This function itself is a destructor function for the CxList.
   682  *
   683  * @param list the list which shall be destroyed
   684  */
   685 __attribute__((__nonnull__))
   686 void cxListDestroy(CxList *list);
   688 #ifdef __cplusplus
   689 } // extern "C"
   690 #endif
   692 #endif // UCX_LIST_H

mercurial