docs/Writerside/topics/array_list.h.md

Thu, 18 Dec 2025 14:53:16 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 18 Dec 2025 14:53:16 +0100
changeset 1622
27e7a4bf1a39
parent 1621
c52a4c67e29e
child 1625
89a2d53308e4
permissions
-rw-r--r--

add missing compare_func2 variants of the array functions - relates to #622

1143
0559812df10c assign proper names to the documentation topics
Mike Becker <universe@uap-core.de>
parents: 1142
diff changeset
1 # Array List
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
3 Next to an array list implementation of the list interface,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
4 UCX offers several functions to work with plain C arrays equipped with a size and a capacity.
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
5
1390
ff077f793c5d kv-list: add documentation
Mike Becker <universe@uap-core.de>
parents: 1322
diff changeset
6 The high-level [list interface](list.h.md) is documented on a separate page and explains how lists are used
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
7 that are created by one of the following functions.
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
8
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
9 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
10 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
11
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
12 CxList *cxArrayListCreate(const CxAllocator *allocator,
1605
55b13f583356 refactor the list and map construction functions and remove the simple macros
Mike Becker <universe@uap-core.de>
parents: 1507
diff changeset
13 size_t elem_size, size_t initial_capacity);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
14 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
15
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
16 The remaining documentation on this page concentrates on dealing with plain C arrays.
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
17
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
18 ## Declaring and Initializing
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
19
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
20 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
21 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
22
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
23 #define CX_ARRAY(type, name)
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
24
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
25 int cx_array_init(CxArray array, size_t capacity);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
26
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
27 int cx_array_init_a(const CxAllocator *allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
28 CxArray array, size_t capacity);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
29
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
30 void cx_array_init_fixed(CxArray array,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
31 void *fixed_size_array, size_t num_initialized);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
32 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
33
1246
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
34 The purpose of the UCX array functions is to keep track of the size and capacity of a plain C array.
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
35
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
36 The `CX_ARRAY()` macro declares a variable over an anonymous struct which is compatible to `CxArray`.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
37 The thus created struct contains the three members `data`, `size`, and `capacity`.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
38 You can then pass a pseudo-reference to this variable to any of the UCX array functions.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
39 This is realized by macros which will automatically take the address of your variable and cast it to a pointer of `CxArray`.
1246
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
40
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
41 Once the array is declared, you can use one of `cx_array_init()`, `cx_array_init_a()`, or `cx_array_init_fixed()` to initialize it.
1246
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
42
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
43 The former two functions allocate memory for the array using the [default allocator](allocator.h.md#default-allocator) or the allocator passed to the function.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
44 They return zero on success and non-zero on allocation failure.
1246
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
45
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
46 The latter function initializes the array with a fixed-sized array.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
47 The `num_initialized` argument specifies the number of elements that are already initialized in the array.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
48 This is useful, for example, if you want to initialize the array with stack memory and only want to
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
49 allocate memory if the capacity you reserved on the stack is not sufficient.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
50 More on this in the following section, when discussing `cx_array_copy_to_new()`.
1246
5f2c9750204c document declare and init
Mike Becker <universe@uap-core.de>
parents: 1245
diff changeset
51
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
52 ## Reallocation
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
53
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
54 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
55 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
56
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
57 int cx_array_reserve(CxArray array, size_t capacity);
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
58
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
59 int cx_array_copy_to_new(CxArray array, size_t capacity);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
60
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
61 int cx_array_reserve_a(const CxAllocator *allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
62 CxArray array, size_t capacity);
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
63
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
64 int cx_array_copy_to_new_a(const CxAllocator *allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
65 CxArray array, size_t capacity);
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 ```
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
67
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
68 The functions `cx_array_reserve()` and `cx_array_reserve_a()` change the capacity of the array to exactly `capacity` elements.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
69 If the current `size` is larger than `capacity`, the array is truncated to `capacity`.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
70
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
71 The functions `cx_array_copy_to_new()` and `cx_array_copy_to_new_a()` allocate a new memory block with the specified capacity
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
72 and copy the elements of the old memory to the new memory.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
73
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
74 > The functions `cx_array_copy_to_new()` and `cx_array_copy_to_new_a()` do **not** free the old memory.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
75 > If the old memory is not located on the stack, or you don't have another reference to it, it will be leaked.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
76 > The functions are particularly useful when the array was initialized with a fixed-sized array.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
77 >{style="note"}
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
78
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
79 ## Add or Insert Elements
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
80
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
81 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
82 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
83
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
84 int cx_array_add(CxArray array, const void *element);
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
85
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
86 int cx_array_add_array(CxArray array, const void *other, size_t n);
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
87
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
88 int cx_array_insert(CxArray array, size_t index, const void *element);
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
89
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
90 int cx_array_insert_array(CxArray array,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
91 size_t index, const void *other, size_t n);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
92
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
93 int cx_array_add_a(const CxAllocator* allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
94 CxArray array, const void *element);
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
95
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
96 int cx_array_add_array(const CxAllocator* allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
97 CxArray array, const void *other, size_t n);
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
98
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
99 int cx_array_insert_a(const CxAllocator* allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
100 CxArray array, size_t index, const void *element);
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
101
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
102 int cx_array_insert_array_a(const CxAllocator* allocator,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
103 CxArray array, size_t index, const void *other, size_t n);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
104 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
105
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
106 The above functions insert one or more elements into the array at the specified `index`
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
107 or add them to the end of the array.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
108
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
109 When the array capacity is not sufficient, a re-allocation is attempted.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
110 If the allocation fails, the function returns non-zero.
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
111
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
112 > Be careful when using these functions on an array that was initialized with fixed-sized memory.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
113 > In this case, you MUST make sure that the capacity is sufficient or reallocate the array
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
114 > with `cx_array_copy_to_new()` or `cx_array_copy_to_new_a()` before adding or inserting more elements.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
115 >{style="note"}
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116
1621
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
117 ## Sorting
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
118
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
119 ```C
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
120 #include <cx/array_list.h>
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
121
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
122 void cx_array_qsort_c(void *array, size_t nmemb, size_t size,
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
123 cx_compare_func2 fn, void *context);
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
124
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
125 void cx_array_sort(CxArray array, cx_compare_func fn);
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
126
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
127 void cx_array_sort_c(CxArray array,
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
128 cx_compare_func2 fn, void *context);
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
129 ```
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
130
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
131 With simple `cx_compare_func` functions, arrays can always be sorted with standard `qsort()`.
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
132 Sorting arrays with `cx_compare_func2` functions, however, need special support by either `qsort_r()` (GNU) or `qsort_s()` (ISO).
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
133 However, both functions come with their own challenges.
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
134 On the one hand, `qsort_r()` is not ISO standard, and on the other hand `qsort_s()` is only optional and has an incorrect parameter order in the Microsoft C library.
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
135
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
136 To provide a platform independent solution, UCX detects if `qsort_r()` is available and implements a fallback when it is not.
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
137 You can safely use `cx_array_qsort_c()` everywhere wehere you would use `qsort_r()`.
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
138
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
139 The functions `cx_array_sort()` and `cx_array_sort_c()` are for convenient work with arrays declared with `CX_ARRAY()`.
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
140
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
141 ## Insertion Sort
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
142
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
143 ```C
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
144 #include <cx/array_list.h>
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
145
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
146 int cx_array_insert_sorted(CxArray array,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
147 const void *element,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
148 cx_compare_func cmp_func);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
149
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
150 int cx_array_insert_sorted_array(CxArray array,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
151 const void *sorted_data, size_t n,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
152 cx_compare_func cmp_func);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
153
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
154 int cx_array_insert_sorted_a(const CxAllocator *allocator,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
155 CxArray array, const void *element,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
156 cx_compare_func cmp_func);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
157
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
158 int cx_array_insert_sorted_array_a(const CxAllocator *allocator,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
159 CxArray array, const void *sorted_data, size_t n,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
160 cx_compare_func cmp_func);
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
161
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
162 int cx_array_insert_sorted_c(CxArray array,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
163 const void *element,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
164 cx_compare_func2 cmp_func, void *context);
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
165
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
166 int cx_array_insert_sorted_array_c(CxArray array,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
167 const void *sorted_data, size_t n,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
168 cx_compare_func2 cmp_func, void *context);
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
169
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
170 int cx_array_insert_sorted_ca(const CxAllocator *allocator,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
171 CxArray array, const void *element,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
172 cx_compare_func2 cmp_func, void *context);
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
173
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
174 int cx_array_insert_sorted_array_ca(const CxAllocator *allocator,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
175 CxArray array, const void *sorted_data, size_t n,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
176 cx_compare_func2 cmp_func, void *context);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
177 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
178
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
179 The above functions are equivalent to `cx_array_insert()` and `cx_array_insert_array()`,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
180 except that they only work on sorted arrays and insert the element at the correct position with respect to the sort order.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
181 If either the array or the `sorted_data` is not sorted according to the given `cmp_func`, the behavior is undefined.
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
182
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
183 ## Insert Unique Elements
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
184
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
185 ```C
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
186 #include <cx/array_list.h>
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
187
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
188 int cx_array_insert_unique(CxArray array,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
189 const void *element,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
190 cx_compare_func cmp_func);
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
191
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
192 int cx_array_insert_unique_array(CxArray array,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
193 const void *sorted_data, size_t n,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
194 cx_compare_func cmp_func);
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
195
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
196 int cx_array_insert_unique_a(const CxAllocator *allocator,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
197 CxArray array, const void *element,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
198 cx_compare_func cmp_func);
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
199
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
200 int cx_array_insert_unique_array_a(const CxAllocator *allocator,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
201 CxArray array, const void *sorted_data, size_t n,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
202 cx_compare_func cmp_func);
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
203
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
204 int cx_array_insert_unique_c(CxArray array,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
205 const void *element,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
206 cx_compare_func2 cmp_func, void *context);
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
207
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
208 int cx_array_insert_unique_array_c(CxArray array,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
209 const void *sorted_data, size_t n,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
210 cx_compare_func2 cmp_func, void *context);
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
211
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
212 int cx_array_insert_unique_ca(const CxAllocator *allocator,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
213 CxArray array, const void *element,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
214 cx_compare_func2 cmp_func, void *context);
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
215
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
216 int cx_array_insert_unique_array_ca(const CxAllocator *allocator,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
217 CxArray array, const void *sorted_data, size_t n,
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
218 cx_compare_func2 cmp_func, void *context);
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
219 ```
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
220
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
221 The above functions are similar to the functions for insertion sort,
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
222 except that they only insert elements that are not already present in the array.
1419
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
223
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
224 ## Binary Search
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
225
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
226 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
227 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
228
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
229 size_t cx_array_binary_search(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
230 const void *arr, size_t size, size_t elem_size,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
231 const void *elem, cx_compare_func cmp_func);
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
232
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
233 size_t cx_array_binary_search_inf(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
234 const void *arr, size_t size, size_t elem_size,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
235 const void *elem, cx_compare_func cmp_func);
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
236
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
237 size_t cx_array_binary_search_sup(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
238 const void *arr, size_t size, size_t elem_size,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
239 const void *elem, cx_compare_func cmp_func);
1618
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
240
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
241 size_t cx_array_binary_search_c(
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
242 const void *arr, size_t size, size_t elem_size,
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
243 const void *elem, cx_compare_func2 cmp_func, void *context);
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
244
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
245 size_t cx_array_binary_search_inf_c(
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
246 const void *arr, size_t size, size_t elem_size,
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
247 const void *elem, cx_compare_func2 cmp_func, void *context);
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
248
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
249 size_t cx_array_binary_search_sup_c(
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
250 const void *arr, size_t size, size_t elem_size,
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
251 const void *elem, cx_compare_func2 cmp_func, void *context);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
252 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
253
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
254 The function `cx_array_binary_search()` searches the array `arr` for the data pointed to by `elem` using the compare function `cmp_func`.
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
255
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
256 Note that these functions do not operate on `CxArray` and are instead more general and also useful on plain C arrays.
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
257
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
258 If the element is found (i.e. `cmp_func` returns zero), the function returns the index of the element.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
259 Otherwise, the function returns `size`.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
260
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
261 The functions `cx_array_binary_search_inf()` and `cx_array_binary_search_sup()` are equivalent,
1424
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
262 except that they return the index of the closest element if the searched element is not found.
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
263 The former function returns the closest element that is less or equal (the greatest lower bound / infimum),
563033aa998c fixes tons of typos and grammar issues across the documentation - fixes #667
Mike Becker <universe@uap-core.de>
parents: 1419
diff changeset
264 and the latter function returns the closest element that is larger or equal (the least upper bound / supremum)
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
265 than the searched element.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
266
1507
f4010cda9a2a stable return value for binary search when there are duplicates in the array
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
267 When the found element appears more than once in the array,
f4010cda9a2a stable return value for binary search when there are duplicates in the array
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
268 the binary search and the infimum report the largest index
f4010cda9a2a stable return value for binary search when there are duplicates in the array
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
269 and the supremum reports the smallest index of the identical items, respectively.
f4010cda9a2a stable return value for binary search when there are duplicates in the array
Mike Becker <universe@uap-core.de>
parents: 1429
diff changeset
270
1618
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
271 The function variants with the `_c` suffix allow additional `context` for the compare function.
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
272
1425
83284b289430 remove unnecessary members from the array reallocator struct - fixes #621
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
273 > Note that all the above functions are only well-defined on arrays which are sorted with respect to the given compare function.
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
274 >
1618
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
275 > This can, for example, easily be achieved by calling the standard library's `qsort()` or `qsort_s()` function.
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
276 >{style="note"}
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
277
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
278 > Despite the name, neither C nor POSIX standards require the standard library's `bsearch()` function to be implemented using binary search.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
279 > But observations show that it usually is.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
280 > This makes `cx_array_binary_search()` likely to be equivalent to `bsearch()`, except that it returns an index rather than a pointer.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
281
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
282 ## Iterators
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
283
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
284 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
285 #include <cx/iterator.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
286
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
287 CxIterator cxIterator(const void *array,
1429
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
288 size_t elem_size, size_t elem_count,
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
289 bool remove_keeps_order);
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
290
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
291 CxIterator cxIteratorPtr(const void *array,
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
292 size_t elem_count,
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
293 bool remove_keeps_order);
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
294 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
295
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
296 Iterators over plain C arrays are defined in [iterator.h](iterator.h.md#creating-an-iterator).
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
297
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
298 ## Other
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
299
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
300 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
301 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
302
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
303 void cx_array_swap(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
304 void *arr,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
305 size_t elem_size,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
306 size_t idx1,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
307 size_t idx2
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
308 );
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
309 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
310
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
311 The function `cx_array_swap()` exchanges two items with a three-way swap.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
312
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
313 No memory is allocated if the element size `elem_size` is not larger than `CX_ARRAY_SWAP_SBO_SIZE` (cf. [](install.md#small-buffer-optimizations)).
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
314
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
315 You have to make sure that both indices are within bounds of the array `arr`.
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
316
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
317 <seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
318 <category ref="apidoc">
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
319 <a href="https://ucx.sourceforge.io/api/array__list_8h.html">array_list.h</a>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
320 </category>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
321 </seealso>

mercurial