src/cx/list.h

Wed, 23 Nov 2022 22:40:55 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 23 Nov 2022 22:40:55 +0100
changeset 629
6c81ee4f11ad
parent 628
1e2be40f0cb5
child 630
ac5e7f789048
permissions
-rw-r--r--

#224 add cxListAddArray()

This also replaces cxLinkedListFromArray().

     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      * The comparator function for the elements.
    75      */
    76     CxListComparator cmpfunc;
    77     /**
    78      * The size of each element (payload only).
    79      */
    80     size_t itemsize;
    81     /**
    82      * The size of the list (number of currently stored elements).
    83      */
    84     size_t size;
    85     /**
    86      * The capacity of the list (maximum number of elements).
    87      */
    88     size_t capacity;
    89     union {
    90         /**
    91          * An optional simple destructor for the list contents that admits the free() interface.
    92          *
    93          * @remark Set content_destructor_type to #CX_DESTRUCTOR_SIMPLE.
    94          *
    95          * @attention Read the documentation of the particular list implementation
    96          * whether this destructor shall only destroy the contents or also free the memory.
    97          */
    98         cx_destructor_func simple_destructor;
    99         /**
   100          * An optional advanced destructor for the list contents providing additional data.
   101          *
   102          * @remark Set content_destructor_type to #CX_DESTRUCTOR_ADVANCED.
   103          *
   104          * @attention Read the documentation of the particular list implementation
   105          * whether this destructor shall only destroy the contents or also free the memory.
   106          */
   107         cx_advanced_destructor advanced_destructor;
   108     };
   109     /**
   110      * The type of destructor to use.
   111      */
   112     enum cx_destructor_type content_destructor_type;
   113 };
   115 /**
   116  * The class definition for arbitrary lists.
   117  */
   118 struct cx_list_class_s {
   119     /**
   120      * Destructor function.
   121      */
   122     void (*destructor)(struct cx_list_s *list);
   124     /**
   125      * Member function for adding an element.
   126      */
   127     int (*add)(
   128             struct cx_list_s *list,
   129             void const *elem
   130     );
   132     /**
   133      * Member function for adding multiple elements.
   134      */
   135     size_t (*add_array)(
   136             struct cx_list_s *list,
   137             void const *array,
   138             size_t n
   139     );
   141     /**
   142      * Member function for inserting an element.
   143      */
   144     int (*insert)(
   145             struct cx_list_s *list,
   146             size_t index,
   147             void const *elem
   148     );
   150     /**
   151      * Member function for inserting an element relative to an iterator position.
   152      */
   153     int (*insert_iter)(
   154             struct cx_iterator_s *iter,
   155             void const *elem,
   156             int prepend
   157     );
   159     /**
   160      * Member function for removing an element.
   161      */
   162     int (*remove)(
   163             struct cx_list_s *list,
   164             size_t index
   165     );
   167     /**
   168      * Member function for element lookup.
   169      */
   170     void *(*at)(
   171             struct cx_list_s const *list,
   172             size_t index
   173     );
   175     /**
   176      * Member function for finding an element.
   177      */
   178     size_t (*find)(
   179             struct cx_list_s const *list,
   180             void const *elem
   181     );
   183     /**
   184      * Member function for sorting the list in place.
   185      */
   186     void (*sort)(struct cx_list_s *list);
   188     /**
   189      * Member function for comparing this list to another list of the same type.
   190      */
   191     int (*compare)(
   192             struct cx_list_s const *list,
   193             struct cx_list_s const *other
   194     );
   196     /**
   197      * Member function for reversing the order of the items.
   198      */
   199     void (*reverse)(struct cx_list_s *list);
   201     /**
   202      * Returns an iterator pointing to the specified index.
   203      */
   204     struct cx_iterator_s (*iterator)(
   205             struct cx_list_s *list,
   206             size_t index
   207     );
   208 };
   210 /**
   211  * Common type for all list implementations.
   212  */
   213 typedef struct cx_list_s CxList;
   215 /**
   216  * Adds an item to the end of the list.
   217  *
   218  * @param list the list
   219  * @param elem a pointer to the element to add
   220  * @return zero on success, non-zero on memory allocation failure
   221  * @see cxListAddArray()
   222  */
   223 __attribute__((__nonnull__))
   224 static inline int cxListAdd(
   225         CxList *list,
   226         void const *elem
   227 ) {
   228     return list->cl->add(list, elem);
   229 }
   231 /**
   232  * Adds multiple items to the end of the list.
   233  *
   234  * This method is more efficient than invoking cxListAdd() multiple times.
   235  *
   236  * If there is not enough memory to add all elements, the returned value is
   237  * less than \p n.
   238  *
   239  * @param list the list
   240  * @param array a pointer to the elements to add
   241  * @param n the number of elements to add
   242  * @return the number of added elements
   243  */
   244 __attribute__((__nonnull__))
   245 static inline size_t cxListAddArray(
   246         CxList *list,
   247         void const *array,
   248         size_t n
   249 ) {
   250     return list->cl->add_array(list, array, n);
   251 }
   253 /**
   254  * Inserts an item at the specified index.
   255  *
   256  * If \p index equals the list \c size, this is effectively cxListAdd().
   257  *
   258  * @param list the list
   259  * @param index the index the element shall have
   260  * @param elem a pointer to the element to add
   261  * @return zero on success, non-zero on memory allocation failure
   262  * or when the index is out of bounds
   263  * @see cxListInsertAfter()
   264  * @see cxListInsertBefore()
   265  */
   266 __attribute__((__nonnull__))
   267 static inline int cxListInsert(
   268         CxList *list,
   269         size_t index,
   270         void const *elem
   271 ) {
   272     return list->cl->insert(list, index, elem);
   273 }
   275 /**
   276  * Inserts an element after 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 cxListInsertBefore()
   289  */
   290 __attribute__((__nonnull__))
   291 static inline int cxListInsertAfter(
   292         CxIterator *iter,
   293         void const *elem
   294 ) {
   295     return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 0);
   296 }
   298 /**
   299  * Inserts an element before the current location of the specified iterator.
   300  *
   301  * The used iterator remains operational, but all other active iterators should
   302  * be considered invalidated.
   303  *
   304  * If \p iter is not a list iterator, the behavior is undefined.
   305  * If \p iter is a past-the-end iterator, the new element gets appended to the list.
   306  *
   307  * @param iter an iterator
   308  * @param elem the element to insert
   309  * @return zero on success, non-zero on memory allocation failure
   310  * @see cxListInsert()
   311  * @see cxListInsertAfter()
   312  */
   313 __attribute__((__nonnull__))
   314 static inline int cxListInsertBefore(
   315         CxIterator *iter,
   316         void const *elem
   317 ) {
   318     return ((struct cx_list_s *) iter->src_handle)->cl->insert_iter(iter, elem, 1);
   319 }
   321 /**
   322  * Removes the element at the specified index.
   323  * @param list the list
   324  * @param index the index of the element
   325  * @return zero on success, non-zero if the index is out of bounds
   326  */
   327 __attribute__((__nonnull__))
   328 static inline int cxListRemove(
   329         CxList *list,
   330         size_t index
   331 ) {
   332     return list->cl->remove(list, index);
   333 }
   335 /**
   336  * Returns a pointer to the element at the specified index.
   337  *
   338  * @param list the list
   339  * @param index the index of the element
   340  * @return a pointer to the element or \c NULL if the index is out of bounds
   341  */
   342 __attribute__((__nonnull__))
   343 static inline void *cxListAt(
   344         CxList *list,
   345         size_t index
   346 ) {
   347     return list->cl->at(list, index);
   348 }
   350 /**
   351  * Returns an iterator pointing to the item at the specified index.
   352  *
   353  * The returned iterator is position-aware.
   354  *
   355  * If the index is out of range, a past-the-end iterator will be returned.
   356  *
   357  * @param list the list
   358  * @param index the index where the iterator shall point at
   359  * @return a new iterator
   360  */
   361 __attribute__((__nonnull__, __warn_unused_result__))
   362 static inline CxIterator cxListIterator(
   363         CxList *list,
   364         size_t index
   365 ) {
   366     return list->cl->iterator(list, index);
   367 }
   369 /**
   370  * Returns an iterator pointing to the first item of the list.
   371  *
   372  * The returned iterator is position-aware.
   373  *
   374  * If the list is empty, a past-the-end iterator will be returned.
   375  *
   376  * @param list the list
   377  * @return a new iterator
   378  */
   379 __attribute__((__nonnull__, __warn_unused_result__))
   380 static inline CxIterator cxListBegin(CxList *list) {
   381     return list->cl->iterator(list, 0);
   382 }
   384 /**
   385  * Returns the index of the first element that equals \p elem.
   386  *
   387  * Determining equality is performed by the list's comparator function.
   388  *
   389  * @param list the list
   390  * @param elem the element to find
   391  * @return the index of the element or \c (size+1) if the element is not found
   392  */
   393 __attribute__((__nonnull__))
   394 static inline size_t cxListFind(
   395         CxList const *list,
   396         void const *elem
   397 ) {
   398     return list->cl->find(list, elem);
   399 }
   401 /**
   402  * Sorts the list in place.
   403  *
   404  * \remark The underlying sort algorithm is implementation defined.
   405  *
   406  * @param list the list
   407  */
   408 __attribute__((__nonnull__))
   409 static inline void cxListSort(CxList *list) {
   410     list->cl->sort(list);
   411 }
   413 /**
   414  * Reverses the order of the items.
   415  *
   416  * @param list the list
   417  */
   418 __attribute__((__nonnull__))
   419 static inline void cxListReverse(CxList *list) {
   420     list->cl->reverse(list);
   421 }
   423 /**
   424  * Compares a list to another list of the same type.
   425  *
   426  * First, the list sizes are compared.
   427  * If they match, the lists are compared element-wise.
   428  *
   429  * @param list the list
   430  * @param other the list to compare to
   431  * @return zero, if both lists are equal element wise,
   432  * negative if the first list is smaller, positive if the first list is larger
   433  */
   434 __attribute__((__nonnull__))
   435 int cxListCompare(
   436         CxList const *list,
   437         CxList const *other
   438 );
   440 /**
   441  * Deallocates the memory of the specified list structure.
   442  *
   443  * Also calls content a destructor function, depending on the configuration
   444  * in CxList.content_destructor_type.
   445  *
   446  * This function itself is a destructor function for the CxList.
   447  *
   448  * @param list the list which shall be destroyed
   449  */
   450 __attribute__((__nonnull__))
   451 void cxListDestroy(CxList *list);
   453 #ifdef __cplusplus
   454 } // extern "C"
   455 #endif
   457 #endif // UCX_LIST_H

mercurial