docs/src/modules.md

changeset 360
fed2ead878ea
parent 359
9f86bc73f96b
child 370
07ac32b385e4
equal deleted inserted replaced
351:87c22ec6a0fd 360:fed2ead878ea
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 Arrays are in most cases much faster than linked list.
54 One can decide, whether to create a new array on the heap with `ucx_array_new()`
55 or to save one indirection by initializing a `UcxArray` structure on the stack
56 with `ucx_array_init()`.
57
58 ### Remove duplicates from an array of strings
59
60 The following example shows, how a `UcxArray` can be built with
61 a standard dynamic C array (pointer+length) as basis.
62
63 ```C
64 #include <stdio.h>
65 #include <ucx/array.h>
66 #include <ucx/string.h>
67 #include <ucx/utils.h>
68
69 UcxArray remove_duplicates(sstr_t* array, size_t arrlen) {
70 // worst case is no duplicates, hence the capacity is set to arrlen
71 UcxArray result = ucx_array_new(arrlen, sizeof(sstr_t));
72 // only append elements, if they are not already present in the array
73 for (size_t i = 0 ; i < arrlen ; ++i) {
74 if (!ucx_array_contains(result, array+i, ucx_cmp_sstr, NULL)) {
75 ucx_array_append(&result, array+i);
76 }
77 }
78 // make the array as small as possible
79 ucx_array_shrink(&result);
80 return result;
81 }
82
83 /* ... */
84
85 sstr_t* array = /* some standard array of strings */
86 size_t arrlen = /* the length of the array */
87
88 UcxArray result = remove_duplicates(array,arrlen);
89
90 /* Iterate over the array and print the elements */
91 for (size_t i = 0 ; i < result.size ; i++) {
92 sstr_t s = ucx_array_at_typed(sstr_t, result, i);
93 printf("%" PRIsstr "\n", SFMT(s));
94 }
95
96 /* Free the array. */
97 ucx_array_free(&result);
98 ```
43 99
44 ## AVL Tree 100 ## AVL Tree
45 101
46 *Header file:* [avl.h](api/avl_8h.html) 102 *Header file:* [avl.h](api/avl_8h.html)
47 *Required modules:* [Allocator](#allocator) 103 *Required modules:* [Allocator](#allocator)
193 249
194 ### Remove duplicates from an array of strings 250 ### Remove duplicates from an array of strings
195 251
196 Assume you are given an array of `sstr_t` and want to create a list of these 252 Assume you are given an array of `sstr_t` and want to create a list of these
197 strings without duplicates. 253 strings without duplicates.
254 This is a similar example to the one [above](#array), but here we are
255 using a `UcxList`.
198 ```C 256 ```C
199 #include <stdio.h> 257 #include <stdio.h>
200 #include <ucx/list.h> 258 #include <ucx/list.h>
201 #include <ucx/string.h> 259 #include <ucx/string.h>
202 #include <ucx/utils.h> 260 #include <ucx/utils.h>

mercurial