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()

universe@103 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@334 4 * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
universe@103 5 *
universe@103 6 * Redistribution and use in source and binary forms, with or without
universe@103 7 * modification, are permitted provided that the following conditions are met:
universe@103 8 *
universe@103 9 * 1. Redistributions of source code must retain the above copyright
universe@103 10 * notice, this list of conditions and the following disclaimer.
universe@103 11 *
universe@103 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@103 13 * notice, this list of conditions and the following disclaimer in the
universe@103 14 * documentation and/or other materials provided with the distribution.
universe@103 15 *
universe@103 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103 26 * POSSIBILITY OF SUCH DAMAGE.
universe@4 27 */
universe@123 28 /**
universe@334 29 * Dynamically allocated array implementation.
universe@123 30 *
universe@334 31 * @file array.h
universe@123 32 * @author Mike Becker
universe@123 33 * @author Olaf Wintermann
universe@123 34 */
universe@4 35
universe@334 36 #ifndef UCX_ARRAY_H
universe@334 37 #define UCX_ARRAY_H
universe@4 38
universe@259 39 #include "ucx.h"
universe@259 40 #include "allocator.h"
universe@7 41
universe@4 42 #ifdef __cplusplus
universe@4 43 extern "C" {
universe@4 44 #endif
universe@4 45
universe@121 46 /**
universe@334 47 * UCX array type.
universe@121 48 */
universe@334 49 typedef struct {
universe@334 50 /**
universe@334 51 * The current capacity of the array.
universe@334 52 */
universe@334 53 size_t capacity;
universe@334 54 /**
universe@334 55 * The actual number of elements in the array.
universe@334 56 */
universe@334 57 size_t size;
universe@334 58 /**
universe@334 59 * The size of an individual element in bytes.
universe@334 60 */
universe@334 61 size_t elemsize;
universe@334 62 /**
universe@334 63 * A pointer to the data.
universe@334 64 */
universe@334 65 void* data;
universe@334 66 /**
universe@334 67 * The allocator used for the data.
universe@334 68 */
universe@334 69 UcxAllocator* allocator;
universe@334 70 } UcxArray;
universe@334 71
universe@121 72
universe@123 73 /**
universe@334 74 * Creates a new UCX array with the given capacity and element size.
universe@334 75 * @param capacity the initial capacity
universe@334 76 * @param elemsize the element size
universe@334 77 * @return a new UCX array structure
universe@123 78 */
universe@334 79 UcxArray ucx_array_new(size_t capacity, size_t elemsize);
universe@146 80
universe@129 81 /**
universe@334 82 * Creates a new UCX array using the specified allocator.
universe@334 83 *
universe@334 84 * @param capacity the initial capacity
universe@334 85 * @param elemsize the element size
universe@334 86 * @param allocator the allocator to use
universe@334 87 * @return a new UCX array structure
universe@129 88 */
universe@334 89 UcxArray ucx_array_new_a(size_t capacity, size_t elemsize,
universe@334 90 UcxAllocator* allocator);
universe@4 91
universe@128 92 /**
universe@334 93 * Creates an shallow copy of an array.
universe@128 94 *
universe@334 95 * This function clones the specified array by using memcpy().
universe@128 96 *
universe@334 97 * @param array the array to copy
universe@334 98 * @return the copy (may be an empty array on allocation errors)
universe@128 99 */
universe@334 100 UcxArray ucx_array_clone(UcxArray array);
universe@334 101
universe@146 102
universe@128 103 /**
universe@334 104 * Compares two UCX arrays element-wise by using a compare function.
universe@334 105 *
universe@334 106 * Elements of the two specified arrays are compared by using the specified
universe@123 107 * compare function and the additional data. The type and content of this
universe@123 108 * additional data depends on the cmp_func() used.
universe@123 109 *
universe@334 110 * This function always returns zero, if the element sizes of the arrays do
universe@334 111 * not match and performs no comparisons in this case.
universe@123 112 *
universe@334 113 * @param array1 the first array
universe@334 114 * @param array2 the second array
universe@123 115 * @param cmpfnc the compare function
universe@123 116 * @param data additional data for the compare function
universe@334 117 * @return 1, if and only if the two arrays equal element-wise, 0 otherwise
universe@123 118 */
universe@334 119 int ucx_array_equals(UcxArray array1, UcxArray array2,
universe@123 120 cmp_func cmpfnc, void* data);
universe@4 121
universe@123 122 /**
universe@334 123 * Destroys the array.
universe@123 124 *
universe@334 125 * The data is freed and both capacity and count are reset to zero.
universe@334 126 * If the array structure itself has been dynamically allocated, it has to be
universe@334 127 * freed separately.
universe@123 128 *
universe@334 129 * @param array the array to free
universe@123 130 */
universe@334 131 void ucx_array_free(UcxArray *array);
universe@146 132
universe@128 133 /**
universe@334 134 * Inserts an element at the end of the array.
universe@128 135 *
universe@334 136 * This is an O(1) operation.
universe@334 137 * The array will automatically grow, if the capacity is exceeded.
universe@334 138 * If a pointer to data is provided, the data is copied into the array with
universe@334 139 * memcpy(). Otherwise the new element is completely zeroed.
universe@128 140 *
universe@334 141 * @param array a pointer the array where to append the data
universe@334 142 * @param data a pointer to the data to insert (may be <code>NULL</code>)
universe@334 143 * @return zero on success, non-zero if a reallocation was necessary but failed
universe@128 144 */
universe@334 145 int ucx_array_append(UcxArray *array, void *data);
universe@334 146
universe@146 147
universe@128 148 /**
universe@334 149 * Inserts an element at the beginning of the array.
universe@211 150 *
universe@334 151 * This is an expensive operation, because the contents must be moved.
universe@334 152 * If there is no particular reason to prepend data, you should use
universe@334 153 * ucx_array_append() instead.
universe@211 154 *
universe@334 155 * @param array a pointer the array where to prepend the data
universe@334 156 * @param data a pointer to the data to insert (may be <code>NULL</code>)
universe@334 157 * @return zero on success, non-zero if a reallocation was necessary but failed
universe@211 158 */
universe@334 159 int ucx_array_prepend(UcxArray *array, void *data);
universe@211 160
universe@211 161 /**
universe@334 162 * Concatenates two arrays.
universe@128 163 *
universe@334 164 * The contents of the second array are appended to the first array in one
universe@334 165 * single operation. The second array is otherwise left untouched.
universe@128 166 *
universe@334 167 * The first array may grow automatically. If this fails, both arrays remain
universe@334 168 * unmodified.
universe@334 169 *
universe@334 170 * @param array1 first array
universe@334 171 * @param array2 second array
universe@334 172 * @return zero on success, non-zero if reallocation was necessary but failed
universe@334 173 * or the element size does not match
universe@128 174 */
universe@334 175 int ucx_array_concat(UcxArray *array1, const UcxArray *array2);
universe@146 176
universe@128 177 /**
universe@334 178 * Returns a pointer to the array element at the specified index.
universe@128 179 *
universe@334 180 * @param array the array to retrieve the element from
universe@334 181 * @param index index of the element to return
universe@334 182 * @return a pointer to the element at the specified index or <code>NULL</code>,
universe@334 183 * if the index is greater than the array size
universe@128 184 */
universe@334 185 void *ucx_array_at(UcxArray array, size_t index);
universe@291 186
universe@291 187 /**
universe@334 188 * Returns an element of the specified type by value.
universe@128 189 *
universe@334 190 * This expression can also be assigned to.
universe@128 191 *
universe@334 192 * If <code>sizeof(type)</code> does not equal the array's element size, the
universe@334 193 * behavior is undefined.
universe@334 194 * If the index is out of bounds, the behavior is undefined.
universe@334 195 *
universe@334 196 * @param type the type of the element
universe@334 197 * @param array the array to retrieve the element from
universe@334 198 * @param index index of the element to return
universe@334 199 * @return the requested element
universe@334 200 * @see ucx_array_at()
universe@128 201 */
universe@334 202 #define ucx_array_at_typed(type, arr, i) (((type*)((arr).data))[i])
universe@146 203
universe@128 204 /**
universe@334 205 * Shorthand for ucx_array_at_typed().
universe@128 206 */
universe@334 207 #define ucx_array_at_int(arr, i) ucx_array_at_typed(int, arr, i)
universe@146 208
universe@128 209 /**
universe@334 210 * Shorthand for ucx_array_at_typed().
universe@128 211 */
universe@334 212 #define ucx_array_at_short(arr, i) ucx_array_at_typed(short, arr, i)
universe@146 213
universe@124 214 /**
universe@334 215 * Shorthand for ucx_array_at_typed().
universe@124 216 */
universe@334 217 #define ucx_array_at_longint(arr, i) ucx_array_at_typed(long int, arr, i)
universe@146 218
universe@124 219 /**
universe@334 220 * Shorthand for ucx_array_at_typed().
universe@124 221 */
universe@334 222 #define ucx_array_at_uint(arr, i) ucx_array_at_typed(unsigned int, arr, i)
universe@146 223
universe@128 224 /**
universe@334 225 * Shorthand for ucx_array_at_typed().
universe@128 226 */
universe@334 227 #define ucx_array_at_ushort(arr, i) ucx_array_at_typed(unsigned short, arr, i)
universe@146 228
universe@128 229 /**
universe@334 230 * Shorthand for ucx_array_at_typed().
universe@128 231 */
universe@334 232 #define ucx_array_at_ulongint(arr, i) \
universe@334 233 ucx_array_at_typed(unsigned long int, arr, i)
universe@146 234
universe@128 235 /**
universe@334 236 * Shorthand for ucx_array_get_typed().
universe@128 237 */
universe@334 238 #define ucx_array_get_float(arr, i) ucx_array_get_typed(float, arr, i)
universe@334 239
universe@334 240 /**
universe@334 241 * Shorthand for ucx_array_get_typed().
universe@334 242 */
universe@334 243 #define ucx_array_get_double(arr, i) ucx_array_get_typed(double, arr, i)
universe@146 244
universe@128 245 /**
universe@128 246 * Returns the index of an element containing the specified data.
universe@128 247 *
universe@128 248 * This function uses a cmp_func() to compare the data of each list element
universe@334 249 * with the specified data. If no cmp_func is provided, memcmp() is used.
universe@128 250 *
universe@334 251 * If the array contains the data more than once, the index of the first
universe@128 252 * occurrence is returned.
universe@334 253 * If the array does not contain the data, the size of array is returned.
universe@128 254 *
universe@334 255 * @param array the array where to search for the data
universe@128 256 * @param elem the element data
universe@128 257 * @param cmpfnc the compare function
universe@128 258 * @param data additional data for the compare function
universe@334 259 * @return the index of the element containing the specified data or the size of
universe@334 260 * the array, if the data is not found in this array
universe@128 261 */
universe@334 262 size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
universe@146 263
universe@128 264 /**
universe@334 265 * Checks, if an array contains a specific element.
universe@128 266 *
universe@334 267 * An element is found, if ucx_array_find() returns a value less than the size.
universe@128 268 *
universe@334 269 * @param array the array where to search for the data
universe@128 270 * @param elem the element data
universe@128 271 * @param cmpfnc the compare function
universe@128 272 * @param data additional data for the compare function
universe@334 273 * @return 1, if and only if the array contains the specified element data
universe@334 274 * @see ucx_array_find()
universe@128 275 */
universe@334 276 int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
universe@35 277
universe@128 278 /**
universe@336 279 * Sorts a UcxArray with an almost in-place merge sort.
universe@128 280 *
universe@336 281 * This function uses additional memory for exactly one element.
universe@128 282 *
universe@334 283 * @param the array to sort
universe@128 284 * @param cmpfnc the function that shall be used to compare the element data
universe@128 285 * @param data additional data for the cmp_func()
universe@128 286 */
universe@336 287 void ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data);
universe@123 288
universe@124 289 /**
universe@334 290 * Removes an element from the array.
universe@124 291 *
universe@334 292 * This is in general an expensive operation, because several elements may
universe@334 293 * be moved. If the order of the elements is not relevant, use
universe@334 294 * ucx_array_remove_fast() instead.
universe@124 295 *
universe@334 296 * @param array pointer to the array from which the element shall be removed
universe@334 297 * @param index the index of the element to remove
universe@124 298 */
universe@334 299 void ucx_array_remove(UcxArray *array, size_t index);
universe@146 300
universe@128 301 /**
universe@334 302 * Removes an element from the array.
universe@128 303 *
universe@334 304 * This is an O(1) operation, but does not maintain the order of the elements.
universe@334 305 * The last element in the array is moved to the location of the removed
universe@334 306 * element.
universe@128 307 *
universe@334 308 * @param array pointer to the array from which the element shall be removed
universe@334 309 * @param index the index of the element to remove
universe@128 310 */
universe@334 311 void ucx_array_remove_fast(UcxArray *array, size_t index);
universe@334 312
universe@334 313 /**
universe@334 314 * Shrinks the memory to exactly fit the contents.
universe@334 315 *
universe@334 316 * After this operation, the capacity equals the size.
universe@334 317 *
universe@334 318 * @param array a pointer to the array
universe@334 319 * @return zero on success, non-zero if reallocation failed
universe@334 320 */
universe@334 321 int ucx_array_shrink(UcxArray* array);
universe@334 322
universe@334 323 /**
universe@334 324 * Sets the capacity of the array.
universe@334 325 *
universe@334 326 * If the new capacity is smaller than the size of the array, the elements
universe@334 327 * are removed and the size is adjusted accordingly.
universe@334 328 *
universe@334 329 * @param array a pointer to the array
universe@334 330 * @param capacity the new capacity
universe@334 331 * @return zero on success, non-zero if reallocation failed
universe@334 332 */
universe@334 333 int ucx_array_resize(UcxArray* array, size_t capacity);
universe@334 334
universe@334 335 /**
universe@334 336 * Resizes the array only, if the capacity is insufficient.
universe@334 337 *
universe@334 338 * If the requested capacity is smaller than the current capacity, this
universe@334 339 * function does nothing.
universe@334 340 *
universe@334 341 * @param array a pointer to the array
universe@334 342 * @param capacity the guaranteed capacity
universe@334 343 * @return zero on success, non-zero if reallocation failed
universe@334 344 */
universe@334 345 int ucx_array_reserve(UcxArray* array, size_t capacity);
universe@334 346
universe@334 347
universe@4 348
universe@4 349 #ifdef __cplusplus
universe@4 350 }
universe@4 351 #endif
universe@4 352
universe@334 353 #endif /* UCX_ARRAY_H */
universe@4 354

mercurial