docs/src/modules.md

branch
feature/array
changeset 340
8acf182f6424
parent 326
3dd7d21fb76b
child 359
9f86bc73f96b
equal deleted inserted replaced
339:ae368664625f 340:8acf182f6424
13 via `#include <ucx/MODULENAME.h>`. 13 via `#include <ucx/MODULENAME.h>`.
14 Required modules are included automatically. 14 Required modules are included automatically.
15 15
16 <div id="modules" align="center"> 16 <div id="modules" align="center">
17 17
18 ----------------------- ---------------------- ---------------------------- ------------------------- 18 ----------------------- ---------------------- -------------------------------- ---------------------------
19 [Allocator](#allocator) [AVL&nbsp;Tree](#avl-tree) [Buffer](#buffer) [List](#list) 19 [String](#string) [Buffer](#buffer)
20 [Logging](#logging) [Map](#map) [Memory&nbsp;Pool](#memory-pool) [Properties](#properties) 20 [Allocator](#allocator) [Stack](#stack) [Memory&nbsp;Pool](#memory-pool)
21 [Stack](#stack) [String](#string) [Testing](#testing) [Utilities](#utilities) 21 [Array](#array) [List](#list) [Map](#map) [AVL&nbsp;Tree](#avl-tree)
22 ----------------------- ---------------------- ---------------------------- ------------------------- 22 [Logging](#logging) [Testing](#testing) [Utilities](#utilities) [Properties](#properties)
23 ----------------------- ---------------------- -------------------------------- ---------------------------
23 24
24 </div> 25 </div>
25 26
26 ## Allocator 27 ## Allocator
27 28
38 memory area / pool as first argument. 39 memory area / pool as first argument.
39 40
40 As the pointer to the memory area / pool can be arbitrarily chosen, any data 41 As the pointer to the memory area / pool can be arbitrarily chosen, any data
41 can be provided to the memory management functions. One example is the 42 can be provided to the memory management functions. One example is the
42 [UCX Memory Pool](#memory-pool). 43 [UCX Memory Pool](#memory-pool).
44
45 ## Array
46
47 *Header file:* [array.h](api/array_8h.html)
48 *Required modules:* [Allocator](#allocator)
49
50 The UCX Array is an implementation of a dynamic array with automatic
51 reallocation. The array structure contains a capacity, the current size,
52 the size of each element, the raw pointer to the memory area and an allocator.
53 Unlike an [UcxList](#list), the array structure is typically passed by value,
54 unless it is subjected to change. Arrays are in most cases much faster than
55 linked list.
56
57 ### Remove duplicates from an array of strings
58
59 The following example shows, how a `UcxArray` can be built with
60 a standard dynamic C array (pointer+length) as basis.
61
62 ```C
63 #include <stdio.h>
64 #include <ucx/array.h>
65 #include <ucx/string.h>
66 #include <ucx/utils.h>
67
68 UcxArray remove_duplicates(sstr_t* array, size_t arrlen) {
69 // worst case is no duplicates, hence the capacity is set to arrlen
70 UcxArray result = ucx_array_new(arrlen, sizeof(sstr_t));
71 // only append elements, if they are not already present in the array
72 for (size_t i = 0 ; i < arrlen ; ++i) {
73 if (!ucx_array_contains(result, array+i, ucx_cmp_sstr, NULL)) {
74 ucx_array_append(&result, array+i);
75 }
76 }
77 // make the array as small as possible
78 ucx_array_shrink(&result);
79 return result;
80 }
81
82 /* ... */
83
84 sstr_t* array = /* some standard array of strings */
85 size_t arrlen = /* the length of the array */
86
87 UcxArray result = remove_duplicates(array,arrlen);
88
89 /* Iterate over the array and print the elements */
90 for (size_t i = 0 ; i < result.size ; i++) {
91 sstr_t s = ucx_array_at_typed(sstr_t, result, i);
92 printf("%" PRIsstr "\n", SFMT(s));
93 }
94
95 /* Free the array. */
96 ucx_array_free(&result);
97 ```
43 98
44 ## AVL Tree 99 ## AVL Tree
45 100
46 *Header file:* [avl.h](api/avl_8h.html) 101 *Header file:* [avl.h](api/avl_8h.html)
47 *Required modules:* [Allocator](#allocator) 102 *Required modules:* [Allocator](#allocator)
193 248
194 ### Remove duplicates from an array of strings 249 ### Remove duplicates from an array of strings
195 250
196 Assume you are given an array of `sstr_t` and want to create a list of these 251 Assume you are given an array of `sstr_t` and want to create a list of these
197 strings without duplicates. 252 strings without duplicates.
253 This is a similar example to the one [above](#array), but here we are
254 using a `UcxList`.
198 ```C 255 ```C
199 #include <stdio.h> 256 #include <stdio.h>
200 #include <ucx/list.h> 257 #include <ucx/list.h>
201 #include <ucx/string.h> 258 #include <ucx/string.h>
202 #include <ucx/utils.h> 259 #include <ucx/utils.h>

mercurial