ucx
UAP Common Extensions
Loading...
Searching...
No Matches
array_list.h
Go to the documentation of this file.
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
35
36
37#ifndef UCX_ARRAY_LIST_H
38#define UCX_ARRAY_LIST_H
39
40#include "list.h"
41
46CX_EXPORT extern const unsigned cx_array_swap_sbo_size;
47
54#define CX_ARRAY(type, name) \
55 struct { \
56 type *data; \
57 size_t size; \
58 size_t capacity; \
59 } name
60
66typedef struct cx_array_s {
68 void *data;
70 size_t size;
72 size_t capacity;
74
88int cx_array_init_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity);
89
104#define cx_array_init_a(allocator, array, capacity) cx_array_init_(allocator, (CxArray*)&(array), sizeof((array).data[0]), capacity)
105
118#define cx_array_init(array, capacity) cx_array_init_a(cxDefaultAllocator, array, capacity)
119
131void cx_array_init_fixed_(CxArray *array, const void *data, size_t capacity, size_t size);
132
157#define cx_array_init_fixed(array, fixed_size_array, num_initialized) \
158 cx_array_init_fixed_((CxArray*)&(array), fixed_size_array, cx_nmemb(fixed_size_array), num_initialized)
159
173int cx_array_reserve_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity);
174
186#define cx_array_reserve_a(allocator, array, capacity) \
187 cx_array_reserve_(allocator, (CxArray*)&(array), sizeof((array).data[0]), capacity)
188
199#define cx_array_reserve(array, capacity) \
200 cx_array_reserve_a(cxDefaultAllocator, array, capacity)
201
215int cx_array_copy_to_new_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity);
216
233#define cx_array_copy_to_new_a(allocator, array, capacity) \
234 cx_array_copy_to_new_(allocator, (CxArray*)&(array), sizeof((array).data[0]), capacity)
235
251#define cx_array_copy_to_new(array, capacity) \
252 cx_array_copy_to_new_a(cxDefaultAllocator, array, capacity)
253
269int cx_array_insert_(const CxAllocator *allocator, CxArray *array,
270 size_t elem_size, size_t index, const void *other, size_t n);
271
283#define cx_array_add_a(allocator, array, element) \
284 cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (array).size, (void*)&(element), 1)
285
296#define cx_array_add(array, element) \
297 cx_array_add_a(cxDefaultAllocator, array, element)
298
311#define cx_array_insert_a(allocator, array, index, element) \
312 cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), index, (void*)&(element), 1)
313
325#define cx_array_insert(array, index, element) \
326 cx_array_insert_a(cxDefaultAllocator, array, index, element)
327
341#define cx_array_insert_array_a(allocator, array, index, other, n) \
342 cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), index, other, n)
343
356#define cx_array_insert_array(array, index, other, n) \
357 cx_array_insert_array_a(cxDefaultAllocator, array, index, other, n)
358
371#define cx_array_add_array_a(allocator, array, other, n) \
372 cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (array).size, other, n)
373
385#define cx_array_add_array(array, other, n) \
386 cx_array_add_array_a(cxDefaultAllocator, array, other, n)
387
404int cx_array_insert_sorted_(const CxAllocator *allocator, CxArray *array,
405 size_t elem_size, const void *sorted_data, size_t n,
406 cx_compare_func cmp_func, bool allow_duplicates);
407
422#define cx_array_insert_sorted_a(allocator, array, element, cmp_func) \
423 cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (void*)&(element), 1, cmp_func, true)
424
438#define cx_array_insert_sorted(array, element, cmp_func) \
439 cx_array_insert_sorted_a(cxDefaultAllocator, array, element, cmp_func)
440
456#define cx_array_insert_sorted_array_a(allocator, array, sorted_data, n, cmp_func) \
457 cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, true)
458
473#define cx_array_insert_sorted_array(array, sorted_data, n, cmp_func) \
474 cx_array_insert_sorted_array_a(cxDefaultAllocator, array, sorted_data, n, cmp_func)
475
490#define cx_array_insert_unique_a(allocator, array, element, cmp_func) \
491 cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (void*)&(element), 1, cmp_func, false)
492
506#define cx_array_insert_unique(array, element, cmp_func) \
507 cx_array_insert_unique_a(cxDefaultAllocator, array, element, cmp_func)
508
524#define cx_array_insert_unique_array_a(allocator, array, sorted_data, n, cmp_func) \
525 cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, false)
526
541#define cx_array_insert_unique_array(array, sorted_data, n, cmp_func) \
542 cx_array_insert_unique_array_a(cxDefaultAllocator, array, sorted_data, n, cmp_func)
543
560CX_EXTERN CX_NONNULL_ARG(1, 2, 4, 6)
561int cx_array_insert_sorted_c_(const CxAllocator *allocator, CxArray *array,
562 size_t elem_size, const void *sorted_data, size_t n,
563 cx_compare_func2 cmp_func, void *context, bool allow_duplicates);
564
580#define cx_array_insert_sorted_ca(allocator, array, element, cmp_func, context) \
581 cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (void*)&(element), 1, cmp_func, context, true)
582
597#define cx_array_insert_sorted_c(array, element, cmp_func, context) \
598 cx_array_insert_sorted_ca(cxDefaultAllocator, array, element, cmp_func, context)
599
616#define cx_array_insert_sorted_array_ca(allocator, array, sorted_data, n, cmp_func, context) \
617 cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, context, true)
618
634#define cx_array_insert_sorted_array_c(array, sorted_data, n, cmp_func, context) \
635 cx_array_insert_sorted_array_ca(cxDefaultAllocator, array, sorted_data, n, cmp_func, context)
636
652#define cx_array_insert_unique_ca(allocator, array, element, cmp_func, context) \
653 cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (void*)&(element), 1, cmp_func, context, false)
654
669#define cx_array_insert_unique_c(array, element, cmp_func, context) \
670 cx_array_insert_unique_ca(cxDefaultAllocator, array, element, cmp_func, context)
671
688#define cx_array_insert_unique_array_ca(allocator, array, sorted_data, n, cmp_func, context) \
689 cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, context, false)
690
706#define cx_array_insert_unique_array_c(array, sorted_data, n, cmp_func, context) \
707 cx_array_insert_unique_array_ca(cxDefaultAllocator, array, sorted_data, n, cmp_func, context)
708
721void cx_array_qsort_c(void *array, size_t nmemb, size_t size,
722 cx_compare_func2 fn, void *context);
723
734void cx_array_sort_(CxArray *array, size_t elem_size,
735 cx_compare_func fn);
736
748void cx_array_sort_c_(CxArray *array, size_t elem_size,
749 cx_compare_func2 fn, void *context);
750
757#define cx_array_sort(array, fn) \
758 cx_array_sort_((CxArray*)&(array), sizeof((array).data[0]), fn)
759
767#define cx_array_sort_c(array, fn, context) \
768 cx_array_sort_c_((CxArray*)&(array), sizeof((array).data[0]), fn, context)
769
780CxIterator cx_array_iterator_(CxArray *array, size_t elem_size);
781
794#define cx_array_iterator(array) \
795 cx_array_iterator_((CxArray*)&(array), sizeof((array).data[0]))
796
807
821#define cx_array_iterator_ptr(array) \
822 cx_array_iterator_ptr_((CxArray*)&(array))
823
824
837void cx_array_remove_(CxArray *array, size_t elem_size, size_t index, size_t n, bool fast);
838
851#define cx_array_remove(array, index) \
852 cx_array_remove_((CxArray*)&(array), sizeof((array).data[0]), index, 1, false)
853
867#define cx_array_remove_fast(array, index) \
868 cx_array_remove_((CxArray*)&(array), sizeof((array).data[0]), index, 1, true)
869
884#define cx_array_remove_array(array, index, n) \
885 cx_array_remove_((CxArray*)&(array), sizeof((array).data[0]), index, n, false)
886
902#define cx_array_remove_array_fast(array, index, n) \
903 cx_array_remove_((CxArray*)&(array), sizeof((array).data[0]), index, n, true)
904
914void cx_array_free_(const CxAllocator *allocator, CxArray *array);
915
924#define cx_array_free(array) cx_array_free_(cxDefaultAllocator, (CxArray*)&(array))
925
935#define cx_array_free_a(allocator, array) cx_array_free_(allocator, (CxArray*)&(array))
936
937
964size_t cx_array_binary_search_inf(const void *arr, size_t size,
965 size_t elem_size, const void *elem, cx_compare_func cmp_func);
966
987size_t cx_array_binary_search(const void *arr, size_t size,
988 size_t elem_size, const void *elem, cx_compare_func cmp_func);
989
1016size_t cx_array_binary_search_sup(const void *arr, size_t size,
1017 size_t elem_size, const void *elem, cx_compare_func cmp_func);
1018
1019
1047size_t cx_array_binary_search_inf_c(const void *arr, size_t size,
1048 size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context);
1049
1071size_t cx_array_binary_search_c(const void *arr, size_t size,
1072 size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context);
1073
1101size_t cx_array_binary_search_sup_c(const void *arr, size_t size,
1102 size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context);
1103
1113void cx_array_swap(void *arr, size_t elem_size, size_t idx1, size_t idx2);
1114
1130 size_t elem_size, size_t initial_capacity);
1131
1132#endif // UCX_ARRAY_LIST_H
struct cx_allocator_s CxAllocator
High-Level type alias for the allocator type.
Definition allocator.h:80
void cx_array_sort_(CxArray *array, size_t elem_size, cx_compare_func fn)
Sorts an array.
CxIterator cx_array_iterator_ptr_(CxArray *array)
Creates an iterator over the elements of an array containing pointers.
int cx_array_init_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity)
Initializes an array by allocating memory.
size_t cx_array_binary_search_sup_c(const void *arr, size_t size, size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context)
Searches the smallest upper bound in a sorted array.
size_t cx_array_binary_search_inf(const void *arr, size_t size, size_t elem_size, const void *elem, cx_compare_func cmp_func)
Searches the largest lower bound in a sorted array.
size_t cx_array_binary_search_c(const void *arr, size_t size, size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context)
Searches an item in a sorted array.
int cx_array_copy_to_new_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity)
Copies the array to a new memory region.
CxIterator cx_array_iterator_(CxArray *array, size_t elem_size)
Creates an iterator over the elements of an array.
size_t cx_array_binary_search_inf_c(const void *arr, size_t size, size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context)
Searches the largest lower bound in a sorted array.
void cx_array_sort_c_(CxArray *array, size_t elem_size, cx_compare_func2 fn, void *context)
Sorts an array.
int cx_array_insert_sorted_c_(const CxAllocator *allocator, CxArray *array, size_t elem_size, const void *sorted_data, size_t n, cx_compare_func2 cmp_func, void *context, bool allow_duplicates)
Inserts sorted data into a sorted array.
const unsigned cx_array_swap_sbo_size
The maximum item size in an array list that fits into a stack buffer when swapped.
CxList * cxArrayListCreate(const CxAllocator *allocator, size_t elem_size, size_t initial_capacity)
Allocates an array list for storing elements with elem_size bytes each.
struct cx_array_s CxArray
Internal structure for arrays.
int cx_array_insert_sorted_(const CxAllocator *allocator, CxArray *array, size_t elem_size, const void *sorted_data, size_t n, cx_compare_func cmp_func, bool allow_duplicates)
Inserts sorted data into a sorted array.
void cx_array_free_(const CxAllocator *allocator, CxArray *array)
Deallocates an array.
void cx_array_init_fixed_(CxArray *array, const void *data, size_t capacity, size_t size)
Initializes an array with fixed size memory.
size_t cx_array_binary_search_sup(const void *arr, size_t size, size_t elem_size, const void *elem, cx_compare_func cmp_func)
Searches the smallest upper bound in a sorted array.
void cx_array_remove_(CxArray *array, size_t elem_size, size_t index, size_t n, bool fast)
Removes elements from the array.
int cx_array_reserve_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity)
Changes the capacity of an array.
size_t cx_array_binary_search(const void *arr, size_t size, size_t elem_size, const void *elem, cx_compare_func cmp_func)
Searches an item in a sorted array.
void cx_array_qsort_c(void *array, size_t nmemb, size_t size, cx_compare_func2 fn, void *context)
An alternative to qsort_r() when that is not available on your platform.
int cx_array_insert_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t index, const void *other, size_t n)
Inserts data into an array.
void cx_array_swap(void *arr, size_t elem_size, size_t idx1, size_t idx2)
Swaps two array elements.
#define CX_MALLOC
The attributed function always returns freshly allocated memory.
Definition common.h:156
#define CX_DEALLOC(...)
Not supported in clang.
Definition common.h:172
#define CX_EXPORT
Only used for building Windows DLLs.
Definition common.h:289
#define CX_NONNULL
All pointer arguments must be non-NULL.
Definition common.h:141
#define CX_NODISCARD
Warn about discarded return value.
Definition common.h:256
#define CX_NONNULL_ARG(...)
The specified pointer arguments must be non-NULL.
Definition common.h:146
#define CX_EXTERN
Declares a function with external linkage.
Definition common.h:297
int(* cx_compare_func)(const void *left, const void *right)
A comparator function comparing two arbitrary values.
Definition compare.h:53
int(* cx_compare_func2)(const void *left, const void *right, void *data)
A comparator function comparing two arbitrary values.
Definition compare.h:60
struct cx_iterator_s CxIterator
Iterator type.
Definition iterator.h:144
Interface for list implementations.
struct cx_list_s CxList
Common type for all list implementations.
Definition list.h:179
void cxListFree(CxList *list)
Deallocates the memory of the specified list structure.
Internal structure for arrays.
Definition array_list.h:66
void * data
The array data.
Definition array_list.h:68
size_t capacity
The maximum number of elements.
Definition array_list.h:72
size_t size
The number of elements.
Definition array_list.h:70