src/cx/array_list.h

Mon, 15 Dec 2025 19:00:51 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 15 Dec 2025 19:00:51 +0100
changeset 1608
46d8a8305948
parent 1607
0ecb13118cac
permissions
-rw-r--r--

complete refactoring of low-level array list functions - relates to #619

now only the documentation needs to be updated

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * @file array_list.h
 * @brief Array list implementation.
 * @author Mike Becker
 * @author Olaf Wintermann
 * @copyright 2-Clause BSD License
 */


#ifndef UCX_ARRAY_LIST_H
#define UCX_ARRAY_LIST_H

#include "list.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * The maximum item size in an array list that fits into
 * a stack buffer when swapped.
 */
CX_EXPORT extern const unsigned cx_array_swap_sbo_size;

#define CX_ARRAY(type, name) \
    struct { \
        type *data; \
        size_t size; \
        size_t capacity; \
    } name

typedef struct cx_array_s {
    void *data;
    size_t size;
    size_t capacity;
} CxArray;

cx_attr_nonnull
CX_EXPORT int cx_array_init_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity);

#define cx_array_init_a(allocator, array, capacity) cx_array_init_(allocator, (CxArray*)&(array), sizeof((array).data[0]), capacity)

#define cx_array_init(array, capacity) cx_array_init_a(cxDefaultAllocator, array, capacity)

cx_attr_nonnull
CX_EXPORT void cx_array_init_fixed_(CxArray *array, const void *data, size_t capacity, size_t size);

#define cx_array_init_fixed(array, fixed_size_array, num_initialized) cx_array_init_fixed_((CxArray*)&(array), fixed_size_array, cx_nmemb(fixed_size_array), num_initialized)

cx_attr_nonnull
CX_EXPORT int cx_array_reserve_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity);

#define cx_array_reserve_a(allocator, array, capacity) cx_array_reserve_(allocator, (CxArray*)&(array), sizeof((array).data[0]), capacity)

#define cx_array_reserve(array, capacity) cx_array_reserve_a(cxDefaultAllocator, array, capacity)

cx_attr_nonnull
CX_EXPORT int cx_array_move_to_new_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity);

#define cx_array_move_to_new_a(allocator, array, capacity) cx_array_move_to_new_(allocator, (CxArray*)&(array), sizeof((array).data[0]), capacity)

#define cx_array_move_to_new(array, capacity) cx_array_move_to_new_a(cxDefaultAllocator, array, capacity)

cx_attr_nonnull_arg(1, 2)
CX_EXPORT int cx_array_insert_(const CxAllocator *allocator, CxArray *array,
        size_t elem_size, size_t index, const void *other, size_t n);

#define cx_array_add_a(allocator, array, element) \
        cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (array).size, element, 1)

#define cx_array_add(array, element) cx_array_add_a(cxDefaultAllocator, array, element)

#define cx_array_insert_a(allocator, array, index, element) \
        cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), index, element, 1)

#define cx_array_insert(array, index, element) cx_array_insert_a(cxDefaultAllocator, array, index, element)

#define cx_array_insert_array_a(allocator, array, index, other, n) \
        cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), index, other, n)

#define cx_array_insert_array(array, index, other, n) cx_array_insert_array_a(cxDefaultAllocator, array, index, other, n)

#define cx_array_add_array_a(allocator, array, other, n) \
        cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (array).size, other, n)

#define cx_array_add_array(array, other, n) cx_array_add_array_a(cxDefaultAllocator, array, other, n)

cx_attr_nonnull
CX_EXPORT int cx_array_insert_sorted_(const CxAllocator *allocator, CxArray *array,
        size_t elem_size, cx_compare_func cmp_func, const void *sorted_data, size_t n,
        bool allow_duplicates);

#define cx_array_insert_sorted_a(allocator, array, cmp_func, element) \
        cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, element, 1, true)

#define cx_array_insert_sorted(array, cmp_func, element) cx_array_insert_sorted_a(cxDefaultAllocator, array, cmp_func, element)

#define cx_array_insert_sorted_array_a(allocator, array, cmp_func, sorted_data, n) \
        cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, sorted_data, n, true)

#define cx_array_insert_sorted_array(array, cmp_func, sorted_data, n) cx_array_insert_sorted_array_a(cxDefaultAllocator, array, cmp_func, sorted_data, n)

#define cx_array_insert_unique_a(allocator, array, cmp_func, element) \
        cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, element, 1, false)

#define cx_array_insert_unique(array, cmp_func, element) cx_array_insert_unique_a(cxDefaultAllocator, array, cmp_func, element)

#define cx_array_insert_unique_array_a(allocator, array, cmp_func, sorted_data, n) \
        cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, sorted_data, n, false)

#define cx_array_insert_unique_array(array, cmp_func, sorted_data, n) cx_array_insert_unique_array_a(cxDefaultAllocator, array, cmp_func, sorted_data, n)

cx_attr_nodiscard cx_attr_nonnull
CX_EXPORT CxIterator cx_array_iterator_(CxArray *array, size_t elem_size);

#define cx_array_iterator(array) cx_array_iterator_((CxArray*)&(array), sizeof((array).data[0]))

cx_attr_nodiscard cx_attr_nonnull
CX_EXPORT CxIterator cx_array_iterator_ptr_(CxArray *array);

#define cx_array_iterator_ptr(array) cx_array_iterator_ptr_((CxArray*)&(array))

cx_attr_nonnull
CX_EXPORT void cx_array_free_(const CxAllocator *allocator, CxArray *array);

#define cx_array_free(array) cx_array_free_(cxDefaultAllocator, (CxArray*)&(array))

#define cx_array_free_a(allocator, array) cx_array_free_(allocator, (CxArray*)&(array))



/**
 * Searches the largest lower bound in a sorted array.
 *
 * In other words, this function returns the index of the largest element
 * in @p arr that is less or equal to @p elem with respect to @p cmp_func.
 * When no such element exists, @p size is returned.
 *
 * When such an element exists more than once, the largest index of all those
 * elements is returned.
 *
 * If @p elem is contained in the array, this is identical to
 * #cx_array_binary_search().
 *
 * If the array is not sorted with respect to the @p cmp_func, the behavior
 * is undefined.
 *
 * @param arr the array to search
 * @param size the size of the array
 * @param elem_size the size of one element
 * @param elem the element to find
 * @param cmp_func the compare function
 * @return the index of the largest lower bound, or @p size
 * @see cx_array_binary_search_sup()
 * @see cx_array_binary_search()
 */
cx_attr_nonnull
CX_EXPORT size_t cx_array_binary_search_inf(const void *arr, size_t size,
        size_t elem_size, const void *elem, cx_compare_func cmp_func);

/**
 * Searches an item in a sorted array.
 *
 * When such an element exists more than once, the largest index of all those
 * elements is returned.
 *
 * If the array is not sorted with respect to the @p cmp_func, the behavior
 * is undefined.
 *
 * @param arr the array to search
 * @param size the size of the array
 * @param elem_size the size of one element
 * @param elem the element to find
 * @param cmp_func the compare function
 * @return the index of the element in the array, or @p size if the element
 * cannot be found
 * @see cx_array_binary_search_inf()
 * @see cx_array_binary_search_sup()
 */
cx_attr_nonnull
CX_EXPORT size_t cx_array_binary_search(const void *arr, size_t size,
        size_t elem_size, const void *elem, cx_compare_func cmp_func);

/**
 * Searches the smallest upper bound in a sorted array.
 *
 * In other words, this function returns the index of the smallest element
 * in @p arr that is greater or equal to @p elem with respect to @p cmp_func.
 * When no such element exists, @p size is returned.
 *
 * When such an element exists more than once, the smallest index of all those
 * elements is returned.
 *
 * If @p elem is contained in the array, this is identical to
 * #cx_array_binary_search().
 *
 * If the array is not sorted with respect to the @p cmp_func, the behavior
 * is undefined.
 *
 * @param arr the array to search
 * @param size the size of the array
 * @param elem_size the size of one element
 * @param elem the element to find
 * @param cmp_func the compare function
 * @return the index of the smallest upper bound, or @p size
 * @see cx_array_binary_search_inf()
 * @see cx_array_binary_search()
 */
cx_attr_nonnull
CX_EXPORT size_t cx_array_binary_search_sup(const void *arr, size_t size,
        size_t elem_size, const void *elem, cx_compare_func cmp_func);

/**
 * Swaps two array elements.
 *
 * @param arr the array
 * @param elem_size the element size
 * @param idx1 index of the first element
 * @param idx2 index of the second element
 */
cx_attr_nonnull
CX_EXPORT void cx_array_swap(void *arr, size_t elem_size, size_t idx1, size_t idx2);

/**
 * Allocates an array list for storing elements with @p elem_size bytes each.
 *
 * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of
 * copies of the added elements, and the compare function will be automatically set
 * to cx_cmp_ptr().
 *
 * @param allocator the allocator for allocating the list memory
 * (if @c NULL, the cxDefaultAllocator will be used)
 * @param elem_size the size of each element in bytes
 * @param initial_capacity the initial number of elements the array can store
 * @return the created list
 */
cx_attr_nodiscard
cx_attr_malloc
cx_attr_dealloc(cxListFree, 1)
CX_EXPORT CxList *cxArrayListCreate(const CxAllocator *allocator,
        size_t elem_size, size_t initial_capacity);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // UCX_ARRAY_LIST_H

mercurial