src/ucx/array.h

Thu, 04 Jul 2019 22:23:15 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 04 Jul 2019 22:23:15 +0200
branch
feature/array
changeset 336
6d7aa8a1a3b3
parent 334
bc81faa9afda
child 337
f695ae118460
permissions
-rw-r--r--

implements ucx_array_sort()

     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;
    73 /**
    74  * Creates a new UCX array with the given capacity and element size.
    75  * @param capacity the initial capacity
    76  * @param elemsize the element size
    77  * @return a new UCX array structure
    78  */
    79 UcxArray ucx_array_new(size_t capacity, size_t elemsize);
    81 /**
    82  * Creates a new UCX array using the specified allocator.
    83  * 
    84  * @param capacity the initial capacity
    85  * @param elemsize the element size
    86  * @param allocator the allocator to use
    87  * @return a new UCX array structure
    88  */
    89 UcxArray ucx_array_new_a(size_t capacity, size_t elemsize,
    90         UcxAllocator* allocator);
    92 /**
    93  * Creates an shallow copy of an array.
    94  * 
    95  * This function clones the specified array by using memcpy().
    96  * 
    97  * @param array the array to copy
    98  * @return the copy (may be an empty array on allocation errors)
    99  */
   100 UcxArray ucx_array_clone(UcxArray array);
   103 /**
   104  * Compares two UCX arrays element-wise by using a compare function.
   105  *
   106  * Elements of the two specified arrays are compared by using the specified
   107  * compare function and the additional data. The type and content of this
   108  * additional data depends on the cmp_func() used.
   109  * 
   110  * This function always returns zero, if the element sizes of the arrays do
   111  * not match and performs no comparisons in this case.
   112  * 
   113  * @param array1 the first array
   114  * @param array2 the second array
   115  * @param cmpfnc the compare function
   116  * @param data additional data for the compare function
   117  * @return 1, if and only if the two arrays equal element-wise, 0 otherwise
   118  */
   119 int ucx_array_equals(UcxArray array1, UcxArray array2,
   120         cmp_func cmpfnc, void* data);
   122 /**
   123  * Destroys the array.
   124  * 
   125  * The data is freed and both capacity and count are reset to zero.
   126  * If the array structure itself has been dynamically allocated, it has to be
   127  * freed separately.
   128  * 
   129  * @param array the array to free
   130  */
   131 void ucx_array_free(UcxArray *array);
   133 /**
   134  * Inserts an element at the end of the array.
   135  * 
   136  * This is an O(1) operation.
   137  * The array will automatically grow, if the capacity is exceeded.
   138  * If a pointer to data is provided, the data is copied into the array with
   139  * memcpy(). Otherwise the new element is completely zeroed.
   140  * 
   141  * @param array a pointer the array where to append the data
   142  * @param data a pointer to the data to insert (may be <code>NULL</code>)
   143  * @return zero on success, non-zero if a reallocation was necessary but failed
   144  */
   145 int ucx_array_append(UcxArray *array, void *data);
   148 /**
   149  * Inserts an element at the beginning of the array.
   150  * 
   151  * This is an expensive operation, because the contents must be moved.
   152  * If there is no particular reason to prepend data, you should use
   153  * ucx_array_append() instead.
   154  * 
   155  * @param array a pointer the array where to prepend the data
   156  * @param data a pointer to the data to insert (may be <code>NULL</code>)
   157  * @return zero on success, non-zero if a reallocation was necessary but failed
   158  */
   159 int ucx_array_prepend(UcxArray *array, void *data);
   161 /**
   162  * Concatenates two arrays.
   163  * 
   164  * The contents of the second array are appended to the first array in one
   165  * single operation. The second array is otherwise left untouched.
   166  * 
   167  * The first array may grow automatically. If this fails, both arrays remain
   168  * unmodified.
   169  * 
   170  * @param array1 first array
   171  * @param array2 second array
   172  * @return zero on success, non-zero if reallocation was necessary but failed 
   173  * or the element size does not match
   174  */
   175 int ucx_array_concat(UcxArray *array1, const UcxArray *array2);
   177 /**
   178  * Returns a pointer to the array element at the specified index.
   179  * 
   180  * @param array the array to retrieve the element from
   181  * @param index index of the element to return
   182  * @return a pointer to the element at the specified index or <code>NULL</code>,
   183  * if the index is greater than the array size
   184  */
   185 void *ucx_array_at(UcxArray array, size_t index);
   187 /**
   188  * Returns an element of the specified type by value.
   189  * 
   190  * This expression can also be assigned to.
   191  * 
   192  * If <code>sizeof(type)</code> does not equal the array's element size, the
   193  * behavior is undefined.
   194  * If the index is out of bounds, the behavior is undefined.
   195  * 
   196  * @param type the type of the element
   197  * @param array the array to retrieve the element from
   198  * @param index index of the element to return
   199  * @return the requested element
   200  * @see ucx_array_at()
   201  */
   202 #define ucx_array_at_typed(type, arr, i) (((type*)((arr).data))[i])
   204 /**
   205  * Shorthand for ucx_array_at_typed().
   206  */
   207 #define ucx_array_at_int(arr, i) ucx_array_at_typed(int, arr, i)
   209 /**
   210  * Shorthand for ucx_array_at_typed().
   211  */
   212 #define ucx_array_at_short(arr, i) ucx_array_at_typed(short, arr, i)
   214 /**
   215  * Shorthand for ucx_array_at_typed().
   216  */
   217 #define ucx_array_at_longint(arr, i) ucx_array_at_typed(long int, arr, i)
   219 /**
   220  * Shorthand for ucx_array_at_typed().
   221  */
   222 #define ucx_array_at_uint(arr, i) ucx_array_at_typed(unsigned int, arr, i)
   224 /**
   225  * Shorthand for ucx_array_at_typed().
   226  */
   227 #define ucx_array_at_ushort(arr, i) ucx_array_at_typed(unsigned short, arr, i)
   229 /**
   230  * Shorthand for ucx_array_at_typed().
   231  */
   232 #define ucx_array_at_ulongint(arr, i) \
   233     ucx_array_at_typed(unsigned long int, arr, i)
   235 /**
   236  * Shorthand for ucx_array_get_typed().
   237  */
   238 #define ucx_array_get_float(arr, i) ucx_array_get_typed(float, arr, i)
   240 /**
   241  * Shorthand for ucx_array_get_typed().
   242  */
   243 #define ucx_array_get_double(arr, i) ucx_array_get_typed(double, arr, i)
   245 /**
   246  * Returns the index of an element containing the specified data.
   247  *
   248  * This function uses a cmp_func() to compare the data of each list element
   249  * with the specified data. If no cmp_func is provided, memcmp() is used.
   250  * 
   251  * If the array contains the data more than once, the index of the first
   252  * occurrence is returned.
   253  * If the array does not contain the data, the size of array is returned.
   254  *  
   255  * @param array the array where to search for the data
   256  * @param elem the element data
   257  * @param cmpfnc the compare function
   258  * @param data additional data for the compare function
   259  * @return the index of the element containing the specified data or the size of
   260  * the array, if the data is not found in this array
   261  */
   262 size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
   264 /**
   265  * Checks, if an array contains a specific element.
   266  * 
   267  * An element is found, if ucx_array_find() returns a value less than the size.
   268  * 
   269  * @param array the array where to search for the data
   270  * @param elem the element data
   271  * @param cmpfnc the compare function
   272  * @param data additional data for the compare function
   273  * @return 1, if and only if the array contains the specified element data
   274  * @see ucx_array_find()
   275  */
   276 int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
   278 /**
   279  * Sorts a UcxArray with an almost in-place merge sort.
   280  * 
   281  * This function uses additional memory for exactly one element.
   282  * 
   283  * @param the array to sort
   284  * @param cmpfnc the function that shall be used to compare the element data
   285  * @param data additional data for the cmp_func()
   286  */
   287 void ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data);
   289 /**
   290  * Removes an element from the array.
   291  * 
   292  * This is in general an expensive operation, because several elements may
   293  * be moved. If the order of the elements is not relevant, use
   294  * ucx_array_remove_fast() instead.
   295  * 
   296  * @param array pointer to the array from which the element shall be removed
   297  * @param index the index of the element to remove
   298  */
   299 void ucx_array_remove(UcxArray *array, size_t index);
   301 /**
   302  * Removes an element from the array.
   303  * 
   304  * This is an O(1) operation, but does not maintain the order of the elements.
   305  * The last element in the array is moved to the location of the removed
   306  * element.
   307  * 
   308  * @param array pointer to the array from which the element shall be removed
   309  * @param index the index of the element to remove
   310  */
   311 void ucx_array_remove_fast(UcxArray *array, size_t index);
   313 /**
   314  * Shrinks the memory to exactly fit the contents.
   315  * 
   316  * After this operation, the capacity equals the size.
   317  * 
   318  * @param array a pointer to the array
   319  * @return zero on success, non-zero if reallocation failed
   320  */
   321 int ucx_array_shrink(UcxArray* array);
   323 /**
   324  * Sets the capacity of the array.
   325  * 
   326  * If the new capacity is smaller than the size of the array, the elements
   327  * are removed and the size is adjusted accordingly.
   328  * 
   329  * @param array a pointer to the array
   330  * @param capacity the new capacity
   331  * @return zero on success, non-zero if reallocation failed
   332  */
   333 int ucx_array_resize(UcxArray* array, size_t capacity);
   335 /**
   336  * Resizes the array only, if the capacity is insufficient.
   337  * 
   338  * If the requested capacity is smaller than the current capacity, this
   339  * function does nothing.
   340  * 
   341  * @param array a pointer to the array
   342  * @param capacity the guaranteed capacity
   343  * @return zero on success, non-zero if reallocation failed
   344  */
   345 int ucx_array_reserve(UcxArray* array, size_t capacity);
   349 #ifdef	__cplusplus
   350 }
   351 #endif
   353 #endif	/* UCX_ARRAY_H */

mercurial