docs/Writerside/topics/array_list.h.md

Sat, 25 Jan 2025 13:44:24 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 25 Jan 2025 13:44:24 +0100
branch
docs/3.1
changeset 1146
151c057faf7c
parent 1143
0559812df10c
permissions
-rw-r--r--

add marker to every incomplete page

relates to #451

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
1146
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
3 <warning>
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
4 Outdated - Rewrite!
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
5 </warning>
151c057faf7c add marker to every incomplete page
Mike Becker <universe@uap-core.de>
parents: 1143
diff changeset
6
1141
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 Since low-level array lists are just plain arrays, there is no need for such many low-level functions as for linked
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 lists.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 However, there is one extremely powerful function that can be used for several complex tasks: `cx_array_copy`.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 The full signature is shown below:
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 ```c
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 int cx_array_copy(
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 void **target,
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 void *size,
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 void *capacity,
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 unsigned width,
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 size_t index,
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 const void *src,
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 size_t elem_size,
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 size_t elem_count,
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 struct cx_array_reallocator_s *reallocator
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 );
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 ```
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 The `target` argument is a pointer to the target array pointer.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 The reason for this additional indirection is that this function writes
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 back the pointer to the possibly reallocated array.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 The next two arguments are pointers to the `size` and `capacity` of the target array for which the width
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 (in bits) is specified in the `width` argument.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 On a successful invocation, the function copies `elem_count` number of elements, each of size `elem_size` from
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 `src` to `*target` and uses the `reallocator` to extend the array when necessary.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 Finally, the size, capacity, and the pointer to the array are all updated and the function returns zero.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 A few things to note:
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 * `*target` and `src` can point to the same memory region, effectively copying elements within the array with `memmove`
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 * `*target` does not need to point to the start of the array, but `size` and `capacity` always start counting from the
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 position, `*target` points to - in this scenario, the need for reallocation must be avoided for obvious reasons
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 * `index` does not need to be within size of the current array
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 * `index` does not even need to be within the capacity of the array
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 * `width` must be one of 8, 16, 32, 64 (only on 64-bit systems), or zero (in which case the native word width is used)
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 If you just want to add one single element to an existing array, you can use the macro `cx_array_add()`.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 You can use `CX_ARRAY_DECLARE()` to declare the necessary fields within a structure and then use the
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 `cx_array_simple_*()` convenience macros to reduce code overhead.
a06a2d27c043 create new page structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 The convenience macros automatically determine the width of the size/capacity variables.
1142
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
46
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
47 ## Undocumented Symbols (TODO)
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
48 ### cx_array_binary_search
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
49 ### cx_array_binary_search_inf
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
50 ### cx_array_binary_search_sup
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
51 ### cx_array_copy
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
52 ### cx_array_default_reallocator
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
53 ### cx_array_default_reallocator_impl
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
54 ### cx_array_insert_sorted
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
55 ### cxArrayListCreate
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
56 ### cx_array_reallocator
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
57 ### cx_array_reserve
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
58 ### cx_array_swap
9437530176bc add symbols that need documentation as TODOs
Mike Becker <universe@uap-core.de>
parents: 1141
diff changeset
59 ### cx_array_swap_sbo_size

mercurial