src/ucx/array.h

Thu, 03 Oct 2019 10:55:39 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 03 Oct 2019 10:55:39 +0200
branch
feature/array
changeset 356
77efe51c6c9a
parent 355
d315a068235a
child 357
0f5732f0dc00
permissions
-rw-r--r--

changes UcxArray from value to pointer semantics

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2019 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  * Dynamically allocated array implementation.
    30  * 
    31  * @file   array.h
    32  * @author Mike Becker
    33  * @author Olaf Wintermann
    34  */
    36 #ifndef UCX_ARRAY_H
    37 #define	UCX_ARRAY_H
    39 #include "ucx.h"
    40 #include "allocator.h"
    42 #ifdef	__cplusplus
    43 extern "C" {
    44 #endif
    46 /**
    47  * UCX array type.
    48  */
    49 typedef struct {
    50     /**
    51      * The current capacity of the array.
    52      */
    53     size_t capacity;
    54     /**
    55      * The actual number of elements in the array.
    56      */
    57     size_t size;
    58     /**
    59      * The size of an individual element in bytes.
    60      */
    61     size_t elemsize;
    62     /**
    63      * A pointer to the data.
    64      */
    65     void* data;
    66     /**
    67      * The allocator used for the data.
    68      */
    69     UcxAllocator* allocator;
    70 } UcxArray;
    72 /**
    73  * Sets an element in an arbitrary user defined array.
    74  * 
    75  * If the capacity is insufficient, the array is automatically reallocated and
    76  * the possibly new pointer is stored in the <code>array</code> argument.
    77  * 
    78  * On reallocation the capacity of the array is doubled until it is sufficient.
    79  * The new capacity is stored back to <code>capacity</code>.
    80  *  
    81  * @param array a pointer to location of the array pointer
    82  * @param capacity a pointer to the capacity
    83  * @param elmsize the size of each element
    84  * @param idx the index of the element to set
    85  * @param data the element data
    86  * @return zero on success or non-zero on error (errno will be set)
    87  */
    88 #define ucx_array_util_set(array, capacity, elmsize, idx, data) \
    89     ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \
    90                          elmsize, idx, data)
    92 /**
    93  * Convenience macro for ucx_array_util_set() which automatically computes
    94  * <code>sizeof(data)</code>.
    95  * 
    96  * @param array a pointer to location of the array pointer
    97  * @param capacity a pointer to the capacity
    98  * @param idx the index of the element to set
    99  * @param data the element data
   100  * @return zero on success or non-zero on error (errno will be set)
   101  * @see ucx_array_util_set()
   102  */
   103 #define UCX_ARRAY_UTIL_SET(array, capacity, idx, data) \
   104     ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \
   105                          sizeof(data), idx, data)
   107 /**
   108  * Sets an element in an arbitrary user defined array.
   109  * 
   110  * If the capacity is insufficient, the array is automatically reallocated
   111  * using the specified allocator and the possibly new pointer is stored in
   112  * the <code>array</code> argument.
   113  * 
   114  * On reallocation the capacity of the array is doubled until it is sufficient.
   115  * The new capacity is stored back to <code>capacity</code>. 
   116  * 
   117  * @param alloc the allocator that shall be used to reallocate the array
   118  * @param array a pointer to location of the array pointer
   119  * @param capacity a pointer to the capacity
   120  * @param elmsize the size of each element
   121  * @param idx the index of the element to set
   122  * @param ... the element data
   123  * @return zero on success or non-zero on error (errno will be set)
   124  */
   125 int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity,
   126     size_t elmsize, size_t idx, ...);
   129 /**
   130  * Convenience macro for ucx_array_util_set_a() which automatically computes
   131  * <code>sizeof(data)</code>.
   132  * 
   133  * @param alloc the allocator that shall be used to reallocate the array
   134  * @param array a pointer to location of the array pointer
   135  * @param capacity a pointer to the capacity
   136  * @param idx the index of the element to set
   137  * @param data the element data
   138  * @return zero on success or non-zero on error (errno will be set)
   139  * @see ucx_array_util_set_a()
   140  */
   141 #define UCX_ARRAY_UTIL_SET_A(alloc, array, capacity, idx, data) \
   142     ucx_array_util_set_a(alloc, capacity, sizeof(data), idx, data)
   144 /**
   145  * Creates a new UCX array with the given capacity and element size.
   146  * @param capacity the initial capacity
   147  * @param elemsize the element size
   148  * @return a pointer to a new UCX array structure
   149  */
   150 UcxArray* ucx_array_new(size_t capacity, size_t elemsize);
   152 /**
   153  * Creates a new UCX array using the specified allocator.
   154  * 
   155  * @param capacity the initial capacity
   156  * @param elemsize the element size
   157  * @param allocator the allocator to use
   158  * @return a pointer to new UCX array structure
   159  */
   160 UcxArray* ucx_array_new_a(size_t capacity, size_t elemsize,
   161         UcxAllocator* allocator);
   163 /**
   164  * Initializes a UCX array structure with the given capacity and element size.
   165  * The structure must be uninitialized as the data pointer will be overwritten.
   166  * 
   167  * @param array the structure to initialize
   168  * @param capacity the initial capacity
   169  * @param elemsize the element size
   170  */
   171 void ucx_array_init(UcxArray* array, size_t capacity, size_t elemsize);
   173 /**
   174  * Initializes a UCX array structure using the specified allocator.
   175  * The structure must be uninitialized as the data pointer will be overwritten.
   176  * 
   177  * @param capacity the initial capacity
   178  * @param elemsize the element size
   179  * @param allocator the allocator to use
   180  */
   181 void ucx_array_init_a(UcxArray* array, size_t capacity, size_t elemsize,
   182         UcxAllocator* allocator);
   184 /**
   185  * Creates an shallow copy of an array.
   186  * 
   187  * This function clones the specified array by using memcpy().
   188  * If the destination capacity is insufficient, an automatic reallocation is
   189  * attempted.
   190  * 
   191  * @param dest the array to copy to
   192  * @param src the array to copy from
   193  * @return zero on success, non-zero on reallocation failure.
   194  */
   195 int ucx_array_clone(UcxArray* dest, UcxArray const* src);
   198 /**
   199  * Compares two UCX arrays element-wise by using a compare function.
   200  *
   201  * Elements of the two specified arrays are compared by using the specified
   202  * compare function and the additional data. The type and content of this
   203  * additional data depends on the cmp_func() used.
   204  * 
   205  * This function always returns zero, if the element sizes of the arrays do
   206  * not match and performs no comparisons in this case.
   207  * 
   208  * @param array1 the first array
   209  * @param array2 the second array
   210  * @param cmpfnc the compare function
   211  * @param data additional data for the compare function
   212  * @return 1, if and only if the two arrays equal element-wise, 0 otherwise
   213  */
   214 int ucx_array_equals(UcxArray const *array1, UcxArray const *array2,
   215         cmp_func cmpfnc, void* data);
   217 /**
   218  * Destroys the array.
   219  * 
   220  * The data is freed and both capacity and count are reset to zero.
   221  * If the array structure itself has been dynamically allocated, it has to be
   222  * freed separately.
   223  * 
   224  * @param array the array to destroy
   225  */
   226 void ucx_array_destroy(UcxArray *array);
   228 /**
   229  * Destroys and frees the array.
   230  * 
   231  * @param array the array to free
   232  */
   233 void ucx_array_free(UcxArray *array);
   235 /**
   236  * Inserts elements at the end of the array.
   237  * 
   238  * This is an O(1) operation.
   239  * The array will automatically grow, if the capacity is exceeded.
   240  * If a pointer to data is provided, the data is copied into the array with
   241  * memcpy(). Otherwise the new elements are completely zeroed.
   242  * 
   243  * @param array a pointer the array where to append the data
   244  * @param data a pointer to the data to insert (may be <code>NULL</code>)
   245  * @param count number of elements to copy from data (if data is
   246  * <code>NULL</code>, zeroed elements are appended)
   247  * @return zero on success, non-zero if a reallocation was necessary but failed
   248  * @see ucx_array_set_from()
   249  * @see ucx_array_append()
   250  */
   251 int ucx_array_append_from(UcxArray *array, void *data, size_t count);
   254 /**
   255  * Inserts elements at the beginning of the array.
   256  * 
   257  * This is an expensive operation, because the contents must be moved.
   258  * If there is no particular reason to prepend data, you should use
   259  * ucx_array_append_from() instead.
   260  * 
   261  * @param array a pointer the array where to prepend the data
   262  * @param data a pointer to the data to insert (may be <code>NULL</code>)
   263  * @param count number of elements to copy from data (if data is
   264  * <code>NULL</code>, zeroed elements are inserted)
   265  * @return zero on success, non-zero if a reallocation was necessary but failed
   266  * @see ucx_array_append_from()
   267  * @see ucx_array_set_from()
   268  * @see ucx_array_prepend()
   269  */
   270 int ucx_array_prepend_from(UcxArray *array, void *data, size_t count);
   273 /**
   274  * Sets elements starting at the specified index.
   275  * 
   276  * If the any index is out of bounds, the array automatically grows.
   277  * The pointer to the data may be NULL, in which case the elements are zeroed. 
   278  * 
   279  * @param array a pointer the array where to set the data
   280  * @param index the index of the element to set
   281  * @param data a pointer to the data to insert (may be <code>NULL</code>)
   282  * @param count number of elements to copy from data (if data is
   283  * <code>NULL</code>, the memory in the array is zeroed)
   284  * @return zero on success, non-zero if a reallocation was necessary but failed
   285  * @see ucx_array_append_from()
   286  * @see ucx_array_set()
   287  */
   288 int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count);
   290 /**
   291  * Inserts an element at the end of the array.
   292  * 
   293  * This is an O(1) operation.
   294  * The array will automatically grow, if the capacity is exceeded.
   295  * If the type of the argument has a different size than the element size of
   296  * this array, the behavior is undefined.
   297  * 
   298  * @param array a pointer the array where to append the data
   299  * @param elem the value to insert
   300  * @return zero on success, non-zero if a reallocation was necessary but failed
   301  * @see ucx_array_append_from()
   302  * @see ucx_array_set()
   303  */
   304 #define ucx_array_append(array, elem) ucx_array_appendv(array, elem)
   306 /**
   307  * For internal use.
   308  * Use ucx_array_append()
   309  * 
   310  * @param array
   311  * @param ... 
   312  * @return 
   313  * @see ucx_array_append()
   314  */
   315 int ucx_array_appendv(UcxArray *array, ...);
   318 /**
   319  * Inserts an element at the beginning of the array.
   320  * 
   321  * This is an expensive operation, because the contents must be moved.
   322  * If there is no particular reason to prepend data, you should use
   323  * ucx_array_append() instead.
   324  * 
   325  * @param array a pointer the array where to prepend the data
   326  * @param elem the value to insert
   327  * @return zero on success, non-zero if a reallocation was necessary but failed
   328  * @see ucx_array_append()
   329  * @see ucx_array_set_from()
   330  * @see ucx_array_prepend_from()
   331  */
   332 #define ucx_array_prepend(array, elem) ucx_array_prependv(array, elem)
   334 /**
   335  * For internal use.
   336  * Use ucx_array_prepend()
   337  * 
   338  * @param array
   339  * @param ... 
   340  * @return 
   341  * @see ucx_array_prepend()
   342  */
   343 int ucx_array_prependv(UcxArray *array, ...);
   346 /**
   347  * Sets an element at the specified index.
   348  * 
   349  * If the any index is out of bounds, the array automatically grows.
   350  * 
   351  * @param array a pointer the array where to set the data
   352  * @param index the index of the element to set
   353  * @param elem the value to set
   354  * @return zero on success, non-zero if a reallocation was necessary but failed
   355  * @see ucx_array_append()
   356  * @see ucx_array_set_from()
   357  */
   358 #define ucx_array_set(array, index, elem) ucx_array_setv(array, index, elem)
   360 /**
   361  * For internal use.
   362  * Use ucx_array_set()
   363  * 
   364  * @param array
   365  * @param index
   366  * @param ... 
   367  * @return 
   368  * @see ucx_array_set()
   369  */
   370 int ucx_array_setv(UcxArray *array, size_t index, ...);
   372 /**
   373  * Concatenates two arrays.
   374  * 
   375  * The contents of the second array are appended to the first array in one
   376  * single operation. The second array is otherwise left untouched.
   377  * 
   378  * The first array may grow automatically. If this fails, both arrays remain
   379  * unmodified.
   380  * 
   381  * @param array1 first array
   382  * @param array2 second array
   383  * @return zero on success, non-zero if reallocation was necessary but failed 
   384  * or the element size does not match
   385  */
   386 int ucx_array_concat(UcxArray *array1, const UcxArray *array2);
   388 /**
   389  * Returns a pointer to the array element at the specified index.
   390  * 
   391  * @param array the array to retrieve the element from
   392  * @param index index of the element to return
   393  * @return a pointer to the element at the specified index or <code>NULL</code>,
   394  * if the index is greater than the array size
   395  */
   396 void *ucx_array_at(UcxArray const* array, size_t index);
   398 /**
   399  * Returns the index of an element containing the specified data.
   400  *
   401  * This function uses a cmp_func() to compare the data of each list element
   402  * with the specified data. If no cmp_func is provided, memcmp() is used.
   403  * 
   404  * If the array contains the data more than once, the index of the first
   405  * occurrence is returned.
   406  * If the array does not contain the data, the size of array is returned.
   407  *  
   408  * @param array the array where to search for the data
   409  * @param elem the element data
   410  * @param cmpfnc the compare function
   411  * @param data additional data for the compare function
   412  * @return the index of the element containing the specified data or the size of
   413  * the array, if the data is not found in this array
   414  */
   415 size_t ucx_array_find(UcxArray const *array, void *elem,
   416     cmp_func cmpfnc, void *data);
   418 /**
   419  * Checks, if an array contains a specific element.
   420  * 
   421  * An element is found, if ucx_array_find() returns a value less than the size.
   422  * 
   423  * @param array the array where to search for the data
   424  * @param elem the element data
   425  * @param cmpfnc the compare function
   426  * @param data additional data for the compare function
   427  * @return 1, if and only if the array contains the specified element data
   428  * @see ucx_array_find()
   429  */
   430 int ucx_array_contains(UcxArray const *array, void *elem,
   431     cmp_func cmpfnc, void *data);
   433 /**
   434  * Sorts a UcxArray with the best available sort algorithm.
   435  * 
   436  * The qsort_r() function is used, if available (glibc, FreeBSD or MacOS).
   437  * The order of arguments is automatically adjusted for the FreeBSD and MacOS
   438  * version of qsort_r().
   439  * 
   440  * If qsort_r() is not available, a merge sort algorithm is used, which is
   441  * guaranteed to use no more additional memory than for exactly one element.
   442  * 
   443  * @param array the array to sort
   444  * @param cmpfnc the function that shall be used to compare the element data
   445  * @param data additional data for the cmp_func() or <code>NULL</code>
   446  */
   447 void ucx_array_sort(UcxArray* array, cmp_func cmpfnc, void *data);
   449 /**
   450  * Removes an element from the array.
   451  * 
   452  * This is in general an expensive operation, because several elements may
   453  * be moved. If the order of the elements is not relevant, use
   454  * ucx_array_remove_fast() instead.
   455  * 
   456  * @param array pointer to the array from which the element shall be removed
   457  * @param index the index of the element to remove
   458  */
   459 void ucx_array_remove(UcxArray *array, size_t index);
   461 /**
   462  * Removes an element from the array.
   463  * 
   464  * This is an O(1) operation, but does not maintain the order of the elements.
   465  * The last element in the array is moved to the location of the removed
   466  * element.
   467  * 
   468  * @param array pointer to the array from which the element shall be removed
   469  * @param index the index of the element to remove
   470  */
   471 void ucx_array_remove_fast(UcxArray *array, size_t index);
   473 /**
   474  * Shrinks the memory to exactly fit the contents.
   475  * 
   476  * After this operation, the capacity equals the size.
   477  * 
   478  * @param array a pointer to the array
   479  * @return zero on success, non-zero if reallocation failed
   480  */
   481 int ucx_array_shrink(UcxArray* array);
   483 /**
   484  * Sets the capacity of the array.
   485  * 
   486  * If the new capacity is smaller than the size of the array, the elements
   487  * are removed and the size is adjusted accordingly.
   488  * 
   489  * @param array a pointer to the array
   490  * @param capacity the new capacity
   491  * @return zero on success, non-zero if reallocation failed
   492  */
   493 int ucx_array_resize(UcxArray* array, size_t capacity);
   495 /**
   496  * Resizes the array only, if the capacity is insufficient.
   497  * 
   498  * If the requested capacity is smaller than the current capacity, this
   499  * function does nothing.
   500  * 
   501  * @param array a pointer to the array
   502  * @param capacity the guaranteed capacity
   503  * @return zero on success, non-zero if reallocation failed
   504  */
   505 int ucx_array_reserve(UcxArray* array, size_t capacity);
   509 #ifdef	__cplusplus
   510 }
   511 #endif
   513 #endif	/* UCX_ARRAY_H */

mercurial