universe@103: /*
universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103: *
universe@334: * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
universe@103: *
universe@103: * Redistribution and use in source and binary forms, with or without
universe@103: * modification, are permitted provided that the following conditions are met:
universe@103: *
universe@103: * 1. Redistributions of source code must retain the above copyright
universe@103: * notice, this list of conditions and the following disclaimer.
universe@103: *
universe@103: * 2. Redistributions in binary form must reproduce the above copyright
universe@103: * notice, this list of conditions and the following disclaimer in the
universe@103: * documentation and/or other materials provided with the distribution.
universe@103: *
universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103: * POSSIBILITY OF SUCH DAMAGE.
universe@4: */
universe@123: /**
universe@334: * Dynamically allocated array implementation.
universe@123: *
universe@334: * @file array.h
universe@123: * @author Mike Becker
universe@123: * @author Olaf Wintermann
universe@123: */
universe@4:
universe@334: #ifndef UCX_ARRAY_H
universe@334: #define UCX_ARRAY_H
universe@4:
universe@259: #include "ucx.h"
universe@259: #include "allocator.h"
universe@7:
universe@4: #ifdef __cplusplus
universe@4: extern "C" {
universe@4: #endif
universe@4:
universe@121: /**
universe@334: * UCX array type.
universe@121: */
universe@334: typedef struct {
universe@334: /**
universe@334: * The current capacity of the array.
universe@334: */
universe@334: size_t capacity;
universe@334: /**
universe@334: * The actual number of elements in the array.
universe@334: */
universe@334: size_t size;
universe@334: /**
universe@334: * The size of an individual element in bytes.
universe@334: */
universe@334: size_t elemsize;
universe@334: /**
universe@334: * A pointer to the data.
universe@334: */
universe@334: void* data;
universe@334: /**
universe@334: * The allocator used for the data.
universe@334: */
universe@334: UcxAllocator* allocator;
universe@334: } UcxArray;
universe@334:
universe@121:
universe@123: /**
universe@334: * Creates a new UCX array with the given capacity and element size.
universe@334: * @param capacity the initial capacity
universe@334: * @param elemsize the element size
universe@334: * @return a new UCX array structure
universe@123: */
universe@334: UcxArray ucx_array_new(size_t capacity, size_t elemsize);
universe@146:
universe@129: /**
universe@334: * Creates a new UCX array using the specified allocator.
universe@334: *
universe@334: * @param capacity the initial capacity
universe@334: * @param elemsize the element size
universe@334: * @param allocator the allocator to use
universe@334: * @return a new UCX array structure
universe@129: */
universe@334: UcxArray ucx_array_new_a(size_t capacity, size_t elemsize,
universe@334: UcxAllocator* allocator);
universe@4:
universe@128: /**
universe@334: * Creates an shallow copy of an array.
universe@128: *
universe@334: * This function clones the specified array by using memcpy().
universe@128: *
universe@334: * @param array the array to copy
universe@334: * @return the copy (may be an empty array on allocation errors)
universe@128: */
universe@334: UcxArray ucx_array_clone(UcxArray array);
universe@334:
universe@146:
universe@128: /**
universe@334: * Compares two UCX arrays element-wise by using a compare function.
universe@334: *
universe@334: * Elements of the two specified arrays are compared by using the specified
universe@123: * compare function and the additional data. The type and content of this
universe@123: * additional data depends on the cmp_func() used.
universe@123: *
universe@334: * This function always returns zero, if the element sizes of the arrays do
universe@334: * not match and performs no comparisons in this case.
universe@123: *
universe@334: * @param array1 the first array
universe@334: * @param array2 the second array
universe@123: * @param cmpfnc the compare function
universe@123: * @param data additional data for the compare function
universe@334: * @return 1, if and only if the two arrays equal element-wise, 0 otherwise
universe@123: */
universe@334: int ucx_array_equals(UcxArray array1, UcxArray array2,
universe@123: cmp_func cmpfnc, void* data);
universe@4:
universe@123: /**
universe@334: * Destroys the array.
universe@123: *
universe@334: * The data is freed and both capacity and count are reset to zero.
universe@334: * If the array structure itself has been dynamically allocated, it has to be
universe@334: * freed separately.
universe@123: *
universe@334: * @param array the array to free
universe@123: */
universe@334: void ucx_array_free(UcxArray *array);
universe@146:
universe@128: /**
universe@334: * Inserts an element at the end of the array.
universe@128: *
universe@334: * This is an O(1) operation.
universe@334: * The array will automatically grow, if the capacity is exceeded.
universe@334: * If a pointer to data is provided, the data is copied into the array with
universe@334: * memcpy(). Otherwise the new element is completely zeroed.
universe@128: *
universe@334: * @param array a pointer the array where to append the data
universe@334: * @param data a pointer to the data to insert (may be NULL
)
universe@334: * @return zero on success, non-zero if a reallocation was necessary but failed
universe@128: */
universe@334: int ucx_array_append(UcxArray *array, void *data);
universe@334:
universe@146:
universe@128: /**
universe@334: * Inserts an element at the beginning of the array.
universe@211: *
universe@334: * This is an expensive operation, because the contents must be moved.
universe@334: * If there is no particular reason to prepend data, you should use
universe@334: * ucx_array_append() instead.
universe@211: *
universe@334: * @param array a pointer the array where to prepend the data
universe@334: * @param data a pointer to the data to insert (may be NULL
)
universe@334: * @return zero on success, non-zero if a reallocation was necessary but failed
universe@211: */
universe@334: int ucx_array_prepend(UcxArray *array, void *data);
universe@211:
universe@211: /**
universe@334: * Concatenates two arrays.
universe@128: *
universe@334: * The contents of the second array are appended to the first array in one
universe@334: * single operation. The second array is otherwise left untouched.
universe@128: *
universe@334: * The first array may grow automatically. If this fails, both arrays remain
universe@334: * unmodified.
universe@334: *
universe@334: * @param array1 first array
universe@334: * @param array2 second array
universe@334: * @return zero on success, non-zero if reallocation was necessary but failed
universe@334: * or the element size does not match
universe@128: */
universe@334: int ucx_array_concat(UcxArray *array1, const UcxArray *array2);
universe@146:
universe@128: /**
universe@334: * Returns a pointer to the array element at the specified index.
universe@128: *
universe@334: * @param array the array to retrieve the element from
universe@334: * @param index index of the element to return
universe@334: * @return a pointer to the element at the specified index or NULL
,
universe@334: * if the index is greater than the array size
universe@128: */
universe@334: void *ucx_array_at(UcxArray array, size_t index);
universe@291:
universe@291: /**
universe@334: * Returns an element of the specified type by value.
universe@128: *
universe@334: * This expression can also be assigned to.
universe@128: *
universe@334: * If sizeof(type)
does not equal the array's element size, the
universe@334: * behavior is undefined.
universe@334: * If the index is out of bounds, the behavior is undefined.
universe@334: *
universe@334: * @param type the type of the element
universe@334: * @param array the array to retrieve the element from
universe@334: * @param index index of the element to return
universe@334: * @return the requested element
universe@334: * @see ucx_array_at()
universe@128: */
universe@334: #define ucx_array_at_typed(type, arr, i) (((type*)((arr).data))[i])
universe@146:
universe@128: /**
universe@334: * Shorthand for ucx_array_at_typed().
universe@128: */
universe@334: #define ucx_array_at_int(arr, i) ucx_array_at_typed(int, arr, i)
universe@146:
universe@128: /**
universe@334: * Shorthand for ucx_array_at_typed().
universe@128: */
universe@334: #define ucx_array_at_short(arr, i) ucx_array_at_typed(short, arr, i)
universe@146:
universe@124: /**
universe@334: * Shorthand for ucx_array_at_typed().
universe@124: */
universe@334: #define ucx_array_at_longint(arr, i) ucx_array_at_typed(long int, arr, i)
universe@146:
universe@124: /**
universe@334: * Shorthand for ucx_array_at_typed().
universe@124: */
universe@334: #define ucx_array_at_uint(arr, i) ucx_array_at_typed(unsigned int, arr, i)
universe@146:
universe@128: /**
universe@334: * Shorthand for ucx_array_at_typed().
universe@128: */
universe@334: #define ucx_array_at_ushort(arr, i) ucx_array_at_typed(unsigned short, arr, i)
universe@146:
universe@128: /**
universe@334: * Shorthand for ucx_array_at_typed().
universe@128: */
universe@334: #define ucx_array_at_ulongint(arr, i) \
universe@334: ucx_array_at_typed(unsigned long int, arr, i)
universe@146:
universe@128: /**
universe@334: * Shorthand for ucx_array_get_typed().
universe@128: */
universe@334: #define ucx_array_get_float(arr, i) ucx_array_get_typed(float, arr, i)
universe@334:
universe@334: /**
universe@334: * Shorthand for ucx_array_get_typed().
universe@334: */
universe@334: #define ucx_array_get_double(arr, i) ucx_array_get_typed(double, arr, i)
universe@146:
universe@128: /**
universe@128: * Returns the index of an element containing the specified data.
universe@128: *
universe@128: * This function uses a cmp_func() to compare the data of each list element
universe@334: * with the specified data. If no cmp_func is provided, memcmp() is used.
universe@128: *
universe@334: * If the array contains the data more than once, the index of the first
universe@128: * occurrence is returned.
universe@334: * If the array does not contain the data, the size of array is returned.
universe@128: *
universe@334: * @param array the array where to search for the data
universe@128: * @param elem the element data
universe@128: * @param cmpfnc the compare function
universe@128: * @param data additional data for the compare function
universe@334: * @return the index of the element containing the specified data or the size of
universe@334: * the array, if the data is not found in this array
universe@128: */
universe@334: size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
universe@146:
universe@128: /**
universe@334: * Checks, if an array contains a specific element.
universe@128: *
universe@334: * An element is found, if ucx_array_find() returns a value less than the size.
universe@128: *
universe@334: * @param array the array where to search for the data
universe@128: * @param elem the element data
universe@128: * @param cmpfnc the compare function
universe@128: * @param data additional data for the compare function
universe@334: * @return 1, if and only if the array contains the specified element data
universe@334: * @see ucx_array_find()
universe@128: */
universe@334: int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
universe@35:
universe@128: /**
universe@336: * Sorts a UcxArray with an almost in-place merge sort.
universe@128: *
universe@336: * This function uses additional memory for exactly one element.
universe@128: *
universe@334: * @param the array to sort
universe@128: * @param cmpfnc the function that shall be used to compare the element data
universe@128: * @param data additional data for the cmp_func()
universe@128: */
universe@336: void ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data);
universe@123:
universe@124: /**
universe@334: * Removes an element from the array.
universe@124: *
universe@334: * This is in general an expensive operation, because several elements may
universe@334: * be moved. If the order of the elements is not relevant, use
universe@334: * ucx_array_remove_fast() instead.
universe@124: *
universe@334: * @param array pointer to the array from which the element shall be removed
universe@334: * @param index the index of the element to remove
universe@124: */
universe@334: void ucx_array_remove(UcxArray *array, size_t index);
universe@146:
universe@128: /**
universe@334: * Removes an element from the array.
universe@128: *
universe@334: * This is an O(1) operation, but does not maintain the order of the elements.
universe@334: * The last element in the array is moved to the location of the removed
universe@334: * element.
universe@128: *
universe@334: * @param array pointer to the array from which the element shall be removed
universe@334: * @param index the index of the element to remove
universe@128: */
universe@334: void ucx_array_remove_fast(UcxArray *array, size_t index);
universe@334:
universe@334: /**
universe@334: * Shrinks the memory to exactly fit the contents.
universe@334: *
universe@334: * After this operation, the capacity equals the size.
universe@334: *
universe@334: * @param array a pointer to the array
universe@334: * @return zero on success, non-zero if reallocation failed
universe@334: */
universe@334: int ucx_array_shrink(UcxArray* array);
universe@334:
universe@334: /**
universe@334: * Sets the capacity of the array.
universe@334: *
universe@334: * If the new capacity is smaller than the size of the array, the elements
universe@334: * are removed and the size is adjusted accordingly.
universe@334: *
universe@334: * @param array a pointer to the array
universe@334: * @param capacity the new capacity
universe@334: * @return zero on success, non-zero if reallocation failed
universe@334: */
universe@334: int ucx_array_resize(UcxArray* array, size_t capacity);
universe@334:
universe@334: /**
universe@334: * Resizes the array only, if the capacity is insufficient.
universe@334: *
universe@334: * If the requested capacity is smaller than the current capacity, this
universe@334: * function does nothing.
universe@334: *
universe@334: * @param array a pointer to the array
universe@334: * @param capacity the guaranteed capacity
universe@334: * @return zero on success, non-zero if reallocation failed
universe@334: */
universe@334: int ucx_array_reserve(UcxArray* array, size_t capacity);
universe@334:
universe@334:
universe@4:
universe@4: #ifdef __cplusplus
universe@4: }
universe@4: #endif
universe@4:
universe@334: #endif /* UCX_ARRAY_H */
universe@4: