Mon, 06 Jan 2025 21:18:36 +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 | /** |
852 | 47 | * The maximum item size in an array list that fits into stack buffer |
48 | * 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 | */ |
852 | 50 | extern const unsigned cx_array_swap_sbo_size; |
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 | * |
852 | 86 | * The size and capacity variables will have @c size_t type. |
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 | * |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
125 | * The memory for the array is allocated with stdlib malloc(). |
852 | 126 | * @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
|
127 | * @param capacity the initial capacity |
852 | 128 | * @see cx_array_initialize_a() |
129 | * @see CX_ARRAY_DECLARE_SIZED() | |
130 | * @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
|
131 | */ |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
132 | #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
|
133 | array##_capacity = capacity; \ |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
134 | array##_size = 0; \ |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
135 | array = malloc(sizeof(array[0]) * capacity) |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
136 | |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
137 | /** |
852 | 138 | * Initializes an array with the given capacity using the specified allocator. |
139 | * | |
140 | * @par Example | |
141 | * @code | |
142 | * CX_ARRAY_DECLARE(int, myarray) | |
143 | * | |
144 | * | |
145 | * const CxAllocator *al = // ... | |
146 | * cx_array_initialize_a(al, myarray, 128); | |
147 | * // ... | |
148 | * cxFree(al, myarray); // don't forget to free with same allocator | |
149 | * @endcode | |
150 | * | |
151 | * The memory for the array is allocated with stdlib malloc(). | |
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. |
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
174 | * This allows reallocation of static memory by allocating heap memory |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
175 | * and copying the array contents. The information in the custom fields of |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
176 | * the referenced allocator can be used to track the state of the memory |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
177 | * or to transport other additional data. |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178 | * |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
179 | * @param array the array to reallocate |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
180 | * @param capacity the new capacity (number of elements) |
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 | */ |
852 | 185 | cx_attr_nodiscard |
186 | cx_attr_nonnull_arg(4) | |
187 | cx_attr_allocsize(2, 3) | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188 | void *(*realloc)( |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
189 | void *array, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190 | size_t capacity, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
191 | size_t elem_size, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
192 | struct cx_array_reallocator_s *alloc |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
193 | ); |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
194 | |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
195 | /** |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
196 | * Custom data pointer. |
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 | void *ptr1; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
199 | /** |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
200 | * Custom data pointer. |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
201 | */ |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
202 | void *ptr2; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
203 | /** |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
204 | * Custom data integer. |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
205 | */ |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
206 | size_t int1; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
207 | /** |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
208 | * Custom data integer. |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
209 | */ |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
210 | size_t int2; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
211 | }; |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
212 | |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
213 | /** |
852 | 214 | * Typedef for the array reallocator struct. |
215 | */ | |
216 | typedef struct cx_array_reallocator_s CxArrayReallocator; | |
217 | ||
218 | /** | |
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
219 | * A default stdlib-based 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
|
220 | */ |
852 | 221 | extern CxArrayReallocator *cx_array_default_reallocator; |
222 | ||
223 | /** | |
224 | * Creates a new array reallocator. | |
225 | * | |
226 | * When @p allocator is @c NULL, the stdlib default allocator will be used. | |
227 | * | |
228 | * When @p stackmem is not @c NULL, the reallocator is supposed to be used | |
229 | * @em only for the specific array that is initially located at @p stackmem. | |
230 | * When reallocation is needed, the reallocator checks, if the array is | |
231 | * still located at @p stackmem and copies the contents to the heap. | |
232 | * | |
233 | * @note Invoking this function with both arguments @c NULL will return a | |
234 | * reallocator that behaves like #cx_array_default_reallocator. | |
235 | * | |
236 | * @param allocator the allocator this reallocator shall be based on | |
237 | * @param stackmem the address of the array when the array is initially located | |
238 | * on the stack or shall not reallocated in place | |
239 | * @return an array reallocator | |
240 | */ | |
241 | CxArrayReallocator cx_array_reallocator( | |
242 | const struct cx_allocator_s *allocator, | |
243 | const void *stackmem | |
244 | ); | |
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
245 | |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
246 | /** |
852 | 247 | * Reserves memory for additional elements. |
248 | * | |
249 | * This function checks if the @p capacity of the array is sufficient to hold | |
250 | * at least @p size plus @p elem_count elements. If not, a reallocation is | |
251 | * performed with the specified @p reallocator. | |
252 | * You can create your own reallocator by hand, use #cx_array_default_reallocator, | |
253 | * or use the convenience function cx_array_reallocator() to create a custom reallocator. | |
254 | * | |
255 | * This function can be useful to replace subsequent calls to cx_array_copy() | |
256 | * with one single cx_array_reserve() and then - after guaranteeing a | |
257 | * sufficient capacity - use simple memmove() or memcpy(). | |
258 | * | |
259 | * The @p width in bytes refers to the size and capacity. | |
260 | * Both must have the same width. | |
261 | * Supported are 0, 1, 2, and 4, as well as 8 if running on a 64 bit | |
262 | * architecture. If set to zero, the native word width is used. | |
263 | * | |
264 | * @param array a pointer to the target array | |
265 | * @param size a pointer to the size of the array | |
266 | * @param capacity a pointer to the capacity of the array | |
267 | * @param width the width in bytes for the @p size and @p capacity or zero for default | |
268 | * @param elem_size the size of one element | |
269 | * @param elem_count the number of expected additional elements | |
270 | * @param reallocator the array reallocator to use | |
271 | * (@c NULL defaults to #cx_array_default_reallocator) | |
272 | * @retval zero success | |
273 | * @retval non-zero failure | |
274 | * @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
|
275 | */ |
852 | 276 | cx_attr_nonnull_arg(1, 2, 3) |
277 | int cx_array_reserve( | |
278 | void **array, | |
279 | void *size, | |
280 | void *capacity, | |
281 | unsigned width, | |
282 | size_t elem_size, | |
283 | size_t elem_count, | |
284 | CxArrayReallocator *reallocator | |
285 | ); | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
286 | |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
287 | /** |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
288 | * 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
|
289 | * |
852 | 290 | * The elements are copied to the @p target array at the specified @p index, |
291 | * overwriting possible elements. The @p index does not need to be in range of | |
292 | * the current array @p size. If the new index plus the number of elements added | |
293 | * would extend 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
|
294 | * |
852 | 295 | * If the @p capacity is also insufficient to hold the new data, a reallocation |
296 | * attempt is made with the specified @p reallocator. | |
297 | * You can create your own reallocator by hand, use #cx_array_default_reallocator, | |
298 | * or use the convenience function cx_array_reallocator() to create a custom reallocator. | |
299 | * | |
300 | * The @p width in bytes refers to the size and capacity. | |
301 | * Both must have the same width. | |
302 | * Supported are 0, 1, 2, and 4, as well as 8 if running on a 64 bit | |
303 | * 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
|
304 | * |
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
305 | * @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
|
306 | * @param size a pointer to the size of the target array |
852 | 307 | * @param capacity a pointer to the capacity of the target array |
308 | * @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
|
309 | * @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
|
310 | * @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
|
311 | * @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
|
312 | * @param elem_count the number of elements to copy |
852 | 313 | * @param reallocator the array reallocator to use |
314 | * (@c NULL defaults to #cx_array_default_reallocator) | |
315 | * @retval zero success | |
316 | * @retval non-zero failure | |
317 | * @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
|
318 | */ |
852 | 319 | cx_attr_nonnull_arg(1, 2, 3, 6) |
320 | int cx_array_copy( | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
321 | void **target, |
852 | 322 | void *size, |
323 | void *capacity, | |
324 | unsigned width, | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
325 | size_t index, |
852 | 326 | const void *src, |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
327 | size_t elem_size, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
328 | size_t elem_count, |
852 | 329 | CxArrayReallocator *reallocator |
330 | ); | |
331 | ||
332 | /** | |
333 | * Convenience macro that uses cx_array_copy() with a default layout and | |
334 | * the specified reallocator. | |
335 | * | |
336 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
337 | * @param array the name of the array (NOT a pointer or alias to the array) | |
338 | * @param index (@c size_t) the index where the copied elements shall be placed | |
339 | * @param src (@c void*) the source array | |
340 | * @param count (@c size_t) the number of elements to copy | |
341 | * @retval zero success | |
342 | * @retval non-zero failure | |
343 | * @see CX_ARRAY_DECLARE() | |
344 | * @see cx_array_simple_copy() | |
345 | */ | |
346 | #define cx_array_simple_copy_a(reallocator, array, index, src, count) \ | |
347 | cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \ | |
348 | sizeof(array##_size), index, src, sizeof((array)[0]), count, \ | |
349 | reallocator) | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
350 | |
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
351 | /** |
852 | 352 | * Convenience macro that uses cx_array_copy() with a default layout and |
353 | * 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
|
354 | * |
852 | 355 | * @param array the name of the array (NOT a pointer or alias to the array) |
356 | * @param index (@c size_t) the index where the copied elements shall be placed | |
357 | * @param src (@c void*) the source array | |
358 | * @param count (@c size_t) the number of elements to copy | |
359 | * @retval zero success | |
360 | * @retval non-zero failure | |
361 | * @see CX_ARRAY_DECLARE() | |
362 | * @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
|
363 | */ |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
364 | #define cx_array_simple_copy(array, index, src, count) \ |
852 | 365 | cx_array_simple_copy_a(NULL, array, index, src, count) |
366 | ||
367 | /** | |
368 | * Convenience macro that uses cx_array_reserve() with a default layout and | |
369 | * the specified reallocator. | |
370 | * | |
371 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
372 | * @param array the name of the array (NOT a pointer or alias to the array) | |
373 | * @param count (@c size_t) the number of expected @em additional elements | |
374 | * @retval zero success | |
375 | * @retval non-zero failure | |
376 | * @see CX_ARRAY_DECLARE() | |
377 | * @see cx_array_simple_reserve() | |
378 | */ | |
379 | #define cx_array_simple_reserve_a(reallocator, array, count) \ | |
380 | cx_array_reserve((void**)&(array), &(array##_size), &(array##_capacity), \ | |
381 | sizeof(array##_size), sizeof((array)[0]), count, \ | |
382 | reallocator) | |
383 | ||
384 | /** | |
385 | * Convenience macro that uses cx_array_reserve() with a default layout and | |
386 | * the default reallocator. | |
387 | * | |
388 | * @param array the name of the array (NOT a pointer or alias to the array) | |
389 | * @param count (@c size_t) the number of expected additional elements | |
390 | * @retval zero success | |
391 | * @retval non-zero failure | |
392 | * @see CX_ARRAY_DECLARE() | |
393 | * @see cx_array_simple_reserve_a() | |
394 | */ | |
395 | #define cx_array_simple_reserve(array, count) \ | |
396 | 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
|
397 | |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
398 | /** |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
399 | * 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
|
400 | * |
852 | 401 | * The element @p elem is added to the end of the @p target array which contains |
402 | * @p size elements, already. The @p capacity must point to a variable denoting | |
403 | * the current maximum number of elements the array can hold. | |
404 | * | |
405 | * If the capacity is insufficient to hold the new element, an attempt to | |
406 | * increase the @p capacity is made and the new capacity is written back. | |
407 | * | |
408 | * The \@ SIZE_TYPE is flexible and can be any unsigned integer type. | |
409 | * It is important, however, that @p size and @p capacity are pointers to | |
410 | * variables of the same type. | |
411 | * | |
412 | * @param target (@c void**) a pointer to the target array | |
413 | * @param size (@c SIZE_TYPE*) a pointer to the size of the target array | |
414 | * @param capacity (@c SIZE_TYPE*) a pointer to the capacity of the target array | |
415 | * @param elem_size (@c size_t) the size of one element | |
416 | * @param elem (@c void*) a pointer to the element to add | |
417 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
418 | * @retval zero success | |
419 | * @retval non-zero failure | |
420 | */ | |
421 | #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \ | |
422 | cx_array_copy((void**)(target), size, capacity, sizeof(*(size)), \ | |
423 | *(size), elem, elem_size, 1, reallocator) | |
424 | ||
425 | /** | |
426 | * Convenience macro that uses cx_array_add() with a default layout and | |
427 | * 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
|
428 | * |
852 | 429 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use |
430 | * @param array the name of the array (NOT a pointer or alias to the array) | |
431 | * @param elem the element to add (NOT a pointer, address is automatically taken) | |
432 | * @retval zero success | |
433 | * @retval non-zero failure | |
434 | * @see CX_ARRAY_DECLARE() | |
435 | * @see cx_array_simple_add() | |
436 | */ | |
437 | #define cx_array_simple_add_a(reallocator, array, elem) \ | |
438 | cx_array_simple_copy_a(reallocator, array, array##_size, &(elem), 1) | |
439 | ||
440 | /** | |
441 | * Convenience macro that uses cx_array_add() with a default layout and | |
442 | * the default reallocator. | |
443 | * | |
444 | * @param array the name of the array (NOT a pointer or alias to the array) | |
445 | * @param elem the element to add (NOT a pointer, address is automatically taken) | |
446 | * @retval zero success | |
447 | * @retval non-zero failure | |
448 | * @see CX_ARRAY_DECLARE() | |
449 | * @see cx_array_simple_add_a() | |
450 | */ | |
451 | #define cx_array_simple_add(array, elem) \ | |
452 | cx_array_simple_add_a(cx_array_default_reallocator, array, elem) | |
453 | ||
454 | /** | |
455 | * Inserts a sorted array into another sorted array. | |
456 | * | |
457 | * If either the target or the source array is not already sorted with respect | |
458 | * to the specified @p cmp_func, the behavior is undefined. | |
459 | * | |
460 | * If the capacity is insufficient to hold the new data, a reallocation | |
461 | * attempt is made. | |
462 | * You can create your own reallocator by hand, use #cx_array_default_reallocator, | |
463 | * 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
|
464 | * |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
465 | * @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
|
466 | * @param size a pointer to the size of the target array |
852 | 467 | * @param capacity a pointer to the capacity of the target array |
468 | * @param cmp_func the compare function for the elements | |
469 | * @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
|
470 | * @param elem_size the size of one element |
852 | 471 | * @param elem_count the number of elements to insert |
472 | * @param reallocator the array reallocator to use | |
473 | * (@c NULL defaults to #cx_array_default_reallocator) | |
474 | * @retval zero success | |
475 | * @retval non-zero failure | |
476 | */ | |
477 | cx_attr_nonnull_arg(1, 2, 3, 5) | |
478 | int cx_array_insert_sorted( | |
479 | void **target, | |
480 | size_t *size, | |
481 | size_t *capacity, | |
482 | cx_compare_func cmp_func, | |
483 | const void *src, | |
484 | size_t elem_size, | |
485 | size_t elem_count, | |
486 | CxArrayReallocator *reallocator | |
487 | ); | |
488 | ||
489 | /** | |
490 | * Inserts an element into a sorted array. | |
491 | * | |
492 | * If the target array is not already sorted with respect | |
493 | * to the specified @p cmp_func, the behavior is undefined. | |
494 | * | |
495 | * If the capacity is insufficient to hold the new data, a reallocation | |
496 | * attempt is made. | |
497 | * | |
498 | * The \@ SIZE_TYPE is flexible and can be any unsigned integer type. | |
499 | * It is important, however, that @p size and @p capacity are pointers to | |
500 | * variables of the same type. | |
501 | * | |
502 | * @param target (@c void**) a pointer to the target array | |
503 | * @param size (@c SIZE_TYPE*) a pointer to the size of the target array | |
504 | * @param capacity (@c SIZE_TYPE*) a pointer to the capacity of the target array | |
505 | * @param elem_size (@c size_t) the size of one element | |
506 | * @param elem (@c void*) a pointer to the element to add | |
507 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
508 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
509 | * @retval zero success | |
510 | * @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
|
511 | */ |
852 | 512 | #define cx_array_add_sorted(target, size, capacity, elem_size, elem, cmp_func, reallocator) \ |
513 | cx_array_insert_sorted((void**)(target), size, capacity, cmp_func, elem, elem_size, 1, reallocator) | |
514 | ||
515 | /** | |
516 | * Convenience macro for cx_array_add_sorted() with a default | |
517 | * layout and the specified reallocator. | |
518 | * | |
519 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
520 | * @param array the name of the array (NOT a pointer or alias to the array) | |
521 | * @param elem the element to add (NOT a pointer, address is automatically taken) | |
522 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
523 | * @retval zero success | |
524 | * @retval non-zero failure | |
525 | * @see CX_ARRAY_DECLARE() | |
526 | * @see cx_array_simple_add_sorted() | |
527 | */ | |
528 | #define cx_array_simple_add_sorted_a(reallocator, array, elem, cmp_func) \ | |
529 | cx_array_add_sorted(&array, &(array##_size), &(array##_capacity), \ | |
530 | sizeof((array)[0]), &(elem), cmp_func, reallocator) | |
531 | ||
532 | /** | |
533 | * Convenience macro for cx_array_add_sorted() with a default | |
534 | * layout and the default reallocator. | |
535 | * | |
536 | * @param array the name of the array (NOT a pointer or alias to the array) | |
537 | * @param elem the element to add (NOT a pointer, address is automatically taken) | |
538 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
539 | * @retval zero success | |
540 | * @retval non-zero failure | |
541 | * @see CX_ARRAY_DECLARE() | |
542 | * @see cx_array_simple_add_sorted_a() | |
543 | */ | |
544 | #define cx_array_simple_add_sorted(array, elem, cmp_func) \ | |
545 | cx_array_simple_add_sorted_a(NULL, array, elem, cmp_func) | |
546 | ||
547 | /** | |
548 | * Convenience macro for cx_array_insert_sorted() with a default | |
549 | * layout and the specified reallocator. | |
550 | * | |
551 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
552 | * @param array the name of the array (NOT a pointer or alias to the array) | |
553 | * @param src (@c void*) pointer to the source array | |
554 | * @param n (@c size_t) number of elements in the source array | |
555 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
556 | * @retval zero success | |
557 | * @retval non-zero failure | |
558 | * @see CX_ARRAY_DECLARE() | |
559 | * @see cx_array_simple_insert_sorted() | |
560 | */ | |
561 | #define cx_array_simple_insert_sorted_a(reallocator, array, src, n, cmp_func) \ | |
562 | cx_array_insert_sorted((void**)(&array), &(array##_size), &(array##_capacity), \ | |
563 | 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
|
564 | |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
565 | /** |
852 | 566 | * Convenience macro for cx_array_insert_sorted() with a default |
567 | * layout and the default reallocator. | |
568 | * | |
569 | * @param array the name of the array (NOT a pointer or alias to the array) | |
570 | * @param src (@c void*) pointer to the source array | |
571 | * @param n (@c size_t) number of elements in the source array | |
572 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
573 | * @retval zero success | |
574 | * @retval non-zero failure | |
575 | * @see CX_ARRAY_DECLARE() | |
576 | * @see cx_array_simple_insert_sorted_a() | |
577 | */ | |
578 | #define cx_array_simple_insert_sorted(array, src, n, cmp_func) \ | |
579 | cx_array_simple_insert_sorted_a(NULL, array, src, n, cmp_func) | |
580 | ||
581 | /** | |
582 | * Searches the largest lower bound in a sorted array. | |
583 | * | |
584 | * In other words, this function returns the index of the largest element | |
585 | * in @p arr that is less or equal to @p elem with respect to @p cmp_func. | |
586 | * When no such element exists, @p size is returned. | |
587 | * | |
588 | * If @p elem is contained in the array, this is identical to | |
589 | * #cx_array_binary_search(). | |
590 | * | |
591 | * If the array is not sorted with respect to the @p cmp_func, the behavior | |
592 | * is undefined. | |
593 | * | |
594 | * @param arr the array to search | |
595 | * @param size the size of the array | |
596 | * @param elem_size the size of one element | |
597 | * @param elem the element to find | |
598 | * @param cmp_func the compare function | |
599 | * @return the index of the largest lower bound, or @p size | |
600 | * @see cx_array_binary_search_sup() | |
601 | * @see cx_array_binary_search() | |
602 | */ | |
603 | cx_attr_nonnull | |
604 | size_t cx_array_binary_search_inf( | |
605 | const void *arr, | |
606 | size_t size, | |
607 | size_t elem_size, | |
608 | const void *elem, | |
609 | cx_compare_func cmp_func | |
610 | ); | |
611 | ||
612 | /** | |
613 | * 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
|
614 | * |
852 | 615 | * If the array is not sorted with respect to the @p cmp_func, the behavior |
616 | * is undefined. | |
617 | * | |
618 | * @param arr the array to search | |
619 | * @param size the size of the array | |
620 | * @param elem_size the size of one element | |
621 | * @param elem the element to find | |
622 | * @param cmp_func the compare function | |
623 | * @return the index of the element in the array, or @p size if the element | |
624 | * cannot be found | |
625 | * @see cx_array_binary_search_inf() | |
626 | * @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
|
627 | */ |
852 | 628 | cx_attr_nonnull |
629 | size_t cx_array_binary_search( | |
630 | const void *arr, | |
631 | size_t size, | |
632 | size_t elem_size, | |
633 | const void *elem, | |
634 | cx_compare_func cmp_func | |
635 | ); | |
636 | ||
637 | /** | |
638 | * Searches the smallest upper bound in a sorted array. | |
639 | * | |
640 | * In other words, this function returns the index of the smallest element | |
641 | * in @p arr that is greater or equal to @p elem with respect to @p cmp_func. | |
642 | * When no such element exists, @p size is returned. | |
643 | * | |
644 | * If @p elem is contained in the array, this is identical to | |
645 | * #cx_array_binary_search(). | |
646 | * | |
647 | * If the array is not sorted with respect to the @p cmp_func, the behavior | |
648 | * is undefined. | |
649 | * | |
650 | * @param arr the array to search | |
651 | * @param size the size of the array | |
652 | * @param elem_size the size of one element | |
653 | * @param elem the element to find | |
654 | * @param cmp_func the compare function | |
655 | * @return the index of the smallest upper bound, or @p size | |
656 | * @see cx_array_binary_search_inf() | |
657 | * @see cx_array_binary_search() | |
658 | */ | |
659 | cx_attr_nonnull | |
660 | size_t cx_array_binary_search_sup( | |
661 | const void *arr, | |
662 | size_t size, | |
663 | size_t elem_size, | |
664 | const void *elem, | |
665 | cx_compare_func cmp_func | |
666 | ); | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
667 | |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
668 | /** |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
669 | * 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
|
670 | * |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
671 | * @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
|
672 | * @param elem_size the element size |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
673 | * @param idx1 index of first element |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
674 | * @param idx2 index of second element |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
675 | */ |
852 | 676 | cx_attr_nonnull |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
677 | void cx_array_swap( |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
678 | void *arr, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
679 | size_t elem_size, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
680 | size_t idx1, |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
681 | size_t idx2 |
852 | 682 | ); |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
683 | |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
684 | /** |
852 | 685 | * 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
|
686 | * |
852 | 687 | * If @p elem_size is CX_STORE_POINTERS, the created list will be created as if |
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
688 | * cxListStorePointers() was called immediately after creation and the compare |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
689 | * function will be automatically set 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
|
690 | * |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
691 | * @param allocator the allocator for allocating the list memory |
852 | 692 | * (if @c NULL, a default stdlib allocator 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
|
693 | * @param comparator the comparator for the elements |
852 | 694 | * (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
|
695 | * 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
|
696 | * @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
|
697 | * @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
|
698 | * @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
|
699 | */ |
852 | 700 | cx_attr_nodiscard |
701 | cx_attr_malloc | |
702 | cx_attr_dealloc(cxListFree, 1) | |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
703 | CxList *cxArrayListCreate( |
852 | 704 | 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
|
705 | cx_compare_func comparator, |
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
706 | size_t elem_size, |
747
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
707 | size_t initial_capacity |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
708 | ); |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
709 | |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
710 | /** |
852 | 711 | * 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
|
712 | * |
852 | 713 | * 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
|
714 | * 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
|
715 | * 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
|
716 | * |
852 | 717 | * If @p elem_size is CX_STORE_POINTERS, the created list will be created as if |
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
718 | * cxListStorePointers() was called immediately after creation and the compare |
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
719 | * function will be automatically set 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
|
720 | * |
852 | 721 | * @param elem_size (@c size_t) the size of each element in bytes |
722 | * @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
|
723 | * @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
|
724 | */ |
816
839fefbdedc7
compatibility with UCX 3.1 plus several minor code fixes
Mike Becker <universe@uap-core.de>
parents:
747
diff
changeset
|
725 | #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
|
726 | 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
|
727 | |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
728 | #ifdef __cplusplus |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
729 | } // extern "C" |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
730 | #endif |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
731 | |
efbd59642577
ucx 3 update, basic dav commands work, most stuff is still broken
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
732 | #endif // UCX_ARRAY_LIST_H |