src/cx/array_list.h

changeset 998
bb196054f3fd
parent 993
b642eca4b956
child 999
84fc42b04d3b
equal deleted inserted replaced
997:d14f3d5f47d1 998:bb196054f3fd
50 extern const unsigned cx_array_swap_sbo_size; 50 extern const unsigned cx_array_swap_sbo_size;
51 51
52 /** 52 /**
53 * Declares variables for an array that can be used with the convenience macros. 53 * Declares variables for an array that can be used with the convenience macros.
54 * 54 *
55 * @param type the type of the data
56 * @param name the name of the array
57 * @param size_type the type of the size (should be uint8_t, uint16_t, uint32_t, or size_t)
58 *
55 * @see cx_array_simple_add() 59 * @see cx_array_simple_add()
56 * @see cx_array_simple_copy() 60 * @see cx_array_simple_copy()
57 * @see cx_array_initialize() 61 * @see cx_array_initialize()
58 * @see cx_array_simple_add_sorted() 62 * @see cx_array_simple_add_sorted()
59 * @see cx_array_simple_insert_sorted() 63 * @see cx_array_simple_insert_sorted()
60 */ 64 */
61 #define CX_ARRAY_DECLARE(type, name) \ 65 #define CX_ARRAY_DECLARE_SIZED(type, name, size_type) \
62 type * name; \ 66 type * name; \
63 size_t name##_size; \ 67 size_type name##_size; \
64 size_t name##_capacity 68 size_type name##_capacity
69
70 /**
71 * Declares variables for an array that can be used with the convenience macros.
72 *
73 * The size and capacity variables will have `size_t` type.
74 * Use #CX_ARRAY_DECLARE_SIZED() to specify a different type.
75 *
76 * @param type the type of the data
77 * @param name the name of the array
78 *
79 * @see cx_array_simple_add()
80 * @see cx_array_simple_copy()
81 * @see cx_array_initialize()
82 * @see cx_array_simple_add_sorted()
83 * @see cx_array_simple_insert_sorted()
84 */
85 #define CX_ARRAY_DECLARE(type, name) CX_ARRAY_DECLARE_SIZED(type, name, size_t)
65 86
66 /** 87 /**
67 * Initializes an array declared with CX_ARRAY_DECLARE(). 88 * Initializes an array declared with CX_ARRAY_DECLARE().
68 * 89 *
69 * The memory for the array is allocated with stdlib malloc(). 90 * The memory for the array is allocated with stdlib malloc().
161 * would extend the array's size, the remaining \p capacity is used. 182 * would extend the array's size, the remaining \p capacity is used.
162 * 183 *
163 * If the \p capacity is also insufficient to hold the new data, a reallocation 184 * If the \p capacity is also insufficient to hold the new data, a reallocation
164 * attempt is made with the specified \p reallocator. 185 * attempt is made with the specified \p reallocator.
165 * 186 *
187 * The \p width refers to the size and capacity. Both must have the same width.
188 * Supported are 0, 8, 16, and 32, as well as 64 if running on a 64 bit
189 * architecture. If set to zero, the native word width is used.
190 *
166 * @param target a pointer to the target array 191 * @param target a pointer to the target array
167 * @param size a pointer to the size of the target array 192 * @param size a pointer to the size of the target array
168 * @param capacity a pointer to the capacity of the target array 193 * @param capacity a pointer to the capacity of the target array
194 * @param width the width in bytes for the \p size and \p capacity or zero for default
169 * @param index the index where the copied elements shall be placed 195 * @param index the index where the copied elements shall be placed
170 * @param src the source array 196 * @param src the source array
171 * @param elem_size the size of one element 197 * @param elem_size the size of one element
172 * @param elem_count the number of elements to copy 198 * @param elem_count the number of elements to copy
173 * @param reallocator the array reallocator to use 199 * @param reallocator the array reallocator to use
174 * @return zero on success, non-zero on failure 200 * @return zero on success, non-zero on failure
175 */ 201 */
176 cx_attr_nonnull 202 cx_attr_nonnull
177 int cx_array_copy( 203 int cx_array_copy(
178 void **target, 204 void **target,
179 size_t *size, 205 void *size,
180 size_t *capacity, 206 void *capacity,
207 unsigned width,
181 size_t index, 208 size_t index,
182 const void *src, 209 const void *src,
183 size_t elem_size, 210 size_t elem_size,
184 size_t elem_count, 211 size_t elem_count,
185 struct cx_array_reallocator_s *reallocator 212 struct cx_array_reallocator_s *reallocator
196 * @return zero on success, non-zero on failure 223 * @return zero on success, non-zero on failure
197 * @see CX_ARRAY_DECLARE() 224 * @see CX_ARRAY_DECLARE()
198 */ 225 */
199 #define cx_array_simple_copy(array, index, src, count) \ 226 #define cx_array_simple_copy(array, index, src, count) \
200 cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \ 227 cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \
201 index, src, sizeof((array)[0]), count, cx_array_default_reallocator) 228 8*sizeof(array##_size), index, src, sizeof((array)[0]), count, \
229 cx_array_default_reallocator)
202 230
203 /** 231 /**
204 * Adds an element to an array with the possibility of allocating more space. 232 * Adds an element to an array with the possibility of allocating more space.
205 * 233 *
206 * The element \p elem is added to the end of the \p target array which contains 234 * The element \p elem is added to the end of the \p target array which contains
217 * @param elem a pointer to the element to add 245 * @param elem a pointer to the element to add
218 * @param reallocator the array reallocator to use 246 * @param reallocator the array reallocator to use
219 * @return zero on success, non-zero on failure 247 * @return zero on success, non-zero on failure
220 */ 248 */
221 #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \ 249 #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \
222 cx_array_copy((void**)(target), size, capacity, *(size), elem, elem_size, 1, reallocator) 250 cx_array_copy((void**)(target), size, capacity, 8*sizeof(*(size)), \
251 *(size), elem, elem_size, 1, reallocator)
223 252
224 /** 253 /**
225 * Convenience macro that uses cx_array_add() with a default layout and 254 * Convenience macro that uses cx_array_add() with a default layout and
226 * the default reallocator. 255 * the default reallocator.
227 * 256 *

mercurial