Sat, 08 Nov 2025 23:06:11 +0100
update ucx
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | /** |
| 852 | 29 | * @file array_list.h |
| 30 | * @brief Array list implementation. | |
| 31 | * @author Mike Becker | |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | */ |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | #ifndef UCX_ARRAY_LIST_H |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | #define UCX_ARRAY_LIST_H |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | #include "list.h" |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | #ifdef __cplusplus |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | extern "C" { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | #endif |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | /** |
| 889 | 47 | * The maximum item size in an array list that fits into |
| 48 | * a stack buffer when swapped. | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
49 | */ |
| 889 | 50 | CX_EXPORT extern const unsigned cx_array_swap_sbo_size; |
| 852 | 51 | |
| 52 | /** | |
| 53 | * Declares variables for an array that can be used with the convenience macros. | |
| 54 | * | |
| 55 | * @par Examples | |
| 56 | * @code | |
| 57 | * // integer array with at most 255 elements | |
| 58 | * CX_ARRAY_DECLARE_SIZED(int, myarray, uint8_t) | |
| 59 | * | |
| 60 | * // array of MyObject* pointers where size and capacity are stored as unsigned int | |
| 61 | * CX_ARRAY_DECLARE_SIZED(MyObject*, objects, unsigned int) | |
| 62 | * | |
| 63 | * // initializing code | |
| 64 | * cx_array_initialize(myarray, 16); // reserve space for 16 | |
| 65 | * cx_array_initialize(objects, 100); // reserve space for 100 | |
| 66 | * @endcode | |
| 67 | * | |
| 68 | * @param type the type of the data | |
| 69 | * @param name the name of the array | |
| 70 | * @param size_type the type of the size (should be uint8_t, uint16_t, uint32_t, or size_t) | |
| 71 | * | |
| 72 | * @see cx_array_initialize() | |
| 73 | * @see cx_array_simple_add() | |
| 74 | * @see cx_array_simple_copy() | |
| 75 | * @see cx_array_simple_add_sorted() | |
| 76 | * @see cx_array_simple_insert_sorted() | |
| 77 | */ | |
| 78 | #define CX_ARRAY_DECLARE_SIZED(type, name, size_type) \ | |
| 79 | type * name; \ | |
| 80 | /** Array size. */ size_type name##_size; \ | |
| 81 | /** Array capacity. */ size_type name##_capacity | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
82 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
83 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
84 | * Declares variables for an array that can be used with the convenience macros. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
85 | * |
| 889 | 86 | * The size and capacity variables will have type @c size_t. |
| 852 | 87 | * Use #CX_ARRAY_DECLARE_SIZED() to specify a different type. |
| 88 | * | |
| 89 | * @par Examples | |
| 90 | * @code | |
| 91 | * // int array | |
| 92 | * CX_ARRAY_DECLARE(int, myarray) | |
| 93 | * | |
| 94 | * // initializing code | |
| 95 | * cx_array_initialize(myarray, 32); // reserve space for 32 | |
| 96 | * @endcode | |
| 97 | * | |
| 98 | * @param type the type of the data | |
| 99 | * @param name the name of the array | |
| 100 | * | |
| 101 | * @see cx_array_initialize() | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
102 | * @see cx_array_simple_add() |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
103 | * @see cx_array_simple_copy() |
| 852 | 104 | * @see cx_array_simple_add_sorted() |
| 105 | * @see cx_array_simple_insert_sorted() | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
106 | */ |
| 852 | 107 | #define CX_ARRAY_DECLARE(type, name) CX_ARRAY_DECLARE_SIZED(type, name, size_t) |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
108 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
109 | /** |
| 852 | 110 | * Initializes an array with the given capacity. |
| 111 | * | |
| 112 | * The type of the capacity depends on the type used during declaration. | |
| 113 | * | |
| 114 | * @par Examples | |
| 115 | * @code | |
| 116 | * CX_ARRAY_DECLARE_SIZED(int, arr1, uint8_t) | |
| 117 | * CX_ARRAY_DECLARE(int, arr2) // size and capacity are implicitly size_t | |
| 118 | * | |
| 119 | * // initializing code | |
| 120 | * cx_array_initialize(arr1, 500); // error: maximum for uint8_t is 255 | |
| 121 | * cx_array_initialize(arr2, 500); // OK | |
| 122 | * @endcode | |
| 123 | * | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
124 | * |
| 886 | 125 | * The memory for the array is allocated with the cxDefaultAllocator. |
| 126 | * | |
| 852 | 127 | * @param array the name of the array |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
128 | * @param capacity the initial capacity |
| 852 | 129 | * @see cx_array_initialize_a() |
| 130 | * @see CX_ARRAY_DECLARE_SIZED() | |
| 131 | * @see CX_ARRAY_DECLARE() | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
132 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
133 | #define cx_array_initialize(array, capacity) \ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
134 | array##_capacity = capacity; \ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
135 | array##_size = 0; \ |
| 886 | 136 | array = cxMallocDefault(sizeof(array[0]) * capacity) |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
137 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
138 | /** |
| 852 | 139 | * Initializes an array with the given capacity using the specified allocator. |
| 140 | * | |
| 141 | * @par Example | |
| 142 | * @code | |
| 143 | * CX_ARRAY_DECLARE(int, myarray) | |
| 144 | * | |
| 145 | * | |
| 146 | * const CxAllocator *al = // ... | |
| 147 | * cx_array_initialize_a(al, myarray, 128); | |
| 148 | * // ... | |
| 889 | 149 | * cxFree(al, myarray); // remember to free with the same allocator |
| 852 | 150 | * @endcode |
| 151 | * | |
| 152 | * @param allocator (@c CxAllocator*) the allocator | |
| 153 | * @param array the name of the array | |
| 154 | * @param capacity the initial capacity | |
| 155 | * @see cx_array_initialize() | |
| 156 | * @see CX_ARRAY_DECLARE_SIZED() | |
| 157 | * @see CX_ARRAY_DECLARE() | |
| 158 | */ | |
| 159 | #define cx_array_initialize_a(allocator, array, capacity) \ | |
| 160 | array##_capacity = capacity; \ | |
| 161 | array##_size = 0; \ | |
| 162 | array = cxMalloc(allocator, sizeof(array[0]) * capacity) | |
| 163 | ||
| 164 | /** | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
165 | * Defines a reallocation mechanism for arrays. |
| 852 | 166 | * You can create your own, use cx_array_reallocator(), or |
| 167 | * use the #cx_array_default_reallocator. | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
168 | */ |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
169 | struct cx_array_reallocator_s { |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
170 | /** |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
171 | * Reallocates space for the given array. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
172 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
173 | * Implementations are not required to free the original array. |
| 889 | 174 | * This allows reallocation of static or stack memory by allocating heap memory |
| 175 | * and copying the array contents; namely when @c stack_ptr in this struct | |
| 176 | * is not @c NULL and @p array equals @c stack_ptr. | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
177 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178 | * @param array the array to reallocate |
| 886 | 179 | * @param old_capacity the old number of elements |
| 180 | * @param new_capacity the new number of elements | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
181 | * @param elem_size the size of each element |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
182 | * @param alloc a reference to this allocator |
| 852 | 183 | * @return a pointer to the reallocated memory or @c NULL on failure |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
184 | */ |
| 889 | 185 | void *(*realloc)( void *array, size_t old_capacity, size_t new_capacity, |
| 186 | size_t elem_size, struct cx_array_reallocator_s *alloc); | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
187 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188 | /** |
| 889 | 189 | * The allocator that shall be used for the reallocations. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190 | */ |
| 889 | 191 | const CxAllocator *allocator; |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
192 | /** |
| 889 | 193 | * Optional pointer to stack memory |
| 194 | * if the array is originally located on the stack. | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
195 | */ |
| 889 | 196 | const void *stack_ptr; |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
197 | }; |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
198 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
199 | /** |
| 852 | 200 | * Typedef for the array reallocator struct. |
| 201 | */ | |
| 202 | typedef struct cx_array_reallocator_s CxArrayReallocator; | |
| 203 | ||
| 204 | /** | |
| 886 | 205 | * A default array reallocator that is based on the cxDefaultAllocator. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
206 | */ |
| 889 | 207 | CX_EXPORT extern CxArrayReallocator *cx_array_default_reallocator; |
| 852 | 208 | |
| 209 | /** | |
| 210 | * Creates a new array reallocator. | |
| 211 | * | |
| 886 | 212 | * When @p allocator is @c NULL, the cxDefaultAllocator will be used. |
| 852 | 213 | * |
| 889 | 214 | * When @p stack_ptr is not @c NULL, the reallocator is supposed to be used |
| 215 | * @em only for the specific array initially located at @p stack_ptr. | |
| 216 | * When reallocation is needed, the reallocator checks if the array is | |
| 217 | * still located at @p stack_ptr and copies the contents to the heap. | |
| 852 | 218 | * |
| 889 | 219 | * @note Invoking this function with both arguments being @c NULL will return a |
| 852 | 220 | * reallocator that behaves like #cx_array_default_reallocator. |
| 221 | * | |
| 222 | * @param allocator the allocator this reallocator shall be based on | |
| 889 | 223 | * @param stack_ptr the address of the array when the array is initially located |
| 224 | * on the stack or shall not reallocate in place | |
| 852 | 225 | * @return an array reallocator |
| 226 | */ | |
| 889 | 227 | CX_EXPORT CxArrayReallocator cx_array_reallocator( |
| 228 | const struct cx_allocator_s *allocator, const void *stack_ptr); | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
229 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
230 | /** |
| 852 | 231 | * Reserves memory for additional elements. |
| 232 | * | |
| 233 | * This function checks if the @p capacity of the array is sufficient to hold | |
| 234 | * at least @p size plus @p elem_count elements. If not, a reallocation is | |
| 235 | * performed with the specified @p reallocator. | |
| 236 | * You can create your own reallocator by hand, use #cx_array_default_reallocator, | |
| 237 | * or use the convenience function cx_array_reallocator() to create a custom reallocator. | |
| 238 | * | |
| 239 | * This function can be useful to replace subsequent calls to cx_array_copy() | |
| 240 | * with one single cx_array_reserve() and then - after guaranteeing a | |
| 241 | * sufficient capacity - use simple memmove() or memcpy(). | |
| 242 | * | |
| 243 | * The @p width in bytes refers to the size and capacity. | |
| 244 | * Both must have the same width. | |
| 889 | 245 | * Supported are 0, 1, 2, and 4, as well as 8 if running on a 64-bit |
| 852 | 246 | * architecture. If set to zero, the native word width is used. |
| 247 | * | |
| 248 | * @param array a pointer to the target array | |
| 249 | * @param size a pointer to the size of the array | |
| 250 | * @param capacity a pointer to the capacity of the array | |
| 251 | * @param width the width in bytes for the @p size and @p capacity or zero for default | |
| 252 | * @param elem_size the size of one element | |
| 253 | * @param elem_count the number of expected additional elements | |
| 254 | * @param reallocator the array reallocator to use | |
| 255 | * (@c NULL defaults to #cx_array_default_reallocator) | |
| 256 | * @retval zero success | |
| 257 | * @retval non-zero failure | |
| 258 | * @see cx_array_reallocator() | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
259 | */ |
| 852 | 260 | cx_attr_nonnull_arg(1, 2, 3) |
| 889 | 261 | CX_EXPORT int cx_array_reserve(void **array, void *size, void *capacity, |
| 262 | unsigned width, size_t elem_size, size_t elem_count, | |
| 263 | CxArrayReallocator *reallocator); | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
264 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
265 | /** |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
266 | * Copies elements from one array to another. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
267 | * |
| 852 | 268 | * The elements are copied to the @p target array at the specified @p index, |
| 269 | * overwriting possible elements. The @p index does not need to be in range of | |
| 270 | * the current array @p size. If the new index plus the number of elements added | |
| 889 | 271 | * extends the array's size, the remaining @p capacity is used. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
272 | * |
| 852 | 273 | * If the @p capacity is also insufficient to hold the new data, a reallocation |
| 274 | * attempt is made with the specified @p reallocator. | |
| 275 | * You can create your own reallocator by hand, use #cx_array_default_reallocator, | |
| 276 | * or use the convenience function cx_array_reallocator() to create a custom reallocator. | |
| 277 | * | |
| 278 | * The @p width in bytes refers to the size and capacity. | |
| 279 | * Both must have the same width. | |
| 889 | 280 | * Supported are 0, 1, 2, and 4, as well as 8 if running on a 64-bit |
| 852 | 281 | * architecture. If set to zero, the native word width is used. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
282 | * |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
283 | * @param target a pointer to the target array |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
284 | * @param size a pointer to the size of the target array |
| 852 | 285 | * @param capacity a pointer to the capacity of the target array |
| 286 | * @param width the width in bytes for the @p size and @p capacity or zero for default | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
287 | * @param index the index where the copied elements shall be placed |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
288 | * @param src the source array |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
289 | * @param elem_size the size of one element |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
290 | * @param elem_count the number of elements to copy |
| 852 | 291 | * @param reallocator the array reallocator to use |
| 292 | * (@c NULL defaults to #cx_array_default_reallocator) | |
| 293 | * @retval zero success | |
| 294 | * @retval non-zero failure | |
| 295 | * @see cx_array_reallocator() | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
296 | */ |
| 852 | 297 | cx_attr_nonnull_arg(1, 2, 3, 6) |
| 889 | 298 | CX_EXPORT int cx_array_copy(void **target, void *size, void *capacity, unsigned width, |
| 299 | size_t index, const void *src, size_t elem_size, size_t elem_count, | |
| 300 | CxArrayReallocator *reallocator); | |
| 852 | 301 | |
| 302 | /** | |
| 303 | * Convenience macro that uses cx_array_copy() with a default layout and | |
| 304 | * the specified reallocator. | |
| 305 | * | |
| 306 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 307 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 308 | * @param index (@c size_t) the index where the copied elements shall be placed | |
| 309 | * @param src (@c void*) the source array | |
| 310 | * @param count (@c size_t) the number of elements to copy | |
| 311 | * @retval zero success | |
| 312 | * @retval non-zero failure | |
| 313 | * @see CX_ARRAY_DECLARE() | |
| 314 | * @see cx_array_simple_copy() | |
| 315 | */ | |
| 316 | #define cx_array_simple_copy_a(reallocator, array, index, src, count) \ | |
| 317 | cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \ | |
| 318 | sizeof(array##_size), index, src, sizeof((array)[0]), count, \ | |
| 319 | reallocator) | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
320 | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
321 | /** |
| 852 | 322 | * Convenience macro that uses cx_array_copy() with a default layout and |
| 323 | * the default reallocator. | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
324 | * |
| 852 | 325 | * @param array the name of the array (NOT a pointer or alias to the array) |
| 326 | * @param index (@c size_t) the index where the copied elements shall be placed | |
| 327 | * @param src (@c void*) the source array | |
| 328 | * @param count (@c size_t) the number of elements to copy | |
| 329 | * @retval zero success | |
| 330 | * @retval non-zero failure | |
| 331 | * @see CX_ARRAY_DECLARE() | |
| 332 | * @see cx_array_simple_copy_a() | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
333 | */ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
334 | #define cx_array_simple_copy(array, index, src, count) \ |
| 852 | 335 | cx_array_simple_copy_a(NULL, array, index, src, count) |
| 336 | ||
| 337 | /** | |
| 338 | * Convenience macro that uses cx_array_reserve() with a default layout and | |
| 339 | * the specified reallocator. | |
| 340 | * | |
| 341 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 342 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 343 | * @param count (@c size_t) the number of expected @em additional elements | |
| 344 | * @retval zero success | |
| 345 | * @retval non-zero failure | |
| 346 | * @see CX_ARRAY_DECLARE() | |
| 347 | * @see cx_array_simple_reserve() | |
| 348 | */ | |
| 349 | #define cx_array_simple_reserve_a(reallocator, array, count) \ | |
| 350 | cx_array_reserve((void**)&(array), &(array##_size), &(array##_capacity), \ | |
| 351 | sizeof(array##_size), sizeof((array)[0]), count, \ | |
| 352 | reallocator) | |
| 353 | ||
| 354 | /** | |
| 355 | * Convenience macro that uses cx_array_reserve() with a default layout and | |
| 356 | * the default reallocator. | |
| 357 | * | |
| 358 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 359 | * @param count (@c size_t) the number of expected additional elements | |
| 360 | * @retval zero success | |
| 361 | * @retval non-zero failure | |
| 362 | * @see CX_ARRAY_DECLARE() | |
| 363 | * @see cx_array_simple_reserve_a() | |
| 364 | */ | |
| 365 | #define cx_array_simple_reserve(array, count) \ | |
| 366 | cx_array_simple_reserve_a(NULL, array, count) | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
367 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
368 | /** |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
369 | * Adds an element to an array with the possibility of allocating more space. |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
370 | * |
| 852 | 371 | * The element @p elem is added to the end of the @p target array which contains |
| 372 | * @p size elements, already. The @p capacity must point to a variable denoting | |
| 373 | * the current maximum number of elements the array can hold. | |
| 374 | * | |
| 375 | * If the capacity is insufficient to hold the new element, an attempt to | |
| 376 | * increase the @p capacity is made and the new capacity is written back. | |
| 377 | * | |
| 378 | * The \@ SIZE_TYPE is flexible and can be any unsigned integer type. | |
| 379 | * It is important, however, that @p size and @p capacity are pointers to | |
| 380 | * variables of the same type. | |
| 381 | * | |
| 382 | * @param target (@c void**) a pointer to the target array | |
| 383 | * @param size (@c SIZE_TYPE*) a pointer to the size of the target array | |
| 384 | * @param capacity (@c SIZE_TYPE*) a pointer to the capacity of the target array | |
| 385 | * @param elem_size (@c size_t) the size of one element | |
| 386 | * @param elem (@c void*) a pointer to the element to add | |
| 387 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 388 | * @retval zero success | |
| 389 | * @retval non-zero failure | |
| 390 | */ | |
| 391 | #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \ | |
| 392 | cx_array_copy((void**)(target), size, capacity, sizeof(*(size)), \ | |
| 393 | *(size), elem, elem_size, 1, reallocator) | |
| 394 | ||
| 395 | /** | |
| 396 | * Convenience macro that uses cx_array_add() with a default layout and | |
| 397 | * the specified reallocator. | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
398 | * |
| 852 | 399 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use |
| 400 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 401 | * @param elem the element to add (NOT a pointer, address is automatically taken) | |
| 402 | * @retval zero success | |
| 403 | * @retval non-zero failure | |
| 404 | * @see CX_ARRAY_DECLARE() | |
| 405 | * @see cx_array_simple_add() | |
| 406 | */ | |
| 407 | #define cx_array_simple_add_a(reallocator, array, elem) \ | |
| 408 | cx_array_simple_copy_a(reallocator, array, array##_size, &(elem), 1) | |
| 409 | ||
| 410 | /** | |
| 411 | * Convenience macro that uses cx_array_add() with a default layout and | |
| 412 | * the default reallocator. | |
| 413 | * | |
| 414 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 415 | * @param elem the element to add (NOT a pointer, address is automatically taken) | |
| 416 | * @retval zero success | |
| 417 | * @retval non-zero failure | |
| 418 | * @see CX_ARRAY_DECLARE() | |
| 419 | * @see cx_array_simple_add_a() | |
| 420 | */ | |
| 421 | #define cx_array_simple_add(array, elem) \ | |
| 422 | cx_array_simple_add_a(cx_array_default_reallocator, array, elem) | |
| 423 | ||
| 424 | /** | |
| 425 | * Inserts a sorted array into another sorted array. | |
| 426 | * | |
| 427 | * If either the target or the source array is not already sorted with respect | |
| 428 | * to the specified @p cmp_func, the behavior is undefined. | |
| 429 | * | |
| 430 | * If the capacity is insufficient to hold the new data, a reallocation | |
| 431 | * attempt is made. | |
| 432 | * You can create your own reallocator by hand, use #cx_array_default_reallocator, | |
| 433 | * or use the convenience function cx_array_reallocator() to create a custom reallocator. | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
434 | * |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
435 | * @param target a pointer to the target array |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
436 | * @param size a pointer to the size of the target array |
| 852 | 437 | * @param capacity a pointer to the capacity of the target array |
| 438 | * @param cmp_func the compare function for the elements | |
| 439 | * @param src the source array | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
440 | * @param elem_size the size of one element |
| 852 | 441 | * @param elem_count the number of elements to insert |
| 442 | * @param reallocator the array reallocator to use | |
| 443 | * (@c NULL defaults to #cx_array_default_reallocator) | |
| 444 | * @retval zero success | |
| 445 | * @retval non-zero failure | |
| 446 | */ | |
| 447 | cx_attr_nonnull_arg(1, 2, 3, 5) | |
| 889 | 448 | CX_EXPORT int cx_array_insert_sorted(void **target, size_t *size, size_t *capacity, |
| 449 | cx_compare_func cmp_func, const void *src, size_t elem_size, size_t elem_count, | |
| 450 | CxArrayReallocator *reallocator); | |
| 852 | 451 | |
| 452 | /** | |
| 453 | * Inserts an element into a sorted array. | |
| 454 | * | |
| 455 | * If the target array is not already sorted with respect | |
| 456 | * to the specified @p cmp_func, the behavior is undefined. | |
| 457 | * | |
| 889 | 458 | * If the capacity is not enough to hold the new data, a reallocation |
| 852 | 459 | * attempt is made. |
| 460 | * | |
| 461 | * The \@ SIZE_TYPE is flexible and can be any unsigned integer type. | |
| 462 | * It is important, however, that @p size and @p capacity are pointers to | |
| 463 | * variables of the same type. | |
| 464 | * | |
| 465 | * @param target (@c void**) a pointer to the target array | |
| 466 | * @param size (@c SIZE_TYPE*) a pointer to the size of the target array | |
| 467 | * @param capacity (@c SIZE_TYPE*) a pointer to the capacity of the target array | |
| 468 | * @param elem_size (@c size_t) the size of one element | |
| 469 | * @param elem (@c void*) a pointer to the element to add | |
| 470 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 471 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 472 | * @retval zero success | |
| 473 | * @retval non-zero failure | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
474 | */ |
| 852 | 475 | #define cx_array_add_sorted(target, size, capacity, elem_size, elem, cmp_func, reallocator) \ |
| 476 | cx_array_insert_sorted((void**)(target), size, capacity, cmp_func, elem, elem_size, 1, reallocator) | |
| 477 | ||
| 478 | /** | |
| 479 | * Convenience macro for cx_array_add_sorted() with a default | |
| 480 | * layout and the specified reallocator. | |
| 481 | * | |
| 482 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 483 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 484 | * @param elem the element to add (NOT a pointer, address is automatically taken) | |
| 485 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 486 | * @retval zero success | |
| 487 | * @retval non-zero failure | |
| 488 | * @see CX_ARRAY_DECLARE() | |
| 489 | * @see cx_array_simple_add_sorted() | |
| 490 | */ | |
| 491 | #define cx_array_simple_add_sorted_a(reallocator, array, elem, cmp_func) \ | |
| 492 | cx_array_add_sorted(&array, &(array##_size), &(array##_capacity), \ | |
| 493 | sizeof((array)[0]), &(elem), cmp_func, reallocator) | |
| 494 | ||
| 495 | /** | |
| 496 | * Convenience macro for cx_array_add_sorted() with a default | |
| 497 | * layout and the default reallocator. | |
| 498 | * | |
| 499 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 500 | * @param elem the element to add (NOT a pointer, address is automatically taken) | |
| 501 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 502 | * @retval zero success | |
| 503 | * @retval non-zero failure | |
| 504 | * @see CX_ARRAY_DECLARE() | |
| 505 | * @see cx_array_simple_add_sorted_a() | |
| 506 | */ | |
| 507 | #define cx_array_simple_add_sorted(array, elem, cmp_func) \ | |
| 508 | cx_array_simple_add_sorted_a(NULL, array, elem, cmp_func) | |
| 509 | ||
| 510 | /** | |
| 511 | * Convenience macro for cx_array_insert_sorted() with a default | |
| 512 | * layout and the specified reallocator. | |
| 513 | * | |
| 514 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 515 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 516 | * @param src (@c void*) pointer to the source array | |
| 517 | * @param n (@c size_t) number of elements in the source array | |
| 518 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 519 | * @retval zero success | |
| 520 | * @retval non-zero failure | |
| 521 | * @see CX_ARRAY_DECLARE() | |
| 522 | * @see cx_array_simple_insert_sorted() | |
| 523 | */ | |
| 524 | #define cx_array_simple_insert_sorted_a(reallocator, array, src, n, cmp_func) \ | |
| 525 | cx_array_insert_sorted((void**)(&array), &(array##_size), &(array##_capacity), \ | |
| 526 | cmp_func, src, sizeof((array)[0]), n, reallocator) | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
527 | |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
528 | /** |
| 852 | 529 | * Convenience macro for cx_array_insert_sorted() with a default |
| 530 | * layout and the default reallocator. | |
| 531 | * | |
| 532 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 533 | * @param src (@c void*) pointer to the source array | |
| 534 | * @param n (@c size_t) number of elements in the source array | |
| 535 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 536 | * @retval zero success | |
| 537 | * @retval non-zero failure | |
| 538 | * @see CX_ARRAY_DECLARE() | |
| 539 | * @see cx_array_simple_insert_sorted_a() | |
| 540 | */ | |
| 541 | #define cx_array_simple_insert_sorted(array, src, n, cmp_func) \ | |
| 542 | cx_array_simple_insert_sorted_a(NULL, array, src, n, cmp_func) | |
| 543 | ||
| 889 | 544 | |
| 545 | /** | |
| 546 | * Inserts a sorted array into another sorted array, avoiding duplicates. | |
| 547 | * | |
| 548 | * If either the target or the source array is not already sorted with respect | |
| 549 | * to the specified @p cmp_func, the behavior is undefined. | |
| 550 | * | |
| 551 | * If the capacity is insufficient to hold the new data, a reallocation | |
| 552 | * attempt is made. | |
| 553 | * You can create your own reallocator by hand, use #cx_array_default_reallocator, | |
| 554 | * or use the convenience function cx_array_reallocator() to create a custom reallocator. | |
| 555 | * | |
| 556 | * @param target a pointer to the target array | |
| 557 | * @param size a pointer to the size of the target array | |
| 558 | * @param capacity a pointer to the capacity of the target array | |
| 559 | * @param cmp_func the compare function for the elements | |
| 560 | * @param src the source array | |
| 561 | * @param elem_size the size of one element | |
| 562 | * @param elem_count the number of elements to insert | |
| 563 | * @param reallocator the array reallocator to use | |
| 564 | * (@c NULL defaults to #cx_array_default_reallocator) | |
| 565 | * @retval zero success | |
| 566 | * @retval non-zero failure | |
| 567 | */ | |
| 568 | cx_attr_nonnull_arg(1, 2, 3, 5) | |
| 569 | CX_EXPORT int cx_array_insert_unique(void **target, size_t *size, size_t *capacity, | |
| 570 | cx_compare_func cmp_func, const void *src, size_t elem_size, size_t elem_count, | |
| 571 | CxArrayReallocator *reallocator); | |
| 572 | ||
| 573 | /** | |
| 574 | * Inserts an element into a sorted array if it does not exist. | |
| 575 | * | |
| 576 | * If the target array is not already sorted with respect | |
| 577 | * to the specified @p cmp_func, the behavior is undefined. | |
| 578 | * | |
| 579 | * If the capacity is insufficient to hold the new data, a reallocation | |
| 580 | * attempt is made. | |
| 581 | * | |
| 582 | * The \@ SIZE_TYPE is flexible and can be any unsigned integer type. | |
| 583 | * It is important, however, that @p size and @p capacity are pointers to | |
| 584 | * variables of the same type. | |
| 585 | * | |
| 586 | * @param target (@c void**) a pointer to the target array | |
| 587 | * @param size (@c SIZE_TYPE*) a pointer to the size of the target array | |
| 588 | * @param capacity (@c SIZE_TYPE*) a pointer to the capacity of the target array | |
| 589 | * @param elem_size (@c size_t) the size of one element | |
| 590 | * @param elem (@c void*) a pointer to the element to add | |
| 591 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 592 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 593 | * @retval zero success (also when the element was already present) | |
| 594 | * @retval non-zero failure | |
| 595 | */ | |
| 596 | #define cx_array_add_unique(target, size, capacity, elem_size, elem, cmp_func, reallocator) \ | |
| 597 | cx_array_insert_unique((void**)(target), size, capacity, cmp_func, elem, elem_size, 1, reallocator) | |
| 598 | ||
| 599 | /** | |
| 600 | * Convenience macro for cx_array_add_unique() with a default | |
| 601 | * layout and the specified reallocator. | |
| 602 | * | |
| 603 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 604 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 605 | * @param elem the element to add (NOT a pointer, address is automatically taken) | |
| 606 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 607 | * @retval zero success | |
| 608 | * @retval non-zero failure | |
| 609 | * @see CX_ARRAY_DECLARE() | |
| 610 | * @see cx_array_simple_add_unique() | |
| 611 | */ | |
| 612 | #define cx_array_simple_add_unique_a(reallocator, array, elem, cmp_func) \ | |
| 613 | cx_array_add_unique(&array, &(array##_size), &(array##_capacity), \ | |
| 614 | sizeof((array)[0]), &(elem), cmp_func, reallocator) | |
| 615 | ||
| 616 | /** | |
| 617 | * Convenience macro for cx_array_add_unique() with a default | |
| 618 | * layout and the default reallocator. | |
| 619 | * | |
| 620 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 621 | * @param elem the element to add (NOT a pointer, address is automatically taken) | |
| 622 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 623 | * @retval zero success | |
| 624 | * @retval non-zero failure | |
| 625 | * @see CX_ARRAY_DECLARE() | |
| 626 | * @see cx_array_simple_add_unique_a() | |
| 627 | */ | |
| 628 | #define cx_array_simple_add_unique(array, elem, cmp_func) \ | |
| 629 | cx_array_simple_add_unique_a(NULL, array, elem, cmp_func) | |
| 630 | ||
| 631 | /** | |
| 632 | * Convenience macro for cx_array_insert_unique() with a default | |
| 633 | * layout and the specified reallocator. | |
| 634 | * | |
| 635 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 636 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 637 | * @param src (@c void*) pointer to the source array | |
| 638 | * @param n (@c size_t) number of elements in the source array | |
| 639 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 640 | * @retval zero success | |
| 641 | * @retval non-zero failure | |
| 642 | * @see CX_ARRAY_DECLARE() | |
| 643 | * @see cx_array_simple_insert_unique() | |
| 644 | */ | |
| 645 | #define cx_array_simple_insert_unique_a(reallocator, array, src, n, cmp_func) \ | |
| 646 | cx_array_insert_unique((void**)(&array), &(array##_size), &(array##_capacity), \ | |
| 647 | cmp_func, src, sizeof((array)[0]), n, reallocator) | |
| 648 | ||
| 649 | /** | |
| 650 | * Convenience macro for cx_array_insert_unique() with a default | |
| 651 | * layout and the default reallocator. | |
| 652 | * | |
| 653 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 654 | * @param src (@c void*) pointer to the source array | |
| 655 | * @param n (@c size_t) number of elements in the source array | |
| 656 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 657 | * @retval zero success | |
| 658 | * @retval non-zero failure | |
| 659 | * @see CX_ARRAY_DECLARE() | |
| 660 | * @see cx_array_simple_insert_unique_a() | |
| 661 | */ | |
| 662 | #define cx_array_simple_insert_unique(array, src, n, cmp_func) \ | |
| 663 | cx_array_simple_insert_unique_a(NULL, array, src, n, cmp_func) | |
| 664 | ||
| 852 | 665 | /** |
| 666 | * Searches the largest lower bound in a sorted array. | |
| 667 | * | |
| 668 | * In other words, this function returns the index of the largest element | |
| 669 | * in @p arr that is less or equal to @p elem with respect to @p cmp_func. | |
| 670 | * When no such element exists, @p size is returned. | |
| 671 | * | |
| 672 | * If @p elem is contained in the array, this is identical to | |
| 673 | * #cx_array_binary_search(). | |
| 674 | * | |
| 675 | * If the array is not sorted with respect to the @p cmp_func, the behavior | |
| 676 | * is undefined. | |
| 677 | * | |
| 678 | * @param arr the array to search | |
| 679 | * @param size the size of the array | |
| 680 | * @param elem_size the size of one element | |
| 681 | * @param elem the element to find | |
| 682 | * @param cmp_func the compare function | |
| 683 | * @return the index of the largest lower bound, or @p size | |
| 684 | * @see cx_array_binary_search_sup() | |
| 685 | * @see cx_array_binary_search() | |
| 686 | */ | |
| 687 | cx_attr_nonnull | |
| 889 | 688 | CX_EXPORT size_t cx_array_binary_search_inf(const void *arr, size_t size, |
| 689 | size_t elem_size, const void *elem, cx_compare_func cmp_func); | |
| 852 | 690 | |
| 691 | /** | |
| 692 | * Searches an item in a sorted array. | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
693 | * |
| 852 | 694 | * If the array is not sorted with respect to the @p cmp_func, the behavior |
| 695 | * is undefined. | |
| 696 | * | |
| 697 | * @param arr the array to search | |
| 698 | * @param size the size of the array | |
| 699 | * @param elem_size the size of one element | |
| 700 | * @param elem the element to find | |
| 701 | * @param cmp_func the compare function | |
| 702 | * @return the index of the element in the array, or @p size if the element | |
| 703 | * cannot be found | |
| 704 | * @see cx_array_binary_search_inf() | |
| 705 | * @see cx_array_binary_search_sup() | |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
706 | */ |
| 852 | 707 | cx_attr_nonnull |
| 889 | 708 | CX_EXPORT size_t cx_array_binary_search(const void *arr, size_t size, |
| 709 | size_t elem_size, const void *elem, cx_compare_func cmp_func); | |
| 852 | 710 | |
| 711 | /** | |
| 712 | * Searches the smallest upper bound in a sorted array. | |
| 713 | * | |
| 714 | * In other words, this function returns the index of the smallest element | |
| 715 | * in @p arr that is greater or equal to @p elem with respect to @p cmp_func. | |
| 716 | * When no such element exists, @p size is returned. | |
| 717 | * | |
| 718 | * If @p elem is contained in the array, this is identical to | |
| 719 | * #cx_array_binary_search(). | |
| 720 | * | |
| 721 | * If the array is not sorted with respect to the @p cmp_func, the behavior | |
| 722 | * is undefined. | |
| 723 | * | |
| 724 | * @param arr the array to search | |
| 725 | * @param size the size of the array | |
| 726 | * @param elem_size the size of one element | |
| 727 | * @param elem the element to find | |
| 728 | * @param cmp_func the compare function | |
| 729 | * @return the index of the smallest upper bound, or @p size | |
| 730 | * @see cx_array_binary_search_inf() | |
| 731 | * @see cx_array_binary_search() | |
| 732 | */ | |
| 733 | cx_attr_nonnull | |
| 889 | 734 | CX_EXPORT size_t cx_array_binary_search_sup(const void *arr, size_t size, |
| 735 | size_t elem_size, const void *elem, cx_compare_func cmp_func); | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
736 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
737 | /** |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
738 | * Swaps two array elements. |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
739 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
740 | * @param arr the array |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
741 | * @param elem_size the element size |
| 889 | 742 | * @param idx1 index of the first element |
| 743 | * @param idx2 index of the second element | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
744 | */ |
| 852 | 745 | cx_attr_nonnull |
| 889 | 746 | CX_EXPORT void cx_array_swap(void *arr, size_t elem_size, size_t idx1, size_t idx2); |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
747 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
748 | /** |
| 852 | 749 | * Allocates an array list for storing elements with @p elem_size bytes each. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
750 | * |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
751 | * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of |
| 889 | 752 | * copies of the added elements, and the compare function will be automatically set |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
753 | * to cx_cmp_ptr(), if none is given. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
754 | * |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
755 | * @param allocator the allocator for allocating the list memory |
| 886 | 756 | * (if @c NULL, the cxDefaultAllocator will be used) |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
757 | * @param comparator the comparator for the elements |
| 852 | 758 | * (if @c NULL, and the list is not storing pointers, sort and find |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
759 | * functions will not work) |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
760 | * @param elem_size the size of each element in bytes |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
761 | * @param initial_capacity the initial number of elements the array can store |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
762 | * @return the created list |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
763 | */ |
| 852 | 764 | cx_attr_nodiscard |
| 765 | cx_attr_malloc | |
| 766 | cx_attr_dealloc(cxListFree, 1) | |
| 889 | 767 | CX_EXPORT CxList *cxArrayListCreate(const CxAllocator *allocator, |
| 768 | cx_compare_func comparator, size_t elem_size, size_t initial_capacity); | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
769 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
770 | /** |
| 852 | 771 | * Allocates an array list for storing elements with @p elem_size bytes each. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
772 | * |
| 852 | 773 | * The list will use the cxDefaultAllocator and @em NO compare function. |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
774 | * If you want to call functions that need a compare function, you have to |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
775 | * set it immediately after creation or use cxArrayListCreate(). |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
776 | * |
|
854
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
777 | * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of |
|
1c8401ece69e
update ucx to version 3.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
852
diff
changeset
|
778 | * copies of the added elements and the compare function will be automatically set |
| 886 | 779 | * to cx_cmp_ptr(). |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
780 | * |
| 852 | 781 | * @param elem_size (@c size_t) the size of each element in bytes |
| 782 | * @param initial_capacity (@c size_t) the initial number of elements the array can store | |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
783 | * @return the created list |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
784 | */ |
|
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
785 | #define cxArrayListCreateSimple(elem_size, initial_capacity) \ |
|
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
786 | cxArrayListCreate(NULL, NULL, elem_size, initial_capacity) |
|
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
787 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
788 | #ifdef __cplusplus |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
789 | } // extern "C" |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
790 | #endif |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
791 | |
|
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
792 | #endif // UCX_ARRAY_LIST_H |