ucx/cx/array_list.h

changeset 870
e167cf006213
parent 845
f3ab28ed22e5
child 943
9b5948aa5b90
equal deleted inserted replaced
869:6b7a178cff7c 870:e167cf006213
45 45
46 /** 46 /**
47 * The maximum item size in an array list that fits into 47 * The maximum item size in an array list that fits into
48 * a stack buffer when swapped. 48 * a stack buffer when swapped.
49 */ 49 */
50 cx_attr_export 50 CX_EXPORT extern const unsigned cx_array_swap_sbo_size;
51 extern const unsigned cx_array_swap_sbo_size;
52 51
53 /** 52 /**
54 * 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.
55 * 54 *
56 * @par Examples 55 * @par Examples
170 struct cx_array_reallocator_s { 169 struct cx_array_reallocator_s {
171 /** 170 /**
172 * Reallocates space for the given array. 171 * Reallocates space for the given array.
173 * 172 *
174 * Implementations are not required to free the original array. 173 * Implementations are not required to free the original array.
175 * This allows reallocation of static memory by allocating heap memory 174 * This allows reallocation of static or stack memory by allocating heap memory
176 * and copying the array contents. The information in the custom fields of 175 * and copying the array contents; namely when @c stack_ptr in this struct
177 * the referenced allocator can be used to track the state of the memory 176 * is not @c NULL and @p array equals @c stack_ptr.
178 * or to transport other additional data.
179 * 177 *
180 * @param array the array to reallocate 178 * @param array the array to reallocate
181 * @param old_capacity the old number of elements 179 * @param old_capacity the old number of elements
182 * @param new_capacity the new number of elements 180 * @param new_capacity the new number of elements
183 * @param elem_size the size of each element 181 * @param elem_size the size of each element
184 * @param alloc a reference to this allocator 182 * @param alloc a reference to this allocator
185 * @return a pointer to the reallocated memory or @c NULL on failure 183 * @return a pointer to the reallocated memory or @c NULL on failure
186 */ 184 */
187 cx_attr_nodiscard 185 void *(*realloc)( void *array, size_t old_capacity, size_t new_capacity,
188 cx_attr_nonnull_arg(5) 186 size_t elem_size, struct cx_array_reallocator_s *alloc);
189 cx_attr_allocsize(3, 4)
190 void *(*realloc)(
191 void *array,
192 size_t old_capacity,
193 size_t new_capacity,
194 size_t elem_size,
195 struct cx_array_reallocator_s *alloc
196 );
197 187
198 /** 188 /**
199 * Custom data pointer. 189 * The allocator that shall be used for the reallocations.
200 */ 190 */
201 void *ptr1; 191 const CxAllocator *allocator;
202 /** 192 /**
203 * Custom data pointer. 193 * Optional pointer to stack memory
194 * if the array is originally located on the stack.
204 */ 195 */
205 void *ptr2; 196 const void *stack_ptr;
206 /**
207 * Custom data integer.
208 */
209 size_t int1;
210 /**
211 * Custom data integer.
212 */
213 size_t int2;
214 }; 197 };
215 198
216 /** 199 /**
217 * Typedef for the array reallocator struct. 200 * Typedef for the array reallocator struct.
218 */ 201 */
219 typedef struct cx_array_reallocator_s CxArrayReallocator; 202 typedef struct cx_array_reallocator_s CxArrayReallocator;
220 203
221 /** 204 /**
222 * A default array reallocator that is based on the cxDefaultAllocator. 205 * A default array reallocator that is based on the cxDefaultAllocator.
223 */ 206 */
224 cx_attr_export 207 CX_EXPORT extern CxArrayReallocator *cx_array_default_reallocator;
225 extern CxArrayReallocator *cx_array_default_reallocator;
226 208
227 /** 209 /**
228 * Creates a new array reallocator. 210 * Creates a new array reallocator.
229 * 211 *
230 * When @p allocator is @c NULL, the cxDefaultAllocator will be used. 212 * When @p allocator is @c NULL, the cxDefaultAllocator will be used.
231 * 213 *
232 * When @p stackmem is not @c NULL, the reallocator is supposed to be used 214 * When @p stack_ptr is not @c NULL, the reallocator is supposed to be used
233 * @em only for the specific array initially located at @p stackmem. 215 * @em only for the specific array initially located at @p stack_ptr.
234 * When reallocation is needed, the reallocator checks if the array is 216 * When reallocation is needed, the reallocator checks if the array is
235 * still located at @p stackmem and copies the contents to the heap. 217 * still located at @p stack_ptr and copies the contents to the heap.
236 * 218 *
237 * @note Invoking this function with both arguments being @c NULL will return a 219 * @note Invoking this function with both arguments being @c NULL will return a
238 * reallocator that behaves like #cx_array_default_reallocator. 220 * reallocator that behaves like #cx_array_default_reallocator.
239 * 221 *
240 * @param allocator the allocator this reallocator shall be based on 222 * @param allocator the allocator this reallocator shall be based on
241 * @param stackmem the address of the array when the array is initially located 223 * @param stack_ptr the address of the array when the array is initially located
242 * on the stack or shall not reallocate in place 224 * on the stack or shall not reallocate in place
243 * @return an array reallocator 225 * @return an array reallocator
244 */ 226 */
245 cx_attr_export 227 CX_EXPORT CxArrayReallocator cx_array_reallocator(
246 CxArrayReallocator cx_array_reallocator( 228 const struct cx_allocator_s *allocator, const void *stack_ptr);
247 const struct cx_allocator_s *allocator,
248 const void *stackmem
249 );
250 229
251 /** 230 /**
252 * Reserves memory for additional elements. 231 * Reserves memory for additional elements.
253 * 232 *
254 * This function checks if the @p capacity of the array is sufficient to hold 233 * This function checks if the @p capacity of the array is sufficient to hold
277 * @retval zero success 256 * @retval zero success
278 * @retval non-zero failure 257 * @retval non-zero failure
279 * @see cx_array_reallocator() 258 * @see cx_array_reallocator()
280 */ 259 */
281 cx_attr_nonnull_arg(1, 2, 3) 260 cx_attr_nonnull_arg(1, 2, 3)
282 cx_attr_export 261 CX_EXPORT int cx_array_reserve(void **array, void *size, void *capacity,
283 int cx_array_reserve( 262 unsigned width, size_t elem_size, size_t elem_count,
284 void **array, 263 CxArrayReallocator *reallocator);
285 void *size,
286 void *capacity,
287 unsigned width,
288 size_t elem_size,
289 size_t elem_count,
290 CxArrayReallocator *reallocator
291 );
292 264
293 /** 265 /**
294 * Copies elements from one array to another. 266 * Copies elements from one array to another.
295 * 267 *
296 * The elements are copied to the @p target array at the specified @p index, 268 * The elements are copied to the @p target array at the specified @p index,
321 * @retval zero success 293 * @retval zero success
322 * @retval non-zero failure 294 * @retval non-zero failure
323 * @see cx_array_reallocator() 295 * @see cx_array_reallocator()
324 */ 296 */
325 cx_attr_nonnull_arg(1, 2, 3, 6) 297 cx_attr_nonnull_arg(1, 2, 3, 6)
326 cx_attr_export 298 CX_EXPORT int cx_array_copy(void **target, void *size, void *capacity, unsigned width,
327 int cx_array_copy( 299 size_t index, const void *src, size_t elem_size, size_t elem_count,
328 void **target, 300 CxArrayReallocator *reallocator);
329 void *size,
330 void *capacity,
331 unsigned width,
332 size_t index,
333 const void *src,
334 size_t elem_size,
335 size_t elem_count,
336 CxArrayReallocator *reallocator
337 );
338 301
339 /** 302 /**
340 * Convenience macro that uses cx_array_copy() with a default layout and 303 * Convenience macro that uses cx_array_copy() with a default layout and
341 * the specified reallocator. 304 * the specified reallocator.
342 * 305 *
480 * (@c NULL defaults to #cx_array_default_reallocator) 443 * (@c NULL defaults to #cx_array_default_reallocator)
481 * @retval zero success 444 * @retval zero success
482 * @retval non-zero failure 445 * @retval non-zero failure
483 */ 446 */
484 cx_attr_nonnull_arg(1, 2, 3, 5) 447 cx_attr_nonnull_arg(1, 2, 3, 5)
485 cx_attr_export 448 CX_EXPORT int cx_array_insert_sorted(void **target, size_t *size, size_t *capacity,
486 int cx_array_insert_sorted( 449 cx_compare_func cmp_func, const void *src, size_t elem_size, size_t elem_count,
487 void **target, 450 CxArrayReallocator *reallocator);
488 size_t *size,
489 size_t *capacity,
490 cx_compare_func cmp_func,
491 const void *src,
492 size_t elem_size,
493 size_t elem_count,
494 CxArrayReallocator *reallocator
495 );
496 451
497 /** 452 /**
498 * Inserts an element into a sorted array. 453 * Inserts an element into a sorted array.
499 * 454 *
500 * If the target array is not already sorted with respect 455 * If the target array is not already sorted with respect
501 * to the specified @p cmp_func, the behavior is undefined. 456 * to the specified @p cmp_func, the behavior is undefined.
502 * 457 *
503 * If the capacity is insufficient to hold the new data, a reallocation 458 * If the capacity is not enough to hold the new data, a reallocation
504 * attempt is made. 459 * attempt is made.
505 * 460 *
506 * The \@ SIZE_TYPE is flexible and can be any unsigned integer type. 461 * The \@ SIZE_TYPE is flexible and can be any unsigned integer type.
507 * It is important, however, that @p size and @p capacity are pointers to 462 * It is important, however, that @p size and @p capacity are pointers to
508 * variables of the same type. 463 * variables of the same type.
609 * (@c NULL defaults to #cx_array_default_reallocator) 564 * (@c NULL defaults to #cx_array_default_reallocator)
610 * @retval zero success 565 * @retval zero success
611 * @retval non-zero failure 566 * @retval non-zero failure
612 */ 567 */
613 cx_attr_nonnull_arg(1, 2, 3, 5) 568 cx_attr_nonnull_arg(1, 2, 3, 5)
614 cx_attr_export 569 CX_EXPORT int cx_array_insert_unique(void **target, size_t *size, size_t *capacity,
615 int cx_array_insert_unique( 570 cx_compare_func cmp_func, const void *src, size_t elem_size, size_t elem_count,
616 void **target, 571 CxArrayReallocator *reallocator);
617 size_t *size,
618 size_t *capacity,
619 cx_compare_func cmp_func,
620 const void *src,
621 size_t elem_size,
622 size_t elem_count,
623 CxArrayReallocator *reallocator
624 );
625 572
626 /** 573 /**
627 * Inserts an element into a sorted array if it does not exist. 574 * Inserts an element into a sorted array if it does not exist.
628 * 575 *
629 * If the target array is not already sorted with respect 576 * If the target array is not already sorted with respect
736 * @return the index of the largest lower bound, or @p size 683 * @return the index of the largest lower bound, or @p size
737 * @see cx_array_binary_search_sup() 684 * @see cx_array_binary_search_sup()
738 * @see cx_array_binary_search() 685 * @see cx_array_binary_search()
739 */ 686 */
740 cx_attr_nonnull 687 cx_attr_nonnull
741 cx_attr_export 688 CX_EXPORT size_t cx_array_binary_search_inf(const void *arr, size_t size,
742 size_t cx_array_binary_search_inf( 689 size_t elem_size, const void *elem, cx_compare_func cmp_func);
743 const void *arr,
744 size_t size,
745 size_t elem_size,
746 const void *elem,
747 cx_compare_func cmp_func
748 );
749 690
750 /** 691 /**
751 * Searches an item in a sorted array. 692 * Searches an item in a sorted array.
752 * 693 *
753 * If the array is not sorted with respect to the @p cmp_func, the behavior 694 * If the array is not sorted with respect to the @p cmp_func, the behavior
762 * cannot be found 703 * cannot be found
763 * @see cx_array_binary_search_inf() 704 * @see cx_array_binary_search_inf()
764 * @see cx_array_binary_search_sup() 705 * @see cx_array_binary_search_sup()
765 */ 706 */
766 cx_attr_nonnull 707 cx_attr_nonnull
767 cx_attr_export 708 CX_EXPORT size_t cx_array_binary_search(const void *arr, size_t size,
768 size_t cx_array_binary_search( 709 size_t elem_size, const void *elem, cx_compare_func cmp_func);
769 const void *arr,
770 size_t size,
771 size_t elem_size,
772 const void *elem,
773 cx_compare_func cmp_func
774 );
775 710
776 /** 711 /**
777 * Searches the smallest upper bound in a sorted array. 712 * Searches the smallest upper bound in a sorted array.
778 * 713 *
779 * In other words, this function returns the index of the smallest element 714 * In other words, this function returns the index of the smallest element
794 * @return the index of the smallest upper bound, or @p size 729 * @return the index of the smallest upper bound, or @p size
795 * @see cx_array_binary_search_inf() 730 * @see cx_array_binary_search_inf()
796 * @see cx_array_binary_search() 731 * @see cx_array_binary_search()
797 */ 732 */
798 cx_attr_nonnull 733 cx_attr_nonnull
799 cx_attr_export 734 CX_EXPORT size_t cx_array_binary_search_sup(const void *arr, size_t size,
800 size_t cx_array_binary_search_sup( 735 size_t elem_size, const void *elem, cx_compare_func cmp_func);
801 const void *arr,
802 size_t size,
803 size_t elem_size,
804 const void *elem,
805 cx_compare_func cmp_func
806 );
807 736
808 /** 737 /**
809 * Swaps two array elements. 738 * Swaps two array elements.
810 * 739 *
811 * @param arr the array 740 * @param arr the array
812 * @param elem_size the element size 741 * @param elem_size the element size
813 * @param idx1 index of the first element 742 * @param idx1 index of the first element
814 * @param idx2 index of the second element 743 * @param idx2 index of the second element
815 */ 744 */
816 cx_attr_nonnull 745 cx_attr_nonnull
817 cx_attr_export 746 CX_EXPORT void cx_array_swap(void *arr, size_t elem_size, size_t idx1, size_t idx2);
818 void cx_array_swap(
819 void *arr,
820 size_t elem_size,
821 size_t idx1,
822 size_t idx2
823 );
824 747
825 /** 748 /**
826 * Allocates an array list for storing elements with @p elem_size bytes each. 749 * Allocates an array list for storing elements with @p elem_size bytes each.
827 * 750 *
828 * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of 751 * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of
839 * @return the created list 762 * @return the created list
840 */ 763 */
841 cx_attr_nodiscard 764 cx_attr_nodiscard
842 cx_attr_malloc 765 cx_attr_malloc
843 cx_attr_dealloc(cxListFree, 1) 766 cx_attr_dealloc(cxListFree, 1)
844 cx_attr_export 767 CX_EXPORT CxList *cxArrayListCreate(const CxAllocator *allocator,
845 CxList *cxArrayListCreate( 768 cx_compare_func comparator, size_t elem_size, size_t initial_capacity);
846 const CxAllocator *allocator,
847 cx_compare_func comparator,
848 size_t elem_size,
849 size_t initial_capacity
850 );
851 769
852 /** 770 /**
853 * Allocates an array list for storing elements with @p elem_size bytes each. 771 * Allocates an array list for storing elements with @p elem_size bytes each.
854 * 772 *
855 * The list will use the cxDefaultAllocator and @em NO compare function. 773 * The list will use the cxDefaultAllocator and @em NO compare function.

mercurial