docs/Writerside/topics/array_list.h.md

Fri, 19 Dec 2025 12:40:58 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 19 Dec 2025 12:40:58 +0100
changeset 1626
a2565f9fc6de
parent 1625
89a2d53308e4
permissions
-rw-r--r--

add cx_array_remove() and cx_array_remove_fast()

relates to #787 and relates to #619

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
1625
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
84 int cx_array_add(CxArray array, Any element);
1614
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
1625
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
88 int cx_array_insert(CxArray array, size_t index, Any element);
1614
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,
1625
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
94 CxArray array, Any element);
1614
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,
1625
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
100 CxArray array, size_t index, Any element);
1614
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
1625
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
112 > Since the "functions" are actually macros, the variants which add or insert one single element
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
113 > also conveniently take the address of the passed argument, so you don't have to do it.
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
114 > This is why the type is specified as `Any` instead of `const void*` here.
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
115
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
116 > 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
117 > 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
118 > 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
119 >{style="note"}
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120
1626
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
121 ## Remove Elements
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
122
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
123 ```C
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
124 #include <cx/array_list.h>
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
125
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
126 void cx_array_remove(CxArray array, size_t index);
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
127
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
128 void cx_array_remove_fast(CxArray array, size_t index);
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
129
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
130 void cx_array_remove_array(CxArray array, size_t index, size_t n);
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
131
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
132 void cx_array_remove_array_fast(CxArray array, size_t index, size_t n);
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
133 ```
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
134
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
135 The functions `cx_array_remove()` and `cx_array_remove_array()` remove one or more elements from the array by shifting the remaining elements by the number of removed elements.
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
136 The functions `cx_array_remove_fast()` and `cx_array_remove_array_fast()` on the other hand copy elements from the end of the array into the gap to fill the removed elements.
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
137 Therefore, if the order of the elements does not matter, you can use the fast versions of these functions for better performance.
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
138
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
139 > When you specify an `index` that is out-of-bounds or choose a number `n` of elements that would overflow the array,
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
140 > the above functions only remove as many elements as possible.
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
141 > So it's safe to use them without any bounds checking.
a2565f9fc6de add cx_array_remove() and cx_array_remove_fast()
Mike Becker <universe@uap-core.de>
parents: 1625
diff changeset
142
1621
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
143 ## Sorting
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
144
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
145 ```C
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
146 #include <cx/array_list.h>
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
147
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
148 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
149 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
150
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
151 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
152
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
153 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
154 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
155 ```
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
156
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
157 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
158 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
159 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
160 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
161
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
162 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
163 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
164
c52a4c67e29e add convenience array_sort functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1618
diff changeset
165 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
166
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
167 ## Insertion Sort
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
168
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
169 ```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
170 #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
171
1625
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
172 int cx_array_insert_sorted(CxArray array, Any element,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
173 cx_compare_func cmp_func);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
174
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
175 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
176 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
177 cx_compare_func cmp_func);
1245
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 int cx_array_insert_sorted_a(const CxAllocator *allocator,
1625
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
180 CxArray array, Any element,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
181 cx_compare_func cmp_func);
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
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 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
184 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
185 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
186
1625
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
187 int cx_array_insert_sorted_c(CxArray array, Any element,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
188 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
189
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
190 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
191 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
192 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
193
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
194 int cx_array_insert_sorted_ca(const CxAllocator *allocator,
1625
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
195 CxArray array, Any element,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
196 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
197
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
198 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
199 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
200 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
201 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
202
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
203 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
204 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
205 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
206
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
207 ## 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
208
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
209 ```C
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
210 #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
211
1625
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
212 int cx_array_insert_unique(CxArray array, Any element,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
213 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
214
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
215 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
216 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
217 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
218
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
219 int cx_array_insert_unique_a(const CxAllocator *allocator,
1625
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
220 CxArray array, Any element,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
221 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
222
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
223 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
224 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
225 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
226
1625
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
227 int cx_array_insert_unique_c(CxArray array, Any element,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
228 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
229
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
230 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
231 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
232 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
233
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
234 int cx_array_insert_unique_ca(const CxAllocator *allocator,
1625
89a2d53308e4 changes macros for adding/inserting a single element into an array to automatically take the address - relates to #619
Mike Becker <universe@uap-core.de>
parents: 1622
diff changeset
235 CxArray array, Any element,
1622
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
236 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
237
27e7a4bf1a39 add missing compare_func2 variants of the array functions - relates to #622
Mike Becker <universe@uap-core.de>
parents: 1621
diff changeset
238 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
239 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
240 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
241 ```
e46406fd1b3c add functions to insert elements into lists/arrays without duplicates - resolves #557
Mike Becker <universe@uap-core.de>
parents: 1390
diff changeset
242
1614
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
243 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
244 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
245
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
246 ## Binary Search
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
247
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
248 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
249 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
250
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
251 size_t cx_array_binary_search(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
252 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
253 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
254
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
255 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
256 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
257 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
258
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
259 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
260 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
261 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
262
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
263 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
264 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
265 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
266
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
267 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
268 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
269 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
270
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
271 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
272 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
273 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
274 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
275
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
276 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
277
1e0e7f08ccd6 update the online docs with the new array API - resolves #619
Mike Becker <universe@uap-core.de>
parents: 1605
diff changeset
278 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
279
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
280 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
281 Otherwise, the function returns `size`.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
282
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
283 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
284 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
285 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
286 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
287 than the searched element.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
288
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
289 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
290 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
291 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
292
1618
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
293 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
294
1425
83284b289430 remove unnecessary members from the array reallocator struct - fixes #621
Mike Becker <universe@uap-core.de>
parents: 1424
diff changeset
295 > 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
296 >
1618
ef7cab6eb131 huge refactoring of collections to add support for 3-arg compare functions
Mike Becker <universe@uap-core.de>
parents: 1614
diff changeset
297 > 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
298 >{style="note"}
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
299
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
300 > 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
301 > But observations show that it usually is.
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
302 > 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
303
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
304 ## Iterators
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
305
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
306 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
307 #include <cx/iterator.h>
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 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
310 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
311 bool remove_keeps_order);
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
312
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
313 CxIterator cxIteratorPtr(const void *array,
6e0c3a8a914a remove the concept of "mutating iterators" - resolves #579
Mike Becker <universe@uap-core.de>
parents: 1425
diff changeset
314 size_t elem_count,
1245
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
315 bool remove_keeps_order);
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
316 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
317
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
318 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
319
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
320 ## Other
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
321
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
322 ```C
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
323 #include <cx/array_list.h>
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
324
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
325 void cx_array_swap(
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
326 void *arr,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
327 size_t elem_size,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
328 size_t idx1,
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
329 size_t idx2
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
330 );
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
331 ```
721e2032fa25 define structure for array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1190
diff changeset
332
1248
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
333 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
334
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
335 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
336
fc5e63b04281 complete array_list.h documentation
Mike Becker <universe@uap-core.de>
parents: 1246
diff changeset
337 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
338
1190
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
339 <seealso>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
340 <category ref="apidoc">
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
341 <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
342 </category>
a7b913d5d589 bring incomplete docs into a shape that can be released
Mike Becker <universe@uap-core.de>
parents: 1146
diff changeset
343 </seealso>

mercurial